Skip to content

← Metrics & Evaluation step 5 of 7

Medium Primitives

Expected Calibration Error

Expected calibration error (ECE) compares a classifier's confidence with how often its predictions are correct. Group predictions with similar confidence, measure the gap in each group, then average those gaps in proportion to the number of examples in each group.

This exercise computes binned predicted-class confidence ECE for a binary classifier. A probability of 0.1 for class 1 means a prediction of class 0 with confidence 0.9, not confidence 0.1.

Inputs and output

Implement expected_calibration_error(probs, labels, num_bins=10).

  • probs has shape (N,) and contains finite probabilities of class 1 in [0, 1].
  • labels has shape (N,) and contains true labels 0 or 1, delivered as floats.
  • num_bins is a positive integer giving the number of equal-width bins.
  • Return ECE as a Python float in [0, 1]. For N=0, return 0.0 as this exercise's empty-input convention. Leave inputs unchanged.

Compute confidence before binning

For each example:

prediction = 1 if probability > 0.5 else 0
confidence = probability if prediction == 1 else 1 - probability
correct = 1 if prediction == label else 0

A probability of exactly 0.5 predicts class 0 with confidence 0.5. Bin by confidence, not by the original probability of class 1. Confidence therefore lies in [0.5, 1], though the bin grid spans all of [0, 1].

Bin b, numbered from 0, covers [b / num_bins, (b + 1) / num_bins). The final bin includes its right endpoint, 1.0. An exact interior boundary belongs to the bin on its right. For each nonempty bin, compute:

mean_confidence = mean(confidence of examples in the bin)
accuracy = mean(correct for examples in the bin)
contribution = (number in bin / N) * abs(mean_confidence - accuracy)

Sum those contributions. Empty bins contribute zero. Take the absolute gap after averaging within each bin, not separately for each example.

Example

With probs=[0.1, 0.9], labels=[0, 1] and num_bins=2, both predictions are correct and have confidence 0.9. Both enter the upper bin, whose accuracy is 1.0, so ECE is abs(0.9 - 1.0) = 0.1.

With probs=[0, 1] and matching labels [0, 1], both confidence and accuracy are 1, giving ECE 0. A correct class-0 prediction should not be penalized just because its probability of class 1 is low.

Interpreting the result

This is the confidence-versus-accuracy form discussed by Guo et al. (2017), with the explicit left-closed bin convention above. The result depends on the chosen bins and the evaluation sample. A small binned ECE does not prove perfect calibration, and the absolute value does not distinguish overconfidence from underconfidence.