Sub topic 7
Expected calibration error and its pitfalls
This chapter assumes you can read a reliability diagram, know what log loss and Brier score measure, and can write basic NumPy. Expected calibration error (ECE) compresses a reliability diagram into one number. It is easy to compute and easy to misuse.
What ECE measures
ECE bins the predictions, then takes the gap between the observed positive rate and the mean predicted probability in each bin. It averages those gaps, weighting each bin by the share of samples it holds.
For bins B1 to BM with n samples in total:
ECE = sum over m of (|Bm| / n) × |acc(Bm) − conf(B_m)|
Here acc is the fraction of positives in the bin and conf is the mean predicted probability. A perfectly calibrated model has ECE of 0.
Computing it
import numpy as np
def expected_calibration_error(y_true, y_prob, n_bins=10):
y_true = np.asarray(y_true)
y_prob = np.asarray(y_prob)
edges = np.linspace(0.0, 1.0, n_bins + 1)
bin_ids = np.digitize(y_prob, edges[1:-1], right=True)
ece = 0.0
for b in range(n_bins):
mask = bin_ids == b
if mask.any():
weight = mask.mean()
gap = abs(y_true[mask].mean() - y_prob[mask].mean())
ece += weight * gap
return eceThe function uses equal-width bins on [0, 1]. np.digitize against the inner edges assigns each probability to a bin index from 0 to n_bins - 1. Empty bins are skipped. mask.mean() is the share of samples in the bin, which is the weight.
Pitfalls
The number depends on binning
Changing n_bins or switching between equal-width and quantile bins changes ECE. With few bins, errors that point in opposite directions cancel inside a bin. With many bins, each bin holds few samples, so the observed rate is noisy and ECE is biased upward even for a perfect model.
Equal-width bins can be dominated by one region
Many classifiers, such as boosted trees and neural networks, put most predictions near 0 or 1. Equal-width bins then put nearly all the weight on the extreme bins. Quantile bins give each bin the same number of samples, but their widths vary, so scores are not comparable across the two schemes.
It is not a proper scoring rule
A model can lower ECE without becoming a better probability forecaster. A constant prediction equal to the base rate has an ECE near 0, yet it carries no information about individual cases. Always report ECE next to log loss or Brier score, and next to a ranking metric such as AUC.
Small samples give noisy estimates
With a few hundred test samples, ECE differences of 0.01 are usually noise. Use a large held-out set, or bootstrap the test set and report an interval.
Binary versus multiclass
The function above covers binary problems. For multiclass models, a common variant measures only the top predicted class: confidence is the maximum probability and accuracy is whether that class was right. That checks less than full calibration of every class probability.
Practical use
- Fix the binning scheme and bin count, and report them with the number.
- Check a reliability diagram alongside the number to see where the gaps are.
- Compare models with log loss or Brier score first. Use ECE as a diagnostic.
Exercise
A model outputs 0.5 for every sample, and the true positive rate is 0.5. What is its ECE, and why is ECE alone not enough to judge it?
Show answer
ECE is 0 (up to sampling noise), because the single occupied bin has mean prediction 0.5 and observed rate 0.5. The model has no ability to separate positives from negatives, so AUC is 0.5 and its Brier score and log loss are no better than predicting the base rate. ECE does not penalize that lack of information.
Exercise
You compute ECE on 300 test samples with 30 equal-width bins and get 0.09 for a model you believe is well calibrated. What is a likely cause, and what would you change?
Show answer
Each bin holds only about 10 samples on average, so observed rates are very noisy, which inflates ECE. Use fewer bins (for example 10 or fewer), use quantile bins, collect more test data, or bootstrap to get an interval.