Coverage for scripts/v643_rcal4.py: 100%
43 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""R-CAL4 (V6.4.3, rule in ml/evaluation/v643/r_cal4_rule.json): score one candidate simulator option against the BT5 / BT7 / BT8
2Lean runs and decide it by the rule. The candidate is chosen from BT5's ladder (`scripts/lean_calibration.py`): there the
3`live_gates` rung (live's resize threshold, entry cooldown and trailing stop replicas) lowers the daily correlation, so
4C1 = the calibrated rung without those replicas (next-open fills, the fee floor, learned-topology sizing).
6 python scripts/v643_rcal4.py [--calibration ml/evaluation/v643/calibration.json] [--out ml/evaluation/v643/r_cal4_result.json]
7"""
9from __future__ import annotations
11import argparse
12import json
13import sys
14from pathlib import Path
16ROOT_DIR = Path(__file__).resolve().parents[1]
17sys.path.insert(0, str(ROOT_DIR))
18sys.path.insert(0, str(ROOT_DIR / "scripts"))
20WINDOWS = {
21 "bt5": "bt5=backtests/2026-10-22_10-56-13@2023-07-01..2026-10-16:backtest+holdout",
22 "bt7": "bt7=backtests/2026-10-24_05-28-07@2023-07-01..2026-10-16:backtest+holdout",
23 "bt8": "bt8=backtests/2026-10-24_18-50-22@2023-07-01..2026-10-16:backtest+holdout",
24}
25CANDIDATE = "c1_no_live_gates"
26CANDIDATE_RUNG = {CANDIDATE: {"base": "min_fee", "topology": "mid"}}
27MIN_GAIN = 0.05
28BRANCH_A = 0.9
31def verdict(base: dict, cand: dict) -> dict:
32 """R-CAL4's clauses: chosen on BT5 (gain >= +0.05 there), BT7 and BT8 not lower."""
33 gains = {name: (cand[name] - base[name]) if cand.get(name) is not None and base.get(name) is not None else None for name in base}
34 checks = {
35 "bt5_gain_ge_0.05": gains["bt5"] is not None and gains["bt5"] >= MIN_GAIN,
36 "bt7_not_lower": gains["bt7"] is not None and gains["bt7"] >= 0.0,
37 "bt8_not_lower": gains["bt8"] is not None and gains["bt8"] >= 0.0,
38 }
39 adopted = all(checks.values())
40 best = cand if adopted else base
41 return {"candidate": CANDIDATE, "base_correlation": base, "candidate_correlation": cand, "gains": gains, "checks": checks,
42 "adopted": adopted, "branch": "A" if all(value is not None and value >= BRANCH_A for value in best.values()) else "B"}
45def main() -> int:
46 from lean_calibration import BOOK_COMPONENTS, parse_window, run_window
48 from evaluation.sim_lean_calibration import gap_report
50 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
51 parser.add_argument("--calibration", default=str(ROOT_DIR / "ml" / "evaluation" / "v643" / "calibration.json"))
52 parser.add_argument("--dataset", default=str(ROOT_DIR / "ml" / "datasets" / "full_dataset.csv"))
53 parser.add_argument("--out", default=str(ROOT_DIR / "ml" / "evaluation" / "v643" / "r_cal4_result.json"))
54 args = parser.parse_args()
55 calibration = json.loads(Path(args.calibration).read_text(encoding="utf-8"))
56 config = json.loads((ROOT_DIR / "config.json").read_text(encoding="utf-8"))
57 base, cand, rows = {}, {}, {}
58 for name, text in WINDOWS.items():
59 base[name] = calibration["windows"][name]["variants"]["calibrated"]["daily_correlation"]
60 report = run_window(parse_window(text), Path(args.dataset), config, BOOK_COMPONENTS, extra_ladder=CANDIDATE_RUNG)
61 rows[name] = report["variants"][CANDIDATE]
62 cand[name] = rows[name].get("daily_correlation")
63 result = verdict(base, cand)
64 result["candidate_rows"] = {name: {key: value for key, value in row.items() if key != "disagreement"} for name, row in rows.items()}
65 result["gap_report"] = gap_report(calibration)
66 Path(args.out).write_text(json.dumps(result, indent=1, default=str), encoding="utf-8")
67 print("R-CAL4:", json.dumps({key: result[key] for key in ("gains", "checks", "adopted", "branch")}))
68 return 0
71if __name__ == "__main__":
72 raise SystemExit(main())