Conformal classification under label shift¶
Weighted conformal prediction sets that maintain coverage when class priors \(P(Y)\) shift between calibration and deployment while \(P(X \mid Y)\) remains fixed.
| Use it when | Class prevalence changes in production (e.g. fraud or disease rate surges) while the appearance of each class stays the same. |
| Guarantee | Exact finite-sample coverage \(\ge 1 - \alpha\) on the shifted target distribution with known class_weights; automatic EM estimation (class_weights="mlls") from unlabeled test probabilities when target priors are unknown. |
| In the library | LabelShiftConformalClassifier, estimate_label_shift_weights |
| Source | Podkopaev and Ramdas (UAI 2021) |
| Example needs | pyconformal |
Label-shift guarantee
Reweighting calibration scores by class importance ratios \(w(y) = P_{\text{test}}(Y=y) / P_{\text{cal}}(Y=y)\) and evaluating candidate class \(y\) with test weight \(w(y)\) yields exact finite-sample coverage \(\mathbb{P}_{\text{test}}(Y_{n+1} \in \hat{C}(X_{n+1})) \ge 1 - \alpha\) under label shift.
Minimal example¶
Copy, paste, run.
import numpy as np
from pyconformal import LabelShiftConformalClassifier
rng = np.random.default_rng(0)
means = np.array([[0.0, 0.0], [2.5, 0.0], [0.0, 2.5]])
scales = np.array([0.8, 1.5, 0.9])
p_src, p_tgt = np.array([0.70, 0.10, 0.20]), np.array([0.15, 0.70, 0.15])
def draw(priors, n):
y = rng.choice(3, size=n, p=priors)
X = means[y] + rng.normal(size=(n, 2)) * scales[y, None]
logp = np.column_stack([
np.log(p_src[k]) - 0.5 * np.sum(((X - means[k]) / scales[k]) ** 2, axis=1) - 2 * np.log(scales[k])
for k in range(3)
])
P = np.exp(logp - logp.max(axis=1, keepdims=True))
return y, P / P.sum(axis=1, keepdims=True)
y_cal, P_cal = draw(p_src, 1200)
y_test, P_test = draw(p_tgt, 1200)
clf = LabelShiftConformalClassifier(alpha=0.1, score="lac", class_weights="mlls")
clf.calibrate(proba=P_cal, y=y_cal)
sets = clf.predict_set(proba=P_test)
print(f"MLLS target coverage: {sets[np.arange(1200), y_test].mean():.3f} estimated weights: {np.round(clf.estimated_weights_, 2)}")
More¶
- Full argument list: API reference
- All methods: what's inside