Calibration & Uncertainty
A model that says 90% should be right 90% of the time. Modern networks are not — and one number fitted on held-out data fixes most of the gap.
A classifier that outputs 0.9 is making a promise: among the cases it labels with 90 percent confidence, about nine in ten should be correct. Calibration means the promise holds.
Accuracy and calibration are different things, and a model can be good at one and bad at the other. Modern deep networks are usually overconfident: they report 0.96 on cases they get right 86 percent of the time. The score is a ranking signal that was never trained to be a real probability.
The standard picture is the reliability diagram. Group predictions by confidence, measure the average accuracy in each group, and plot accuracy against confidence. A perfectly calibrated model lies on the diagonal. Points below the diagonal mean overconfidence; points above mean underconfidence.
The headline number is expected calibration error: ECE = Σ (n_b/n)·|accuracy_b − confidence_b|. For each confidence group b, take the gap between its average accuracy and its average confidence, then weight that gap by how many samples landed there: n_b samples out of n total. The weighting stops the sparse extremes from dominating the score.
How you build the groups matters more than people expect. Equal-width bins cut the confidence range into fixed slices, which can leave the extremes empty or crowded. Equal-frequency bins sort the predictions and cut them into groups of equal size, so every group has enough samples to say something.
The same model can report a different gap under each scheme. That is a property of the measure, not a contradiction: the number is only as meaningful as the grouping behind it.
Temperature scaling is the cheapest fix that works. Take the raw scores (the logits) from the model, divide them all by a single number T, and choose T on data the model has not seen by minimizing negative log-likelihood — the score that punishes confident mistakes. It is the same temperature as in the softmax article, used for a different purpose: there it shaped random sampling, here it repairs probabilities.
Because T is positive, it cannot change the ranking. Accuracy and every threshold-free measure stay exactly where they were. Only the confidences move — and that is precisely what a refusal threshold or an agent's self-report reads.
- ECE now
- 0.118
- ECE at T = 1
- 0.118
- accuracy
- 49.7% (fixed)
- mean confidence
- 51.4%
- NLL
- 0.665
- fitted T
- 2.50
The fixture is 320 predictions from a deliberately overconfident model (logit scaled by 2.4). Softening with T moves mean confidence down toward accuracy without reordering a single prediction.
T = 1.00 with equal-width bins (8). ECE 0.118 versus 0.118 at T = 1. Accuracy is 49.7% and does not move with T; mean confidence 51.4%. NLL 0.665. The NLL-fitted temperature is 2.50.
The demo's fixture is 320 predictions from a deliberately overconfident model, with the scores inflated by a factor of 2.4. At T = 1 the points sit well below the diagonal and the calibration error is high. Raising T softens the confidences toward the accuracy line while accuracy itself stays pinned.
Press Fit T to jump to the best temperature, then switch the bin mode and watch equal-width and equal-frequency disagree about the exact number. Both report a large drop. The story never changes: overconfidence shrinks, ordering does not.
Calibration stopped being a statistics footnote. A RAG system refuses when the best evidence scores below a threshold, so that score has to mean something; otherwise the system either answers without evidence or refuses everything. Agent confidence is monitored as a live signal. And any language model used as a judge is calibrated against human labels before it is trusted to grade a pipeline — published guidance puts the target around 80 to 85 percent agreement.
Selective prediction is the deployment face of calibration: pick a confidence threshold, abstain below it, answer above it, and the coverage–accuracy curve shows whether abstaining buys reliability or just hides the hard cases.
Two caveats keep the method honest. Calibration is not sharpness: a model that says 0.5 for everything can be perfectly calibrated and useless, so read the calibration error next to a scoring rule such as negative log-likelihood or the Brier score, which measures squared error on probabilities. And calibration does not travel: a temperature fitted on last quarter's data fails silently when the inputs shift, so refit it whenever the inputs move.
The problems below compute temperature scaling, build calibration bins, and measure expected calibration error three ways, including equal-frequency bins and the uplift variant.
Practice this
Open a problem and implement the idea from scratch. Progress saves to this browser.
Try it hands-on
Run this topic on real data in a lab, or beat a baseline in a research challenge.