Foundations to Frontier
Tests
I. Probabilistic ML and Uncertainty Quantification › I4 Calibration and Conformal Prediction

Calibration metrics: ECE and its relatives

A model is calibrated when its stated confidence matches how often it is right. The expected calibration error (ECE) is the standard summary: bin the predictions by confidence and average the gap between confidence and accuracy. It is also a noisy, biased estimator whose value depends on the number of bins, the binning scheme, the sample size and which probabilities you look at (top-label, class-wise or the full vector). This page builds on the reliability-diagram page and goes deeper into the metric itself: top-label, class-wise and canonical calibration, equal-width and equal-mass bins, the debiased estimator of Kumar et al. (2019), smooth and kernel ECE, and why ECE must be reported next to a proper scoring rule such as the Brier score or NLL. In the exercises you implement ECE, MCE, debiased ECE, class-wise ECE and the Brier score. You then measure the estimator on simulated predictions with known truth. A perfectly calibrated model scored ECE 0.057 with 100 examples and 0.006 with 10 000. With 1000 examples it scored 0.030 with 50 bins, and the debiased estimator cut this to 0.015. A useless constant predictor scored a perfect ECE of 0, while its Brier score showed it was worse.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Does 90% mean 90%?

A weather forecaster who says “70% chance of rain” is calibrated if it rains on about 70% of such days. A classifier’s confidence should mean the same. To check, group the predictions by confidence, then compare each group’s average confidence with how often it was right. The expected calibration error is the weighted average of those gaps. It sounds simple, but with few examples per group the gaps are mostly noise, so even a perfect model scores above zero. The demo lets you see this by changing the number of examples and bins.

Calibration, conformal and shift lab
Bars: accuracy per bin; dots: mean confidence; dashed: perfect calibration. Estimated ECE 0.0234, true ECE 0.000. With s = 1 the model is perfectly calibrated, yet the estimate is positive: binomial noise in small bins. More bins or fewer examples inflate it.
Why engineers care

An Earth-observation cloud classifier whose 0.95 confidence is right only 80% of the time will send cloudy scenes to the ground as clear. Downlink scheduling, tasking and sensor fusion all use the probabilities as if they were true.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A model is calibrated if…
  2. 2. ECE averages the gaps between…
  3. 3. A perfectly calibrated model evaluated on 100 examples has an estimated ECE that is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Top-label calibration (Guo et al., 2017): P(Ŷ = Y given confidence p) = p, using only the maximum probability. This is what the usual ECE measures.
  • Class-wise calibration: each class probability p_k is calibrated on its own. Canonical (full) calibration: the whole probability vector is calibrated, P(Y = k given p̂ = p) = p_k. Canonical implies class-wise, which implies top-label; none of the converses holds.
  • Binned ECE: split [0, 1] into B bins; ECE = Σ_b (n_b / N) · |conf_b − acc_b|. MCE takes the largest gap instead. Typical choices: 15 equal-width bins (Guo et al.) or equal-mass bins with the same count in each (Nixon et al., 2019).
  • Bias: in a calibrated bin of n_b examples, acc_b − conf_b has standard deviation about √(p(1 − p)/n_b), so the absolute gap is positive on average. ECE is biased upwards, more so with more bins and fewer examples.
  • Debiased l2 error (Kumar et al., 2019): subtract acc_b(1 − acc_b)/(n_b − 1) from each squared gap, then take the square root.
  • Smoother estimators: kernel ECE (Zhang et al., 2020), the ECE sweep that picks the largest monotone bin count (Roelofs et al., 2022) and smooth ECE (Błasiak et al., 2023) avoid hard bins.
  • Proper scoring rules: the Brier score and NLL are minimised only by the true probabilities. ECE is not proper: predicting the overall accuracy for every input gives ECE ≈ 0. Always report a proper score next to ECE.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Which is the strongest notion?
  2. 2. Why is ECE biased upwards?
  3. 3. Which score is proper?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Computing calibration metrics

Exercise 1.

  • Equal-width and equal-mass bin assignment.
  • ECE, MCE and the debiased l2 calibration error.
  • Top-label confidences, class-wise ECE and the multiclass Brier score.

Practice.

  • Report the bin count, binning scheme and test-set size with every ECE, or the number cannot be compared.
  • Use at least a few thousand test examples; show a reliability diagram with bin counts, not only the scalar.
  • Report a proper score (NLL or Brier) as well; ECE alone can be gamed.
  • For multiclass problems state whether ECE is top-label or class-wise; class-wise matters when every class probability is used downstream.
  • Bootstrap the test set for an interval on ECE; differences smaller than the noise floor mean nothing.
  • Libraries: torchmetrics (MulticlassCalibrationError, norm l1, l2 or max), scikit-learn calibration_curve (strategy uniform or quantile), netcal, and relplot for smooth ECE.
import torch
from torchmetrics.classification import MulticlassCalibrationError
from sklearn.calibration import calibration_curve

ece = MulticlassCalibrationError(num_classes=10, n_bins=15, norm="l1")   # top-label ECE
print(ece(probs, labels))               # probs: (N, 10) softmax outputs, labels: (N,)
conf, pred = probs.max(dim=1)
acc_per_bin, conf_per_bin = calibration_curve((pred == labels).numpy(), conf.numpy(),
                                              n_bins=15, strategy="quantile")   # equal-mass bins
🐍 Exercise 1: ECE, MCE, debiased ECE and Brier
Assign confidences to bins and compute ECE, MCE, the debiased calibration error, class-wise ECE and the Brier score.
📝 Quick check (Level 2)
0/3 answered
  1. 1. scikit-learn's equal-mass bins are chosen with…
  2. 2. With every ECE you should report…
  3. 3. Next to ECE also report…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Size of the bias. In a calibrated bin with accuracy p and n_b examples, acc_b − conf_b is roughly normal with mean 0 and standard deviation σ_b = √(p(1 − p)/n_b), so E|acc_b − conf_b| ≈ σ_b √(2/π). Summing with weights n_b/N gives an expected ECE of about √(2/π) · √(p(1 − p) · B_eff / N), where B_eff is the number of occupied bins. The bias shrinks like 1/√N and grows like √B. With N = 1000, p ≈ 0.9 and about 7.5 occupied bins (15 equal-width bins, confidences all above 0.5) this gives 0.021; the simulation measured 0.018.

Debiasing. For the squared gap, E[(acc_b − conf_b)²] = (true gap)² + p(1 − p)/n_b, and acc_b(1 − acc_b)/(n_b − 1) is an unbiased estimate of the noise term. Subtracting it gives an estimate of the l2 calibration error that is unbiased up to binning error; the square root adds a small bias back.

Binning bias goes the other way. Averaging inside a bin hides miscalibration that changes sign within it, so coarse bins underestimate the true error. Few bins: low variance, more binning bias. Many bins: more noise bias.

Proper scores. The Brier score decomposes (Murphy, 1973) into reliability (a calibration term) − resolution + uncertainty. A constant prediction has zero reliability error and zero resolution; a sharp, calibrated model keeps zero reliability and gains resolution. ECE sees only the first term.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The noise bias of ECE scales like…
  2. 2. Coarse bins tend to…
  3. 3. The Brier score rewards resolution, which ECE…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How far can you trust an ECE number?

History.

  • A. H. Murphy (1973) decomposed the Brier score; M. DeGroot and S. Fienberg (1983) formalised calibration and refinement for forecasters.
  • M. P. Naeini, G. Cooper and M. Hauskrecht (2015) introduced the binned ECE; C. Guo et al. (2017), “On calibration of modern neural networks”, made 15-bin top-label ECE the standard.
  • J. Nixon et al. (2019) “Measuring calibration in deep learning”; A. Kumar, P. Liang and T. Ma (2019) “Verified uncertainty calibration” (debiased estimator); R. Roelofs et al. (2022) ECE sweep; J. Błasiak et al. (2023) smooth ECE.

Exercise 2. Simulated top-label predictions with known truth.

  • Confidences: c = 1 − B/2 with B ~ Beta(1, 4), so confidences lie in [0.5, 1] with mean 0.9.
  • Calibrated model: correct with probability c (true ECE 0). Overconfident model: correct with probability 0.5 + 0.8(c − 0.5), true ECE 0.08 and true l2 error 0.0816.
  • Estimates: averaged over 100 repeated test sets.
perfectly calibrated, 15 bins mean ECE
N = 100 0.0567
N = 1000 0.0181
N = 10 000 0.0058
perfectly calibrated, N = 1000 equal-width equal-mass debiased l2
5 bins 0.0102 0.0147 0.0068
15 bins 0.0179 0.0263 0.0114
50 bins 0.0299 0.0469 0.0146
overconfident (true 0.08), N = 1000 equal-width ECE debiased l2 (true 0.0816)
5 bins 0.0813 ± 0.0117 0.0783 ± 0.0121
15 bins 0.0811 ± 0.0123 0.0800 ± 0.0130
50 bins 0.0889 ± 0.0106 0.0790 ± 0.0156
  • The noise floor is large. A perfectly calibrated model scored 0.057 on 100 examples, about what papers report as a real miscalibration. The estimate shrank tenfold for a hundredfold more data, as √(B/N) predicts.
  • More bins, more bias: 0.010 with 5 bins, 0.030 with 50. Equal-mass bins were worse here (0.047 at 50 bins). With all confidences above 0.5, equal-width bins leave half their bins empty, so equal-mass bins really use more bins. The debiased estimator halved the bias.
  • Under real miscalibration all estimators were close to the truth at 5 and 15 bins. At 50 bins, plain ECE overshot to 0.089 and the debiased one stayed near 0.08. The ± spread (about 0.012) is the run-to-run noise on a single test set of 1000.
  • ECE can be gamed: predicting the overall accuracy for everything gave ECE 0.0000 but Brier 0.0918, worse than the sharp calibrated model’s 0.0857 (ECE 0.0043).
  • Seeds 1 and 2: N = 100 gave 0.0545 and 0.0488; N = 1000 gave 0.0178 and 0.0174; N = 10 000 gave 0.0055 and 0.0058.
🐍 Exercise 2 (research): how far can you trust an ECE number?
Simulate calibrated and overconfident predictions with known calibration error; measure how binned, equal-mass and debiased ECE depend on sample size and bin count; compare ECE and Brier for a constant predictor.

Calibration metrics in AI, robotics and aerospace

  • Model selection: Minderer et al. (2021) re-measured calibration of modern vision models; comparisons only held once test size and binning were fixed.
  • Perception stacks: class-wise calibration matters when every class probability feeds a tracker or a Bayes filter, not only the top label.
  • LLMs: verbalised and token-probability confidences are scored with ECE and Brier on question-answering sets; the same noise floor applies to small evaluation sets.

References

  1. C. Guo, G. Pleiss, Y. Sun and K. Q. Weinberger (2017). “On calibration of modern neural networks.” ICML.
  2. A. Kumar, P. Liang and T. Ma (2019). “Verified uncertainty calibration.” NeurIPS.
  3. J. Nixon et al. (2019). “Measuring calibration in deep learning.” CVPR Workshops.
  4. R. Roelofs et al. (2022). “Mitigating bias in calibration error estimation.” AISTATS.
  5. J. Błasiak et al. (2023). “Smooth ECE: principled reliability diagrams via kernel smoothing.” arXiv.