"""H5 (exploratory): cost-aware signal evaluation from saved predictions (runs where pred_*.npz live). Median of the cumulative return = sum of per-minute Student-t locations (symmetric marginals).""" from __future__ import annotations import argparse import json import sys from pathlib import Path import numpy as np import pandas as pd ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from chronos_x.eval.economic import signal_pnl # noqa: E402 def main() -> None: ap = argparse.ArgumentParser(); ap.add_argument("--exp", default="main"); ap.add_argument("--tag", default="test") ap.add_argument("--models", default="mrpt_small,mrpt_base,mlp,tcn"); ap.add_argument("--horizons", default="5,15,30,60") a = ap.parse_args() d = ROOT / "results" / "metrics" / a.exp tab = pd.read_csv(d / f"origins_{a.tag}.csv") out = [] for m in a.models.split(","): f = d / f"pred_{m}_{a.tag}.npz" if not f.exists(): continue p = np.load(f) mu = p["mu"]; ym = p["y_mask"] for H in (int(h) for h in a.horizons.split(",")): med = (mu[:, :H] * ym[:, :H]).sum(1) real = tab[f"cum_{H}"].values assert np.allclose((p["y"][:, :H] * ym[:, :H]).sum(1), real, atol=1e-6) for cost in (0.0, 1.0, 2.5, 5.0, 10.0): for k in (1.0, 2.0): r = signal_pnl(med, real, tab, cost_bps=cost, k=k) out.append({"model": m, "H": H, **r}) # information content without trading: correlation and regression slope of realised on median sl = np.polyfit(med, real, 1)[0] if med.std() > 0 else np.nan out.append({"model": m, "H": H, "cost_bps": -1, "k": 0, "corr": float(np.corrcoef(med, real)[0, 1]), "slope": float(sl), "n": int(len(med)), "median_abs_bps": float(np.abs(med).mean() * 1e4)}) df = pd.DataFrame(out); df.to_csv(d / f"economic_{a.tag}.csv", index=False) print(df.to_string(index=False, float_format=lambda x: f"{x:.4g}")) if __name__ == "__main__": main()