ML0155 Binned Calibration & Log-Loss Curves

Bin your classifier’s predictions into 10 buckets and build the calibration curve. Then explain how regularization strength reshapes the train/test log-loss curves differently.

Three dashed panels. Panel 1: an L2 logistic regression scores 10,000 held-out examples, 43 percent positive, and an arrow labeled bucket into 10 bins points to a row of ten empty cells spanning predicted probability 0 to 1, with the observed positive rate in each bin marked as unknown. Panel 2: a sweep table with lambda 0.001, 0.1 and 10 showing train log-loss 0.29, 0.42 and 0.60, test log-loss 0.52, 0.45 and 0.62, and test accuracy 79.1, 79.5 and 67.1 percent. Panel 3: three open questions asking what the 10-bin calibration curve looks like at each lambda, why train loss rises steadily while test loss bends, and which lambda ships.

The Problem: An L2-regularized logistic regression scores 10,000 held-out examples (43% positive), and at \lambda = 0.001, 0.1 and 10 its train log-loss reads 0.29, 0.42 and 0.60 while test log-loss reads 0.52, 0.45 and 0.62. We must bin the predictions into a 10-bucket calibration curve, explain why the two loss curves move differently, and pick the \lambda to ship.

Answer

To build the calibration curve, sort held-out predictions into 10 equal-width bins on [0, 1] and plot each bin’s mean predicted probability against its observed fraction of positives. A calibrated model sits on the diagonal, and the count-weighted mean of the vertical gaps is the expected calibration error (ECE). Regularization strength reshapes the two log-loss curves differently: train log-loss rises monotonically with \lambda, while test log-loss is U-shaped. On the weak side, overconfident probabilities let a few confident mistakes dominate the unbounded log term, and the calibration curve flattens below the diagonal at high scores. On the strong side, predictions shrink toward the base rate, the curve turns steeper than the diagonal, and both losses converge on the base-rate entropy (0.68 nats at 43% positives).

(1) Binning Recipe: Cut [0, 1] into ten intervals of width 0.1, assign each held-out prediction to one, and record the count, the mean predicted probability, and the observed positive rate per bin. Plot rate against mean and print the counts, because a 30-point bin is mostly noise.
(2) Reading The Shape: A curve flatter than the diagonal (above it at low scores, below it at high scores) means overconfidence, and a curve steeper than the diagonal means underconfidence. The overall mean can match the base rate while both tails are wrong.
(3) ECE And Its Noise: ECE weights each gap by the bin’s share of the data. Each observed rate carries a standard error of about \sqrt{q(1-q)/n_m}, so a 0.01 gap in a 400-point bin is noise while a 0.12 gap in a 2,200-point bin is roughly 15 standard errors.
(4) Train Loss Is Monotone: The data term of the regularized optimum is nondecreasing in \lambda, so train log-loss only rises as the penalty grows, from near zero on separable data toward the base-rate entropy.
(5) Test Loss Is An Asymmetric U: The weak-\lambda arm is steep and unbounded, since one miss at \hat p = 0.99 costs 4.6 nats. The strong-\lambda arm is gentle and capped at H(\pi), and the train/test gap narrows as \lambda grows.
(6) Log-Loss Bends Before Accuracy: Accuracy only checks which side of 0.5 a score lands on, so it can stay flat while probabilities degrade. Choose \lambda on held-out log-loss whenever the probabilities themselves feed decisions.

Mathematical Formulation:
I_m = \left[ \frac{m-1}{M}, \frac{m}{M} \right)
B_m = \{ i : \hat p_i \in I_m \}
\mathrm{conf}(B_m) = \frac{1}{n_m}\sum_{i \in B_m} \hat p_i
\mathrm{acc}(B_m) = \frac{1}{n_m}\sum_{i \in B_m} y_i
\mathrm{ECE} = \sum_{m=1}^{M} \frac{n_m}{N} \left| \mathrm{acc}(B_m) - \mathrm{conf}(B_m) \right|
\ell(y, \hat p) = -y \ln \hat p - (1-y) \ln(1-\hat p)
\hat p(x) = \sigma(w^\top x + b)
L_S(w, b) = \frac{1}{|S|}\sum_{i \in S} \ell(y_i, \hat p(x_i))
J_\lambda(w, b) = L_{\mathrm{train}}(w, b) + \lambda \|w\|_2^2
\mathbb{E}[\ell] = \mathbb{E}[\mathrm{KL}(q \,\|\, \hat p)] + \mathbb{E}[H(q)]
\lim_{\lambda \to \infty} L_S = H(\pi)

Where:

  • I_m is the m-th of M = 10 equal-width intervals (the last one also includes 1), and B_m is the set of test examples whose prediction \hat p_i falls in it, with count n_m = |B_m|.
  • i \in \{1, \ldots, N\} indexes the N = 10{,}000 test examples, each with label y_i \in \{0, 1\}.
  • \mathrm{conf}(B_m) and \mathrm{acc}(B_m) are the bin’s mean predicted probability and observed positive rate; plotting acc against conf for every non-empty bin gives the reliability diagram.
  • \mathrm{ECE} is the count-weighted mean absolute gap, lying in [0, 1].
  • \ell is the per-example log-loss in nats, \sigma is the logistic function, w are the weights, and b is the unpenalized intercept.
  • L_S is mean log-loss on split S \in \{\mathrm{train}, \mathrm{test}\}, measured without the penalty. The two curves are L_{\mathrm{train}} and L_{\mathrm{test}} evaluated at the minimizer of J_\lambda, where \lambda \geq 0 is the L2 strength (scikit-learn’s C plays the inverse role).
  • q = P(Y = 1 \mid \hat p) is the true positive rate among examples that receive score \hat p. The binary \mathrm{KL}(q \,\|\, \hat p) is the calibration term (zero only when calibrated), and the binary entropy H(q) is the refinement term (small when scores separate the classes).
  • \pi is the base rate, and H(\pi) = -\pi \ln \pi - (1-\pi) \ln(1-\pi) equals 0.68 nats at \pi = 0.43: the loss of predicting the base rate for everyone, which a fully shrunk model with a free intercept approaches.

Worked Example:
Take the \lambda = 0.001 model on the 10,000 test points. The table lists each bin’s count, mean predicted probability, and observed positive rate, and each ECE share is n_m/N times the absolute gap. The top bin holds 2,200 predictions averaging 0.96 that are right only 84% of the time, contributing 0.0264 on its own. The bottom bin’s 3,500 predictions at 0.04 turn out positive 10% of the time. Summing the ten shares gives ECE = 0.0748, with both tails off in the overconfident direction. Yet the mean prediction, 0.440, sits close to the 0.428 base rate, so a single global check would have passed this model.

BinRangeCountMean predictedObserved rateGapECE share
1[0.0, 0.1)3,5000.040.100.060.0210
2[0.1, 0.2)9000.150.220.070.0063
3[0.2, 0.3)5000.250.300.050.0025
4[0.3, 0.4)4000.350.380.030.0012
5[0.4, 0.5)4000.450.460.010.0004
6[0.5, 0.6)3000.550.530.020.0006
7[0.6, 0.7)4000.650.600.050.0020
8[0.7, 0.8)5000.750.660.090.0045
9[0.8, 0.9)9000.850.740.110.0099
10[0.9, 1.0]2,2000.960.840.120.0264
Total[0, 1]10,0000.4400.428weighted0.0748

Regularization acts on the two loss curves through different mechanisms. On the training set, any extra penalty can only trade fit for smaller weights, so train log-loss climbs from 0.29 at \lambda = 0.001 to 0.60 at \lambda = 10 with no turning point. On the test set, a small \lambda lets \|w\| grow until the logits saturate. Predictions then crowd 0 and 1, and every confident miss costs -\ln(1-\hat p), already 3.2 nats at \hat p = 0.96. A large \lambda shrinks the logits toward the intercept, so predictions bunch around the 43% base rate and both losses rise toward H(\pi) because the model has stopped separating the classes. The two arms are therefore not mirror images. The left arm is overconfidence and variance and can climb without bound, while the right arm is underfitting and is capped at 0.68 nats. Accuracy hides most of this: \lambda = 0.001 and \lambda = 0.1 differ by 0.4 accuracy points but by 0.07 nats of test log-loss.

Two-panel chart. Left: ten-bin reliability diagrams on the same 10,000 test points with marker area proportional to bin count. The lambda 0.001 curve is flatter than the dashed diagonal, with big markers at both tails and its top bin at 0.96 predicted versus 0.84 observed, ECE 0.075. The lambda 0.1 curve follows the diagonal with ECE 0.010. The lambda 10 curve occupies only predicted values 0.27 to 0.62 and is steeper than the diagonal, ECE 0.083. Right: train and test log-loss against lambda on a log axis from 0.0001 to 100. Train log-loss rises monotonically from 0.25 to 0.66, test log-loss falls from 0.62 to a minimum of 0.45 near lambda 0.1 and rises again to 0.67, a vertical arrow marks a gap of 0.23 at lambda 0.001, and a dashed line marks the base-rate entropy of 0.68.

Figure 1: (a) Ten-bin reliability diagrams, marker area proportional to bin count. \lambda = 0.001 is flatter than the diagonal (ECE 0.075), \lambda = 10 is steeper and packed into 0.27 to 0.62 (ECE 0.083), and \lambda = 0.1 tracks the diagonal (ECE 0.010). (b) Train log-loss rises monotonically while test log-loss is an asymmetric U with its minimum near \lambda = 0.1. Both approach H(0.43) = 0.68, and the curves interpolate the three measured settings.


Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *