Sub topic 2
What it means for probabilities to be calibrated
This chapter assumes you know binary classification, train/test splits, and the sigmoid function. It also assumes you can run basic scikit-learn code. You should already know that many classifiers output a score between 0 and 1 for each example.
The definition
A classifier is calibrated if its predicted probabilities match observed frequencies. Take every example where the model predicts 0.8 for the positive class. If the model is calibrated, about 80% of those examples are actually positive.
Formally, for a predicted probability p̂ and a true label y:
P(y = 1 | p̂ = p) = p
This must hold for every value of p. A model can be right on average and still be miscalibrated at specific values.
Why a probability needs this property
A score only counts as a probability if you can act on its value. Consider a fraud model that outputs 0.9. If you read that as "90% chance of fraud" and the true rate is 60%, your cost estimates, review queues, and thresholds will all be wrong.
Many models output scores that look like probabilities but are not. Naive Bayes, random forests, boosted trees, and support vector machines often produce scores that rank examples well but do not match real frequencies.
A worked example
Suppose a model makes ten predictions of 0.7, and seven of those examples are positive. At that value the model is calibrated. Now suppose it makes ten predictions of 0.9, and only six are positive. The model is overconfident at 0.9.
The two directions of error have names:
| Pattern | What the model does | Observed frequency vs. prediction |
|---|---|---|
| Overconfident | Predicts extreme values too often | Lower than predicted near 1, higher than predicted near 0 |
| Underconfident | Predicts values too close to 0.5 | Higher than predicted near 1, lower than predicted near 0 |
Checking it in practice
You rarely see many examples with exactly the same predicted value. So you group predictions into bins and compare two numbers in each bin: the mean predicted probability and the fraction of actual positives.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import calibration_curve
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
model = GaussianNB().fit(X_train, y_train)
p = model.predict_proba(X_test)[:, 1]
frac_pos, mean_pred = calibration_curve(y_test, p, n_bins=10, strategy="quantile")
for m, f in zip(mean_pred, frac_pos):
print(f"mean predicted {m:.2f} -> observed fraction positive {f:.2f}")Each printed line is one bin. For a calibrated model the two numbers are close. Naive Bayes treats correlated features as independent. This example has 10 redundant features, so the model tends to push its probabilities toward 0 and 1. You should see observed fractions that sit away from the predicted means near the extremes.
How the pieces fit
The diagram shows the comparison you make for each bin.
Common errors
- Checking on training data. A model often looks different on data it was trained on. Use held-out data.
- Using too few examples per bin. A bin with 20 examples gives a noisy observed fraction, so a gap there may be chance.
- Reading a gap as a ranking problem. Calibration says nothing about whether positives are ranked above negatives. The next chapter covers that distinction.
Exercise
A model predicts 0.3 for 200 test examples, and 90 of them are positive. Is the model calibrated at 0.3? Is it overconfident or underconfident about the positive class there?
Show answer
No. The observed positive rate is 90/200 = 0.45, which is higher than the predicted 0.3. The model underestimates the chance of a positive at this value, so it is underconfident about the positive class here.