Coverage for scripts/v650_write_rules.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Writes ml/evaluation/v650/rules.json and its hash, before any V6.5.0 candidate has a score. Refuses to overwrite. 

2 

3 python scripts/v650_write_rules.py 

4""" 

5# ruff: noqa: E501 

6 

7from __future__ import annotations 

8 

9import hashlib 

10import json 

11import sys 

12from dataclasses import asdict 

13from pathlib import Path 

14 

15ROOT = Path(__file__).resolve().parents[1] 

16sys.path.insert(0, str(ROOT)) 

17sys.path.insert(0, str(ROOT / "scripts")) 

18 

19from v650_trials import ABLATION_IDS, ALL_SPECS, COMBINATION_IDS # noqa: E402 

20 

21from portfolio.premia_signals import WeightSettings # noqa: E402 

22 

23OUT = ROOT / "ml" / "evaluation" / "v650" 

24 

25SELECTION = {"rule_sleeves": ["2014-12-01", "2019-03-15", "2023-06-30"], "ml_sleeves": ["2019-01-01", "2021-04-01", "2023-06-30"]} 

26WINDOWS = { 

27 "verification": ["2023-07-01", "2026-11-02"], 

28 "long": ["2014-12-01", "2026-11-02"], 

29 "pre2013": ["2004-06-01", "2012-12-31"], 

30 "selection_lean": ["2014-12-01", "2023-06-30"], 

31} 

32 

33 

34def rules() -> dict: 

35 return { 

36 "written": "2026-11-04, before any V6.5.0 candidate score; text = the plan the owner started on 2026-11-04", 

37 "disclosure": [ 

38 "t3_trend_blend's 2014-2023 result was read in V6.4.4 (Problems.md #262); the V6.4.4 trend rule failed on 2023-2025.", 

39 "The installed models were trained on 2014-2021 and validated on 2022-2023 with the V5.8.0 recipe; T1/T2 recipes share its design, so the ML selection window is not unseen in that sense.", 

40 "The BT18-BT23 ablations run on the verification window, already read for this path by every run since V5.8; they compete only in R-LEAN19.", 

41 "The universe was chosen with hindsight and has no delisted names: long legs and reversal are flattered; null time_shift partly catches it.", 

42 "EDGAR earnings-surprise trials: SEC refused the generic client (HTTP 403) and no personal contact is sent without the owner's say; those 2 planned trials are closed as data unavailable, never scored, and not counted.", 

43 "Only 2-core and 4-core Codespace machines are available; nothing in the rules depends on the machine.", 

44 ], 

45 "selection_windows": SELECTION, 

46 "windows": WINDOWS, 

47 "trial_budget": 60, 

48 "weight_settings": asdict(WeightSettings()), 

49 "costs": { 

50 "equity_and_bond": "max(1.5 bps, $1) per side + 3 bps slippage", 

51 "forex": "max(0.2 bps, $2) per side + 1 bp slippage; minimum trade $5,000", 

52 "crypto": "Coinbase date-effective taker fee + 10 bps slippage, long only, 5% per name, 15% class", 

53 "financing": "debit at Lean's rate + 150 bps; stock borrow 25 bps; forex r_base - r_quote, less 50 bps on |notional|", 

54 "sensitivity": "2x costs reported for the installed candidate, not a gate", 

55 }, 

56 "risk_on_the_target_path": { 

57 "target_vol": 0.07, "gross_cap": 1.4, "max_total_drawdown_pct": 0.15, "max_daily_drawdown_pct": 0.05, 

58 "max_drawdown_velocity_pct_per_bar": "3 x 7% / sqrt(252)", "rolling_sharpe_trigger": "off", 

59 "bypass_sticky_trade_lock": "false in every verification run", 

60 }, 

61 "trials": { 

62 "sleeves": [ 

63 {"id": spec.id, "track": spec.track, "kind": spec.kind, "family": spec.family, "universe": list(spec.universe), 

64 "rebalance_every": spec.rebalance_every, "params": dict(spec.params)} 

65 for spec in ALL_SPECS 

66 ], 

67 "combinations": COMBINATION_IDS, 

68 "ablations": ABLATION_IDS, 

69 }, 

70 "R-INSTR1": "G0. Daily return correlation engine vs Lean >= 0.90 on BT13, BT14 and BT15, annual return gap <= 100 bps, within <= 3 fix cycles. If it fails, thin Lean itself scores the trials and each survivor gets 20 Lean placebos instead of 200 offline.", 

71 "R-SLEEVE2": "G1 (evaluation.premia_research.selection_gate). Net of costs and financing, median over 5 rebalance phases: Sharpe >= 0.40 in both halves and >= 0.50 pooled on its selection window, and p < 0.05 against each applicable null (200 offline placebos per null: asset_block and time_shift for rankings, sign_block and time_shift for directions and timing). Among survivors daily correlation > 0.7 drops the lower Sharpe; at most 5 remain, by pooled Sharpe. Zero survivors go to R-LEAN19.", 

72 "PBO": "evaluation.pbo over every selection-window trial's daily returns. PBO >= 0.5 goes to R-LEAN19.", 

73 "T6": "Over the survivors only: t6_inverse_vol (trailing-year inverse volatility) and t6_hrp (compute_hrp_weights), monthly, never fitted to returns; the one with the higher selection Sharpe goes on. A single survivor is its own combination.", 

74 "R-LEAN20": "G2. The T6 winner in Lean over 2014-12-01..2023-06-30: alpha Newey-West t >= 2.0 vs SPY/AGG/BTC, drawdown <= 15%, complete run. Failure goes to R-LEAN19 with no verification read.", 

75 "R-EDGE1": { 

76 "freeze": "Candidate weight code and config hashed and recorded in the local vault before any row after 2023-06-30 is generated.", 

77 "E1": "Lean Sharpe >= 1.00 on 2023-07-01..2026-11-02: median of 5 phase offsets; the median run becomes the pin.", 

78 "E2": "Alpha Newey-West t >= 3.0 vs SPY/AGG/BTC on Lean 2014-12-01..2026-11-02.", 

79 "E3": "Deflated Sharpe >= 0.95 on the E2 series: N = 78 legacy trials + all V6.5.0 candidates; variance = same-window V6.5.0 candidates, floored at 0.04. The legacy-variance figure is reported beside it and does not block (owner saw this choice at the start).", 

80 "E4": "Lean placebos on the E1 window: 40 per null, the candidate's ex-T-bill Sharpe beaten by at most 1 in each set (p = 2/41).", 

81 "E5": "Lean on data_pre2013: ex-T-bill Sharpe > 0 and alpha > 0 at the same costs; 20 placebos reported, not blocking.", 

82 "reads": "Each once; no iteration on a verification window after a failure.", 

83 }, 

84 "R-LEAN19": "Fallback when R-EDGE1 fails or is not reached. Pool: the T6 winner, every G1 survivor alone, BT18-BT23. Each gets 5 phases on 2023-07-01..2026-11-02 (the gated BT18-BT23 path runs its one configured phase). The best by median ex-T-bill Sharpe installs iff: Lean Sharpe >= BT11 + 0.10 (>= -0.104); ex-T-bill Sharpe >= BT11 + 0.10 (>= 0.368), which leverage alone cannot pass; vol <= 10%; drawdown <= 15%; gross <= 2x; complete run with no engine-log errors; long-window ex-T-bill Sharpe > 0 where it can be scored. README label: 'improved, not verified'. No pass: BT11 stays.", 

85 "R-ALPHA2": "Descriptive: the E2/E3 figures of whatever is pinned at the end, reported for every candidate run.", 

86 "placebos_and_plumbing": "Excluded from N and from the variance: they are nulls or tests of the machinery and can never be selected (owner saw this choice at the start).", 

87 } 

88 

89 

90def main() -> int: 

91 OUT.mkdir(parents=True, exist_ok=True) 

92 path = OUT / "rules.json" 

93 if path.exists(): 

94 print(f"{path} exists; rules are written once", file=sys.stderr) 

95 return 1 

96 text = json.dumps(rules(), indent=1) 

97 path.write_text(text, encoding="utf-8", newline="\n") 

98 digest = hashlib.sha256(text.encode("utf-8")).hexdigest() 

99 (OUT / "rules.sha256").write_text(digest + "\n", encoding="utf-8", newline="\n") 

100 print(digest) 

101 return 0 

102 

103 

104if __name__ == "__main__": 

105 raise SystemExit(main())