Calibration: can you trust the probabilities
A model can rank well and still output wrong probabilities. Read a calibration curve and learn Platt scaling and isotonic calibration.
Lesson 39 of 6612 minAdvanced
You will learn to
- Read a calibration (reliability) curve
- Explain Platt scaling and isotonic calibration
- Know when calibration matters
Before this lesson
Intuition
When a weather forecast says "70 percent chance of rain", you expect rain on about 7 out of 10 such days. If it rains on only 4 out of 10, the forecaster may still be good at telling wetter days from drier days, but the number 70 is not trustworthy. That difference is calibration: do the predicted probabilities match how often the event really happens?
Ranking and calibration are separate skills. The AUC from the previous lesson only checks the ranking. Many strong models, such as tree ensembles, boosted trees and support vector machines, produce good rankings and scores that are not true probabilities. Others, such as logistic regression, tend to be better calibrated. If you only sort a list ("call the top 100 customers"), calibration does not matter. If you multiply the probability by money, or show it to a person as a percentage, it does.
Picture it
A calibration curve, also called a reliability diagram, groups rows into bins by predicted probability. For each bin, it plots the average predicted probability (horizontal) against the share of rows that were really positive (vertical). A perfect model lies on the diagonal.
Points below the diagonal mean over-confidence (predicting more than happens); points above mean under-confidence. An S shape, with low probabilities too low and high ones too high, is common for models that push scores toward the extremes.
The math
Two numbers summarise calibration. The Brier score is the mean squared difference between the predicted probability and the outcome . Lower is better:
The expected calibration error groups the rows into bins , with rows, mean prediction and observed rate :
To repair calibration, fit a small second model that maps the scores to probabilities, using rows the first model was not trained on:
- Platt scaling fits a sigmoid to the raw score : . It has only two numbers, so it works with little data, but it assumes the distortion has a sigmoid shape.
- Isotonic regression fits a free-form step function that never decreases. It can correct any monotone distortion, but it needs more data (a rule of thumb is thousands of rows) or it overfits.
Both keep the order of the scores, so they leave the ranking (and almost the AUC) unchanged.
Worked example
A churn model gives probabilities, which we group into five bins over 1,000 held-out customers (illustrative numbers):
| Bin | Mean predicted | Observed rate | Customers | Gap times customers |
|---|---|---|---|---|
| 1 | 0.10 | 0.05 | 400 | 0.05 × 400 = 20 |
| 2 | 0.30 | 0.18 | 250 | 0.12 × 250 = 30 |
| 3 | 0.50 | 0.42 | 150 | 0.08 × 150 = 12 |
| 4 | 0.70 | 0.58 | 120 | 0.12 × 120 = 14.4 |
| 5 | 0.90 | 0.78 | 80 | 0.12 × 80 = 9.6 |
The ECE is (20 + 30 + 12 + 14.4 + 9.6) / 1,000 = 86 / 1,000 = 0.086. Every bin sits below the diagonal: the model is over-confident everywhere.
A small Brier example for four rows: predictions 0.9, 0.8, 0.3, 0.6 and outcomes 1, 1, 0, 0 give ((0.1)² + (0.2)² + (0.3)² + (0.6)²) / 4 = (0.01 + 0.04 + 0.09 + 0.36) / 4 = 0.125.
Why it matters: a coupon costs 5 and earns a profit of 20 if the customer returns, so it pays when the probability is above 5 / 20 = 0.25. The model says 0.30 for bin 2, so the expected profit looks like 0.30 × 20 - 5 = +1. The real rate is 0.18, so the real expected profit is 0.18 × 20 - 5 = -1.4. The ranking was fine, but the decision rule was wrong because the probability was not honest. Fitting isotonic regression to these bins would map 0.30 to about 0.18 and fix the rule. As always, fit the calibrator on rows the model did not train on, and check the curve again on other rows.
How mlflow does it
The Plots tab of a model candidate (binary classification only) has a Calibration plot: ten bins of predicted probability against the observed share of positives, computed on the held-out test rows. Use it to compare candidates. The app does not add a calibration step by itself. If you work with the engine library directly, a calibrated classifier wrapper is available there. See Classification for the plots available per task.
Common mistakes
- Equating a high AUC with trustworthy probabilities. AUC only checks the order. Look at the calibration curve before using a probability in a calculation.
- Calibrating on the training rows. The model's scores on rows it trained on are too good, so the fix learns the wrong mapping. Use separate rows or cross-validation.
- Using isotonic regression on a small dataset. It builds a step function with many steps and overfits. Prefer Platt scaling when you have few rows.
- Reading a bumpy curve with few rows per bin. A bin with 10 rows has a very noisy observed rate. Check how many rows each point represents.
- Forgetting that calibration drifts. If the share of positives changes in production, calibrated probabilities become wrong again. Monitor the curve on fresh data.
Try it in mlflow
Open the calibration curve
- Open Runs, open a finished binary classification run and click a model candidate.
- Open the Plots tab and choose Calibration.
- Compare the curve with the diagonal. Points below the diagonal mean the model is over-confident; points above mean it is under-confident.
- Open a second candidate (for example a linear model and a tree-based one) and compare how close each one is to the diagonal.