Sub topic 12
Choosing between sigmoid and isotonic calibration
This chapter assumes you have read the earlier chapters on reliability diagrams, Brier score and log loss, and on fitting a calibrator without leaking data. You should also be comfortable with CalibratedClassifierCV. Here the question is which of its two methods, "sigmoid" or "isotonic", to use.
What each method can express
Both methods learn a function g that maps a model's raw score to a calibrated probability. They differ in how much freedom g has.
- Sigmoid (Platt scaling) fits
g(s) = 1 / (1 + exp(A·s + B)). It has two parameters. The curve is smooth, strictly monotone, and S-shaped. - Isotonic regression fits any non-decreasing step function. It has no fixed shape. The number of parameters grows with the data, because each flat step is its own parameter.
Everything else in the decision follows from this. The sigmoid is rigid and low-variance. Isotonic is flexible and high-variance.
| Property | Sigmoid | Isotonic |
|---|---|---|
| Parameters | 2 | One per step, up to the number of distinct scores |
| Shape assumption | Logistic distortion of the score | Monotone only |
| Output | Smooth, strictly increasing | Piecewise constant, with ties |
| Small calibration set | Stable | Overfits, with ragged steps |
| Large calibration set | Can underfit if the distortion is not sigmoid | Matches the true curve closely |
| Effect on ranking | Preserves it exactly | Can merge distinct scores into ties |
Bias versus variance in the calibrator
Choosing a calibrator is a bias-variance trade-off, as with any model.
The sigmoid has high bias when the true distortion is not logistic-shaped. A naive Bayes model, for example, is often overconfident in a way that a two-parameter curve cannot fully correct. In that case the reliability diagram still bends away from the diagonal after calibration.
Isotonic has high variance when data is scarce. With a few hundred calibration points, a single run of positives in a narrow score range creates a step that reflects noise. The calibrated model then looks good on the calibration data and worse on new data.
A common rule of thumb, which scikit-learn's documentation echoes, is that isotonic needs on the order of 1,000 or more calibration samples to be reliable. That figure is a guide, not a threshold. The true minimum depends on class balance and on how rough the distortion is.
Class imbalance moves the threshold up. Isotonic estimates each step from the examples that land in it. If positives are 2% of the data, a step covering 50 examples contains about one positive. What matters is the number of minority-class examples, not the total.
Model type as a prior
Model family tells you what kind of distortion to expect, so it is a useful starting guess.
- Max-margin and boosted models (SVMs, AdaBoost, and often gradient boosting with few trees) tend to push probabilities away from 0 and 1. This produces a sigmoid-shaped reliability curve, and Platt scaling works well.
- Naive Bayes is typically overconfident, with predictions piled up near 0 and 1. The distortion is rarely a clean sigmoid, so isotonic often wins when there is enough data.
- Random forests tend to be underconfident near the extremes, because averaging trees rarely produces exact 0 or 1. The curve is flexible enough that isotonic usually helps with enough data.
- Logistic regression is usually well calibrated already. Check the reliability diagram first, and calibrate only if it shows a problem.
Treat these as priors and not as rules. The reliability diagram and the scoring metrics on held-out data decide the matter.
Seeing the trade-off in code
The script below compares both methods as the labeled data grows. It trains and calibrates on the first n rows of a pool, using 5-fold cross-validation inside CalibratedClassifierCV. It evaluates on a fixed test set. It also counts distinct predicted values, which exposes the step-function behavior of isotonic.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import brier_score_loss, log_loss, roc_auc_score
X, y = make_classification(
n_samples=25000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_pool, y_pool = X[:20000], y[:20000]
X_test, y_test = X[20000:], y[20000:]
print(f"{'n':>6} {'method':<9} {'brier':>7} {'logloss':>8} {'auc':>6} {'unique':>7}")
for n in (200, 1000, 5000, 20000):
for method in ("sigmoid", "isotonic"):
model = CalibratedClassifierCV(GaussianNB(), method=method, cv=5)
model.fit(X_pool[:n], y_pool[:n])
p = model.predict_proba(X_test)[:, 1]
print(
f"{n:>6} {method:<9} "
f"{brier_score_loss(y_test, p):>7.4f} "
f"{log_loss(y_test, p):>8.4f} "
f"{roc_auc_score(y_test, p):>6.4f} "
f"{len(np.unique(p)):>7}"
)When you run it, look at three things:
- The gap between methods as
ngrows. At smalln, isotonic is usually no better than the sigmoid and sometimes worse. At largenit usually pulls ahead if the distortion is not sigmoid-shaped. - The
uniquecolumn. The sigmoid gives a distinct probability to nearly every test row. Isotonic gives far fewer distinct values, because each step maps a whole range of scores to one probability. - The AUC column. Ties lose ranking information, so isotonic can lower AUC slightly. The sigmoid is monotone and strictly increasing, so it leaves AUC unchanged for a single fitted model.
Gotchas specific to isotonic
Ties. Many examples share one probability. This matters if you rank by score, for example to take the top 100 candidates. It also matters if you use a threshold that falls inside a plateau, because a whole group of examples crosses it at once.
Exact 0 and 1. A step can sit at exactly 0 or 1 if a score range contained only one class. If a test example in that range turns out to be the other class, log loss is very large, and scikit-learn clips probabilities to avoid infinity. Brier score is bounded and is much less sensitive to this. If log loss is your evaluation metric, check the extremes of isotonic output.
Scores outside the training range. Isotonic has no information beyond the scores it saw. CalibratedClassifierCV clips such inputs to the boundary values. The sigmoid extrapolates smoothly.
Noisy steps. Isotonic can produce a very fine staircase that fits noise. Cross-validated averaging helps, because the default ensemble=True averages the calibrated probabilities from each fold's model, which smooths the result. It does not remove the problem.
A practical decision procedure
In short:
- Look at the reliability diagram of the uncalibrated model on held-out data. If it is already close to the diagonal, stop.
- If calibration data is small or the minority class is rare, use the sigmoid. Its two parameters cannot overfit much.
- If you have plenty of data, fit both with cross-validation and compare them on a test set that neither calibrator saw. Use Brier score or log loss, plus a new reliability diagram.
- Prefer the simpler method unless isotonic wins by a clear margin. A tiny improvement in Brier score may not survive a different random split.
- If you consume scores as a ranking, or the downstream threshold sits inside a plateau, include AUC and the number of ties in the comparison.
What neither method fixes
Both methods are monotone, so neither can change which of two examples is ranked higher (apart from the ties isotonic adds). If the model's ranking is poor, calibration will not repair it. Both also assume the calibration data resembles the data the model will see. If the input distribution shifts, a calibrator fitted before the shift can be wrong, whichever method you chose. That is the subject of a later chapter.
Exercise
A fraud model trained with gradient boosting has 600 labeled calibration examples, of which only 12 are fraud. The reliability diagram shows overconfident predictions in the upper range. A colleague proposes isotonic regression because it is more flexible. What do you recommend, and what would you check before changing your mind?
Show answer
Recommend sigmoid (Platt) scaling. With only 12 positives, an isotonic step in the upper score range would be estimated from a handful of examples, so the staircase would mostly reflect noise and overfit. The sigmoid has two parameters and is far more stable. To reconsider isotonic, first get more labeled data, especially more positives. Then fit both methods with cross-validation and compare Brier score and a reliability diagram on a separate test set. Switch only if isotonic wins by a clear margin, and also check the ties and the AUC.
Exercise
After switching a model from sigmoid to isotonic calibration, the Brier score improves slightly but a team that ranks customers by predicted probability reports that many customers now have identical scores. Why does this happen, and what are two ways to respond?
Show answer
Isotonic regression produces a piecewise-constant function, so every example whose raw score falls in the same step receives the same probability. This creates ties that the strictly increasing sigmoid does not. One response is to go back to sigmoid calibration for the ranking use case. Another is to rank by the original uncalibrated score, which is unaffected by ties from the calibrator, and use the calibrated probability only where actual probability values are needed, such as expected-cost calculations.