Search

Search papers and lessons

Model Calibration

Level 3 · 11 min read

A model is calibrated when its stated probabilities match how often those events really happen; a model can rank well and still be badly calibrated.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Probability
  • Learning basics
  • Neural network basics
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A model is calibrated when the probabilities it states can be trusted at face value. If it says "70 percent" about a set of cases, about 70 out of 100 of those cases should turn out to be true. Calibration is a separate property from accuracy: a model can order its guesses correctly and still claim far too much confidence, and a calibration score measures that gap.

Builds on: Conditional Probability, Expected Value, Softmax · Leads to: Proper Scoring Rule, Softmax Temperature, Cross-Entropy Loss

Where it's used

Weather forecasts. A forecaster who says "30 percent chance of rain" on many days is useful only if it rains on roughly 30 percent of those days. Forecasting agencies check this kind of record so that the numbers in the public forecast mean what they say.

Spam and fraud filters. A filter that flags a message with score 0.95 is making a claim about risk. If the messages it flags at that level are only half spam, the score is misleading, and a team that sets a threshold at 0.95 will pick the wrong cutoff. Calibration checks decide whether a score can be used as a probability at all.

Language models that abstain. A system that answers when it is confident and says "I am not sure" otherwise depends on its confidence. If the model's confidence is high on answers that are often wrong, the abstention rule will let mistakes through. Calibration is measured before such a rule is trusted, and temperature scaling is a common fix (see Softmax Temperature).

How it works

Take a classifier that outputs a probability for "spam" on each of 10 emails. We ask one question: when the model says about 0.6, is it right about 60 percent of the time, and when it says about 0.9, is it right about 90 percent of the time?

  1. Collect the predictions. Each email gives a predicted probability (its confidence) and a true label. The 5 emails in the first group have confidences that average 0.6, and the 5 in the second group average 0.9.
  2. Group them into bins by confidence. Bin A holds the 5 emails near 0.6. Bin B holds the 5 near 0.9. In practice bins are often ten equal ranges, such as 0.0 to 0.1, 0.1 to 0.2 and so on, but two bins keep the arithmetic visible.
  3. Measure accuracy inside each bin. In bin A, 3 of the 5 emails are really spam, so the accuracy is 3 out of 5, which is 0.6. In bin B, also 3 of 5 are spam, so the accuracy is 0.6.
  4. Compare confidence with accuracy. In bin A the average confidence is 0.6 and the accuracy is 0.6, so the gap is 0. This bin is calibrated. In bin B the average confidence is 0.9 and the accuracy is 0.6, so the gap is 0.3. This bin is overconfident. Check: 0.9 minus 0.6 is 0.3.
  5. Average the gaps, weighted by bin size. Each bin has half the emails, so the weights are 5 out of 10 each. The expected calibration error is 0.5×0+0.5×0.3=0.150.5 \times 0 + 0.5 \times 0.3 = 0.15. Check: half of 0 is 0, half of 0.3 is 0.15.
  6. Read the answer. An error of 0.15 means the typical stated probability is off by about 15 percentage points. A perfectly calibrated model would score 0.

Plot each bin as a point, with its average confidence on the horizontal axis and its accuracy on the vertical axis. A calibrated bin lies on the diagonal. The 0.9 bin lies below it, because its accuracy is lower than its confidence.

00.20.40.60.8100.20.40.60.81average confidence in binaccuracy in bin
  • perfectly calibrated
  • the two bins
Each point is a bin from the walkthrough. The 0.6 bin lies on the diagonal; the 0.9 bin is right only 0.6 of the time, so it sits below.

The math

Step 1 in symbols: predictions as pairs

For each example ii, the model gives a predicted class y^i\hat{y}_i and a confidence p^i\hat{p}_i, the probability it assigns to that class. The confidence is the largest entry of the predicted distribution, and the true label is yiy_i. The example is correct when y^i=yi\hat{y}_i = y_i.

Step 2 in symbols: bins

Split the interval [0,1][0, 1] into BB bins I1,…,IBI_1, \dots, I_B of equal width. Let Bb\mathcal{B}_b be the set of examples whose confidence falls in bin IbI_b, and let nbn_b be its size. The total number of examples is nn.

Step 3 in symbols: accuracy and confidence per bin

The accuracy of bin bb and its average confidence are

acc(b)=1nb∑i∈Bb1[y^i=yi],conf(b)=1nb∑i∈Bbp^i\text{acc}(b) = \frac{1}{n_b} \sum_{i \in \mathcal{B}_b} \mathbf{1}\big[\hat{y}_i = y_i\big], \qquad \text{conf}(b) = \frac{1}{n_b} \sum_{i \in \mathcal{B}_b} \hat{p}_i

Here 1[⋅]\mathbf{1}[\cdot] is 1 when the statement is true and 0 otherwise. The accuracy is the fraction correct and the confidence is the mean stated probability.

Step 4 in symbols: the gap

The gap in bin bb is ∣acc(b)−conf(b)∣|\text{acc}(b) - \text{conf}(b)|. It is 0 when the bin is calibrated and grows as the bin's claims move away from its results.

Step 5 in symbols: expected calibration error

Weight each gap by the bin's share of the examples and add:

ECE=∑b=1Bnbn ∣acc(b)−conf(b)∣\text{ECE} = \sum_{b=1}^{B} \frac{n_b}{n} \, \big| \text{acc}(b) - \text{conf}(b) \big|

The walkthrough had n=10n = 10, two bins each with nb=5n_b = 5, gaps 0 and 0.3, and so ECE=0.15\text{ECE} = 0.15.

The ideal condition

The exact target is a statement about conditional probability. A model is calibrated when, for every confidence level cc,

P(Y=Y^∣P^=c)=cP\big(Y = \hat{Y} \mid \hat{P} = c\big) = c

In words: among all cases where the model says cc, the event happens a fraction cc of the time. ECE is a finite-sample estimate of this condition, which is why it depends on the bins.

Worked examples

Example
Example 1: a forecaster who says 70 percent

A forecaster gives "70 percent chance of rain" on 10 days. It rains on 6 of them.

  1. The stated probability is 0.7 on every day, so the average confidence is 0.7.
  2. The observed frequency is 6 out of 10, which is 0.6.
  3. The gap is ∣0.6−0.7∣=0.1|0.6 - 0.7| = 0.1. Check: 0.7 minus 0.6 is 0.1.
  4. The sign matters here. The stated 0.7 is higher than the observed 0.6, so the forecaster is too confident that rain will come. It would be calibrated on these days if it had said 0.6.
Example
Example 2: two bins, twenty predictions

A model makes 20 predictions in two bins of 10.

  1. Bin 1 has an average confidence of 0.6, and 8 of its 10 predictions are correct, so acc=0.8\text{acc} = 0.8.
  2. The gap in bin 1 is ∣0.8−0.6∣=0.2|0.8 - 0.6| = 0.2.
  3. Bin 2 has an average confidence of 0.9, and 7 of its 10 predictions are correct, so acc=0.7\text{acc} = 0.7.
  4. The gap in bin 2 is ∣0.7−0.9∣=0.2|0.7 - 0.9| = 0.2.
  5. Each bin holds half the examples, so ECE=0.5×0.2+0.5×0.2=0.2\text{ECE} = 0.5 \times 0.2 + 0.5 \times 0.2 = 0.2.
  6. Interpretation: bin 1 is underconfident (the model is right more often than it says) and bin 2 is overconfident. The ECE hides the direction. The reliability curve would show it.
Example
Example 3: softmax confidence and a temperature fix

A classifier produces raw scores (logits) z=(2.0,0.5,−1.0)z = (2.0, 0.5, -1.0) for three classes. Its confidence is the largest Softmax probability.

  1. Exponentiate each score: e2.0≈7.389e^{2.0} \approx 7.389, e0.5≈1.649e^{0.5} \approx 1.649, e−1.0≈0.368e^{-1.0} \approx 0.368.
  2. Add them: 7.389+1.649+0.368=9.4067.389 + 1.649 + 0.368 = 9.406.
  3. Divide: the probabilities are 7.389/9.406≈0.7867.389 / 9.406 \approx 0.786, 1.649/9.406≈0.1751.649 / 9.406 \approx 0.175 and 0.368/9.406≈0.0390.368 / 9.406 \approx 0.039.
  4. The confidence is 0.786. Check: the three probabilities sum to about 1.000.
  5. Now divide every score by a temperature T=2T = 2 before the softmax. The scores become (1.0,0.25,−0.5)(1.0, 0.25, -0.5).
  6. Exponentiate: e1.0≈2.718e^{1.0} \approx 2.718, e0.25≈1.284e^{0.25} \approx 1.284, e−0.5≈0.607e^{-0.5} \approx 0.607. Their sum is 4.6094.609.
  7. The new top probability is 2.718/4.609≈0.5902.718 / 4.609 \approx 0.590. The confidence fell from 0.786 to 0.590 without changing which class wins.
  8. In practice TT is chosen on held-out validation data so that the stated probabilities match observed frequencies. The ranking of classes never changes, which is why accuracy stays the same.

Try it: with T=0.5T = 0.5, the scores become (4.0,1.0,−2.0)(4.0, 1.0, -2.0). What is the new top probability, and does the model get more or less confident? Answer: e4≈54.60e^{4} \approx 54.60, e1≈2.718e^{1} \approx 2.718, e−2≈0.135e^{-2} \approx 0.135, sum 57.4557.45, top probability 54.60/57.45≈0.95054.60 / 57.45 \approx 0.950. The model is more confident, because a small temperature sharpens the distribution.

Common mistakes and tradeoffs

It is tempting to think that a model with high accuracy is calibrated. In fact accuracy and calibration are different properties, because accuracy counts correct answers and calibration counts whether the stated numbers match the frequencies. A model can be 90 percent accurate and still say 99 percent on every answer.

It is tempting to think that a model that ranks examples well is calibrated. In fact a ranking metric such as the [[area-under-roc-curve|area under the ROC curve]] only asks whether positives get higher scores than negatives. Squaring every confidence keeps the order of the examples unchanged (0.9 becomes 0.81, 0.6 becomes 0.36), but it moves the numbers away from the observed frequencies.

It is tempting to think that ECE is a complete measure. In fact ECE depends on the number and placement of bins, is noisy when a bin has few examples, and can be near zero while individual bins are badly off in opposite directions. Pair it with a reliability diagram and a proper scoring rule such as the Brier score or the log loss, which reward calibrated numbers and penalise confident mistakes.

When calibration matters: when the probability itself is used, such as in a threshold, an expected-cost calculation or a decision that combines risks. When it matters less: when only the ranking is used, for example to pick the top five candidates from a list.

Alternatives: temperature scaling (one parameter, cheap, often effective), histogram binning and isotonic regression (more flexible, need more data), and training with a proper scoring rule in the first place.

History

In 1950 the meteorologist Glenn Brier proposed a score for probability forecasts that rewards numbers close to what happens. Scoring the stated probability itself, rather than only whether the event happened, made calibration a standard topic in forecasting and decision analysis.

In machine learning, calibration mattered less while models were small. Papers in the late 2010s showed that deep neural networks trained to high accuracy were often overconfident. Guo and colleagues, in 2017, documented this for modern image classifiers and showed that a single temperature parameter fitted on validation data could largely fix it. That result made calibration a routine check in model evaluation.

Recap

  • A model is calibrated when a stated probability cc matches the observed frequency of the event, roughly cc of the time.
  • Calibration is separate from accuracy and from ranking, so a model can have one without the others.
  • Measure it by binning predictions by confidence, comparing each bin's accuracy with its average confidence, and averaging the gaps weighted by bin size to get the expected calibration error.
  • Softmax confidence can be adjusted by a temperature, which changes the numbers but not the top class.
  • What to read next: the scoring rule that rewards calibrated numbers in Proper Scoring Rule, and the temperature mechanism in Softmax Temperature.

Check yourself

QuestionA model's confidence is 0.8 on 50 predictions and it is correct on 30 of them. What is the gap, and is the model overconfident or underconfident?Show answer

The accuracy is 30 out of 50, which is 0.6. The gap is ∣0.6−0.8∣=0.2|0.6 - 0.8| = 0.2. The model is overconfident, because it claims 0.8 but is right 0.6 of the time.

QuestionCompute the expected calibration error for two bins. Bin A has 6 examples, average confidence 0.55 and accuracy 0.5. Bin B has 4 examples, average confidence 0.9 and accuracy 0.9. What is the ECE?Show answer

Total n=10n = 10. Bin A's gap is ∣0.5−0.55∣=0.05|0.5 - 0.55| = 0.05, weighted by 6/10=0.66/10 = 0.6, giving 0.03. Bin B's gap is 0, weighted by 4/104/10, giving 0. The ECE is 0.03+0=0.030.03 + 0 = 0.03.

QuestionWhy does temperature scaling leave a classifier's accuracy unchanged, even though it changes every confidence?Show answer

Dividing all scores by the same positive temperature before the softmax does not change which score is largest, so the predicted class is the same for every example. Accuracy depends only on the predicted class, so it is unchanged, while the confidence values move toward or away from uniform.

QuestionA deployed model has high accuracy and a low ECE on its validation set, yet its confidence on new data is far too high. Give one likely reason and one check you would run.Show answer

The new data may come from a different distribution than the validation set, so the calibration fitted on validation no longer holds. A check is to compute the ECE and a reliability diagram on a recent labelled sample from the new data and compare it with the validation numbers.

Sources