Roadmap / Evaluation & Safety

✓

Expected Calibration Error

easyevaluationcalibration

The problem

When a model says it is 80% sure, is it right 80% of the time? A model whose confidence matches its accuracy is calibrated. Modern networks are often overconfident: they say 95% and are right 75% of the time. The ECE (expected calibration error) measures the mismatch with one number.

For nn predictions with confidences ci∈[0,1]c_i \in [0, 1] (the probability of the predicted class) and correctness yi∈{0,1}y_i \in \{0, 1\}, split [0,1][0, 1] into MM equal-width bins and compare, in each bin, the accuracy (the fraction right) with the mean confidence:

ECE=∑m=1M∣Bm∣n gapm\mathrm{ECE} = \sum_{m=1}^{M} \frac{|B_m|}{n}\, \mathrm{gap}_mgapm=∣acc(Bm)−conf(Bm)∣\mathrm{gap}_m = \big|\mathrm{acc}(B_m) - \mathrm{conf}(B_m)\big|

where BmB_m is the set of predictions in bin mm, ∣Bm∣|B_m| its size, and each gap is weighted by how many predictions the bin holds.

The bins

Bin mm (counting from 0) holds the confidences in (m/M,(m+1)/M](m/M, (m+1)/M]: the left end is excluded and the right end included, and confidence exactly 0 goes in bin 0. So with M=10M = 10: c=0.1c = 0.1 is in bin 0, c=0.6c = 0.6 in bin 5, c=1.0c = 1.0 in bin 9. The bin of a confidence is ⌈cM⌉−1\lceil cM \rceil - 1, clipped to [0,M−1][0, M-1] (so that 0 does not give −1-1).

Example. Six predictions with M=5M = 5 bins of width 0.20.2: confidences 0.95,0.90,0.85,0.60,0.55,0.300.95, 0.90, 0.85, 0.60, 0.55, 0.30 and correctness 1,0,1,1,0,01, 0, 1, 1, 0, 0.

  • 2: (0.4,0.6](0.4, 0.6] — holds: 0.60,0.550.60, 0.55; count: 2; accuracy: 0.50.5; mean confidence: 0.5750.575; gap: 0.0750.075
  • 1: (0.2,0.4](0.2, 0.4] — holds: 0.300.30; count: 1; accuracy: 00; mean confidence: 0.30.3; gap: 0.30.3
  • 4: (0.8,1](0.8, 1] — holds: 0.95,0.90,0.850.95, 0.90, 0.85; count: 3; accuracy: 0.6670.667; mean confidence: 0.90.9; gap: 0.2330.233

(Bins 0 and 3 are empty: count 0, accuracy and confidence 0, no contribution.) ECE =26⋅0.075+16⋅0.3+36⋅0.233=0.192= \tfrac{2}{6} \cdot 0.075 + \tfrac{1}{6} \cdot 0.3 + \tfrac{3}{6} \cdot 0.233 = 0.192.

What to implement

  • bin_index(conf, n_bins): the bin of each confidence.
  • reliability(conf, correct, n_bins): three arrays of length MM: the count, the accuracy and the mean confidence of each bin (accuracy and confidence are 0 for empty bins).
  • ece(conf, correct, n_bins=15): the expected calibration error. Empty bins contribute nothing.

Requirements

  • No loops over predictions.

Hint

np.ceil(conf * n_bins) - 1, as an integer and clipped to [0,M−1][0, M-1], gives the bin; np.bincount(bins, weights=values, minlength=n_bins) sums values per bin, and without weights it counts.

solution.py
Python idle

Press Run (⌘/Ctrl + Enter) to test your code.