Coverage for evaluation/power_analysis.py: 100%

22 statements  

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

1"""What a t-test on an alpha can and can't show, given how much history exists (V6.6.0). 

2 

3An alpha Sharpe of s over T years has a t of about s * sqrt(T), so a bar of t >= 3 is out of reach of a real but modest edge on a 

4short window, and a failed test proves little unless it was powerful. A refutation quotes `excluded_alpha_sharpe` instead of "no 

5alpha". Normal approximation throughout, with daily data's autocorrelation ignored: figures to compare, not to decimal places. 

6""" 

7 

8from __future__ import annotations 

9 

10import math 

11from statistics import NormalDist 

12from typing import TYPE_CHECKING, Any 

13 

14if TYPE_CHECKING: 

15 from collections.abc import Sequence 

16 

17_NORMAL = NormalDist() 

18 

19 

20def minimum_detectable_alpha_sharpe(years: float, t_bar: float = 3.0) -> float: 

21 """The alpha Sharpe whose expected t just reaches `t_bar` over `years`.""" 

22 if years <= 0.0: 

23 return math.inf 

24 return float(t_bar) / math.sqrt(years) 

25 

26 

27def power_of_t_test(alpha_sharpe: float, years: float, t_bar: float = 3.0) -> float: 

28 """Chance that a true alpha Sharpe of `alpha_sharpe` clears `t_bar` over `years`.""" 

29 if years <= 0.0: 

30 return 0.0 

31 return 1.0 - _NORMAL.cdf(float(t_bar) - float(alpha_sharpe) * math.sqrt(years)) 

32 

33 

34def excluded_alpha_sharpe(observed_t: float, years: float, confidence: float = 0.95) -> float: 

35 """One-sided upper bound on the true alpha Sharpe, given the t observed: true values above this are rejected at `confidence`.""" 

36 if years <= 0.0: 

37 return math.inf 

38 return (float(observed_t) + _NORMAL.inv_cdf(float(confidence))) / math.sqrt(years) 

39 

40 

41def fundamental_law_ir(ic: float, names: int, rebalances_per_year: float, transfer: float = 1.0) -> float: 

42 """Grinold's IR = transfer * IC * sqrt(breadth), breadth = independent bets a year. Names aren't independent, so this is a ceiling.""" 

43 breadth = max(0.0, float(names) * float(rebalances_per_year)) 

44 return float(transfer) * float(ic) * math.sqrt(breadth) 

45 

46 

47def power_table(years_list: Sequence[float]=(3.3, 8.0, 15.8, 22.8), alpha_sharpes: Sequence[float]=(0.25, 0.5, 0.75, 1.0, 1.5), t_bar: float = 3.0) -> dict[str, Any]: 

48 """The table `ml/evaluation/v660/power.json` holds, written before any score of the phase exists.""" 

49 return { 

50 "t_bar": t_bar, 

51 "minimum_detectable_alpha_sharpe": {str(years): minimum_detectable_alpha_sharpe(years, t_bar) for years in years_list}, 

52 "power": { 

53 str(years): {str(sharpe): power_of_t_test(sharpe, years, t_bar) for sharpe in alpha_sharpes} for years in years_list 

54 }, 

55 "fundamental_law_ir_at_ic_0_02_monthly": { 

56 str(names): fundamental_law_ir(0.02, names, 12.0) for names in (55, 250, 500) 

57 }, 

58 }