Skip to content

Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC009 · Algorithm

Conformalized quantile regression (CQR)

Calibrate fitted lower and upper conditional quantile functions to obtain finite-sample marginal prediction coverage.

Runs in CodeSplash (Predict) Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash feature runs this entry inside the app, not as a Python and R method. There's no Set up button; open it where the label says.

Do it in CodeSplash Run it on your own data in the app: Predict run page › Uncertainty tab › conformalized quantile regression (a lower and an upper quantile run).

Availability
Runs in CodeSplash (Predict)
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Calibrate fitted lower and upper conditional quantile functions to obtain finite-sample marginal prediction coverage. This is a specific heteroscedastic interval construction within conformal prediction, extending the existing generic split-conformal entry. The quantile model can be neural or classical; CQR itself is an algorithm, not a network architecture.

Question / hypotheses

Can a new response be enclosed with prespecified marginal coverage while allowing interval widths to adapt to the input? Coverage is over a fresh exchangeable calibration/test draw; it is not a universal conditional-on-every-input guarantee.

Architecture / computational structure

Fit two quantile regressors on a training split, freeze them, compute signed conformity errors on a separate calibration split, and enlarge or shrink the fitted quantile band by an empirical order statistic. The calibration layer has no gradient-based fine-tuning step.

Assumptions

Calibration examples and the future example must be exchangeable conditional on the fitted training model. Model selection cannot reuse calibration labels without adjustment. Ordinary split CQR is not automatically valid for time dependence, group dependence, covariate shift or online adaptivity.

Formula / computational definition

For calibration size mm, define si=max⁡{q^L(xi)−yi, yi−q^U(xi)}s_i=\max\{\hat q_L(x_i)-y_i,\ y_i-\hat q_U(x_i)\}, k=⌈(m+1)(1−α)⌉k=\lceil(m+1)(1-\alpha)\rceil, and s^=s(k)\hat s=s_{(k)}, taking s(m+1)=+∞s_{(m+1)}=+\infty. Return [q^L(x)−s^,q^U(x)+s^][\hat q_L(x)-\hat s,\hat q_U(x)+\hat s]. Quantile fits commonly minimize pinball loss ρτ(u)=u(τ−1[u<0])\rho_\tau(u)=u(\tau-\mathbf1[u<0]).

Parameters and notation

Learned/calibrated: quantile-model coefficients/weights and the calibration order statistic. Hyperparameters/design choices: miscoverage α, quantile levels, data-split policy, model complexity and calibration size. α is fixed by the desired coverage, not tuned after seeing test failures. Negative calibration scores can legitimately shrink a conservative base interval.

Fitting, fine-tuning, and adaptation

Fine-tune the base quantile model only on training/adaptation data, then recompute calibration scores on independent representative target data. Reusing the old calibration correction after changing the predictor invalidates its original interpretation. Adapted conformal procedures for dependence or shift require their own assumptions.

How to use and calibrate

Tune quantile estimators using training-only validation, then calibrate once. Report coverage with sampling uncertainty, interval width, and prespecified subgroup diagnostics on a final test set. Check quantile crossing and define its correction before calibration. Preserve the order-statistic convention; interpolated software quantiles can change finite-sample guarantees.

Example application

Give response intervals whose widths increase in noisier portions of a scientific instrument’s operating range. Plot raw quantile bands beside calibrated bands and summarize widths versus inputs.

Interpretation / pitfalls

Watch out

Marginal coverage can coexist with poor coverage in a rare subgroup. Extremely small calibration sets may require an infinite interval for the requested α. Distribution-free does not mean assumption-free; exchangeability is the central requirement.

Python and R code examples

Scope: Complete finite-sample CQR calibration and interval construction given fixed hypothetical quantile predictions. Quantile fitting and coverage validation on independent test data are not performed.

Dependencies / validation: use the shared NumPy/SciPy or base-R setup above. Python executed successfully with assertions; R reviewed but not executed.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Complete finite-sample CQR calibration and interval construction given fixed hypothetical quantile predictions. Quantile fitting and coverage validation on independent test data are not performed.

Shared setup for this chapter
import numpy as np
from scipy.special import logsumexp, gammaln
from scipy.linalg import expm
from itertools import product

def rbf(a, b, ell=1.0):
    a, b = np.atleast_2d(a), np.atleast_2d(b)
    return np.exp(-np.sum((a[:, None, :] - b[None, :, :])**2, axis=2)/(2*ell**2))
y = np.array([.2,.7,1.4,1.8,2.1,2.7,3.5,4.0])
lower = np.array([0.,.4,.8,1.2,1.6,2.,2.4,2.8])
upper = lower+1.0; alpha = .2; m = len(y)
scores = np.maximum(lower-y,y-upper)
k = int(np.ceil((m+1)*(1-alpha)))
q = np.inf if k>m else np.sort(scores)[k-1]
base_new = np.array([1.1,2.1]); interval = base_new+np.array([-q,q])
assert k==8 and interval[0]<=interval[1]
print({'rank':k,'correction':float(q),'new_interval':interval.tolist()})
R

Complete calculation — Complete finite-sample CQR calibration and interval construction given fixed hypothetical quantile predictions. Quantile fitting and coverage validation on independent test data are not performed.

y <- c(.2,.7,1.4,1.8,2.1,2.7,3.5,4)
lower <- c(0,.4,.8,1.2,1.6,2,2.4,2.8); upper <- lower+1
alpha <- .2; m <- length(y); scores <- pmax(lower-y,y-upper)
k <- ceiling((m+1)*(1-alpha)); q <- if(k>m) Inf else sort(scores)[k]
interval <- c(1.1,2.1)+c(-q,q)
stopifnot(k==8,interval[1]<=interval[2])
print(list(rank=k,correction=q,new_interval=interval))

Sources

From the CodeSplash reference, entry UNC009.