Coverage for scripts/make_variant_configs.py: 100%

49 statements  

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

1"""Write one config.json per training variant (used with `train_multitask.py` / `train_sequence.py --config-path`). 

2 

3The control is the shipped config. Each variant changes ONE thing under `phase_v2.retraining.<model>_training` 

4(`--model multitask` by default, `--model sequence` for the same changes on the sequence model): 

5 

6 v1_gate_off gate_aware_ranking_weights.enabled = false (the missing #95 control) 

7 v2_book_heads residual/sector-neutral head loss weights = 0 (#112: capacity only for heads that trade or gate) 

8 v3_listnet ranking_loss.objective = "listnet" 

9 v4_stop_rank5d early_stop_head = "rank_5d" (the shipped book trades rank_5d, early stopping watched rank_20d) 

10 v8_rank60 a 60-day rank target and head (V6.0.0): phase1 extra_horizon_days [60], label purge 60 days, 

11 horizon_heads.rank_60d enabled (multitask only; its dataset must be built with the same config) 

12 v9_reg weight decay x16 AND dropout 0.5 together (V6.1.0: train IC ~0.37 against ~0 on validation; the 

13 single wd / dropout changes stayed inside the seed spread) 

14 v11_snr20 early stopping on the sector-neutral 20-day head, its loss weight 1.0 (V6.1.0: the only head that 

15 passes its promotion gate on validation and backtest) 

16 

17 python scripts/make_variant_configs.py <out_dir> [--base config.json] [--model multitask|sequence] 

18""" 

19 

20from __future__ import annotations 

21 

22import argparse 

23import copy 

24import json 

25from pathlib import Path 

26 

27MODEL_ROOTS = { 

28 "multitask": ("phase_v2", "retraining", "multitask_training"), 

29 "sequence": ("phase_v2", "retraining", "sequence_training"), 

30} 

31MULTITASK_PATH = MODEL_ROOTS["multitask"] 

32 

33VARIANTS: dict[str, list[tuple[tuple[str, ...], object]]] = { 

34 "v1_gate_off": [(("gate_aware_ranking_weights", "enabled"), False)], 

35 "v2_book_heads": [ 

36 (("horizon_heads", "residual_rank_5d", "loss_weight"), 0.0), 

37 (("horizon_heads", "residual_rank_20d", "loss_weight"), 0.0), 

38 (("horizon_heads", "sector_neutral_rank_20d", "loss_weight"), 0.0), 

39 ], 

40 "v3_listnet": [(("ranking_loss", "objective"), "listnet")], 

41 "v4_stop_rank5d": [(("early_stop_head",), "rank_5d")], 

42 # V5.7.0 overfitting controls (train rank_20d IC 0.44-0.52 against 0.03-0.04 on validation). 

43 "v5_wd_x4": [(("weight_decay",), 0.04)], 

44 "v6_dropout_35": [(("dropout",), 0.35)], 

45 "v7_small_net": [(("hidden_layers",), [32, 16])], 

46 "v8_rank60": [], 

47 "v9_reg": [(("weight_decay",), 0.16), (("dropout",), 0.5)], 

48 "v11_snr20": [(("early_stop_head",), "sector_neutral_rank_20d"), (("horizon_heads", "sector_neutral_rank_20d", "loss_weight"), 1.0)], 

49 # V6.2.0 (lead 2): v9_reg pushed further - rank-label noise, and/or the train-chosen feature subset, which the 

50 # session script passes as --feature-schema-path (ml/evaluation/v620/feature_schema_subset.json). 

51 "v12_reg_noise": [(("weight_decay",), 0.16), (("dropout",), 0.5), (("label_noise_std",), 0.10)], 

52 "v13_reg_subset": [(("weight_decay",), 0.16), (("dropout",), 0.5)], 

53 "v14_reg_noise_subset": [(("weight_decay",), 0.16), (("dropout",), 0.5), (("label_noise_std",), 0.10)], 

54} 

55 

56# Changes outside <model>_training, by absolute path; the leaf may be new (V6.0.0: the 60-day target). 

57GLOBAL_CHANGES: dict[str, list[tuple[tuple[str, ...], object]]] = { 

58 "v8_rank60": [ 

59 (("phase1", "target", "ranking", "extra_horizon_days"), [60]), 

60 (("phase1", "windows", "purge_label_horizon_days"), 60), 

61 ], 

62} 

63# Keys a variant may ADD under <model>_training (every other change must hit an existing key). 

64NEW_MODEL_KEYS: dict[str, list[tuple[tuple[str, ...], object]]] = { 

65 "v8_rank60": [(("horizon_heads", "rank_60d"), {"enabled": True, "loss_weight": 1.0})], 

66} 

67MULTITASK_ONLY = {"v8_rank60"} 

68 

69# The sequence model has no `hidden_layers`; its capacity knob is the TCN channel list. 

70SEQUENCE_OVERRIDES: dict[str, list[tuple[tuple[str, ...], object]]] = { 

71 "v7_small_net": [(("channels",), [16, 16])], 

72} 

73 

74 

75def apply_changes( 

76 config: dict, changes: list[tuple[tuple[str, ...], object]], model: str = "multitask", 

77 *, new_keys: list[tuple[tuple[str, ...], object]] = (), global_changes: list[tuple[tuple[str, ...], object]] = (), 

78) -> dict: 

79 """A deep copy of `config` with each dotted-path change applied under <model>_training; a missing 

80 intermediate key raises KeyError (a typo must fail loudly, not silently train the control). `new_keys` may add a 

81 leaf under <model>_training, `global_changes` set a leaf by absolute path (both need existing parents).""" 

82 result = copy.deepcopy(config) 

83 root = result 

84 for key in MODEL_ROOTS[model]: 

85 root = root[key] 

86 for path, value in changes: 

87 node = root 

88 for key in path[:-1]: 

89 node = node[key] 

90 if path[-1] not in node: 

91 raise KeyError(f"{'.'.join(path)} is not a key of {model}_training") 

92 node[path[-1]] = value 

93 for base, edits in ((root, new_keys), (result, global_changes)): 

94 for path, value in edits: 

95 node = base 

96 for key in path[:-1]: 

97 node = node[key] 

98 node[path[-1]] = copy.deepcopy(value) 

99 return result 

100 

101 

102def build_variant_configs(base_config: dict, model: str = "multitask") -> dict[str, dict]: 

103 return { 

104 name: apply_changes( 

105 base_config, SEQUENCE_OVERRIDES.get(name, changes) if model == "sequence" else changes, model, 

106 new_keys=NEW_MODEL_KEYS.get(name, []), global_changes=GLOBAL_CHANGES.get(name, []), 

107 ) 

108 for name, changes in VARIANTS.items() 

109 if model == "multitask" or name not in MULTITASK_ONLY 

110 } 

111 

112 

113def main() -> int: 

114 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 

115 parser.add_argument("out_dir") 

116 parser.add_argument("--base", default="config.json") 

117 parser.add_argument("--model", choices=sorted(MODEL_ROOTS), default="multitask") 

118 args = parser.parse_args() 

119 base = json.loads(Path(args.base).read_text(encoding="utf-8")) 

120 out = Path(args.out_dir) 

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

122 for name, config in build_variant_configs(base, args.model).items(): 

123 target = out / f"cfg_{args.model}_{name}.json" 

124 target.write_text(json.dumps(config, indent=2), encoding="utf-8") 

125 print(f"wrote {target}") 

126 return 0 

127 

128 

129if __name__ == "__main__": 

130 raise SystemExit(main())