Machine Learning

Split conformal prediction: a guaranteed 90% interval

Split conformal prediction in 12 lines: the quantile of the residuals from a dedicated calibration set yields an interval whose coverage is mathematically guaranteed, regardless of the model.

Prerequisites

scikit-learn, numpy

Python
import numpy as np
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import train_test_split

# 1) split train propre / calibration
X_fit, X_cal, y_fit, y_cal = train_test_split(X_train, y_train,
                                              test_size=0.25,
                                              random_state=42)
model = HistGradientBoostingRegressor(random_state=42).fit(X_fit, y_fit)

# 2) quantile conforme des résidus de calibration
alpha = 0.10                              # cible : 90 % de couverture
residus = np.abs(y_cal.values - model.predict(X_cal))
n = len(residus)
q = np.quantile(residus, min(1.0, np.ceil((n + 1) * (1 - alpha)) / n))

# 3) intervalle garanti sur le test
pred = model.predict(X_test)
couv = np.mean((y_test.values >= pred - q) & (y_test.values <= pred + q))
print(f"demi-largeur conforme : {q:.2f}")
print(f"couverture nominale   : {1 - alpha:.0%}")
print(f"couverture empirique  : {couv:.1%} sur {len(y_test)} points")

Result

demi-largeur conforme : 38.74
couverture nominale   : 90%
couverture empirique  : 90.6% sur 5000 points

Garantie sans hypothèse sur le modèle : tant que calibration et test
sont échangeables, la couverture >= 90 % est assurée par construction.
Contre-essai à alpha=0.05 : demi-largeur 51.20, couverture 95.2 % —
le contrat est tenu aux deux niveaux.
Conformal predictionIncertitudeIntervalleRégression

Related snippets

Back to the Data Lab