Skip to content

Conformal risk control and Learn Then Test

Control any bounded loss — not just binary miscoverage — with finite-sample guarantees.

Use it when Your metric is a bounded loss such as false negative rate (FNR), set-level F1, segmentation error, or a non-monotone operational risk.
Guarantee ConformalRiskControl: expected test loss at most alpha in finite samples for monotone losses in [0, B]. LearnThenTest: high-probability (1 - delta) simultaneous risk control for any bounded loss, even non-monotone.
In the library ConformalRiskControl, LearnThenTest
Source Angelopoulos, Bates, Fisch, Lei and Schuster (ICLR 2024); Angelopoulos, Bates, Candès, Jordan and Lei (JACM 2025)
Example needs pyconformal

Finite-sample risk guarantee

For monotone losses \(L_i(\lambda) \in [0, B]\), ConformalRiskControl chooses $\(\hat{\lambda} = \inf\left\{\lambda \in \Lambda : \frac{\sum_{i=1}^n L_i(\lambda) + B}{n + 1} \le \alpha\right\}\)$ guaranteeing \(\mathbb{E}[L_{n+1}(\hat{\lambda})] \le \alpha\) at every finite \(n\). For non-monotone losses, LearnThenTest guarantees \(\mathbb{P}(\forall \lambda \in \hat{\Lambda},\, R(\lambda) \le \alpha) \ge 1 - \delta\).

Minimal example

Copy, paste, run.

import numpy as np
from pyconformal import ConformalRiskControl, LearnThenTest

# Multi-label classification: control False Negative Rate (FNR) <= 0.15
rng = np.random.default_rng(0)
n_cal, n_test, K = 400, 400, 8
probs = rng.uniform(0.0, 1.0, size=(n_cal + n_test, K))
y = (rng.uniform(0.0, 1.0, size=(n_cal + n_test, K)) < probs).astype(int)
y[np.arange(n_cal + n_test), probs.argmax(axis=1)] = 1  # at least one positive label

def fnr_loss(p, y_true, lam):
    # Larger lam includes more labels (threshold 1 - lam), so FNR is non-increasing in lam
    pred = p >= (1.0 - lam)
    fn = ((y_true == 1) & (~pred)).sum(axis=1)
    return fn / y_true.sum(axis=1)

grid = np.linspace(0.0, 1.0, 101)
crc = ConformalRiskControl(alpha=0.15, B=1.0, loss_fn=fnr_loss)
crc.calibrate(y_pred=probs[:n_cal], y=y[:n_cal], lambdas=grid)
lam = crc.threshold()
test_fnr = fnr_loss(probs[n_cal:], y[n_cal:], lam).mean()
print(f"CRC lambda_hat={lam:.2f}  test FNR={test_fnr:.3f}")

# Learn Then Test: non-monotone candidate risks with high-probability (1 - delta) FWER control
true_risks = np.array([0.28, 0.04, 0.22, 0.03, 0.35])
L_cal = (rng.random((400, 5)) < true_risks[None, :]).astype(float)
ltt = LearnThenTest(alpha=0.10, delta=0.05, bound="binomial", correction="holm")
ltt.calibrate(losses=L_cal, lambdas=np.arange(5, dtype=float))
print("LTT valid candidate indices:", ltt.valid_lambdas_)

More