Coverage for scripts/v661_sanity.py: 100%

32 statements  

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

1"""Can the pooled book see an edge at all? An oracle must score hugely, its reversal hugely badly, a random signal about zero less costs. Plumbing: these enter no 

2ledger, no PBO and no count of trials, and read only cell S. 

3 

4 python scripts/v661_sanity.py 

5""" 

6 

7from __future__ import annotations 

8 

9import json 

10import sys 

11from pathlib import Path 

12 

13import numpy as np 

14import pandas as pd 

15 

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

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

18 

19from evaluation.premia_research import score_sleeve # noqa: E402 

20from evaluation.wide_context import SECTORS, equity_cell, signal_ic_t # noqa: E402 

21from portfolio.premia_signals import POOLED, SleeveSpec # noqa: E402 

22 

23OUT = ROOT / "ml" / "evaluation" / "v661" / "sanity_pooled.json" 

24 

25 

26def main() -> int: 

27 base = json.loads((ROOT / "config.json").read_text(encoding="utf-8")) 

28 cell = equity_cell(ROOT, base, "S", with_info=False) 

29 label = cell.label.where(cell.member) 

30 rng = np.random.default_rng(661) 

31 random_table = pd.DataFrame(rng.normal(size=label.shape), index=label.index, columns=label.columns).where(cell.member) 

32 report = {} 

33 for name, table in {"oracle": label, "reversed_oracle": -label, "random": random_table}.items(): 

34 cell.context.panel.extras[name] = table 

35 spec = SleeveSpec(name, "sanity", POOLED, "external_score", SECTORS, 20, {"table": name}) 

36 score = score_sleeve(spec, cell.context, start=cell.cell.start, split=cell.cell.split, end=cell.cell.end, signal=table) 

37 score.pop("daily") 

38 ic = signal_ic_t(table, cell.label, start=cell.cell.start, end=cell.cell.end) 

39 report[name] = {"sharpe": score["sharpe"], "first_half": score["sharpe_first_half"], "second_half": score["sharpe_second_half"], "mean_gross": score["mean_gross"], "turnover": score["turnover"], "mean_ic": ic["mean_ic"]} 

40 print(name, report[name], flush=True) 

41 OUT.parent.mkdir(parents=True, exist_ok=True) 

42 OUT.write_text(json.dumps(report, indent=1), encoding="utf-8", newline="\n") 

43 return 0 

44 

45 

46if __name__ == "__main__": 

47 raise SystemExit(main())