Sam Babichenko

Reading notes / Cost-sensitive classification and calibration

Temperature scaling and calibration

Guo et al., 2017; Niculescu-Mizil and Caruana, 2005

Context. Learners were mostly compared on accuracy and ROC area, but a cost threshold p∗p^* makes the right decision only if p^(x)\hat p(x) is an accurate probability. Niculescu-Mizil and Caruana (2005) asked which of ten standard learners produce calibrated probabilities and how much held-out data a post-hoc fix needs, and found neural nets among the best calibrated. Twelve years later the nets are deep, wide, batch-normalized, lightly regularized and far more accurate, and Guo et al. find them overconfident. Miscalibration has a shape set by the learning method, and since any increasing map preserves the ranking, calibration is a separate one-dimensional regression on held-out data whose family should be the smallest one that contains the distortion. Margin maximizers squeeze scores into a sigmoid, naive Bayes pushes them to the extremes, and modern nets inflate their logits by one global scale, which a single temperature undoes without changing a prediction. For rare positives the distortion that matters can be a base-rate error, which needs an intercept, and it has to be checked in the tail where decisions are made.

Setting. A score is calibrated if P(y=1∣p^(x)=p)=pP(y=1\mid\hat p(x)=p)=p; for a KK-class network with logits zz, the confidence P^=max⁡ksoftmax⁡(z)k\hat P=\max_k\operatorname{softmax}(z)_k is calibrated if P(Y^=Y∣P^=p)=pP(\hat Y=Y\mid\hat P=p)=p. A reliability diagram plots mean prediction against observed rate in equal-width bins, and with M=15M=15 bins BmB_mECE=∑m=1M∣Bm∣n ∣acc(Bm)−conf(Bm)∣.\mathrm{ECE}=\sum_{m=1}^M\frac{|B_m|}n\,\big|\mathrm{acc}(B_m)-\mathrm{conf}(B_m)\big|.The calibrators, all fit on held-out data, are Platt scaling, P^(y=1∣f)=1/(1+eAf+B)\hat P(y=1\mid f)=1/(1+e^{Af+B}) fit by maximum likelihood with smoothed targets (N++1)/(N++2)(N_++1)/(N_++2) and 1/(N−+2)1/(N_-+2); isotonic regression, the least-squares increasing step function by pool-adjacent-violators; histogram binning and Bayesian binning into quantiles (BBQ), run one class against the rest and renormalized; and three multiclass extensions of Platt, matrix scaling softmax⁡(Wz+b)\operatorname{softmax}(Wz+b), vector scaling (diagonal WW), and temperature scaling softmax⁡(z/T)\operatorname{softmax}(z/T) with one scalar T>0T>0 chosen by validation NLL. The 2005 study uses eight binary problems with 3% to 53% positives, 4,000 training and 1,000 calibration cases; the 2017 study uses vision and text benchmarks with ResNets, DenseNets and their kin.

Step 1

Maximum-margin methods squeeze scores toward the middle, and a sigmoid undoes it. Boosted-tree scores have almost no mass near 0 or 1 and the reliability curve is a sigmoid on seven of eight problems; boosted stumps are the most extreme, and SVMs behave alike. The companion paper explains this through margins. To widen the margin of hard cases near the boundary, boosting gives up margin on easy ones, so scores pile up on either side of it. Friedman et al.'s reading of AdaBoost as fitting half the log-odds makes a sigmoid of the normalized vote f=∑tαtht/∑tαtf=\sum_t\alpha_th_t/\sum_t\alpha_t the right family (my derivation):logit⁡P(y=1∣x)≈2F=2(∑tαt)(2f−1),\operatorname{logit}P(y=1\mid x)\approx2F=2\Big(\sum_t\alpha_t\Big)(2f-1),affine in ff, leaving a slope and an offset to fit. The slope and offset the theory implies fail for boosted full trees, which separate the training set and inflate ∑tαt\sum_t\alpha_t, so the two parameters are fit on held-out data.

Step 2

Naive Bayes pushes scores to the extremes, averaging keeps them off the edges, and log-loss learners are already calibrated. Naive Bayes adds one log-likelihood ratio per attribute, so correlated attributes count the same evidence several times and the log-odds overshoot; the correction it needs bends the other way from a sigmoid in ff, so isotonic regression fits it better; a sigmoid in log⁡f\log f and log⁡(1−f)\log(1-f) can bend either way, which beta calibration later added (Kull et al., 2017). Random forests show a mild sigmoid on five problems because an average reaches 0 only if every tree says 0, so near the edges any noise moves it inward; bagged trees stay close to calibrated. A neural net with a sigmoid output trained on log loss is in effect Platt-scaled during training, so in 2005 it sat on the diagonal with logistic regression.

Step 3

Platt wins on small calibration sets, isotonic on large ones, and calibrating a calibrated model hurts. The tradeoff is bias against variance in the map. Two parameters with smoothed targets cannot overfit much, while an isotonic fit has as many steps as the data allow. Platt is better for every learner plotted below roughly 200 to 1,000 calibration cases, and isotonic is as good or better from 1,000 on. The paper counts cases, but an isotonic step is determined by the positives in it (my reading): at 3% positives a set of 1,000 holds about 30, and at fraud-like rates of 0.1% it holds one, so the calibration set has to be sized in positives. On already calibrated models, Platt scaling moves mass away from 0 and 1, because a sigmoid of a [0,1][0,1] score is never the identity; fitting it on the log-odds instead puts the identity at A=−1,B=0A=-1,B=0. After calibration, boosted trees give the best probabilities of the ten learners, and in the companion paper boosting full trees directly on log loss does badly, pushing predictions toward 0 and 1.

Step 4

Modern nets are overconfident, and the settings that maximize accuracy are not the ones that calibrate best. Most uncalibrated 2017 models have ECE between 4% and 10%, up to 16.5% on CIFAR-100. There LeNet's average confidence is close to its accuracy, while ResNet-110 sits well above it. On CIFAR-100, ECE grows with depth and width while error falls, batch normalization worsens calibration while slightly improving accuracy, and calibration keeps improving as weight decay rises well past the value that minimizes error. These are single-dataset correlations with no causal claim, as the paper says.

Step 5

NLL overfits while 0/1 error does not, and this produces the overconfidence. Training a ResNet-110 on CIFAR-100, test NLL rises after the learning rate is lowered while test error keeps falling. Once nearly every training point is classified correctly, the loss can still fall by scaling the logits up, which raises confidence everywhere, including on test points the net gets wrong, and the optimal temperature rises steadily through training. So high-capacity models that seem immune to overfitting do overfit, in log loss. For a linear model on separable data, gradient descent grows the weight norm like log⁡t\log t while the direction converges (Soudry et al.), and a growing norm at a fixed direction is exactly a falling temperature.

Step 6

Temperature scaling fits that one degree of freedom, and on benchmarks the simplest method wins. Dividing by a positive scalar keeps the argmax, so accuracy is untouched, and the NLL is convex in 1/T1/T. Its first-order condition is a moment match, the maximum-entropy characterization the paper derives:∑i=1nzi(yi)=∑i=1n∑k=1Kzi(k) qi(k),\sum_{i=1}^nz_i^{(y_i)}=\sum_{i=1}^n\sum_{k=1}^Kz_i^{(k)}\,q_i^{(k)},the average logit of the true class equals its expectation under the model. That one parameter suffices is an empirical finding: vector scaling, free to scale each class, lands on nearly constant entries. On CIFAR-100 ResNet-110, ECE is 16.53% uncalibrated, 2.66% with histogram binning, 4.99% isotonic, 1.26% with temperature scaling and 25.49% with matrix scaling, whose K2K^2 parameters overfit with many classes. The one-against-rest binning methods can change the argmax after renormalization: on ImageNet DenseNet-161, error goes from 22.57% to 48.32% with histogram binning. On text temperature scaling is comparable but not always best.

Consequences and limits. Discrimination and calibration are separate jobs, and the right calibrator is the smallest family that contains the distortion: a sigmoid for margin methods and small sets, isotonic regression for large ones, and mostly a scale for modern nets. A temperature corrects scale only, though, and in a binary problem with rare positives an error in the base rate is an intercept, which needs Platt's (a,b)(a,b) on the logit. Binned ECE can also hide what matters for a cost threshold. In a check of mine, a model at a 0.7% base rate (true log-odds normal with mean −6-6 and standard deviation 1.5) whose odds are all doubled has 15-bin ECE of 0.66%, which is just its average overprediction and looks small on the usual 4% to 10% scale, while among its top 1% of scores it predicts 22% against an observed 12.7%. The best temperature (T=0.84T=0.84) leaves 18.7% against 12.7%, and a two-parameter Platt fit recovers the intercept, −0.74-0.74 against −log⁡2=−0.69-\log2=-0.69. For a fraud stack ending in a gradient-boosted tree model, only the procedure carries over directly. A modern GBDT trained on log loss with shrinkage and shallow trees is not the AdaBoost studied here, and log-loss boosting of high-capacity trees errs toward 0 and 1. So draw the reliability curve on recent held-out data around p∗p^*, calibrate on the raw margin, where the identity is in the Platt family, with an intercept, and refit when base rates or fraud patterns shift, since a calibration map holds only for the distribution it was fit on. Later work found binned ECE to underestimate miscalibration (Kumar et al., 2019), in-distribution temperature scaling to degrade under shift (Ovadia et al., 2019), and newer non-convolutional architectures among the better calibrated, so "bigger is worse" is a trend of the 2017 model families (Minderer et al., 2021).

Terms

Reliability diagram
Mean prediction against observed positive rate in ten equal-width bins; calibrated models lie on the diagonal.
Sigmoid distortion
Scores squeezed toward the middle (boosting, SVMs); the true rate is a sigmoid of the score.
Inverted sigmoid
Scores pushed to 0 and 1 (naive Bayes, which double-counts correlated evidence).
Platt scaling
P^=1/(1+eAf+B)\hat P=1/(1+e^{Af+B}), two parameters fit by likelihood on held-out data with targets (N++1)/(N++2)(N_++1)/(N_++2), 1/(N−+2)1/(N_-+2).
Isotonic regression
Least-squares increasing step function of the score, by pool-adjacent-violators; fits any monotone distortion.
Data rule
Platt below about 200 to 1,000 calibration cases; isotonic at least as good from 1,000 up.
ECE
∑m∣Bm∣n∣acc(Bm)−conf(Bm)∣\sum_m\frac{|B_m|}n|\mathrm{acc}(B_m)-\mathrm{conf}(B_m)| over 15 equal-width bins; weights bins by their mass.
NLL overfitting
Test log loss rises while test error falls, because the logits keep growing after the fit is perfect.
Temperature scaling
softmax⁡(z/T)\operatorname{softmax}(z/T), one scalar fit by validation NLL; keeps the argmax, so accuracy is unchanged.
Binary temperature
Platt scaling on the logit with no intercept; cannot correct a base-rate error.

Quiz

  1. What evidence supports the claim that miscalibration is low dimensional?
  2. A binary fraud model doubles every odds because of a base-rate error. What fixes it?
  3. Why can a rare-positive model have small ECE yet badly misstate probabilities near its threshold?
  4. Platt scaling on the [0,1][0,1] score slightly hurts already calibrated neural nets. What is the structural reason?

Sources