Prediction Intervals and Split Conformal Prediction › Formula

Michael BrenndoerferOctober 29, 202616 min read
Machine Learning from ScratchAll chapters
Part 12 of 15Calibration and Prediction Uncertainty
Chapter 77Prediction Intervals and Split Conformal Prediction › Formula
Reading section 4 of 10 · Formula · 16 minSection 4 of 10 · 16 min

Section 4 of 10 · 16 min read

Prediction Intervals and Split Conformal Prediction › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

We now build the machinery. The aim is to turn a bare point prediction into a band, and to do it in a way that comes with a guarantee we can state precisely. Three questions have to be answered along the way:

  1. What are we trying to cover? Not the regression surface, but the next observation. If the response retains nondegenerate noise, additional fitting data do not remove that noise.
  2. How do we measure a miss? We need a number that grows when a candidate observation becomes implausible under the fitted model, a nonconformity score.
  3. How do we convert finitely many measured misses into a margin that is valid for the next one? This is where exchangeability enters, and where a seemingly cosmetic +1+1 turns out to carry the entire guarantee.

Notation throughout: fit the predictor and score on Dtrain\mathcal D_{\mathrm{train}}, then use a disjoint calibration set Dcal={(Xi,Yi)}i=1n\mathcal D_{\mathrm{cal}}=\{(X_i,Y_i)\}_{i=1}^n. A fresh test pair is (Xn+1,Yn+1)(X_{n+1},Y_{n+1}). Conditional on the fitted score function, require these n+1n+1 calibration and test scores to be exchangeable. Independent observations from one distribution, with an independent fitting split, are a sufficient setting. The separate test sample in the example evaluates the completed procedure.

Confidence intervals, prediction intervals, and prediction sets

Before building the procedure, distinguish three targets: a population quantity, a future observation, and a class label. The target determines which inclusion statement we need to establish.

A confidence interval targets a population quantity, such as a mean, coefficient or quantile. For example, an interval for μ(x)=E[Y∣X=x]\mu(x)=\mathbb E[Y\mid X=x] aims to contain that fixed conditional mean across repeated fitting samples at the specified input. Its width can shrink under regular consistent estimation; shrinking is not an unconditional property of every confidence procedure.

A prediction interval targets a future observation and aims for a specified inclusion probability. It accounts for response variation as well as uncertainty in the fitted predictor. With persistent nondegenerate noise it can remain wide even when mean estimation is precise. For a symmetric noise distribution, a symmetric band around its center uses the (1−α)(1-\alpha) quantile of the absolute noise as its half-width.

A prediction set is the classification analogue: a random subset C^(x)⊆{1,…,K}\hat C(x) \subseteq \{1,\dots,K\} with P(Yn+1∈C^(Xn+1))≥1−αP\big(Y_{n+1} \in \hat C(X_{n+1})\big) \ge 1-\alpha. Whereas an interval is an ordered range on the real line, a set is an unordered collection of labels, and its natural size measure is cardinality rather than length.

For comparison, consider the correctly specified linear model with nn fitting observations and pp nonconstant predictors plus an intercept. In this comparison only, nn denotes fitting size rather than calibration size. Assume n>p+1n>p+1, a fixed full-rank design, independent Gaussian errors with variance σ2\sigma^2, and an independent future error with the same variance. At a fixed new design vector x0x_0, the Gaussian prediction interval is

y^0±tn−p−1, 1−α/2  σ^1+x0⊤(X⊤X)−1x0,\hat y_0 \pm t_{n-p-1,\,1-\alpha/2}\;\hat\sigma\sqrt{1 + x_0^\top (X^\top X)^{-1} x_0},

where:

  • y^0=x0⊤β^\hat y_0 = x_0^\top \hat\beta: the fitted value at the new input
  • σ^2\hat\sigma^2: the residual variance estimate (the sum of squared residuals divided by n−p−1n-p-1)
  • XX: the full-rank n×(p+1)n\times(p+1) training design matrix, including an intercept column and pp nonconstant predictors
  • tn−p−1,1−α/2t_{n-p-1,1-\alpha/2}: the 1−α/21-\alpha/2 quantile of a tt distribution with n−p−1n-p-1 degrees of freedom
  • x0⊤(X⊤X)−1x0x_0^\top (X^\top X)^{-1} x_0: the new-point leverage, with x0x_0 containing the intercept and the same pp predictors as the design matrix

The "1+1+" inside the square root reflects the additional noise of the future observation. It is there because the error of prediction decomposes as

Yn+1−y^n+1  =  (Yn+1−μ(xn+1))⏟irreducible noise  +  (μ(xn+1)−y^n+1)⏟estimation error.Y_{n+1} - \hat y_{n+1} \;=\; \underbrace{\big(Y_{n+1} - \mu(x_{n+1})\big)}_{\text{irreducible noise}} \;+\; \underbrace{\big(\mu(x_{n+1}) - \hat y_{n+1}\big)}_{\text{estimation error}} .

Under these Gaussian-model assumptions, conditional on the fixed design and new input, the new observation error is independent of the fitting-sample estimation error. Their variances therefore add:

Var⁡(Yn+1−y^n+1)=Var⁡(Yn+1−μ(xn+1))⏟noise variance σ2+Var⁡(μ(xn+1)−y^n+1)⏟estimation variance.\operatorname{Var}(Y_{n+1} - \hat y_{n+1}) = \underbrace{\operatorname{Var}\big(Y_{n+1} - \mu(x_{n+1})\big)}_{\text{noise variance } \sigma^2} + \underbrace{\operatorname{Var}\big(\mu(x_{n+1}) - \hat y_{n+1}\big)}_{\text{estimation variance}} .

where:

  • Yn+1Y_{n+1}: the future observation whose value is not yet known
  • y^n+1\hat y_{n+1}: the fitted model's prediction at the future input xn+1x_{n+1}
  • μ(xn+1)\mu(x_{n+1}): the true conditional mean E[Y∣X=xn+1]\mathbb{E}[Y \mid X = x_{n+1}]
  • σ2\sigma^2: the noise variance Var⁡(Yn+1−μ(xn+1))\operatorname{Var}(Y_{n+1} - \mu(x_{n+1}))

Under regular increasing-sample designs with a fixed number of predictors, mean-estimation uncertainty decreases. The mean confidence interval uses only the estimation term, while the prediction interval also contains the future-noise term. For a 95% Gaussian interval, its large-sample noise margin is approximately 1.96σ1.96\sigma. This comparison explains the extra 11 in the displayed square root.

The weakness of this construction is the assumption stack: Gaussian errors, constant variance, a correctly specified linear model, and independent observations. Split conformal keeps the same target, the observation, but replaces every one of those assumptions with exchangeability alone.

Calibration scores and finite-sample quantiles

The engine of conformal prediction is the nonconformity score. The idea is to condense each candidate pair (x,y)(x, y) and the fitted model f^\hat f into a single number, s(x,y)s(x,y), that grows as the pair becomes less plausible under the model. A score is doing its job when it is small for observations the model explains well and large for observations it explains poorly.

For regression with a scalar response the standard choice is the absolute residual

si=∣ yi−f^(xi) ∣,s_i = \big|\,y_i - \hat f(x_i)\,\big|,

where:

  • sis_i: the nonconformity score for calibration point ii, measuring how far the model's prediction misses the observed response
  • yiy_i: the observed response for calibration point ii
  • f^(xi)\hat f(x_i): the fitted model's prediction for calibration point ii

For classification with fitted class probabilities p^(y∣x)\hat p(y \mid x) the standard choice is

si=1−p^(yi∣xi),s_i = 1 - \hat p\big(y_i \mid x_i\big),

where:

  • sis_i: the nonconformity score for calibration point ii, equal to one minus the probability of the observed label
  • p^(yi∣xi)\hat p(y_i \mid x_i): the fitted probability the model assigns to the observed label yiy_i at input xix_i
  • yiy_i: the observed class label of calibration point ii

This score is small when the model assigns high probability to the observed label and large when it assigns low probability. Many other scores are possible; we will meet several. Choose and freeze the score function without using the calibration or test outcomes for fitting or selection. At prediction time, evaluate it for candidate labels or responses; the unknown true future response is not supplied to the procedure.

Given the scores s1,…,sns_1,\dots,s_n, sort them and write the order statistics as

s(1)≤s(2)≤⋯≤s(n).s_{(1)} \le s_{(2)} \le \dots \le s_{(n)} .

The conformal quantile at miscoverage level α\alpha is the order statistic at index

k=⌈(n+1)(1−α)⌉,k = \left\lceil (n+1)(1-\alpha) \right\rceil ,

For k≤nk\le n, set q^=s(k)\hat q=s_{(k)}. If k=n+1k=n+1, set q^=+∞\hat q=+\infty and return the full response space. Clipping kk to nn would lose the requested guarantee at small calibration sizes.

where:

  • nn: the calibration set size
  • α\alpha: the miscoverage level, so 1−α1-\alpha is the nominal coverage
  • kk: the ceiling index ⌈(n+1)(1−α)⌉\lceil (n+1)(1-\alpha)\rceil, which can equal n+1n+1; that case uses an infinite threshold
  • s(k)s_{(k)}: the kk-th smallest calibration score
  • q^\hat q: the conformal score threshold; for absolute-residual regression it is the interval half-width

This implementation selects an order statistic without interpolation. Other quantile conventions need to be checked against the stated finite-sample cutoff; an ordinary interpolating call is not generally equivalent.

The ⌈⋅⌉\lceil \cdot \rceil symbol denotes the ceiling function: the smallest integer greater than or equal to its argument. For n=100n = 100 and α=0.10\alpha = 0.10 we get (n+1)(1−α)=101×0.90=90.9(n+1)(1-\alpha) = 101 \times 0.90 = 90.9, so k=⌈90.9⌉=91k = \lceil 90.9 \rceil = 91 and q^=s(91)\hat q = s_{(91)}.

To see the role of n+1n+1, first assume the n+1n+1 exchangeable scores are almost surely distinct. The test score then has a uniform rank in {1,…,n+1}\{1,\ldots,n+1\}. For k≤nk\le n, it falls below the kkth calibration score exactly when that rank is at most kk. Thus

P(sn+1≤s(k))=kn+1,P\big(s_{n+1} \le s_{(k)}\big) = \frac{k}{n+1},

where:

  • sn+1s_{n+1}: the nonconformity score of the future test point, an exchangeable draw from the same pool as the calibration scores
  • s(k)s_{(k)}: the kk-th smallest calibration score, used as the conformal quantile q^\hat q
  • kk: the ceiling index ⌈(n+1)(1−α)⌉\lceil (n+1)(1-\alpha)\rceil, computed from the calibration size nn and miscoverage level α\alpha

The equality holds because sn+1≤s(k)s_{n+1} \le s_{(k)} happens exactly when the rank of sn+1s_{n+1} is one of the kk smallest.

The rank calculation behind the guarantee can be inspected directly. With n=10n=10 and α=0.10\alpha=0.10, we have (n+1)(1−α)=11×0.90=9.9(n+1)(1-\alpha)=11\times0.90=9.9, so k=⌈9.9⌉=10k=\lceil9.9\rceil=10. Under distinct exchangeable scores, the test score has eleven equally likely ranks, and ranks one through ten fall at or below the calibrated cutoff s(10)s_{(10)}; only the largest rank falls outside it. The cell below prepares the rank probabilities for the figure: it computes the ceiling index without clipping, builds uniform rank probabilities, and flags the ranks covered by the cutoff. This is an exact rank illustration, with no sampled data.

PythonIn [2]
CollapseExpand
import numpy as np

n_rank = 10
alpha_rank = 0.10
k_rank = int(np.ceil((n_rank + 1) * (1 - alpha_rank)))
ranks = np.arange(1, n_rank + 2)
rank_prob = np.full(ranks.shape, 1.0 / (n_rank + 1))
covered_rank = (k_rank - ranks) >= 0
VisualizationOut [3]
CollapseExpand
Eleven equal rank probabilities; ten covered bars sum to 10/11 and one is uncovered.
Uniform rank probabilities for eleven exchangeable scores at n = 10 and alpha = 0.10. Under distinct exchangeable scores, each rank is equally likely. With k = 10, ranks one through ten are covered by the conformal cutoff, so the covered bars sum to coverage probability 10/11.

Setting k=⌈(n+1)(1−α)⌉k = \lceil (n+1)(1-\alpha)\rceil gives

P(sn+1≤q^)=⌈(n+1)(1−α)⌉n+1  ≥  1−α.P\big(s_{n+1} \le \hat q\big) = \frac{\lceil (n+1)(1-\alpha)\rceil}{n+1} \;\ge\; 1-\alpha .

The ceiling gives the lower bound in the distinct-score case, provided k≤nk\le n so the displayed finite order statistic is used. Below the finite-threshold cutoff, k=n+1k=n+1, so k/(n+1)=1k/(n+1)=1 and the infinite threshold covers the full response space. With ties, independently randomizing the order within equal scores gives a uniform rank, and the deterministic event sn+1≤q^s_{n+1}\le\hat q includes every rank-at-most-kk case. Its probability is therefore at least k/(n+1)k/(n+1), although equality need not hold. If k=n+1k=n+1, the infinite threshold covers the full response space. Using ⌈n(1−α)⌉\lceil n(1-\alpha)\rceil or an ordinary interpolating quantile does not supply this general finite-sample guarantee; the values can coincide for some inputs, so the issue is the missing guarantee rather than universal under-coverage.

There is a matching upper bound. If the scores are almost surely distinct, then

1−α  ≤  P(sn+1≤q^)  ≤  1−α+1n+1,1-\alpha \;\le\; P\big(s_{n+1} \le \hat q\big) \;\le\; 1-\alpha + \frac{1}{n+1},

so the guarantee is tight to within 1/(n+1)1/(n+1). For α=0.10\alpha = 0.10 and n=100n = 100, coverage lands between 0.9000.900 and roughly 0.9100.910. This statement requires exchangeable scores, while leaving the response distribution otherwise unrestricted.

A finite threshold under this convention requires k≤nk\le n, equivalently α≥1/(n+1)\alpha\ge1/(n+1). At least nine calibration observations permit a finite 90% threshold; at least 99 permit a finite 99% threshold. Smaller calibration samples still give valid sets through q^=+∞\hat q=+\infty, but those sets are uninformative. These cutoffs do not guarantee stable or narrow finite thresholds.

The finite-threshold cutoff is only part of the story. At fixed α\alpha, the guaranteed floor k/(n+1)k/(n+1) equals one while the infinite-threshold branch is active at small calibration sizes. Once a finite threshold exists, the ceiling index makes the floor vary nonmonotonically as nn grows. The figure below plots the rank-based floors for calibration sizes from 1 to 200. It evaluates the formula directly, with no sampled data.

PythonIn [4]
CollapseExpand
n_grid = np.arange(1, 201)


def coverage_floor_for(nominal):
    alpha_level = 1.0 - nominal
    k = np.ceil((n_grid + 1) * (1.0 - alpha_level)).astype(int)
    return np.where(k > n_grid, 1.0, k / (n_grid + 1))


coverage_floor_90 = coverage_floor_for(0.90)
coverage_floor_95 = coverage_floor_for(0.95)
VisualizationOut [5]
CollapseExpand
Coverage floors by calibration size for nominal 0.90 and 0.95, with granular steps.
Finite-sample guaranteed coverage floor k/(n+1) against calibration-set size for nominal 0.90 and 0.95. The sawtooth shows rank granularity: between ceiling-index jumps the ratio falls, then rises at the next jump. The floor equals one while the infinite-threshold branch is active at small calibration sizes.

This bound arises because k≤nk \le n requires ⌈(n+1)(1−α)⌉≤n\lceil (n+1)(1-\alpha)\rceil \le n, equivalently (n+1)(1−α)≤n(n+1)(1-\alpha) \le n since nn is an integer. Solving:

(n+1)(1−α)≤nn+1−αn−α≤n1−α≤αnn≥1−αα=1α−1.\begin{aligned} (n+1)(1-\alpha) &\le n \\ n+1 - \alpha n - \alpha &\le n \\ 1 - \alpha &\le \alpha n \\ n &\ge \frac{1-\alpha}{\alpha} = \frac{1}{\alpha} - 1 . \end{aligned}

Split conformal regression and classification

Putting the pieces together, split conformal regression is a four-step procedure, sometimes called inductive conformal prediction (Papadopoulos et al., 2002). Notice how cleanly the steps separate the modelling from the calibration. The model is fit and frozen first, and only then does the conformal machinery measure its mistakes.

  1. Fit the point predictor f^\hat f on Dtrain\mathcal{D}_{\text{train}}. Nothing about this step is special; use whatever model you like and whatever model selection procedure you like, as long as it never touches Dcal\mathcal{D}_{\text{cal}}.
  2. Compute calibration scores si=∣yi−f^(xi)∣s_i = |y_i - \hat f(x_i)| for i=1,…,ni = 1,\dots,n.
  3. Compute k=⌈(n+1)(1−α)⌉k=\lceil(n+1)(1-\alpha)\rceil. Set q^=s(k)\hat q=s_{(k)} if k≤nk\le n and q^=+∞\hat q=+\infty otherwise.
  4. For a new input xx, return C^(x)=[f^(x)−q^,  f^(x)+q^]\hat C(x) = \big[\hat f(x) - \hat q,\; \hat f(x) + \hat q\big] when q^\hat q is finite, and return the full response space when q^=+∞\hat q = +\infty.

The interval is the point prediction plus or minus a constant margin. When q^\hat q is finite its width is exactly 2q^2\hat q everywhere, independent of xx; when q^=+∞\hat q = +\infty the set is the full response space. Here q^\hat q is the conformal quantile computed from calibration scores, and there is no guarantee that averaging a realized test batch reproduces the nominal coverage. That is both its simplicity and its main weakness, and we will return to the consequence shortly.

Step 4 is worth reading backwards, because that is how the guarantee is usually proved. The event Yn+1∈C^(Xn+1)Y_{n+1} \in \hat C(X_{n+1}) is the same as ∣Yn+1−f^(Xn+1)∣≤q^|Y_{n+1} - \hat f(X_{n+1})| \le \hat q, which is the same as sn+1≤q^s_{n+1} \le \hat q. Everything reduces to the rank argument in the previous subsection. The proof does not require a particular predictor family or parametric response law. Its sampling argument uses exchangeability of the held-out scores.

For classification, the same four steps apply with a different score, and step 4 becomes a set construction:

C^(x)={ y∈{1,…,K}  :  1−p^(y∣x)≤q^ }={ y  :  p^(y∣x)≥1−q^ }.\hat C(x) = \big\{\, y \in \{1,\dots,K\} \;:\; 1 - \hat p(y \mid x) \le \hat q \,\big\} = \big\{\, y \;:\; \hat p(y \mid x) \ge 1 - \hat q \,\big\}.

where:

  • yy: a candidate class label in {1,…,K}\{1,\dots,K\}
  • p^(y∣x)\hat p(y \mid x): the fitted probability the model assigns to label yy at input xx
  • q^\hat q: the conformal quantile from the calibration scores, so 1−q^1-\hat q is the probability threshold

For the score 1−p^(y∣x)1-\hat p(y\mid x), classification keeps labels whose fitted probability is at or above 1−q^1-\hat q. The calibration step chooses the threshold automatically, in such a way that the fraction of calibration points whose nonconformity score 1−p^(yi∣xi)1-\hat p(y_i\mid x_i) exceeds q^\hat q is at most α\alpha. This uses held-out outcomes to calibrate set inclusion. It is different from Probability Calibration, which evaluates or adjusts probability reliability. Accurate class probabilities are useful, but are not required for this marginal set-coverage proof.

The threshold applies to each candidate label, so set size varies with the probability vector and the calibrated cutoff. Several labels may pass, or none may pass. The marginal guarantee controls overall inclusion under its sampling assumptions. For a proper nonempty subset of the class space, set size alone does not determine its conditional inclusion probability. Set size alone also does not establish an out-of-distribution interpretation.

Mathematical Properties

Several properties follow from the construction and are worth naming. Treat them as a checklist of what the formula does and does not give us.

  • Coverage is finite-sample. Under the score-exchangeability condition, P(Yn+1∈C^(Xn+1))≥1−αP(Y_{n+1}\in\hat C(X_{n+1}))\ge1-\alpha holds for every n≥1n\ge1 and 0<α<10<\alpha<1, using the infinite-threshold branch when necessary. No asymptotic approximation is used.
  • The guarantee is distribution-free over score distributions satisfying exchangeability. The predictor and score construction must be fixed before calibration; no correctly specified parametric response model is required.
  • Monotonicity in α\alpha. With the same calibration scores, decreasing α\alpha cannot decrease kk or q^\hat q. The sets are nested, although the discrete index can stay unchanged over a range of levels.
  • Invariance to monotone rescaling of scores. Replacing ss by any strictly increasing transform g(s)g(s) yields the same prediction sets, since the ranking is unchanged. The transform must be well-defined and the same for every calibration and candidate score. The small-calibration branch still returns the full response space; it does not require evaluating g(+∞)g(+\infty). Dividing by an input-dependent scale is a different score construction and can change the resulting sets.
  • Width depends on the score distribution. With a fixed score and independent calibration samples growing in size, the empirical cutoff converges to its population score quantile at continuity and identifiability points. When the fitted score also changes, additional consistency assumptions are needed. Improving a predictor under one loss does not universally shrink every conformal cutoff.
  • Ties can make deterministic inclusion conservative: P(sn+1≤q^)P(s_{n+1}\le\hat q) may exceed k/(n+1)k/(n+1). This removes the distinct-score upper slack bound; it does not by itself prove that an interval can be narrowed while keeping the same guarantee.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy