Coverage for scripts/v644_make_configs.py: 100%

43 statements  

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

1"""V6.4.4: write rule R-MODEL12's candidate configs (ml/evaluation/v644/configs/) from the shipped config.json. 

2 

3Every candidate is the shipped config plus the honest labels (`beta_estimation: "asof"`, #246), multitask-only scoring, 

4and a walk-forward that trains and tracks the multitask model on the candidate's own live-signal target. The grid is 

5fixed in `GRID` before any V6.4.4 score; `base_asof.json` is the dataset build's config. 

6 

7 python scripts/v644_make_configs.py [--base config.json] [--out ml/evaluation/v644/configs] 

8""" 

9 

10from __future__ import annotations 

11 

12import argparse 

13import hashlib 

14import json 

15import sys 

16from pathlib import Path 

17 

18ROOT_DIR = Path(__file__).resolve().parents[1] 

19sys.path.insert(0, str(ROOT_DIR / "scripts")) 

20 

21MT = "phase_v2.retraining.multitask_training" 

22CSI = "phase1.features.cross_sectional_inputs" 

23SECTOR_HEADS = {"sector_neutral_rank_20d": 1.0} 

24RANK_HEADS = {"rank_20d": 0.5, "rank_5d": 0.5} 

25SECTOR_PRIMARY = { 

26 f"{MT}.horizon_heads.sector_neutral_rank_20d.loss_weight": 1.0, 

27 f"{MT}.horizon_heads.rank_20d.loss_weight": 0.3, 

28 f"{MT}.early_stop_head": "sector_neutral_rank_20d", 

29} 

30RANKED = {f"{CSI}.enabled": True, f"{CSI}.group": "asset_class", f"{CSI}.mode": "replace", f"{CSI}.min_group_size": 5} 

31 

32# ID -> (config patches beyond the base, live-signal heads). Fixed before any V6.4.4 score (rules.json records the shas). 

33GRID: dict[str, tuple[dict, dict]] = { 

34 "M0": ({}, RANK_HEADS), 

35 "M1": (dict(SECTOR_PRIMARY), SECTOR_HEADS), 

36 "M2": (dict(RANKED), RANK_HEADS), 

37 "M3": ({**SECTOR_PRIMARY, **RANKED}, SECTOR_HEADS), 

38 "M4": ({**SECTOR_PRIMARY, **RANKED, f"{MT}.hidden_layers": [32, 16], f"{MT}.weight_decay": 0.04, f"{MT}.dropout": 0.35}, 

39 SECTOR_HEADS), 

40 "M5": ({**SECTOR_PRIMARY, **RANKED, f"{MT}.ranking_loss.objective": "listnet"}, SECTOR_HEADS), 

41 # The plan's "group none" ranks across classes whose bars reach live on different ticks (not reproducible live); 

42 # M6 instead keeps the raw scaled inputs AND adds their within-class ranks ("append"). 

43 "M6": ({**SECTOR_PRIMARY, **RANKED, f"{CSI}.mode": "append"}, SECTOR_HEADS), 

44} 

45 

46 

47def target_head(heads: dict) -> str: 

48 return "sector_neutral_rank_20d" if "sector_neutral_rank_20d" in heads else "rank_20d" 

49 

50 

51def base_patches() -> dict: 

52 return {"phase1.target.ranking.beta_estimation": "asof"} 

53 

54 

55def candidate_patches(grid_id: str) -> dict: 

56 patches, heads = GRID[grid_id] 

57 head = target_head(heads) 

58 return { 

59 **base_patches(), 

60 "phase_v2.rank_signal.heads": dict(heads), 

61 "phase_v2.rank_signal.model_priority": ["multitask"], 

62 "phase_v2.retraining.walk_forward.train_multitask": True, 

63 "phase_v2.retraining.walk_forward.train_sequence": False, 

64 "phase_v2.retraining.walk_forward.tracked_metrics": [ 

65 {"name": "backtest_mcc", "artifact": "training_metrics.json", "path": "backtest.mcc"}, 

66 {"name": "candidate_ic", "artifact": "multitask_training_metrics.json", 

67 "path": f"backtest.{head}_ic_non_overlapping.mean_ic"}, 

68 ], 

69 **patches, 

70 } 

71 

72 

73def write_configs(base: dict, out: Path) -> dict[str, str]: 

74 from v640_research import patch_config 

75 

76 out.mkdir(parents=True, exist_ok=True) 

77 shas = {} 

78 for name, patches in (("base_asof", base_patches()), *((grid_id, candidate_patches(grid_id)) for grid_id in GRID)): 

79 text = json.dumps(patch_config(base, patches), indent=4) + "\n" 

80 path = out / f"{name}.json" 

81 path.write_bytes(text.encode("utf-8")) 

82 shas[name] = hashlib.sha256(text.encode("utf-8")).hexdigest() 

83 return shas 

84 

85 

86def main() -> int: 

87 parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) 

88 parser.add_argument("--base", default=str(ROOT_DIR / "config.json")) 

89 parser.add_argument("--out", default=str(ROOT_DIR / "ml" / "evaluation" / "v644" / "configs")) 

90 args = parser.parse_args() 

91 shas = write_configs(json.loads(Path(args.base).read_text(encoding="utf-8")), Path(args.out)) 

92 print(json.dumps(shas, indent=1)) 

93 return 0 

94 

95 

96if __name__ == "__main__": 

97 raise SystemExit(main())