Repository navigation
Add calibration metrics and patient-clustered bootstrap CIs - #1268
Merged
jhnwu3 merged 1 commit intoOct 8, 2026
Merged
Conversation
binary_metrics_fn had discrimination metrics and ECE, but none of the calibration measures TRIPOD+AI asks for, and PyHealth had no way to put confidence intervals on a metric or on the difference between models. - binary_metrics_fn: "brier" (sklearn), "oe_ratio" (observed/expected events), "calibration_slope" (b in y ~ a + b*logit(p)) and "calibration_intercept" (calibration-in-the-large: a with logit(p) as an offset). The logistic fits are a small unpenalised Newton solve in NumPy; no new dependency. - New pyhealth.metrics.bootstrap: bootstrap_ci and paired_bootstrap_diff. Percentile intervals; `groups=` resamples whole patients (cluster bootstrap); the paired difference scores both models on identical resamples; single-class resamples are skipped and counted; results are deterministic given `seed`. Metric = a binary_metrics_fn name or a callable. Exported from pyhealth.metrics. - tests/core/test_calibration_bootstrap.py: brier vs sklearn, O/E, a calibrated model (slope ~1, intercept ~0, O/E ~1), an overconfident one (slope ~0.5), an under-predicting one (intercept ~+1), slope equal to sklearn's unpenalised logistic regression to 4 places; bootstrap determinism, whole-patient resampling, skipped single-class resamples, identical resamples in the paired difference. - docs: pyhealth.metrics.bootstrap page; metric names in the docstring. - examples/calibration_and_bootstrap_ci.py. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Clinical prediction papers, and reporting guidelines such as TRIPOD+AI, expect:
binary_metrics_fnhad discrimination metrics and ECE only, and PyHealth had no interval utilities, so downstream projects wrote these by hand.Changes
Calibration in
binary_metrics_fn(new metric names):briersklearn.metrics.brier_score_loss)oe_ratiosum(y_true) / sum(y_prob)calibration_slopebin the logistic recalibrationy ~ a + b·logit(p); below 1 = overconfidentcalibration_interceptainy ~ a + offset(logit(p)); above 0 = risks underestimatedThe intercept follows the calibration hierarchy (Van Calster et al.): it is fitted with the slope fixed at 1. The logistic fits are a small unpenalised Newton solve in NumPy, so there's no new dependency.
New
pyhealth.metrics.bootstrap(exported frompyhealth.metrics):groups=resamples whole patients.n_skipped.seed.metricis anybinary_metrics_fnname or a callable.Trainer.evaluate(..., ci=True)is left for a follow-up.Tests, docs, example
tests/core/test_calibration_bootstrap.py(11 tests):briermatches scikit-learn;oe_ratiois exactly observed/expected;LogisticRegressionto 4 decimal places;pyhealth.metrics.bootstrap, and the new metric names in thebinary_metrics_fndocstring. The>>>examples were run as doctests.examples/calibration_and_bootstrap_ci.py: two models with identical ROC-AUC and PR-AUC, where one is clearly miscalibrated (slope 0.49 [0.33, 0.65], O/E 2.5). It also shows the paired Brier difference, whose interval excludes 0.Ran 1408 tests … OK (skipped=76).tools/check_pr_rules.pypasses.🤖 Generated with Claude Code