Sub topic 15
When calibration breaks: distribution shift and monitoring
This chapter assumes you can fit a calibrator with CalibratedClassifierCV, read a reliability diagram, and compute Brier score and expected calibration error (ECE). You will see why a calibrator that worked at training time can fail in production, and how to catch that.
Why calibration does not travel
A calibrator learns a mapping from scores to probabilities on one data distribution. It is correct only while the relationship between scores and outcomes stays the same. When production data differs from the calibration data, that relationship changes, and the probabilities drift without any error message.
Two common kinds of shift matter here:
- Covariate shift. The inputs change, for example a new customer segment. The model may still be calibrated if the conditional probability of the label given the inputs is unchanged. In practice, models are rarely accurate everywhere, so calibration often degrades in the new region.
- Prior (label) shift. The base rate of the positive class changes. A calibrator fitted at a 5% positive rate will systematically under-predict when the rate becomes 15%.
Seeing the effect
The code below calibrates a model on data with a 10% positive rate. It then evaluates on a test set whose positive rate is raised by resampling. The calibrator was never refitted, so the shifted Brier score and ECE get worse.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import brier_score_loss, roc_auc_score
X, y = make_classification(
n_samples=40000, n_features=10, n_informative=5,
weights=[0.9, 0.1], random_state=0,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
model = CalibratedClassifierCV(
LogisticRegression(max_iter=1000), method="sigmoid", cv=5
).fit(X_train, y_train)
rng = np.random.default_rng(0)
pos = np.flatnonzero(y_test == 1)
neg = np.flatnonzero(y_test == 0)
n_neg = len(neg) // 2
idx = np.concatenate([pos, rng.choice(neg, size=n_neg, replace=False)])
X_shift, y_shift = X_test[idx], y_test[idx]
for name, Xs, ys in [("original", X_test, y_test), ("shifted", X_shift, y_shift)]:
p = model.predict_proba(Xs)[:, 1]
print(
f"{name:<9} base rate {ys.mean():.3f} "
f"mean pred {p.mean():.3f} "
f"Brier {brier_score_loss(ys, p):.4f} AUC {roc_auc_score(ys, p):.3f}"
)On the shifted set, the mean prediction stays well below the new base rate, while AUC is essentially unchanged. That gap is the signature of prior shift.
Monitoring in production
Labels often arrive late, so monitor in two layers.
- Label-free signals, available immediately. Track the mean predicted probability and the distribution of scores. A shift in the mean, compared with a reference window, is an early warning. Also track input feature drift.
- Label-based signals, available after a delay. When outcomes arrive, compute Brier score, log loss, and ECE on a rolling window. Compare mean prediction with the observed positive rate directly.
Responding to drift
- Recalibrate before retraining. Refitting only the calibrator is cheap and needs less data than retraining the model. It fixes shifts in the score-to-probability mapping.
- Retrain when ranking degrades. If AUC falls too, the model has lost signal, and recalibration cannot restore it.
- Keep leakage rules. Fit the new calibrator on recent labeled data that the base model did not train on.
- Set thresholds from experience. Choose alert limits using the normal variation of ECE across past windows. Small windows give noisy ECE, so require enough samples per bin.
Exercise
Your model's AUC is unchanged after a deployment, but the mean predicted probability is 0.08 while the observed positive rate is 0.15. What kind of problem is this, and what is the first fix to try?
Show answer
This is a calibration failure, most likely from prior (base rate) shift. Ranking is intact, as the stable AUC shows. The first fix is to refit the calibrator on recent labeled data not used to train the base model, rather than retraining the whole model.