WRITE · TRACE · UNDERSTAND

Estimate calibration with explicit bin semantics

Implement ece(labels,probs,bins) using equal-width bins [i/bins,(i+1)/bins), with 1 in the last.

Read lesson: Estimate calibration with explicit bin semantics →

Your experiment

Implement ece(labels,probs,bins) using equal-width bins [i/bins,(i+1)/bins), with 1 in the last. Validate binary labels, finite [0,1] probabilities, equal nonempty lengths and positive integer bins.

Python runs in your browser. Your draft stays on this device.

What to look for

ECE depends on discretization and sample size. Treat it as an inspectable diagnostic rather than a calibrated confidence interval or a substitute for a proper scoring rule.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Estimate calibration with explicit bin semantics works

The idea

A model is calibrated when its probabilities match observed frequencies: of all rows scored near 0.8, about 80 percent should be positive. Expected calibration error estimates the mismatch by sorting predictions into equal-width probability bins and comparing, in each bin, the mean predicted probability with the fraction of positives. The gaps are averaged with each bin weighted by its share of the rows.

ece(labels, probs, bins) puts p in bin min(int(p * bins), bins - 1), so each bin includes its lower edge and a probability of exactly 1 falls in the last bin. Adding up abs(sum of p - sum of y) / n over the bins is the same weighted gap, because the bin sizes cancel.

How it works

  1. Validate that bins is a positive integer and that labels and probabilities are aligned, nonempty and in range.
  2. Create one empty list per bin.
  3. Place each (y, p) pair in bin min(int(p * bins), bins - 1).
  4. For every nonempty bin, take the absolute difference between the sum of its probabilities and the sum of its labels.
  5. Divide each difference by the total number of rows and add them up.

What it costs

Time
O(n + b)
Space
O(n + b)

Each row is placed in one bin and read again when that bin is summed, on top of the b lists created at the start.

Where it is used

  • Guo and co-authors used expected calibration error in their 2017 paper On Calibration of Modern Neural Networks to show that deep networks are overconfident.
  • scikit-learn's calibration_curve returns the same two numbers per bin: the fraction of positives and the mean predicted probability.
  • TorchMetrics provides the metric as CalibrationError.

Watch out for

  • Computing the bin as int(p * bins) with no cap, which sends a probability of exactly 1 to an index past the last bin.
  • Averaging the bin gaps without weights, so a bin holding one row counts as much as a bin holding thousands.
  • Comparing ECE values computed with different bin counts; with a single bin, opposite errors cancel and the score can read as zero.