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.

000.50.511Mean predicted probabilityObserved share of positivesModelPerfectly calibrated
An over-confident model: every point is below the diagonal. When it says 0.9, the real rate is 0.78. The vertical bars show the gaps.

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 pip_i and the outcome yi∈{0,1}y_i \in \{0, 1\}. Lower is better:

Brier=1n∑i=1n(pi−yi)2\text{Brier} = \frac{1}{n}\sum_{i=1}^{n} (p_i - y_i)^2

The expected calibration error groups the rows into bins bb, with nbn_b rows, mean prediction pˉb\bar{p}_b and observed rate yˉb\bar{y}_b:

ECE=∑bnbn ∣pˉb−yˉb∣\text{ECE} = \sum_b \frac{n_b}{n}\,\big|\bar{p}_b - \bar{y}_b\big|

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 ss: p=1/(1+eAs+B)p = 1 / (1 + e^{A s + B}). 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):

BinMean predictedObserved rateCustomersGap times customers
10.100.054000.05 × 400 = 20
20.300.182500.12 × 250 = 30
30.500.421500.08 × 150 = 12
40.700.581200.12 × 120 = 14.4
50.900.78800.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

  1. Open Runs, open a finished binary classification run and click a model candidate.
  2. Open the Plots tab and choose Calibration.
  3. Compare the curve with the diagonal. Points below the diagonal mean the model is over-confident; points above mean it is under-confident.
  4. Open a second candidate (for example a linear model and a tree-based one) and compare how close each one is to the diagonal.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. On a calibration curve, a point at (0.8, 0.6) means what?
  2. 2. Can a model have a high AUC and poor calibration?
  3. 3. In which case does calibration matter most?