Simple Algorithm
Machine Learning · Convergence · 28 min

Conformal Prediction

Distribution-free prediction sets with guaranteed coverage.

Take any model, however crude, and wrap it so that its prediction sets contain the truth 90% of the time, for every data distribution, with finitely many examples. The only assumption is exchangeability. This lesson proves that guarantee and its matching upper bound, builds the methods used in practice, and then maps the limits: why coverage for each individual input is impossible without assumptions, and what can still be promised when the data shift.

What you’ll learn

  • Prove that split conformal prediction covers with probability between 1−α1-\alpha and 1−α+1/(n+1)1-\alpha+1/(n+1), and describe the coverage distribution conditional on the calibration set.
  • Construct conformal intervals from absolute residuals and by conformalized quantile regression, and classification sets with the least-ambiguous, APS and RAPS scores.
  • Explain full conformal prediction, and prove that the jackknife+ covers with probability at least 1−2α1-2\alpha while the plain jackknife guarantees nothing.
  • State the impossibility of distribution-free conditional coverage, and extend coverage to covariate shift by weighting and to arbitrary sequences by adaptive conformal inference.

One assumption, one rank

Split conformal prediction starts by splitting the data. A model μ^\hat\mu is fitted on a training part; a separate calibration set (X1,Y1),…,(Xn,Yn)(X_1,Y_1),\dots,(X_n,Y_n) and the test point (Xn+1,Yn+1)(X_{n+1},Y_{n+1}) are only scored. A nonconformity score s(x,y)s(x,y), fixed once training is done, measures how unusual a label is for an input, for example s(x,y)=∣y−μ^(x)∣s(x,y)=\abs{y-\hat\mu(x)}. Write Si=s(Xi,Yi)S_i=s(X_i,Y_i) for i=1,…,n+1i=1,\dots,n+1. The single assumption is that the n+1n+1 points are exchangeable: their joint law is invariant under permutations, which i.i.d. sampling implies (exchangeability is discussed in The i.i.d. Assumption & Its Failures).

DerivationProof
  1. Pr⁡[r=j]=1n+1,j=1,…,n+1\Pr[r=j]=\frac1{n+1},\qquad j=1,\dots,n+1

    The score function is fixed given the training data, which are independent of the calibration and test points, so the scores S1,…,Sn+1S_1,\dots,S_{n+1} are exchangeable too. With no ties, the rank rr of Sn+1S_{n+1} among all n+1n+1 scores is therefore uniform:

  2. Pr⁡[Yn+1∈C(Xn+1)]=Pr⁡[r≤k]=kn+1\Pr\big[Y_{n+1}\in C(X_{n+1})\big]=\Pr[r\le k]=\frac{k}{n+1}

    Coverage means Sn+1≤q^=S(k)S_{n+1}\le\hat q=S_{(k)}, the kk-th smallest calibration score. If r≤kr\le k, at most k−1k-1 calibration scores lie below Sn+1S_{n+1}, so S(k)≥Sn+1S_{(k)}\ge S_{n+1}. If r>kr>k, at least kk of them do, so S(k)<Sn+1S_{(k)}<S_{n+1}. Hence:

  3. 1−α ≤ kn+1 < 1−α+1n+11-\alpha\ \le\ \frac{k}{n+1}\ <\ 1-\alpha+\frac1{n+1}

    Since (n+1)(1−α)≤k<(n+1)(1−α)+1(n+1)(1-\alpha)\le k<(n+1)(1-\alpha)+1, both bounds follow. With ties, break them uniformly at random: the randomized rank is still uniform, and r≤kr\le k still forces Sn+1≤S(k)S_{n+1}\le S_{(k)}, so the lower bound survives, while the upper one can fail (a discrete score with many ties can overcover):

Predict before you look

At α=0.05\alpha=0.05, what is the smallest calibration set size nn for which the split conformal threshold q^\hat q is finite?

Show the answer

19 points

q^\hat q is finite when k=⌈(n+1)⋅0.95⌉≤nk=\lceil(n+1)\cdot0.95\rceil\le n, which needs 0.95(n+1)≤n0.95(n+1)\le n, i.e. n≥19n\ge19. At n=19n=19, k=19k=19: the threshold is the largest calibration score, and with continuous scores the coverage is exactly 19/20=95%19/20=95\%. With 18 points the method must return the whole real line, because no finite threshold can be certified.

The guarantee is marginal: it averages over the calibration set as well as the test point. For one fixed calibration set, the coverage over future test points is a random variable, and Vovk (2012) showed that for i.i.d. data with continuous scores it follows a Beta distribution:

Pr⁡[Yn+1∈C(Xn+1) ∣ (Xi,Yi)i=1n] ∼ Beta(n+1−l, l),l=⌊(n+1)α⌋.\Pr\big[Y_{n+1}\in C(X_{n+1})\ \big|\ (X_i,Y_i)_{i=1}^n\big]\ \sim\ \mathrm{Beta}(n+1-l,\ l),\qquad l=\lfloor(n+1)\alpha\rfloor.

Its mean is (n+1−l)/(n+1)≥1−α(n+1-l)/(n+1)\ge1-\alpha and its standard deviation is about α(1−α)/n\sqrt{\alpha(1-\alpha)/n}. With n=1,000n=1{,}000 and α=0.1\alpha=0.1 that is about 0.00950.0095, so a single calibration set typically delivers coverage between 88% and 92% (Angelopoulos & Bates, 2023, recommend on the order of a thousand calibration points for this reason).

Scores that adapt

Validity holds for every score; the score decides the shape of the prediction sets, and hence their usefulness. The absolute residual gives intervals μ^(x)±q^\hat\mu(x)\pm\hat q of constant width: honest on average, too wide where the noise is small and too narrow where it is large. Conformalized quantile regression (CQR; Romano, Patterson & Candès, 2019) starts from quantile regressions q^lo\hat q_{\text{lo}} and q^hi\hat q_{\text{hi}} at levels α/2\alpha/2 and 1−α/21-\alpha/2, fitted on the training part, and conformalizes their miscoverage:

s(x,y)=max⁡{q^lo(x)−y, y−q^hi(x)},C(x)=[q^lo(x)−q^, q^hi(x)+q^].s(x,y)=\max\big\{\hat q_{\text{lo}}(x)-y,\ y-\hat q_{\text{hi}}(x)\big\},\qquad C(x)=\big[\hat q_{\text{lo}}(x)-\hat q,\ \hat q_{\text{hi}}(x)+\hat q\big].

The score is negative inside the quantile band, so q^\hat q can shrink an overcautious band as well as widen an overconfident one. The width now follows the estimated spread of YY given xx, and the coverage guarantee is unchanged.

python
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor

alpha = 0.1
lo = GradientBoostingRegressor(loss="quantile", alpha=alpha / 2).fit(X_train, y_train)
hi = GradientBoostingRegressor(loss="quantile", alpha=1 - alpha / 2).fit(X_train, y_train)

# Conformity scores on a calibration set the models never saw
s = np.maximum(lo.predict(X_cal) - y_cal, y_cal - hi.predict(X_cal))
n = len(s)
k = int(np.ceil((n + 1) * (1 - alpha)))   # needs k <= n, i.e. n >= 1/alpha - 1
q = np.sort(s)[k - 1]                       # k-th smallest score

lower = lo.predict(X_test) - q
upper = hi.predict(X_test) + q

In scikit-learn's quantile loss, alpha is the quantile level. Any quantile regressor works; the conformal step does not care how good it is, only the width of the final intervals does.

Classification scorePrediction setBehavior
1−p^y(x)1-\hat p_y(x), "least ambiguous" (Sadinle, Lei & Wasserman, 2019)all labels with p^y(x)≥1−q^\hat p_y(x)\ge1-\hat qif p^\hat p is the true P(y∣x)P(y\mid x), the smallest expected set size among all sets with 1−α1-\alpha coverage; coverage is uneven, low on hard inputs and high on easy ones
APS (Romano, Sesia & Candès, 2020): probability mass of the labels ranked above yy, plus a uniform fraction u p^y(x)u\,\hat p_y(x)add labels in order of p^\hat p until the accumulated mass reaches q^\hat qsets grow on inputs where the model is unsure, giving better approximate conditional coverage, at the price of larger sets
RAPS (Angelopoulos, Bates, Malik & Jordan, 2021): APS plus λ (rank(y)−kreg)+\lambda\,(\mathrm{rank}(y)-k_{\text{reg}})_+as APS, with a penalty on deep rankskeeps the long tail of tiny probabilities out of the sets; smaller and more stable sets on ImageNet
All three inherit the same marginal guarantee from the rank argument. The uniform uu in APS randomizes ties so that coverage is close to exact rather than merely at least 1−α1-\alpha.
Check your understanding

House prices are much noisier for large houses than for small ones. You calibrate two split conformal methods at 90%: absolute residuals of a mean regression, and CQR. Which statement is right?

  1. Both cover 90% marginally; CQR's intervals are narrower for small houses and wider for large ones, so its coverage is far more even across house sizes
  2. Only CQR is valid, because absolute residuals assume homoscedastic noise
  3. Absolute residuals give 90% coverage for every house size, because the guarantee holds for any score
  4. CQR covers more than 90% marginally because its intervals adapt
Show the answer

A. Both cover 90% marginally; CQR's intervals are narrower for small houses and wider for large ones, so its coverage is far more even across house sizes

Both are valid in the marginal sense. Constant-width intervals overcover small houses and undercover large ones, averaging to 90%. CQR's width tracks the conditional spread, which evens out coverage across sizes; its marginal coverage is still about 90%, not more.

Without a split: full conformal and the jackknife+

Splitting wastes data: the model never sees the calibration points. Full conformal prediction (Vovk, Gammerman & Shafer, 2005) uses all nn points for both jobs. For each candidate label yy, refit the model on the nn training points plus (x,y)(x,y), score all n+1n+1 points with that refit, and keep yy if the test point's score ranks within the lowest ⌈(n+1)(1−α)⌉\lceil(n+1)(1-\alpha)\rceil. The rank argument goes through unchanged as long as the fitting algorithm treats its input points symmetrically, because then the n+1n+1 scores are exchangeable when yy is the true label. The cost is a refit per candidate; for ridge regression the set can be computed exactly, since every residual is an affine function of yy.

The jackknife sits in between: fit nn leave-one-out models μ^−i\hat\mu_{-i}, take the residuals Ri=∣Yi−μ^−i(Xi)∣R_i=\abs{Y_i-\hat\mu_{-i}(X_i)}, and report μ^(x)±\hat\mu(x)\pm their (1−α)(1-\alpha) quantile. It usually works, but it has no distribution-free guarantee: for unstable fitting algorithms its coverage can be arbitrarily poor. The jackknife+ (Barber, Candès, Ramdas & Tibshirani, 2021) changes one thing, centering at the leave-one-out predictions instead of the full fit:

C+(x)=[ q^n,α −{μ^−i(x)−Ri},  q^n,α +{μ^−i(x)+Ri} ],C^{+}(x)=\Big[\ \hat q^{\,-}_{n,\alpha}\big\{\hat\mu_{-i}(x)-R_i\big\},\ \ \hat q^{\,+}_{n,\alpha}\big\{\hat\mu_{-i}(x)+R_i\big\}\ \Big],

where q^n,α +\hat q^{\,+}_{n,\alpha} is the ⌈(1−α)(n+1)⌉\lceil(1-\alpha)(n+1)\rceil-th smallest value and q^n,α −\hat q^{\,-}_{n,\alpha} the ⌊α(n+1)⌋\lfloor\alpha(n+1)\rfloor-th smallest.

DerivationProof: a tournament with few champions
  1. Aij=1{Rij>Rji},S={i: ∑jAij≥(1−α)(n+1)}A_{ij}=\mathbf 1\{R_{ij}>R_{ji}\},\qquad\mathcal S=\Big\{i:\ \textstyle\sum_jA_{ij}\ge(1-\alpha)(n+1)\Big\}

    For the analysis only, imagine fitting a model μ~−(i,j)\tilde\mu_{-(i,j)} on all n+1n+1 points except ii and jj, and let Rij=∣Yi−μ~−(i,j)(Xi)∣R_{ij}=\abs{Y_i-\tilde\mu_{-(i,j)}(X_i)}. Point ii beats point jj if Rij>RjiR_{ij}>R_{ji}, and is strange if it beats at least (1−α)(n+1)(1-\alpha)(n+1) of the others:

  2. s(s−1)2 ≤ s(α(n+1)−1)⟹s=∣S∣ ≤ 2α(n+1)−1\frac{s(s-1)}2\ \le\ s\big(\alpha(n+1)-1\big)\quad\Longrightarrow\quad s=\abs{\mathcal S}\ \le\ 2\alpha(n+1)-1

    Few strange points. A strange point fails to beat at most n−(1−α)(n+1)=α(n+1)−1n-(1-\alpha)(n+1)=\alpha(n+1)-1 others. In each of the s(s−1)/2s(s-1)/2 games between two strange points, at least one of them fails to win, so, deterministically:

  3. Pr⁡[n+1∈S]=E∣S∣n+1 < 2α\Pr[n+1\in\mathcal S]=\frac{\E\abs{\mathcal S}}{n+1}\ <\ 2\alpha

    The test point is rarely strange. Exchangeability and symmetry make the law of the matrix AA invariant under relabeling the points, so each point, the test point included, is strange with the same probability:

  4. Yn+1∉C+(Xn+1) ⟹ n+1∈S ⟹ Pr⁡[Yn+1∉C+(Xn+1)]<2αY_{n+1}\notin C^{+}(X_{n+1})\ \Longrightarrow\ n+1\in\mathcal S\ \Longrightarrow\ \Pr\big[Y_{n+1}\notin C^{+}(X_{n+1})\big]<2\alpha

    Miscoverage implies strangeness. The real leave-one-out fits are μ^−i=μ~−(i,n+1)\hat\mu_{-i}=\tilde\mu_{-(i,n+1)}, so Ri=Ri,n+1R_i=R_{i,n+1} and the test residual against fit ii is Rn+1,iR_{n+1,i}. If Yn+1Y_{n+1} lies above the interval, then Yn+1>μ^−i(Xn+1)+RiY_{n+1}>\hat\mu_{-i}(X_{n+1})+R_i for at least (1−α)(n+1)(1-\alpha)(n+1) indices ii (symmetrically below), and each such inequality says the test point beats point ii. So miscoverage forces n+1∈Sn+1\in\mathcal S, which has probability below 2α2\alpha.

What the guarantee is not, and what survives shift

Marginal coverage can hide systematic failure: 90% overall can mean 99% for a majority and 60% for a minority. What one would like is conditional coverage, Pr⁡[Y∈C(X)∣X=x]≥1−α\Pr[Y\in C(X)\mid X=x]\ge1-\alpha for every xx. For finitely many groups with enough data each, it is easy to get coverage within every group: calibrate separately in each one (Mondrian conformal prediction, in Vovk, Gammerman & Shafer's terms). For every individual input it is impossible.

The intuition is a needle in a haystack: without assumptions, the conditional distribution at a non-atom xx can be anything, since no sample will ever contain another point with that exact xx, so a method that must be right there for every PP cannot rule out arbitrarily large values. Practical relaxations either fix the groups in advance (Mondrian calibration) or ask for coverage under every reweighting of the input distribution in a chosen class. For finite-dimensional classes, such as a collection of overlapping subgroups, Gibbs, Cherian & Candès (2025) achieve this exactly in finite samples; for richer classes, only approximately.

Shift breaks exchangeability, but two repairs are available.

  • Covariate shift with a known ratio (Tibshirani, Foygel Barber, Candès & Ramdas, 2019). If the test inputs follow P~X≪PX\tilde P_X\ll P_X with w=dP~X/dPXw=d\tilde P_X/dP_X known up to a constant, replace the empirical distribution of calibration scores by a weighted one, with weight pi(x)=w(Xi)/(∑j=1nw(Xj)+w(x))p_i(x)=w(X_i)/\big(\sum_{j=1}^nw(X_j)+w(x)\big) on SiS_i and the remaining mass pn+1(x)p_{n+1}(x) placed at +∞+\infty, and take its 1−α1-\alpha quantile. Coverage ≥1−α\ge1-\alpha holds exactly; with an estimated ww it holds approximately.
  • Arbitrary sequences (adaptive conformal inference; Gibbs & Candès, 2021). In a stream, update the working level after each outcome, αt+1=αt+γ(α−errt)\alpha_{t+1}=\alpha_t+\gamma(\alpha-\mathrm{err}_t), where errt=1\mathrm{err}_t=1 if Yt∉CtY_t\notin C_t. With probability one, for every horizon TT and any data sequence, ∣1T∑t=1Terrt−α∣≤max⁡{α1,1−α1}+γγT\Big|\frac1T\sum_{t=1}^T\mathrm{err}_t-\alpha\Big|\le\frac{\max\{\alpha_1,1-\alpha_1\}+\gamma}{\gamma T}: the long-run miscoverage frequency converges to α\alpha, though coverage at any single time can be poor.

For data that are only approximately exchangeable, Barber, Candès, Ramdas & Tibshirani (2023) give the calibration points fixed weights wi∈[0,1]w_i\in[0,1], chosen in advance, and bound the loss of coverage by ∑iwidTV(Z,Zi)/(1+∑iwi)\sum_iw_id_{\mathrm{TV}}(Z,Z^i)/(1+\sum_iw_i), where ZZ is the sequence of calibration and test points and ZiZ^i swaps the test point with calibration point ii; down-weighting old points protects against drift.

Think first A hospital calibrates a conformal predictor on last year's patients and deploys it this year without changes. Patients now come from a new referral network, so the input distribution has shifted, but the disease process (the conditional law of YY given XX) is the same. Is the 90% guarantee still valid?

No. The calibration and test points are no longer exchangeable, and coverage can fall below 90% if the new patients concentrate where the model's residuals are large. With unlabeled inputs from the new population you can estimate the ratio ww (for example with a domain classifier) and use weighted conformal prediction; coverage then holds up to the error in w^\hat w. With labeled outcomes arriving over time, adaptive conformal inference restores the long-run frequency regardless of how the shift unfolds.

Key takeaways

  • Split conformal: with k=⌈(n+1)(1−α)⌉k=\lceil(n+1)(1-\alpha)\rceil and q^\hat q the kk-th smallest calibration score, exchangeability alone gives Pr⁡[Y∈C(X)]≥1−α\Pr[Y\in C(X)]\ge1-\alpha, and almost surely distinct scores add Pr⁡[Y∈C(X)]<1−α+1/(n+1)\Pr[Y\in C(X)]<1-\alpha+1/(n+1); for i.i.d. data with continuous scores, coverage conditional on the calibration set is Beta(n+1−l,l)\mathrm{Beta}(n+1-l,l).
  • The score shapes the sets: CQR adapts interval width to heteroscedasticity; least-ambiguous, APS and RAPS scores trade set size against evenness of coverage for classifiers.
  • Full conformal uses all data at the cost of refits; the jackknife+ guarantees 1−2α1-2\alpha by a tournament argument, while the plain jackknife guarantees nothing without stability.
  • Distribution-free conditional coverage forces infinite expected length; group-wise calibration and coverage over chosen reweighting classes are the achievable compromises.
  • Under covariate shift with a known likelihood ratio, weighted quantiles restore exact coverage; for arbitrary sequences, adaptive conformal inference drives the long-run miscoverage frequency to α\alpha.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Split conformal with n=49n=49 calibration points at α=0.2\alpha=0.2 and continuous scores. Which calibration score is the threshold, and what is the coverage?

  1. The 39th smallest; coverage exactly 78%
  2. The 40th smallest; coverage exactly 80%
  3. The 40th smallest; coverage at least 80%, at most 100%
  4. The 45th smallest; coverage exactly 90%
Show the answer

B. The 40th smallest; coverage exactly 80%

k=⌈50⋅0.8⌉=40k=\lceil50\cdot0.8\rceil=40, and with continuous scores the coverage is exactly k/(n+1)=40/50=0.80k/(n+1)=40/50=0.80. When (n+1)(1−α)(n+1)(1-\alpha) is an integer, the lower and upper bounds coincide.

Question 2

A conformal predictor with 90% marginal coverage covers only 70% of patients over 80. What follows?

  1. The method is broken: conformal guarantees 90% for every subgroup
  2. The data cannot have been exchangeable
  3. Nothing is wrong with the guarantee, which is marginal; calibrate separately within age groups to get 90% in each
  4. Use more calibration data: with enough points every subgroup reaches 90%
Show the answer

C. Nothing is wrong with the guarantee, which is marginal; calibrate separately within age groups to get 90% in each

Marginal coverage averages over all patients, so a small group can be undercovered while others are overcovered. Group-wise (Mondrian) calibration gives the guarantee within each group, provided each has enough calibration points.

Question 3

What separates the jackknife+ from the plain jackknife?

  1. The jackknife+ uses KK folds instead of leave-one-out
  2. The jackknife+ centers on the leave-one-out predictions μ^−i(x)\hat\mu_{-i}(x), which yields a distribution-free guarantee of 1−2α1-2\alpha; the jackknife centers on the full fit and has no such guarantee
  3. The jackknife+ needs a separate calibration set
  4. The jackknife+ guarantees exactly 1−α1-\alpha, the jackknife 1−2α1-2\alpha
Show the answer

B. The jackknife+ centers on the leave-one-out predictions μ^−i(x)\hat\mu_{-i}(x), which yields a distribution-free guarantee of 1−2α1-2\alpha; the jackknife centers on the full fit and has no such guarantee

Using μ^−i(x)\hat\mu_{-i}(x) puts the test point and every training point on the same footing, which the tournament argument exploits. The plain jackknife mixes a full-data fit at the test point with leave-one-out residuals, and for unstable algorithms that mismatch can destroy coverage.

Question 4

In weighted conformal prediction under covariate shift, why is part of the probability mass placed at +∞+\infty?

  1. It represents the unknown score of the test point itself, with weight pn+1(x)∝w(x)p_{n+1}(x)\propto w(x); without it the quantile could be too small to cover
  2. It makes the intervals symmetric
  3. It is a numerical safeguard with no effect when nn is large
  4. It accounts for concept drift
Show the answer

A. It represents the unknown score of the test point itself, with weight pn+1(x)∝w(x)p_{n+1}(x)\propto w(x); without it the quantile could be too small to cover

In the unweighted case the test score is one of n+1n+1 exchangeable scores, and placing its unknown value at +∞+\infty is what produces the ⌈(n+1)(1−α)⌉\lceil(n+1)(1-\alpha)\rceil index. Under weighted exchangeability the test point carries weight proportional to w(x)w(x), which can be large exactly where the source data are thin, and then the interval rightly becomes wide or infinite.

Further reading

  • V. Vovk, A. Gammerman & G. Shafer, Algorithmic Learning in a Random World (Springer, 2005; 2nd ed. 2022).
  • J. Lei, M. G'Sell, A. Rinaldo, R. J. Tibshirani & L. Wasserman, "Distribution-free predictive inference for regression," Journal of the American Statistical Association 113(523), 1094–1111 (2018).
  • Y. Romano, E. Patterson & E. Candès, "Conformalized quantile regression," NeurIPS (2019).
  • R. F. Barber, E. J. Candès, A. Ramdas & R. J. Tibshirani, "Predictive inference with the jackknife+," Annals of Statistics 49(1), 486–507 (2021).
  • A. N. Angelopoulos & S. Bates, "Conformal prediction: a gentle introduction," Foundations and Trends in Machine Learning 16(4), 494–591 (2023).