Sub topic 4
Why classifiers end up miscalibrated
This chapter assumes you know binary classification, train and test splits, logistic regression with the sigmoid function, and basic scikit-learn. You should also know that a calibrated model's prediction of 0.8 is right about 80% of the time. Here we look at why many classifiers break that property.
The loss function sets the target
A model is only well calibrated if its training objective rewards honest probabilities. Log loss does. Logistic regression minimizes log loss directly, so on data that fits its assumptions its outputs tend to be close to calibrated.
Many other models optimize something else. They produce a score that ranks examples well, and then a sigmoid or a normalization turns that score into a number between 0 and 1. That number looks like a probability but was never trained to behave like one.
Common patterns by model type
Different models fail in different, predictable ways.
| Model | Typical behavior | Why |
|---|---|---|
| Naive Bayes | Overconfident, with outputs pushed toward 0 and 1 | It assumes features are independent. Correlated features get counted several times, so the evidence is overstated. |
| Random forest | Underconfident near 0 and 1 | Averaging many trees pulls predictions toward the middle. A tree vote rarely reaches exactly 0 or 1 when some trees disagree. |
| Boosted trees, SVM (hinge loss) | Distorted, often sigmoid-shaped | Margin-based objectives focus on examples near the decision boundary. They do not care about the exact probability. |
| Deep neural networks | Often overconfident | They have high capacity and train long. Log loss keeps falling by making logits larger, even after accuracy stops improving. |
The spine's first code example uses GaussianNB on data with 10 redundant features. That is the independence failure above: redundant features are counted as if they were independent evidence.
Overfitting makes models overconfident
A flexible model can reach near-zero training loss by pushing predicted probabilities toward 0 and 1. On new data it is wrong more often than those probabilities claim. The gap grows with model capacity and with training time.
The same data that trains the model therefore cannot show its true calibration. Training-set probabilities look better than they are. This is why later chapters fit calibrators on separate data.
Other causes
- Regularization. Strong L2 regularization shrinks weights and can make a logistic regression underconfident.
- Class weights and resampling. Balancing classes by oversampling or
class_weight="balanced"shifts predicted probabilities away from the true base rate. The ranking may be fine while the probabilities are off. - Changed data. A model calibrated on last year's data may drift when the population changes. Chapter 15 covers this.
A quick check you can run
Compare a model trained with log loss to one that is not. The sketch below prints the mean predicted probability and the actual positive rate. The first model should match closely. For the second, the average may match while individual bins do not, so use the reliability diagram from the next chapter to see the bins.
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
X, y = make_classification(n_samples=20000, n_features=20, n_informative=5, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
for name, model in [
("logistic", LogisticRegression(max_iter=1000)),
("forest", RandomForestClassifier(n_estimators=200, random_state=0)),
]:
model.fit(X_train, y_train)
p = model.predict_proba(X_test)[:, 1]
print(f"{name:<9} mean p={p.mean():.3f} actual={y_test.mean():.3f} log loss={log_loss(y_test, p):.3f}")Exercise
A random forest gives no prediction above 0.9, even for examples it classifies correctly almost every time. Is this overconfidence or underconfidence, and what mechanism causes it?
Show answer
It is underconfidence. Averaging the votes of many trees pulls probabilities toward the middle, because a few trees almost always disagree. Observed positive rates in the top bins are higher than the predicted values.
Exercise
You train a classifier with class_weight="balanced" on data with 5% positives. Why can its probabilities be miscalibrated even if its AUC is unchanged?
Show answer
Balancing changes the effective base rate the model sees, so its probabilities are inflated for the positive class relative to the real 5%. Ranking, and so AUC, can stay the same while the probability values no longer match observed frequencies.