Uncertainty Quantification › Worked Example: Measuring and Correcting Miscalibration
Language AI HandbookAll chapters Uncertainty Quantification › Worked Example: Measuring and Correcting MiscalibrationOne section · Sections in this chapter5 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To make these concepts concrete, let's trace through a calibration analysis step by step using a simplified but realistic scenario.
Suppose you have a factual QA system built on a large language model. You have 200 held-out questions with known ground-truth answers, and you've extracted the model's confidence score for each response (here, the length-normalized log-probability converted to a probability via an exponential transform). You want to know: how miscalibrated is this model, and how much does temperature scaling help?
Step 1: Group predictions into bins. Divide the 200 predictions into 10 equal-width confidence bins: , , ..., . For each bin, compute the mean confidence and the fraction of predictions in that bin that are correct. If you plot these points (mean confidence on x-axis, fraction correct on y-axis), you get the reliability diagram.
Step 2: Read the diagram. Suppose the result shows that predictions in the bin have a mean confidence of 0.93 but an actual accuracy of only 0.74. Predictions in the bin have mean confidence 0.74 but accuracy 0.62. The curve lies below the diagonal throughout the high-confidence range, confirming systematic overconfidence.
Step 3: Compute ECE. Plug the bin statistics into the ECE formula. Suppose the bin contains 80 of the 200 predictions (40% of the total). That bin contributes to the ECE. Summing over all bins gives a total ECE of, say, 0.11. This is high: a well-calibrated model should have ECE below 0.05.
Step 4: Learn temperature. Take the model's raw logits (before softmax) for each of the 200 predictions and learn the temperature that minimizes negative log-likelihood on this calibration set. The optimizer finds , meaning the model is nearly twice as overconfident as it should be. Applying this temperature to rescale all confidence scores gives a new set of calibrated probabilities.
Step 5: Re-check calibration. Recompute the reliability diagram and ECE with the calibrated probabilities. The curve now tracks much closer to the diagonal, and the ECE drops to 0.03. The model is now well-calibrated: when it says "70% confident," it's right about 70% of the time.
This is the core calibration loop in practice: extract confidence scores, measure miscalibration via ECE and reliability diagram, apply a correction method, and verify improvement. The implementation section below demonstrates this loop in code.