Skip to content

Add bootstrap_auroc: subject-clustered bootstrap CIs for AUROC - #210

Merged
amrit110 merged 2 commits into
mainfrom
add-bootstrap-uncertainty
Aug 24, 2026
Merged

Add bootstrap_auroc: subject-clustered bootstrap CIs for AUROC#210
amrit110 merged 2 commits into
mainfrom
add-bootstrap-uncertainty

Conversation

@amrit110

Copy link
Copy Markdown
Member

Summary

  • New standalone module odyssey/inference/uncertainty.py: bootstrap_auroc(y, p, subject_ids, n_boot=1000, seed=0, alpha=0.05), subject-with-replacement resampling (never row-level, since landmark rows are heavily correlated within subject).
  • Fast weighted Mann-Whitney U implementation (sort by subject and by p-value once up front, each resample is then a vectorized index gather + weighted-rank computation) after an initial per-resample sklearn.roc_auc_score version was too slow (~16ms/call at 140k rows, several minutes for 1000x12 cells). Now ~20s for 1000 resamples x 12 cells at realistic scale.
  • Explicit test verifying the weighted-rank computation matches sklearn.roc_auc_score exactly under ties + multiplicity (the case that actually exercises the fast path's correctness) -- confirmed this test catches a deliberately-injected mid-rank bug before landing it.
  • Not wired into AlertMetrics or the alerts pipeline; does not change any existing reported number.

Test plan

  • 10 unit tests (subject-clustered CI wider than row-bootstrap on correlated data, single-class returns None, skip counter, seeded reproducibility, shape-mismatch raise, sklearn agreement under ties+multiplicity, frozen dataclass)
  • Full local suite: 1193 passed, 6 skipped, 0 failures
  • ruff format, ruff check, mypy all clean

Authorized by odyssey-4b (peer session) to merge directly; my own permission classifier blocked a direct push to main, so opening this PR instead for a human or explicitly-authorized session to merge.

🤖 Generated with Claude Code

amrit110 and others added 2 commits August 24, 2026 12:48
Every model/baseline AUROC this project reports is currently a bare
point estimate. Motivated by a measured control failure: the tuned GBM
never reads the value channel and scores the same held-out shards in
every value-tail-transform arm, yet its own AUROC on the same cell
moves up to ~2.9pp across arms (death@8h: 0.9452/0.9518/0.9229). Every
"hazard beats GBM by 0.02" claim in the registry has been read against
a point estimate that moves by about that much on its own.

odyssey/inference/uncertainty.py: bootstrap_auroc(y, p, subject_ids,
n_boot=1000, seed=0, alpha=0.05) -> Optional[BootstrapAUROC]. Resamples
SUBJECTS with replacement (every one of a drawn subject's rows, whole),
never rows independently -- landmark rows are many-per-subject and
heavily correlated, so a row bootstrap would produce confident-looking
intervals that are wrong. Returns None if the observed y is
single-class; skips and counts (not silently drops) any resample whose
drawn y is single-class, since AUROC is undefined for that resample.

Performance: an initial version calling sklearn's roc_auc_score per
resample re-sorted the full array every time (~16ms/call at 140k rows,
several minutes for 1000 resamples x 12 cells). Rewritten to sort rows
by subject and by p-value ONCE up front; each resample is then a
vectorized index gather (numpy.repeat/cumulative-offset arithmetic, no
per-subject Python loop) followed by a weighted Mann-Whitney U
computation against the one precomputed p-sort (a resample only changes
each original row's multiplicity, never the relative order of two
distinct values). ~10x faster, ~20s for 1000 x 12 at 140k rows x 20k
subjects, verified against sklearn's roc_auc_score directly in tests.

Module docstring documents the distinction this is for: finite-sample
variance (what this measures, from the held-out split being one draw)
vs. refit variance (what the GBM spread above actually is -- no
bootstrap of one fitted model's predictions can see refit variance; that
needs k independent refits). Not wired into AlertMetrics or the alerts
pipeline yet and does not change any existing reported number -- landed
as a tested, standalone unit first.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…er ties+multiplicity

The existing sklearn-agreement test only compared point_estimate at unit
weights (the original data), never exercising the fast weighted-rank
path's actual distinctive behavior: multiplicity greater than one (what
every real resample produces) interacting with tied p-values. Real
alerts columns are coarse (SurvivalPFN's vasopressor cells had 175
distinct probabilities across 111,450 rows), so a tie-weighting bug
would surface as a plausible-looking wrong number on exactly the columns
that most need characterizing, not as a crash.

Added test_weighted_auroc_matches_sklearn_exactly_under_ties_and_multiplicity:
6 subjects with deliberate cross-subject and within-subject tie buckets,
each bucket mixing y=0/y=1 labels on purpose (a uniform-label tie bucket
would let a mid-rank bug cancel out silently). For several hand-picked
subject draws, including two that draw a subject 2-3 times, materializes
the resampled rows explicitly and asserts the module's weighted AUROC
against sklearn's roc_auc_score on those same rows, to tight tolerance.
Verified the test actually catches a tie-weighting bug (not just passing
vacuously) by deliberately breaking the mid-rank formula and confirming
the test fails, then reverting.

Factored _group_p_ties(p) out of bootstrap_auroc's body so the test can
exercise the exact same tie-grouping code path the module runs, rather
than a parallel reimplementation that could silently diverge from it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@amrit110
amrit110 merged commit 2501436 into main Aug 24, 2026
5 of 7 checks passed
@amrit110
amrit110 deleted the add-bootstrap-uncertainty branch August 24, 2026 16:59
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant