WRITE · TRACE · UNDERSTAND

Use the finite-sample conformal rank

Implement conformal_radius(residuals,alpha) with finite nonnegative residuals, nonempty input and 0<alpha<1.

Read lesson: Use the finite-sample conformal rank →

Your experiment

Implement conformal_radius(residuals,alpha) with finite nonnegative residuals, nonempty input and 0<alpha<1. Return sorted residual at one-based rank ceil((n+1)*(1-alpha)); return inf if rank>n.

Python runs in your browser. Your draft stays on this device.

What to look for

The guarantee depends on exchangeable calibration and future scores. An infinite interval honestly reports insufficient calibration information at the requested error level rather than silently overstating confidence.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Use the finite-sample conformal rank works

The idea

Split conformal prediction turns a point prediction into an interval with a stated coverage. Take calibration examples the model was not trained on and record each absolute error. conformal_radius then picks one of those residuals as a radius, so that prediction plus or minus the radius should contain the true value at least 1 - alpha of the time.

The insight is about ranks. If the calibration points and the new point come from the same source, the new residual is equally likely to land in any of the n + 1 positions among the sorted calibration residuals. Taking the residual at rank ceil((n + 1) * (1 - alpha)) therefore gives the promised coverage. With too few residuals that rank does not exist, so the honest answer is infinity.

How it works

  1. Validate: residuals is non-empty, finite and non-negative, and 0 < alpha < 1.
  2. Compute the one-based rank ceil((n + 1) * (1 - alpha)).
  3. If the rank is greater than n, return inf: the sample is too small for this coverage.
  4. Otherwise sort the residuals and return the one at that rank, index rank - 1.
  5. Use the result as a half-width: the interval is the prediction plus or minus the radius.

What it costs

Time
O(n log n)
Space
O(n)

Sorting the n calibration residuals is the main cost; the radius is computed once and then reused for every new prediction.

Where it is used

  • The MAPIE Python library wraps scikit-learn models to produce conformal prediction intervals.
  • Nixtla's statsforecast library offers conformal prediction intervals for time series forecasts.
  • Angelopoulos and co-authors' RAPS method builds conformal label sets for ImageNet classifiers.

Watch out for

  • Using rank ceil(n * (1 - alpha)): the n + 1 accounts for the new point, and without it coverage can fall short.
  • Calibrating on the data the model was trained on: those residuals are too small, so the intervals are too narrow.
  • Assuming the guarantee survives drift: it holds only while new data comes from the same source as the calibration set.