Expected Calibration Error (ECE)
Expected calibration error summarizes how far a model’s predicted probabilities sit from the frequencies actually observed. Predictions are grouped into bins, each bin’s average prediction is compared with what happened in that bin, and the differences are averaged with weights proportional to bin size:
\[ \text{ECE} = \sum_{m=1}^{M} \frac{|B_m|}{N} \big| \text{obs}(B_m) - \text{pred}(B_m) \big| \]
It is the numerical companion to a reliability curve: the curve shows where the model is wrong, and this reduces the same information to one number. Two things about that number are worth knowing before quoting it.
Which ECE
The same acronym covers two quantities, and they are not interchangeable.
| Binary ECE | Top-label ECE | |
|---|---|---|
| What is binned | The predicted probability of the positive class | The largest predicted class probability — the model’s confidence |
| What it is compared with | The observed rate of the positive class in the bin | The accuracy of the predicted class in the bin |
| Question answered | When this model says 0.7, does the event happen 70 percent of the time? | When this model is 70 percent confident, is it right 70 percent of the time? |
| Usual setting | Binary risk scoring — fraud, credit, disease | Multiclass classifiers, and the form used in the neural-network calibration literature |
The top-label version is the one in Guo and colleagues’ work on neural network calibration, which defines the problem as “predicting probability estimates representative of the true correctness likelihood” — a statement about correctness, not about an event’s probability. The binary version is what a risk model needs, and scikit-learn’s definition is the one to hold for it: “a well calibrated (binary) classifier should classify the samples such that among the samples to which it gave a predict_proba value close to, say, 0.8, approximately 80% actually belong to the positive class.”
Mixing them produces numbers that mean nothing. Comparing a bin’s mean predicted positive-class probability against the accuracy of a 0.5-threshold decision, for instance, subtracts two quantities that are not estimating the same thing: a confident and correct negative prediction of 0.05 contributes an apparent error of 0.95. Run that comparison on 200,000 predictions from a model that is exactly right and it reports an error of 0.25, where the binary calculation on the same data reports 0.002. The arithmetic runs, the plot draws, and the number is a measure of something else entirely.
Finite-sample noise can produce positive ECE
One implementation detail is easy to get wrong and silently loses data. Half-open bins written as \(p > \text{lo}\) and \(p \le \text{hi}\) exclude \(p = 0\) from every bin, so a model that ever predicts exactly zero has those cases dropped from the score. The code below closes the first bin on the left so that \([0, 1]\) is covered exactly once.
The more consequential property is that the measured value is biased upward as an estimate of the binned calibration error. Each bin’s observed rate is estimated from finitely many observations, so it differs from the bin’s mean prediction by chance alone, and taking absolute values means those chance differences never cancel — that is Jensen’s inequality applied to \(|\cdot|\). This is sampling noise, and it is a separate matter from the binning itself, which works the other way: averaging within a bin lets a model that is too high at one end of the bin and too low at the other cancel out, so binning can understate a finer-grained calibration error. A model whose probabilities are exactly right scores above zero, and how far above depends on the sample size and on the number of bins.
import numpy as np
def perfectly_calibrated(n, seed):
"""Predictions that are exactly right: an event with stated probability p occurs with probability p."""
rng = np.random.default_rng(seed)
p = rng.uniform(0, 1, n)
return (rng.uniform(size=n) < p).astype(int), p
def ece(y, p, bins=10):
"""Expected calibration error: observed rate against mean prediction, weighted by bin size."""
edges = np.linspace(0, 1, bins + 1)
total = 0.0
for i in range(bins):
lo, hi = edges[i], edges[i + 1]
in_bin = (p >= lo) & (p <= hi) if i == 0 else (p > lo) & (p <= hi)
if in_bin.sum():
total += in_bin.sum() / len(y) * abs(y[in_bin].mean() - p[in_bin].mean())
return total
# A model that is exactly right still scores above zero, and the score depends on the binning.
for n in (500, 5_000, 50_000):
y, p = perfectly_calibrated(n, seed=4)
print(f"{n:>6} " + " ".join(f"{ece(y, p, bins=b):.4f}" for b in (5, 10, 20, 50)))
# 500 0.0227 0.0350 0.0565 0.1032
# 5000 0.0111 0.0151 0.0189 0.0303
# 50000 0.0043 0.0058 0.0066 0.0104
| Sample size | 5 bins | 10 bins | 20 bins | 50 bins |
|---|---|---|---|---|
| 500 | 0.023 | 0.035 | 0.057 | 0.103 |
| 5,000 | 0.011 | 0.015 | 0.019 | 0.030 |
| 50,000 | 0.004 | 0.006 | 0.007 | 0.010 |
The underlying calibration error is zero, but every finite-sample estimate in this run is positive. On 500 predictions the choice of binning alone moves the score from 0.023 to 0.103 — a factor of four and a half, with no change to the model. A reported ECE of 0.03 on a validation set of a few hundred is not evidence of miscalibration at all; it is roughly what perfection scores.
Two consequences follow for reading anyone’s ECE, including your own. It is not comparable across papers, datasets or bin counts unless all three match. And the useful comparison is against the score a perfectly calibrated model would get on a sample of the same size with the same binning — which can be simulated in a few lines, as above, and almost never is.
What the average hides
Weighting by bin size is what makes the number an expectation, and it is also what makes it insensitive to the region that usually matters. A fraud model’s predictions pile up near zero, so the bins above 0.8 hold few cases and contribute almost nothing to the total — and those are exactly the cases an investigator acts on. A model can be badly wrong where it is confident and still post a low ECE.
The standard responses are to report the maximum bin error alongside the average, to use quantile-based bins so that each holds the same number of predictions rather than the same width, or to weight bins by their decision relevance instead of their size. Adaptive binning is the second of these, and it addresses the coverage problem while leaving the bias problem intact.
Reporting it honestly
- Say which ECE. Binary or top-label, and for multiclass which averaging.
- Say the binning. Number of bins and whether they are equal-width or equal-count.
- Say the sample size, and ideally what a perfectly calibrated model would score on it.
- Show the curve. One number cannot say whether the model is overconfident everywhere or wrong in one region, which is the thing a fix depends on.
- Report a proper scoring rule too. The Brier score has no binning parameter and cannot be gamed, and its decomposition separates the calibration part from the discrimination part.
ECE earns its place as a communication device — a single number that makes a calibration problem visible to people who will not read a plot. It is a poor object to optimize, because a model can lower it by predicting the base rate everywhere, which destroys all the information the model had. What calibration is and why it is separate from accuracy is covered under model calibration; the fixes are under Platt scaling.
References
- Chuan Guo, Geoff Pleiss, Yu Sun and Kilian Q. Weinberger, “On Calibration of Modern Neural Networks”, ICML 2017, arXiv:1706.04599
- scikit-learn User Guide, Probability calibration
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
