Library · Validation

Split conformal prediction

Conformal prediction wraps any model. It measures the errors on data set aside, and uses their quantile as the half-width of the prediction interval. The average coverage is guaranteed without assuming a distribution, under one condition.

By , published

Definition

In the split version, the data are divided into a training part and a calibration part of n rows. The model is fitted on the first. On the second, the absolute errors are computed and their quantile q at level ⌈(n + 1)(1 − α)⌉ / n is taken.

interval for a new x: [ f(x) − q, f(x) + q ]

If the calibration rows and the new row are exchangeable, meaning their order carries no information, the interval contains the outcome with probability at least 1 − α. The guarantee holds for any model, good or bad: a poor model simply gives wide intervals.

Worked example

The noise of the outcome grows with the input. A straight line is fitted, intervals at 90 % are built by split conformal prediction, and coverage is measured on new data, overall and by range of the input.

import numpy as np

rng = np.random.default_rng(13)
def sample(n):
    x = rng.uniform(0, 10, n)
    return x, 2 * x + rng.normal(scale=0.5 + 0.4 * x, size=n)     # noise grows with x

x_tr, y_tr = sample(500); x_cal, y_cal = sample(500); x_new, y_new = sample(50_000)
b = np.polyfit(x_tr, y_tr, 1)
resid = np.abs(y_cal - np.polyval(b, x_cal))
n, alpha = len(resid), 0.10
q = np.sort(resid)[int(np.ceil((n + 1) * (1 - alpha))) - 1]

inside = np.abs(y_new - np.polyval(b, x_new)) <= q
print(f"half-width q = {q:.2f}   overall coverage = {inside.mean():.3f}")
for lo, hi in [(0, 2.5), (2.5, 5), (5, 7.5), (7.5, 10)]:
    m = (x_new >= lo) & (x_new < hi)
    print(f"x in [{lo:>3}, {hi:>4}): coverage = {inside[m].mean():.3f}")

Output, produced by running the code above when this page was built:

half-width q = 4.44   overall coverage = 0.891
x in [  0,  2.5): coverage = 1.000
x in [2.5,    5): coverage = 0.970
x in [  5,  7.5): coverage = 0.856
x in [7.5,   10): coverage = 0.737

Overall coverage is close to the promised level. By range of the input it is not: the same half-width is too wide where the noise is small and too narrow where it is large.

Where it misleads

  • The guarantee is on average over all cases, not for each kind of case. Groups can be under-covered, as in the example. Conformalized quantile regression and related methods adapt the width to the input.
  • Exchangeability fails on time series and whenever the data drift. Coverage then degrades without warning, and has to be monitored or the calibration set refreshed.
  • The calibration rows are not used for training. With little data the split is costly.
  • The interval is only as narrow as the model is good. Conformal prediction certifies coverage, not usefulness.

Coverage should still be checked on held-out data, by group, as described in prediction interval coverage.

In our work

We check intervals by group whatever method produced them. In our small area estimation study the standard 95 % intervals contained the census value 54 % of the time, a failure that an average over all communes of a better-calibrated method could have hidden.

References

  1. Angelopoulos, A. N., Bates, S. (2023). Conformal prediction: a gentle introduction. Foundations and Trends in Machine Learning, 16(4), 494-591. doi:10.1561/2200000101
  2. Lei, J., G'Sell, M., Rinaldo, A., Tibshirani, R. J., Wasserman, L. (2018). Distribution-free predictive inference for regression. Journal of the American Statistical Association, 113(523), 1094-1111. doi:10.1080/01621459.2017.1307116
  3. Romano, Y., Patterson, E., Candès, E. J. (2019). Conformalized quantile regression. Advances in Neural Information Processing Systems 32. arxiv.org/abs/1905.03222

Have a question like this one on your own data? Tell us what you need in a few lines.

Send us the brief

Related pages

  • Prediction interval coverage and the Winkler scoreCoverage is the share of outcomes that fall inside the prediction intervals. It is the first check on any interval, and it needs enough periods to mean something. The Winkler score adds a penalty for the interval width.
  • Pinball loss (quantile loss)The pinball loss, or quantile loss, is the measure for a quantile forecast, such as the stock level that covers demand 90 % of the time. It is minimised by the true quantile and by nothing else.
  • Probability calibration: slope and interceptCalibration asks whether predicted probabilities match observed frequencies. Two numbers summarise it: the intercept, for the overall level, and the slope, for the spread. A slope below 1 means predictions that are too extreme.
All library pages