Conformal Prediction
A formal guarantee without trusting the model's own confidence
Most uncertainty estimates from deep networks — softmax scores, MC Dropout variance, even Bayesian posteriors under approximate inference — carry no formal guarantee that they're actually correct; a model can be systematically overconfident and every one of these methods will happily report confidently wrong numbers. Conformal prediction takes a different approach: instead of trusting the model's internal confidence, it uses a held-out calibration dataset to construct prediction sets (for classification) or intervals (for regression) with a mathematically guaranteed coverage probability — valid under remarkably weak assumptions, regardless of whether the underlying model is well-calibrated, miscalibrated, or even a black box.
The exchangeability assumption — and nothing else
Conformal prediction's guarantee rests on a single, comparatively mild assumption: exchangeability — the calibration data and future test data are drawn in a way that makes their joint distribution invariant to reordering, which holds whenever data is i.i.d. (a stronger but sufficient condition). Critically, conformal prediction makes no assumption about the underlying model — it works identically whether the base predictor is a linear model, a deep neural network, or a black-box API you can only query. This model-agnosticism is a major practical advantage: conformal prediction can be wrapped around literally any existing predictor as a post-hoc calibration layer.
How split conformal prediction works
The most common practical variant, split conformal prediction, works in three steps. First, train the base model on a training set as usual. Second, on a separate held-out calibration set, compute a nonconformity score for each example — typically $1 - \hat{P}(y_{\text{true}} | x)$ for classification, or $|y_{\text{true}} - \hat{y}|$ for regression — measuring how 'unusual' each true label looked to the model. Third, at test time, for a desired coverage level $1-\alpha$ (e.g. 90%), take the $(1-\alpha)$ quantile of the calibration nonconformity scores as a threshold $q$, and include in the prediction set every candidate label whose nonconformity score falls below $q$. The guarantee: across many such predictions, the true label falls in the predicted set at least $1-\alpha$ of the time, by construction.
import numpy as np
def conformal_calibrate(calib_probs, calib_true_labels, alpha=0.1):
"""calib_probs: (n_calib, n_classes) model probabilities on calibration data."""
n = len(calib_true_labels)
nonconformity = 1 - calib_probs[np.arange(n), calib_true_labels]
q_level = np.ceil((n + 1) * (1 - alpha)) / n
threshold = np.quantile(nonconformity, min(q_level, 1.0))
return threshold
def conformal_predict_set(test_probs, threshold):
"""Returns the set of classes whose nonconformity score is below threshold."""
nonconformity = 1 - test_probs # per-class score for each test example
return nonconformity <= threshold # boolean mask: True = included in the setThe guarantee is about coverage, not about informativeness
Conformal prediction vs other uncertainty approaches
| Method | Guarantee type | Model-agnostic? |
|---|---|---|
| Conformal prediction | Formal, finite-sample coverage guarantee | Yes — wraps any model |
| Softmax confidence | None (often miscalibrated) | No — specific to the model's own output |
| Temperature scaling | Improves calibration empirically, no formal guarantee | Requires access to logits |
| Bayesian / Gaussian process posterior | Valid under correct model/prior assumptions | No — requires a probabilistic model |
Where exchangeability breaks, and practical extensions
The exchangeability assumption fails in important real-world settings — most notably under distribution shift or time series data, where the future genuinely differs systematically from the calibration data, violating the core assumption the guarantee depends on. Adaptive conformal prediction and related extensions relax the guarantee to handle mild, bounded shift by continually updating the calibration set or threshold online as new data arrives, trading some of the formal finite-sample guarantee for practical robustness to a non-stationary environment. This remains an active research area precisely because the clean exchangeability assumption that makes conformal prediction so appealing is also the thing real deployment environments are most likely to violate.
What's Next?
Conformal prediction closes the loop on a theme that runs through evaluation, fairness, privacy, and robustness alike: a model's raw output is never the whole story — knowing how much to trust it, under what conditions, and with what guarantees is what turns a predictive system into one that can be deployed responsibly.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.