File size: 6,627 Bytes
05707f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f784e93
 
 
 
 
 
 
05707f0
 
 
 
 
 
 
 
 
f784e93
 
 
 
 
 
05707f0
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Point-in-time predictors for next-month factor returns.

Every feature dated month t is a function of returns observed in months <= t only.
The target is the return realised in month t+1. `tests/test_no_leakage.py` checks
this by perturbing future returns and confirming no feature changes.
"""

from __future__ import annotations

import numpy as np
import pandas as pd

FEATURES = [
    "own_r1",   # last month's factor return
    "own_m3",   # trailing 3-month mean
    "own_m12",  # trailing 12-month mean (factor momentum)
    "own_m36",  # trailing 36-month mean (slow-moving premium proxy)
    "own_v12",  # trailing 12-month volatility
    "cty_m12",  # same country, average 12-month mean across all factors
    "dm_m12",   # same factor, average 12-month mean across developed markets
]


def _regional_terms(R: pd.DataFrame, m12: pd.DataFrame, reg: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
    """dm_m12 and the pooled regional mean when region membership can change over time.

    reg has R's shape and holds each series' region in each month. The DM average at t
    uses the series classified DM at t; the pooled mean for region g at t averages every
    past country-month that was in g when it was observed, and a series uses the pooled
    mean of the region it belongs to at t.
    """
    factors = R.columns.get_level_values("factor")
    dm = m12.where(reg == "DM").T.groupby(level="factor").mean().T
    dm_m12 = dm.reindex(columns=factors).set_axis(R.columns, axis=1)
    pooled = pd.DataFrame(np.nan, index=R.index, columns=R.columns)
    for g in pd.unique(reg.to_numpy().ravel()):
        if not isinstance(g, str):
            continue
        member = reg == g
        sub = R.where(member)
        s = sub.T.groupby(level="factor").sum(min_count=1).T.fillna(0).cumsum()
        n = sub.notna().T.groupby(level="factor").sum().T.cumsum()
        pm = (s / n.where(n > 0)).reindex(columns=factors).set_axis(R.columns, axis=1)
        pooled = pooled.where(~member, pm)
    return dm_m12, pooled


def build_features(panel: pd.DataFrame, hist_min: int = 24, kappa: float = 120.0) -> pd.DataFrame:
    """Long table: month, country, factor, region, FEATURES, hist_mean, shrink_mean, target, target_month."""
    wide = panel.pivot_table(index="month", columns=["country", "factor"], values="ret")
    full = pd.period_range(wide.index.min(), wide.index.max(), freq="M")
    R = wide.reindex(full)  # explicit gaps stay NaN; rolling windows do not skip them

    feats: dict[str, pd.DataFrame] = {
        "own_r1": R,
        "own_m3": R.rolling(3, min_periods=3).mean(),
        "own_m12": R.rolling(12, min_periods=12).mean(),
        "own_m36": R.rolling(36, min_periods=24).mean(),
        "own_v12": R.rolling(12, min_periods=12).std(),
    }

    m12 = feats["own_m12"]
    cty = m12.T.groupby(level="country").mean().T  # month x country
    feats["cty_m12"] = cty.reindex(columns=m12.columns.get_level_values("country")).set_axis(m12.columns, axis=1)

    hist_mean = R.expanding(min_periods=hist_min).mean()
    n_own = R.notna().cumsum()

    # Shrinkage benchmark: each series' own expanding mean pulled toward the
    # expanding mean of the same factor pooled over all countries in its region,
    # with weight n / (n + kappa). Beating the own historical mean can reflect
    # nothing more than this kind of cross-sectional pooling; beating the shrunk
    # mean is evidence of timing.
    region = panel.drop_duplicates("country").set_index("country")["region"]
    time_varying = bool((panel.groupby("country")["region"].nunique() > 1).any())
    reg = None
    if time_varying:
        # month x country region, carried across months without data, then spread to series
        reg_c = (panel.drop_duplicates(["month", "country"]).pivot(index="month", columns="country", values="region")
                 .reindex(full).ffill().bfill())
        reg = reg_c.reindex(columns=R.columns.get_level_values("country")).set_axis(R.columns, axis=1)
        feats["dm_m12"], pooled = _regional_terms(R, m12, reg)
    else:
        dm_cols = [c for c in m12.columns if region.get(c[0]) == "DM"]
        if dm_cols:
            dm = m12[dm_cols].T.groupby(level="factor").mean().T  # month x factor
            feats["dm_m12"] = dm.reindex(columns=m12.columns.get_level_values("factor")).set_axis(m12.columns, axis=1)
        else:
            feats["dm_m12"] = pd.DataFrame(np.nan, index=m12.index, columns=m12.columns)
        pooled = pd.DataFrame(np.nan, index=R.index, columns=R.columns)
        for g in region.dropna().unique():
            cols = [c for c in R.columns if region.get(c[0]) == g]
            sub = R[cols]
            s = sub.T.groupby(level="factor").sum(min_count=1).T.fillna(0).cumsum()
            n = sub.notna().T.groupby(level="factor").sum().T.cumsum()
            pm = (s / n.where(n > 0)).reindex(columns=sub.columns.get_level_values("factor"))
            pooled[cols] = pm.to_numpy()
    shrink_mean = (n_own * hist_mean + kappa * pooled) / (n_own + kappa)
    shrink_mean = shrink_mean.where(hist_mean.notna())

    # Empirical Bayes adaptive shrinkage: kappa_t = clip(within_var / between_var, 12, 600)
    within_var = R.expanding(min_periods=hist_min).var().clip(lower=1e-8)
    between_var = ((hist_mean - pooled) ** 2).expanding(min_periods=hist_min).mean().clip(lower=1e-6)
    eb_kappa = (within_var / between_var).clip(lower=12.0, upper=600.0)
    eb_shrink_mean = (n_own * hist_mean + eb_kappa * pooled) / (n_own + eb_kappa)
    eb_shrink_mean = eb_shrink_mean.where(hist_mean.notna())

    target = R.shift(-1)

    def stack(frame: pd.DataFrame, name: str) -> pd.Series:
        s = frame.stack(["country", "factor"], future_stack=True)
        s.name = name
        return s

    out = pd.concat(
        [stack(feats[k], k) for k in FEATURES]
        + [
            stack(hist_mean, "hist_mean"),
            stack(shrink_mean, "shrink_mean"),
            stack(eb_shrink_mean, "eb_shrink_mean"),
            stack(target, "target"),
        ],
        axis=1,
    )
    out.index.names = ["month", "country", "factor"]
    out = out.reset_index()
    out["region"] = stack(reg, "region").to_numpy() if time_varying else out["country"].map(region)
    out["target_month"] = out["month"] + 1
    return out


def model_ready(features: pd.DataFrame, extra: tuple[str, ...] = ()) -> pd.DataFrame:
    """Rows with a realised target, a benchmark and every feature (and extra column) observed."""
    need = FEATURES + list(extra) + ["hist_mean", "shrink_mean", "target"]
    return features.dropna(subset=need).reset_index(drop=True)