Sub topic 8
Calibration: when predicted probabilities can be trusted
Prerequisites and the core idea
Assume you know binary classification metrics, train/validation/test splits, and that your production model logs a probability for the positive class. Calibration asks whether those probabilities match observed frequencies.
A model is calibrated if predictions near 0.8 are positive about 80% of the time. Predictions near 0.2 should be positive about 20% of the time. Calibration concerns the meaning of a score, not just its ability to rank examples.
This matters when labels are delayed. If probabilities are calibrated on a representative population, they can estimate expected accuracy before the true labels arrive. For a prediction with probability p:
- If the model predicts positive, its expected correctness is
p. - If it predicts negative, its expected correctness is
1 - p.
Averaging these values produces a label-free accuracy estimate. The estimate is useful only while calibration remains valid.
A calibration workflow
Use a validation set that was not used to train the original model. First measure calibration, then fit a calibrator if necessary. Common methods include:
- Platt scaling, which fits a logistic transformation.
- Isotonic regression, which fits a flexible monotonic curve.
- Sigmoid calibration, which is often safer with smaller datasets.
- Isotonic calibration, which can overfit when few examples are available.
The calibration data should represent the production population. A random split may be unsuitable if production behavior changes over time.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.calibration import CalibratedClassifierCV
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, brier_score_loss
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_samples=4000, n_features=10, weights=[0.7, 0.3], random_state=7
)
X_train, X_cal, y_train, y_cal = train_test_split(
X, y, test_size=0.35, stratify=y, random_state=7
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
calibrated = CalibratedClassifierCV(
estimator=model, method="sigmoid", cv="prefit"
).fit(X_cal, y_cal)
p = calibrated.predict_proba(X_cal)[:, 1]
predicted_positive = p >= 0.5
expected_accuracy = np.where(predicted_positive, p, 1 - p).mean()
print(f"Observed accuracy: {accuracy_score(y_cal, predicted_positive):.3f}")
print(f"Expected accuracy: {expected_accuracy:.3f}")
print(f"Brier score: {brier_score_loss(y_cal, p):.3f}")The Brier score is the mean squared difference between predicted probabilities and binary outcomes. Lower is better. It measures both calibration and probability sharpness, so compare it with a reliability diagram rather than treating it as a complete diagnosis.
Reading calibration in production
Group predictions into probability bins, such as 0.0–0.1 and 0.1–0.2. For each bin, compare the mean predicted probability with the fraction of positive labels once those labels mature. Large differences indicate miscalibration.
Calibration can fail when the base positive rate changes, features drift, or the relationship between features and outcomes changes. Recalibrate using recent labeled data when monitoring shows persistent error. Do not silently treat missing labels as negatives unless the domain guarantees that an absent outcome means a negative outcome.
| Check | What it tells you |
|---|---|
| Reliability bins | Whether probability values match observed frequencies |
| Brier score | Combined probability error on labeled data |
| Expected versus observed accuracy | Whether the label-free estimate remains trustworthy |
Exercise
Exercise
A production model predicts probabilities of 0.9 for positive cases, but only 0.7 of these cases are eventually positive. Is the model calibrated in this range, and can its expected accuracy estimate be trusted without adjustment?
Show answer
No. It is overconfident in this range. Its expected accuracy estimate is likely too high until calibration is updated or the estimate is corrected.