SPB Git

Repository Public Research preview

05_monthly.py 2.8 KB 61 lines python

1"""Monthly panel: identified CPU shocks (pre-specified shock set 3) x fossil exposure."""2import numpy as np3import pandas as pd45from config import DER, EXCLUDE_BASELINE, RES, RUB_END, SAMPLE_END, SAMPLE_START6from panel import fe_ols7from sample import _abs_gap, exposure89LEV = ["cr25", "cr10", "atm", "bf10", "skew25"]10CTL = ["r_brent", "d_vix", "r_usd", "d_y2"]11SHOCKS = ["cpu_shock", "cpu_instrument", "u_cpu_narrow", "u_cpu_broad", "u_cpu_gavriilidis", "u_cpnews_narrow"]121314def monthly_panel(tenor="1M", fcol="F_netx_pre", exclude=EXCLUDE_BASELINE):15    p = pd.read_parquet(DER / "fx_panel_daily.parquet")16    p = p[(p.tenor == tenor) & (p.date >= "2005-12-01") & (p.date <= SAMPLE_END) & ~p.ccy.isin(exclude)]17    p = p[~((p.ccy == "RUB") & (p.date > RUB_END))].sort_values(["ccy", "date"])18    p["m"] = p.date.dt.to_period("M")19    last = p.groupby(["ccy", "m"]).tail(1)20    last = last[last.date.dt.day >= 20]                      # month-end quote must be from the last third21    q = last.set_index(["ccy", "m"])[LEV]22    d = q.groupby(level=0).diff()23    gap = last.set_index(["ccy", "m"]).index.to_frame()["m"].groupby(level=0).diff().apply(lambda x: x.n if pd.notna(x) else np.nan)24    d = d[gap == 1].add_prefix("d_").reset_index()25    sh = pd.read_parquet(DER / "monthly_shocks.parquet")26    for c in SHOCKS:                                         # unit-variance over the estimation sample27        s = sh.loc["2006-01":"2025-06", c]28        sh[c] = (sh[c] - s.mean()) / s.std()29    dly = pd.read_parquet(DER / "daily_shocks.parquet")30    mc = dly[["r_brent", "d_vix", "r_usd", "d_y2", "r_spx"]].groupby(dly.index.to_period("M")).sum()31    d = d.merge(sh[SHOCKS].join(mc), left_on="m", right_index=True, how="left")32    d = d[(d.m >= "2006-01")]33    ccys = sorted(d.ccy.unique())34    d["F"] = d.ccy.map(exposure(fcol, ccys))35    d["absF"] = d.ccy.map(_abs_gap(fcol, ccys))36    d["date"] = d.m.dt.to_timestamp()37    return d383940def main():41    d = monthly_panel()42    for c in ["d_" + v for v in LEV]:43        lo, hi = d[c].quantile([.005, .995]); d[c] = d[c].clip(lo, hi)44    rows = []45    for y in ["d_cr25", "d_cr10", "d_skew25", "d_bf10", "d_atm"]:46        for s in SHOCKS:47            for ch in (["F"] if y != "d_bf10" else ["F", "absF"]):48                X = []49                for v in [s] + CTL:50                    d[f"{v}:{ch}"] = d[v] * d[ch]; X.append(f"{v}:{ch}")51                f = fe_ols(d, y, X)52                rows.append(dict(y=y, shock=s, char=ch, b=f.b[0], se=f.se()[0], t_2way=f.t()[0], t_dk=f.t("dk")[0],53                                 t_unit=f.t("unit")[0], n=f.n, N=f.ni, T=f.nt))54    r = pd.DataFrame(rows); r.to_csv(RES / "monthly_cpu.csv", index=False)55    print(r.round(4).to_string())56    print(d[["d_cr25", "d_cr10", "d_bf10", "d_atm"]].std())575859if __name__ == "__main__":60    main()61
Files main
Menu

Theme