Skip to content

Implements real APS & dynamic scoring for Conformal Prediction - #1189

Merged
jhnwu3 merged 4 commits into
sunlabuiuc:masterfrom
lehendo:cpdynascoring
Aug 18, 2026
Merged

Implements real APS & dynamic scoring for Conformal Prediction#1189
jhnwu3 merged 4 commits into
sunlabuiuc:masterfrom
lehendo:cpdynascoring

Conversation

@lehendo

@lehendo lehendo commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator
  • APS + score_type implementation across 5 classes
  • doctests on all 4 new public functions in scores.py (verified with a real doctest run)
  • score_type="aps" usage examples added to all 5 class docstrings
  • tests/core/test_scores.py (13 tests: formula correctness, monotonicity, reproducibility, empirical coverage) + 3 new score_type="aps" end-to-end cases added to existing test files
  • Docs updated: added the missing BaseConformal entry, documented score_type, referenced the new scores module
  • CI gate (tools/check_pr_rules.py) passes
  • All 50 relevant tests pass on real hardware

Adds pyhealth/calib/predictionset/scores.py, a shared score module
implementing genuine Adaptive Prediction Sets (Romano, Sesia, and
Candes 2020): nonconformity score = cumulative sum of predicted
probabilities for classes ranked above the target, plus a randomized
U*p(target) term (U ~ Uniform(0,1), one draw per example, shared
across all candidate classes). Previously, BaseConformal's
score_type="aps" was silently just an alias for "threshold" and did
not implement APS at all.

Threads a new score_type parameter ("threshold" [default, unchanged
behavior] or "aps") through BaseConformal, LABEL, ClusterLabel,
CovariateLabel, and NeighborhoodLabel, each with an optional
random_state for reproducible APS randomization. SCRIB and FavMac are
intentionally excluded since their calibration isn't a score-then-
quantile pattern.

Verified via numpy-only synthetic tests: both score types hit ~90%
empirical coverage at alpha=0.1, for marginal and class-conditional
coverage, in both nonconformity and conformity sign conventions.
Adds >>> usage examples to the 4 public functions in
pyhealth/calib/predictionset/scores.py (verified against real computed
output). Adds a score_type="aps" usage example to the docstrings of
BaseConformal, LABEL, ClusterLabel, CovariateLabel, and
NeighborhoodLabel.

Adds tests/core/test_scores.py covering both score types: threshold
backward-compatibility, the APS formula's hand-computable non-randomized
case, monotonicity, reproducibility under a seeded RNG, nonconformity/
conformity complementarity, and empirical marginal coverage at the
target alpha. Extends test_cluster_label.py, test_covariate_label.py,
and test_neighborhood_label.py with score_type="aps" end-to-end cases.

Documents the score_type argument and adds the previously-missing
BaseConformal entry to docs/api/calib/pyhealth.calib.predictionset.rst.
CI's ruff install (pip install 'ruff~=0.15') resolves to the latest
0.x release under PEP 440 compatible-release semantics, which enabled
more pyupgrade/ruff-specific default rules than the older cached ruff
used for local verification. Fixes all 13 flagged violations:
Optional[X]/Union[X, Y] -> X | Y, typing.Dict -> dict (including the
now-modernized pre-existing forward() return annotations this forced),
an unused unpacked variable, and an unsorted __all__.

Verified by reproducing the CI's exact environment: a clean venv with
`pip install 'ruff~=0.15'` (which also resolves to 0.16.3), confirming
`tools/check_pr_rules.py` now passes.
@jhnwu3

jhnwu3 commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Any idea on how long these tests take?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@jhnwu3 jhnwu3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Lmk if any issues.

@jhnwu3
jhnwu3 merged commit 0a75f99 into sunlabuiuc:master Aug 18, 2026
2 checks passed
@lehendo
lehendo deleted the cpdynascoring branch August 18, 2026 01:05
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.

3 participants