|
| 1 | +"""Statistical confidence for the leaderboard ranking (bootstrap CI + paired A/B test). |
| 2 | +
|
| 3 | +The benchmark ranks on the **deflated Sharpe** (which discounts overfit-luck) plus the |
| 4 | +**pass^k** rate (per-run reliability). Neither answers the question a leaderboard has to |
| 5 | +defend when two entries are close: *is A's number better than B's beyond seed noise, or did |
| 6 | +A just draw a kinder held-out band?* This module closes that leg (Advances in Financial |
| 7 | +Machine Learning, Ch. 19, A/B testing under sampling uncertainty): |
| 8 | +
|
| 9 | +* :func:`deflated_sharpe_ci` puts a **seed-paired bootstrap CI** around an entry's deflated |
| 10 | + Sharpe by resampling its held-out seeds (the independent sampling units) with replacement. |
| 11 | + A wide interval means the headline number rests on a few lucky seeds. |
| 12 | +* :func:`paired_dsr_diff` runs a **paired-difference significance test** across the *shared* |
| 13 | + held-out seed band: each bootstrap draw feeds the same resampled seeds to both entries, so |
| 14 | + the common price-path luck cancels and the difference isolates skill. A difference CI that |
| 15 | + straddles zero means the two entries are statistically tied. |
| 16 | +* :func:`pairwise_significance` applies the paired test down a ranked leaderboard, so each |
| 17 | + neighbouring pair is labelled ``a_better`` / ``tied`` and the ranking states which gaps are |
| 18 | + real and which are within seed noise. |
| 19 | +
|
| 20 | +The heavy lifting is the self-contained Rust core (no ``sharpebench-stats`` dependency); the |
| 21 | +deflation footprint is folded on the Rust side so a CI here brackets the same point deflated |
| 22 | +Sharpe :func:`~openoutcry.score_run` reports. Everything is deterministic in |
| 23 | +``resample_seed``, so a confidence report replays bit-for-bit. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import json |
| 29 | +from typing import Sequence |
| 30 | + |
| 31 | +from .openoutcry_py import bootstrap_dsr_ci as _bootstrap_dsr_ci |
| 32 | +from .openoutcry_py import paired_dsr_diff as _paired_dsr_diff |
| 33 | + |
| 34 | +# The scoring kernel's own bootstrap seed (``ScoreConfig::default().bootstrap_seed``), reused |
| 35 | +# so the confidence layer's resampling shares the benchmark's canonical seed by default. |
| 36 | +DEFAULT_RESAMPLE_SEED = 0x5BA7_2026 |
| 37 | +DEFAULT_N_BOOT = 2000 |
| 38 | +DEFAULT_ALPHA = 0.05 |
| 39 | + |
| 40 | +PerSeedReturns = Sequence[Sequence[float]] |
| 41 | + |
| 42 | + |
| 43 | +def deflated_sharpe_ci( |
| 44 | + per_seed_returns: PerSeedReturns, |
| 45 | + n_trials: int = 0, |
| 46 | + *, |
| 47 | + n_boot: int = DEFAULT_N_BOOT, |
| 48 | + resample_seed: int = DEFAULT_RESAMPLE_SEED, |
| 49 | + alpha: float = DEFAULT_ALPHA, |
| 50 | +) -> dict: |
| 51 | + """Seed-paired percentile bootstrap CI on an entry's deflated Sharpe. |
| 52 | +
|
| 53 | + ``per_seed_returns`` is one per-bar return series per held-out seed. ``n_trials`` is the |
| 54 | + entry's *declared* in-sample search budget (folded onto the kernel's baseline footprint |
| 55 | + Rust-side, so the CI brackets the ``score_run`` point). Returns |
| 56 | + ``{point, lo, hi, width, confidence, n_boot}``. |
| 57 | + """ |
| 58 | + rows = [list(map(float, r)) for r in per_seed_returns] |
| 59 | + return json.loads( |
| 60 | + _bootstrap_dsr_ci(rows, int(n_trials), int(n_boot), int(resample_seed), float(alpha)) |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def paired_dsr_diff( |
| 65 | + a_per_seed_returns: PerSeedReturns, |
| 66 | + b_per_seed_returns: PerSeedReturns, |
| 67 | + n_trials: int = 0, |
| 68 | + *, |
| 69 | + n_boot: int = DEFAULT_N_BOOT, |
| 70 | + resample_seed: int = DEFAULT_RESAMPLE_SEED, |
| 71 | + alpha: float = DEFAULT_ALPHA, |
| 72 | +) -> dict: |
| 73 | + """Paired-difference significance test between two entries on the **same** seed band. |
| 74 | +
|
| 75 | + ``a_per_seed_returns[i]`` and ``b_per_seed_returns[i]`` must be the two entries' return |
| 76 | + series on the *same* seed ``i`` (the pairing is what cancels the shared price-path luck). |
| 77 | + Returns ``{point_diff, lo, hi, p_value, confidence, significant, verdict, n_boot}`` with |
| 78 | + ``verdict`` one of ``"a_better"`` / ``"b_better"`` / ``"tied"``. |
| 79 | + """ |
| 80 | + a = [list(map(float, r)) for r in a_per_seed_returns] |
| 81 | + b = [list(map(float, r)) for r in b_per_seed_returns] |
| 82 | + return json.loads( |
| 83 | + _paired_dsr_diff( |
| 84 | + a, b, int(n_trials), int(n_boot), int(resample_seed), float(alpha) |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def pairwise_significance( |
| 90 | + rows: Sequence[dict], |
| 91 | + n_trials: int = 0, |
| 92 | + *, |
| 93 | + n_boot: int = DEFAULT_N_BOOT, |
| 94 | + resample_seed: int = DEFAULT_RESAMPLE_SEED, |
| 95 | + alpha: float = DEFAULT_ALPHA, |
| 96 | +) -> list[dict]: |
| 97 | + """Paired significance verdict for each adjacent pair down a ranked leaderboard. |
| 98 | +
|
| 99 | + ``rows`` are leaderboard entries (each carrying ``"policy"`` and ``"per_seed_returns"``, |
| 100 | + as produced by :func:`~openoutcry.baselines.run_baselines`). They are ranked by deflated |
| 101 | + Sharpe (desc) and each neighbouring pair ``(A, B)`` is tested; ``A`` is the higher-ranked |
| 102 | + entry, so ``verdict == "a_better"`` means the rank gap is real and ``"tied"`` means the |
| 103 | + two are within seed noise. Rows without ``"per_seed_returns"`` are skipped. |
| 104 | + """ |
| 105 | + usable = [r for r in rows if r.get("per_seed_returns")] |
| 106 | + ordered = sorted(usable, key=lambda r: r.get("deflated_sharpe", 0.0), reverse=True) |
| 107 | + out: list[dict] = [] |
| 108 | + for higher, lower in zip(ordered, ordered[1:]): |
| 109 | + diff = paired_dsr_diff( |
| 110 | + higher["per_seed_returns"], |
| 111 | + lower["per_seed_returns"], |
| 112 | + n_trials, |
| 113 | + n_boot=n_boot, |
| 114 | + resample_seed=resample_seed, |
| 115 | + alpha=alpha, |
| 116 | + ) |
| 117 | + out.append( |
| 118 | + { |
| 119 | + "a": higher.get("policy", "?"), |
| 120 | + "b": lower.get("policy", "?"), |
| 121 | + **diff, |
| 122 | + } |
| 123 | + ) |
| 124 | + return out |
| 125 | + |
| 126 | + |
| 127 | +def significance_markdown(comparisons: Sequence[dict]) -> str: |
| 128 | + """Render :func:`pairwise_significance` output as a markdown table. |
| 129 | +
|
| 130 | + One row per adjacent leaderboard pair: the ranked-above entry ``A``, the ranked-below |
| 131 | + entry ``B``, the deflated-Sharpe difference with its bootstrap CI, the two-sided p-value, |
| 132 | + and a plain-English verdict (``A > B beyond seed noise`` vs ``statistically tied``). |
| 133 | + """ |
| 134 | + lines = [ |
| 135 | + "| A (ranked above) | B (ranked below) | Deflated Sharpe diff | 95% CI | p-value | Verdict |", |
| 136 | + "|---|---|---|---|---|---|", |
| 137 | + ] |
| 138 | + for c in comparisons: |
| 139 | + verdict = ( |
| 140 | + f"{c['a']} > {c['b']} beyond seed noise" |
| 141 | + if c.get("significant") |
| 142 | + else "statistically tied" |
| 143 | + ) |
| 144 | + lines.append( |
| 145 | + "| {a} | {b} | {diff:+.4f} | [{lo:+.4f}, {hi:+.4f}] | {p:.3f} | {verdict} |".format( |
| 146 | + a=c.get("a", "?"), |
| 147 | + b=c.get("b", "?"), |
| 148 | + diff=float(c.get("point_diff", 0.0)), |
| 149 | + lo=float(c.get("lo", 0.0)), |
| 150 | + hi=float(c.get("hi", 0.0)), |
| 151 | + p=float(c.get("p_value", 1.0)), |
| 152 | + verdict=verdict, |
| 153 | + ) |
| 154 | + ) |
| 155 | + return "\n".join(lines) |
| 156 | + |
| 157 | + |
| 158 | +__all__ = [ |
| 159 | + "deflated_sharpe_ci", |
| 160 | + "paired_dsr_diff", |
| 161 | + "pairwise_significance", |
| 162 | + "significance_markdown", |
| 163 | + "DEFAULT_RESAMPLE_SEED", |
| 164 | + "DEFAULT_N_BOOT", |
| 165 | + "DEFAULT_ALPHA", |
| 166 | +] |
0 commit comments