generated from mathias/template-go-web
#12 — VaR_breach_rate_99_oos_regime_cond metric: - internal/eval/var.go: VaRBreachRate() + kupiecPOF() + LinearProbePredict() (stdlib math only) - internal/eval/var_test.go: 8 golden tests (zero/all breach, perfect calibration, boundary) - cmd/eval/main.go: -metric var flag (no-leakage probe → VaR → Kupiec P) - scripts/var_breach.py: Python equivalent with METRIC_KEY constant (13 TDD tests) - train.py LOCKED VaR EVAL BLOCK: writes VaR_breach_rate_99_oos_regime_cond + kupiec_p to metrics.json - Fixed bug: train.py used bare 'os' before import; now uses module-level '_os' consistently #13 — HMM regime detector + JEPA conditioning seam: - scripts/prepare_regime.py: GaussianHMM (diag, 3-state) on realized_vol; states sorted by mean vol (0=calm, 1=stressed, 2=crisis); deterministic (random_state=42); outputs eurusd_regime.parquet - tests/test_regime.py: 11 TDD tests (dtype, states, determinism, vol sort, daily fallback) - train.py: JEPA_ENABLE_REGIME toggle + REGIME CONDITIONING SEAM (concat baseline, agent-editable) - requirements.txt: hmmlearn>=0.3, scikit-learn>=1.4 78 Python + all Go tests green. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -36,6 +36,7 @@ PHASE1_JOINT = bool(int(_os.environ.get("JEPA_PHASE1_JOINT", 1)))
|
||||
PHASE1_JOINT_EPOCHS= int(_os.environ.get("JEPA_PHASE1_JOINT_EPOCHS", 30))
|
||||
PHASE1_ENCODER_LR = float(_os.environ.get("JEPA_PHASE1_ENCODER_LR", 3e-6))
|
||||
USE_MULTIPAIR = bool(int(_os.environ.get("JEPA_USE_MULTIPAIR", 0)))
|
||||
JEPA_ENABLE_REGIME = bool(int(_os.environ.get("JEPA_ENABLE_REGIME", 0)))
|
||||
SEED = int(_os.environ.get("JEPA_SEED", 0))
|
||||
# ---------------------------
|
||||
|
||||
@@ -171,6 +172,22 @@ def build():
|
||||
df["date"] = pd.to_datetime(df["date"])
|
||||
FEAT_COLS = ["ret", "realized_vol"]
|
||||
target_col = "realized_vol"
|
||||
# ── REGIME CONDITIONING SEAM — agent may vary this mechanism ─────────────
|
||||
# Baseline: concat regime flag as an additional feature channel (0=calm, 2=crisis).
|
||||
# Agent may swap for FiLM conditioning, learned regime embedding, or gating.
|
||||
_regime_path = "data/processed/eurusd_regime.parquet"
|
||||
if JEPA_ENABLE_REGIME and os.path.exists(_regime_path):
|
||||
_rdf = pd.read_parquet(_regime_path)
|
||||
_ts_col = "datetime" if "datetime" in _rdf.columns else "date"
|
||||
_rdf[_ts_col] = pd.to_datetime(_rdf[_ts_col])
|
||||
df = df.copy()
|
||||
df = df.merge(
|
||||
_rdf.rename(columns={_ts_col: "date"})[["date", "regime"]],
|
||||
on="date", how="left",
|
||||
)
|
||||
df["regime"] = df["regime"].fillna(0).astype(np.float32)
|
||||
FEAT_COLS = list(FEAT_COLS) + ["regime"]
|
||||
# ── END REGIME SEAM ───────────────────────────────────────────────────────
|
||||
feats = df[FEAT_COLS].to_numpy(np.float32)
|
||||
target = df[target_col].to_numpy(np.float32)
|
||||
tr_idx = df.index[df["date"].dt.year <= 2021].tolist()
|
||||
@@ -298,9 +315,18 @@ def main():
|
||||
phase1_r2 = float(1 - ((yte - pred_h) ** 2).sum() / ss_tot)
|
||||
print("phase1_r2 = %.4f (n_test=%d)" % (phase1_r2, len(yte)))
|
||||
|
||||
_metrics_out = os.environ.get("METRICS_OUT", "metrics.json")
|
||||
# ── VaR EVAL BLOCK — do NOT edit (agent boundary) ───────────────────────
|
||||
import sys as _sys
|
||||
_sys.path.insert(0, _os.path.dirname(_os.path.abspath(__file__)))
|
||||
from scripts.var_breach import var_breach_rate as _var_breach_rate, METRIC_KEY as _VAR_KEY
|
||||
_var_rate, _kupiec_p = _var_breach_rate(pred_np.tolist(), yte.tolist())
|
||||
print("%s=%.4f Kupiec_p=%.4f" % (_VAR_KEY, _var_rate, _kupiec_p))
|
||||
# ── END VaR EVAL BLOCK ───────────────────────────────────────────────────
|
||||
|
||||
_metrics_out = _os.environ.get("METRICS_OUT", "metrics.json")
|
||||
json.dump({
|
||||
"val_vol_r2": val_vol_r2, "phase1_r2": phase1_r2, "n_test": len(yte),
|
||||
_VAR_KEY: _var_rate, "kupiec_p": _kupiec_p,
|
||||
"knobs": {"WINDOW": WINDOW, "PATCH_LEN": PATCH_LEN,
|
||||
"D_MODEL": D_MODEL, "DEPTH": DEPTH, "ALPHA": ALPHA,
|
||||
"DELTA_T_MAX": DELTA_T_MAX, "EPOCHS": EPOCHS},
|
||||
|
||||
Reference in New Issue
Block a user