"""Replication entry point for F6-conf-02. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py Reads only data/gc5y_daily.csv; writes out/results.json, out/figure1.png, out/table1.csv. No randomness is used (random_seed = null).""" import json, os import numpy as np import pandas as pd from scipy import stats import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt DATA = "data/gc5y_daily.csv" OUT = "out" PRE_N = 120 os.makedirs(OUT, exist_ok=True) d = pd.read_csv(DATA, dtype={"date": str}) n_rows = len(d) d["dt"] = pd.to_datetime(d["date"], format="%Y-%m-%d") issues = [] if d["yield_5y"].isna().any() or d["post_cut"].isna().any(): issues.append({"issue": "missing values", "handling": "rows with missing yield excluded"}) assert d["date"].is_unique and d["dt"].is_monotonic_increasing # one-day change between consecutive rows, dated t d["chg_bp"] = 100 * d["yield_5y"].diff() issues.append({"issue": "first row has no preceding row, so no one-day change", "handling": "excluded from all change-based statistics (2497 changes from 2498 rows)"}) sample_start, sample_end = d["date"].iloc[0], d["date"].iloc[-1] n_treated = int((d["post_cut"] == 1).sum()) baseline_metric = float(d["chg_bp"].mean()) model_metric = effect = lo = hi = n_analysed = None feasibility, reason, conclusion = "complete", None, None if n_treated == 0: issues.append({"issue": f"file ends {sample_end} (README retrieval end_date=2019-12-31); post_cut=0 for all " f"{n_rows} rows, so no observation on or after 2020-03-04 exists", "handling": "treated window empty; effect, interval, model RMSE and n_analysed set to null; " "post_cut not redefined and no external/proxy data substituted; conclusion premise_invalid"}) feasibility = "infeasible" reason = ("The file covers only 2010-01-04 to 2019-12-31; there are zero rows with post_cut = 1, " "so the post-cut mean change (and hence the effect) cannot be computed.") conclusion = "premise_invalid" else: first = int(np.flatnonzero(d["post_cut"].values == 1)[0]) if first - PRE_N < 1: feasibility, conclusion = "infeasible", "premise_invalid" reason = "Fewer than 120 one-day changes precede the first post_cut=1 row." else: post = d.loc[d["post_cut"] == 1, "chg_bp"].dropna().values pre = d["chg_bp"].iloc[first - PRE_N:first].values effect = float(post.mean() - pre.mean()) v1, v0 = post.var(ddof=1) / len(post), pre.var(ddof=1) / len(pre) se = np.sqrt(v1 + v0) df = (v1 + v0) ** 2 / (v1**2 / (len(post) - 1) + v0**2 / (len(pre) - 1)) q = stats.t.ppf(0.975, df) lo, hi = float(effect - q * se), float(effect + q * se) resid = np.concatenate([post - post.mean(), pre - pre.mean()]) model_metric = float(np.sqrt(np.mean(resid**2))) n_analysed = int(len(post) + len(pre)) conclusion = "positive_effect" if lo > 0 else "negative_effect" if hi < 0 else "no_detectable_effect" results = { "task_id": "F6-conf-02", "sample_start": sample_start, "sample_end": sample_end, "n_treated": n_treated, "baseline_metric": round(baseline_metric, 6), "model_metric": model_metric, "effect_estimate": effect, "effect_ci_low": lo, "effect_ci_high": hi, "conclusion": conclusion, "feasibility": feasibility, "feasibility_reason": reason, "data_issues": issues, "n_input_rows": {"gc5y_daily.csv": int(n_rows)}, "n_analysed": n_analysed, "random_seed": None, } with open(os.path.join(OUT, "results.json"), "w") as f: json.dump(results, f, indent=2) # Table 1: per calendar year d["year"] = d["dt"].dt.year t = d.groupby("year").agg(n_days=("yield_5y", "size"), mean_yield=("yield_5y", "mean"), mean_change_bp=("chg_bp", "mean")).reset_index() t.round(4).to_csv(os.path.join(OUT, "table1.csv"), index=False) # Figure 1 fig, ax = plt.subplots(figsize=(9, 4)) ax.plot(d["dt"], d["yield_5y"], lw=0.8) ax.axvline(pd.Timestamp("2020-03-04"), color="red", ls="--", lw=1, label="2020-03-04 cut (outside sample)") ax.set_xlabel("date"); ax.set_ylabel("5-year GoC yield (%)") ax.set_title(f"GoC 5-year benchmark yield, {sample_start} to {sample_end} (no post-cut data)") ax.legend(loc="upper right"); fig.tight_layout() fig.savefig(os.path.join(OUT, "figure1.png"), dpi=120) print(json.dumps({k: results[k] for k in ["sample_end", "n_treated", "baseline_metric", "conclusion"]}))