Sub topic 2
What It Means for Probabilities to Be Calibrated
This chapter assumes you know binary and multiclass classification, train/validation/test splits, and the sigmoid and softmax functions. You should also be comfortable with Python, NumPy, and scikit-learn. We define calibration precisely here. Later chapters cover measuring it and fixing it.
The core idea
A classifier that outputs a number between 0 and 1 invites the reader to treat it as a probability. Suppose a model says 0.8 for 1,000 different cases. If the model is calibrated, about 800 of those cases are positive. If only 600 are positive, the number 0.8 was a score that looked like a probability, and nothing more.
Calibration is a property of the match between stated confidence and observed frequency. It does not say whether the model is good at telling positives from negatives. That distinction gets its own chapter. Here we only need the definition.
The formal definition
Let f(X) be the model's predicted probability that Y = 1 for an input X. The model is perfectly calibrated if, for every value p between 0 and 1:
P(Y = 1 | f(X) = p) = pRead this as a conditional statement. Take all inputs that receive the score p, and the positive rate among them must equal p. It must hold at every score level, not just on average.
Three consequences follow:
- The statement is about a population. It describes the model together with the data distribution it is evaluated on, not a single prediction. One case cannot be "calibrated", because it is either positive or negative.
- The definition is about long-run frequencies. A prediction of 0.8 is not wrong when the outcome is negative. It is wrong only if cases scored 0.8 are negative far more than 20% of the time.
- Exact equality for every continuous
pcannot be checked from a finite sample, because each exact score value usually occurs once. In practice we group nearby scores into bins and compare averages. The reliability diagram chapter covers this.
Overconfident and underconfident models
Calibration errors have a direction. Comparing observed frequency with predicted probability gives two cases.
| Pattern | What the model says | What happens | Typical cause |
|---|---|---|---|
| Overconfident | 0.9 | Positive about 75% of the time | Very flexible models fit to the training data, such as deep networks |
| Underconfident | 0.6 | Positive about 80% of the time | Margin-based or ensemble methods that pull scores toward the middle |
| Calibrated | 0.8 | Positive about 80% of the time | Well-specified probabilistic models, or models after post-hoc correction |
The causes of miscalibration have their own chapter. For now, treat the table as the vocabulary you will use for diagnosis.
Calibration alone is cheap to satisfy
A model can be perfectly calibrated and nearly useless. Suppose 30% of your cases are positive. A model that outputs 0.3 for every input is calibrated. Among all cases scored 0.3, which is all of them, 30% are positive. It cannot rank any case above another, so it gives no help in deciding which cases to act on.
This shows that calibration is necessary for trustworthy probabilities but not sufficient for a good model. A useful model is both calibrated and sharp, meaning it pushes its predictions away from the base rate toward 0 and 1 when the data supports that. Calibration asks whether the stated probabilities are honest. Sharpness asks whether they are informative.
A simulation you can run
The clearest way to see the definition is to build data where we know the true probabilities. We draw a true probability for each case, sample the label from it, and compare three predictors:
- One that reports the true probability.
- One that exaggerates it, pushing it toward 0 and 1.
- One that always reports the overall base rate.
import numpy as np
rng = np.random.default_rng(0)
n = 200_000
true_p = rng.beta(2, 2, size=n) # true probability for each case
y = rng.binomial(1, true_p) # labels sampled from those probabilities
def sharpen(p, k):
"""Multiply the log-odds by k. k > 1 makes the model overconfident."""
logit = np.log(p / (1.0 - p))
return 1.0 / (1.0 + np.exp(-k * logit))
predictors = {
"true probability": true_p,
"overconfident (k=2)": sharpen(true_p, 2.0),
"constant base rate": np.full(n, y.mean()),
}
edges = np.linspace(0.0, 1.0, 6) # five bins of width 0.2
for name, p in predictors.items():
print(name)
bin_ids = np.digitize(p, edges[1:-1])
for b in range(len(edges) - 1):
mask = bin_ids == b
if mask.any():
print(
f" bin {edges[b]:.1f}-{edges[b + 1]:.1f}: "
f"n={mask.sum():6d} "
f"mean predicted={p[mask].mean():.3f} "
f"observed positive rate={y[mask].mean():.3f}"
)Here is what to look for in the output:
- For the true-probability predictor, mean predicted and observed rate agree in every bin, within sampling noise.
- For the overconfident predictor, the mean predicted value in the top bin is well above the observed rate, and in the bottom bin it is well below. The scores are more extreme than the evidence supports.
- The constant predictor lands in a single bin, and its predicted and observed values match. It is calibrated, and it is also uninformative.
The overconfident predictor ranks cases in the same order as the true one, because multiplying log-odds by a positive constant is monotonic. Its ranking quality is identical, but its calibration is worse. This is the central point of the next chapter.
The procedure behind every calibration check
Every calibration check follows the same steps, whatever the metric. The diagram shows the shared structure.
Beyond the binary case
The definition extends to more than two classes in several ways. They differ in how much they demand.
- Top-label (confidence) calibration looks only at the highest predicted probability. Among cases where the model's top class has confidence
c, the top class should be correct a fractioncof the time. This is the most common definition in the deep learning literature. - Class-wise calibration applies the binary definition to each class separately, treating class
kagainst the rest:P(Y = k | p_k(X) = q) = q. - Full (distribution) calibration requires that, given the whole predicted vector
q, the label distribution is exactlyq. This is the strongest form and very hard to check, because the space of vectors is large.
The requirements are nested in strength: full calibration implies class-wise calibration, which implies top-label calibration. The reverse does not hold. A model can be calibrated on its top label and still give misleading probabilities for the second and third classes. Which one you need depends on how the probabilities are used. A system that only acts on the top prediction may need only confidence calibration. A system that ranks or combines class probabilities needs class-wise calibration at least.
Common mistakes with the definition
- Confusing calibrated with correct. A calibrated 0.8 prediction is wrong 20% of the time by design. Seeing a wrong prediction at 0.8 says nothing about calibration.
- Checking only the average. If the mean predicted probability equals the overall positive rate, that is called calibration in the large. It is much weaker than the full definition. A model can be 0.9 where it should be 0.7, and 0.1 where it should be 0.3, and still average out correctly.
- Evaluating on training data. Flexible models often look calibrated on the data they were fit to and miscalibrated on new data. Always check on held-out data.
- Assuming calibration transfers. Calibration holds for a model on a particular population. If the class balance or input distribution changes, a calibrated model can stop being calibrated. The distribution shift chapter returns to this.
- Calling a probability "true". A calibrated model does not know each case's underlying probability. It only guarantees that the stated value matches the frequency among cases that receive that value. Two models with different inputs can both be calibrated and give different predictions for the same case.
Key takeaways
- A model is calibrated when, among cases scored
p, the positive rate isp, for everyp. - Calibration is a statement about groups of cases and long-run frequency, not about single predictions.
- A constant prediction at the base rate is calibrated but gives no ranking. Calibration must be combined with sharpness for a useful model.
- Overconfident and underconfident are the two failure directions, and sharpening or flattening scores can change calibration without changing ranking.
- Multiclass calibration has top-label, class-wise, and full versions, in increasing order of strength.
Exercise
A fraud model scores 2,000 held-out transactions at between 0.85 and 0.95 (mean predicted probability 0.90). Of these, 1,500 turn out to be fraud. Is the model calibrated in this score range? If not, which direction is the error, and does it tell you anything about the model's ranking quality?
Show answer
The observed fraud rate is 1,500 / 2,000 = 0.75, while the mean predicted probability is 0.90. The model is not calibrated in this range. It is overconfident, because it states 0.90 where the observed frequency is 0.75. This tells you nothing about ranking quality. The model could still rank fraud above non-fraud perfectly well, since a monotonic distortion of scores keeps the order. Ranking would be checked with a metric such as AUC, and the probability values would then be repaired with a post-hoc calibrator.