Sub topic 10
Isotonic regression
This chapter assumes you know how Platt scaling works, how to split data for calibration, and how to read a reliability diagram. It also assumes basic scikit-learn use.
The idea
Isotonic regression is a calibration method that fits a monotonic, non-decreasing, step-shaped function from raw scores to probabilities. Unlike Platt scaling, it does not assume the miscalibration follows a sigmoid. The only assumption is that a higher score should never map to a lower probability.
You give it pairs of (score, 0/1 label) from held-out data. It finds the non-decreasing function that fits the labels best in the least-squares sense. The classic solver is the pool adjacent violators algorithm (PAVA).
How PAVA works
- Sort the examples by score.
- Start with each label (0 or 1) as its own block.
- If a block's mean is higher than the mean of the next block, the order is violated. Merge the two blocks and replace both with their combined mean.
- Repeat until the block means never decrease.
Each final block becomes one flat step. The calibrated probability for a new score is the value of the step it falls into. Scikit-learn interpolates linearly between step positions.
Using it directly
Fit the calibrator on data the base model did not train on. The example below uses a separate calibration split.
from sklearn.datasets import make_classification
from sklearn.isotonic import IsotonicRegression
from sklearn.metrics import brier_score_loss
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_train, X_rest, y_train, y_rest = train_test_split(X, y, test_size=0.5, random_state=0)
X_cal, X_test, y_cal, y_test = train_test_split(X_rest, y_rest, test_size=0.5, random_state=0)
model = GaussianNB().fit(X_train, y_train)
iso = IsotonicRegression(out_of_bounds="clip")
iso.fit(model.predict_proba(X_cal)[:, 1], y_cal)
p_raw = model.predict_proba(X_test)[:, 1]
p_iso = iso.predict(p_raw)
print("raw :", brier_score_loss(y_test, p_raw))
print("isotonic:", brier_score_loss(y_test, p_iso))The setting out_of_bounds="clip" matters. Without it, a test score outside the range seen during fitting raises an error or returns NaN, depending on the version.
In practice, CalibratedClassifierCV(method="isotonic") handles the split and cross-validation for you. The next chapter covers it.
Strengths and risks
- Flexible. It can correct any monotonic distortion, including the flat-then-steep shapes that naive Bayes and boosted trees produce. A sigmoid cannot.
- Data hungry. It has many free parameters, roughly one per step. With a few hundred calibration examples it overfits and produces jagged curves.
- Ties and plateaus. Many different scores can map to the same probability. This can hurt ranking metrics such as AUC slightly, because distinct scores become equal.
- Extreme values. A step with only a handful of examples can output exactly 0 or 1. Log loss then becomes infinite or very large if one of those predictions is wrong.
A common rule of thumb is to prefer isotonic regression when you have thousands of calibration examples, and Platt scaling when you have fewer. Treat this as a starting point and compare Brier score or log loss on held-out data.
Exercise
A calibration set has 300 examples. You fit isotonic regression and the resulting calibration curve is jagged with many small steps. What is the likely cause, and what would you try?
Show answer
Isotonic regression has roughly one parameter per step, so a small calibration set lets it overfit noise. Try Platt scaling, which has only two parameters, or collect more calibration data. Compare the options by Brier score or log loss on separate held-out data.
Exercise
Why can isotonic calibration lower AUC slightly even though it never reverses the order of scores?
Show answer
The fitted function is non-decreasing but not strictly increasing. Flat steps map different scores to the same probability, which creates ties and removes some ranking information.