2.6.11 · D4Model Evaluation & Selection

Exercises — Log-loss and calibration

2,165 words10 min readBack to topic

Every logarithm on this page is the natural log (, base ) unless we say otherwise. That is the ML default. When base changes the answer, we call it out.


Level 1 — Recognition

Exercise 1.1

For a single binary sample with true label and predicted probability , which expression gives its log-loss?

(a) (b) (c) (d)

Recall Solution

The per-sample binary log-loss is With , the second term vanishes (), leaving . Answer: (b). Numerically . Option (d) is the Brier per-sample term (see Brier Score), not log-loss.

Exercise 1.2

A reliability diagram plots a model's curve below the diagonal everywhere. Is the model over- or under-confident?

Recall Solution

The x-axis is confidence (what you claimed), the y-axis is accuracy (what happened). Below the diagonal means accuracy confidence: you said 80%, reality delivered 60%. That is overconfidence. Look at Figure s01 — the orange curve sags under the navy diagonal.

Figure — Log-loss and calibration

Exercise 1.3

State the range of ECE and what value means "perfectly calibrated."

Recall Solution

. It is a weighted average of absolute gaps , each in , so the average stays in . ECE means every bin's accuracy equals its confidence — perfect calibration.


Level 2 — Application

Exercise 2.1

Compute the log-loss of these three predictions (natural log).

Sample
1 1 0.8
2 0 0.3
3 1 0.4
Recall Solution

Per sample, use when and when : Average: Sample 3 (right class, but only 40% confident) dominates — the model was timid on a correct answer.

Exercise 2.2

A 3-class problem. True class is index 2 (one-hot ). Predicted . Give the log-loss.

Recall Solution

Multi-class cross-entropy collapses to the log of the true class's probability only, because the one-hot zeros out the others: Notice this equals — a "coin-flip" amount of surprise. See Information Theory for why is exactly one bit.

Exercise 2.3

Five predictions fall in one bin. Confidences ; outcomes . Find , , and the bin's gap.

Recall Solution

Gap . Here accuracy beats confidence, so this bin is underconfident.


Level 3 — Analysis

Exercise 3.1

Model X predicts and the truth is . Model Y predicts , truth . Both are wrong by the 0.5 threshold. Compare their log-losses and explain the asymmetry.

Recall Solution

Both have , so loss : Ratio . Same "wrong" verdict, wildly different penalty. This is the whole point of log-loss: it grades your confidence, not just your label. The curve is nearly flat near the correct end and shoots to as your assigned probability for the true event heads to 0 — see Figure s02.

Figure — Log-loss and calibration

Exercise 3.2

Example 2 in the parent note reported . Recompute it, then state which bin contributes the most and why.

Bin Count conf acc
0.0–0.2 10 0.15 0.10
0.2–0.4 20 0.30 0.25
0.4–0.6 30 0.50 0.50
0.6–0.8 25 0.70 0.60
0.8–1.0 15 0.90 0.80
Recall Solution

with : Biggest contributor: the 0.6–0.8 bin (0.025). It combines a large weight () with a full gap. The 0.4–0.6 bin, though largest, contributes nothing — it is perfectly calibrated. Lesson: ECE rewards big and accurate bins, punishes big and miscalibrated ones.

Exercise 3.3

A model outputs logits . Someone scales all logits by (temperature scaling with means dividing logits by 2, i.e. multiplying by ). Does the argmax prediction change? What happens to confidence?

Recall Solution

Softmax of : exponentials , sum , so After scaling to : , sum , giving The top class stays index 3 (argmax unchanged — softmax is monotone in the logits), but its confidence drops from to . Temperature softens the distribution without touching accuracy — exactly why it is a pure post-hoc calibration knob.


Level 4 — Synthesis

Exercise 4.1

For a single sample with , compare log-loss against Brier at . Which grows faster as , and what does that imply for training?

Recall Solution
log-loss Brier
0.9 0.10536 0.01
0.5 0.69315 0.25
0.1 2.30259 0.81
As the Brier term saturates at , but log-loss . So log-loss produces much larger gradients on confidently-wrong samples, driving the optimizer harder to fix them. Both are proper scoring rules; log-loss just has an unbounded, steeper tail — one reason it is the default training loss while Brier is often used as a diagnostic.

Exercise 4.2

Design a 2-sample dataset where Model A has lower accuracy but lower (better) log-loss than Model B. Give explicit numbers.

Recall Solution

Let both samples have .

  • Model A: predicts on both. Both correct by threshold ? Yes, both . Accuracy . Hmm — we want A lower accuracy, so tweak:
  • Model A: . Threshold-0.5 predictions: sample 1 predicted 0 (wrong), sample 2 predicted 1 (right). Accuracy = 50%. Log-loss .
  • Model B: . Both predicted 1 (right). Accuracy = 100%. Log-loss .

Model A: lower accuracy (50% vs 100%) yet lower log-loss (0.511 vs 0.673), because its one confident-correct call (0.9) outweighs its near-miss. This mirrors Example 3 in the parent: accuracy and probability quality are different axes.

Exercise 4.3

Two independent predictions have per-sample losses and . Show that the total log-loss equals of the joint likelihood, and explain why this is why we chose the logarithm.

Recall Solution

Independent events multiply: joint likelihood where is the probability the model gave to the actual outcome . Then The log turns a product of likelihoods into a sum of losses. That additivity is exactly what lets us average over a dataset and take clean per-sample gradients. It also makes each read as bits/nats of surprise — the Information Theory interpretation. This is the mathematical reason the log (and not, say, the raw likelihood) is used.


Level 5 — Mastery

Exercise 5.1

Prove that for a single fixed true probability , the expected log-loss is minimized at . (This is the "properness" of log-loss.)

Recall Solution

Define . Differentiate: Set to zero: Second derivative , so this critical point is a minimum. Therefore truthful reporting is optimal — log-loss is a strictly proper scoring rule. Figure s03 shows the bowl for bottoming exactly at .

Figure — Log-loss and calibration

Exercise 5.2

The minimum value of the expected loss at is the entropy . Evaluate it at and interpret.

Recall Solution

Even a perfectly honest model cannot drive log-loss to zero when outcomes are genuinely random (): the floor is the entropy nats — the irreducible surprise of the process. Any log-loss above this is the model's fault; the gap is the KL divergence from truth to prediction.

Exercise 5.3

A calibrated model on a balanced binary task predicts a constant for every sample. What is its log-loss, and why can a smarter model beat it only if the task carries real signal?

Recall Solution

With every term is (this is , exactly 1 bit). So regardless of labels. A model beats this only by assigning away from in the right direction — pushing toward 1 on true positives, toward 0 on true negatives. That requires the features to actually predict (real signal). On pure noise ( everywhere), Exercise 5.1 says the optimal is , so nats is the unbeatable floor. This constant model is the classic log-loss baseline; any real model must undercut it to justify itself.


Recall Quick self-check

Per-sample loss for ::: Per-sample loss for ::: ECE weights each bin by ::: its fraction of samples Temperature scaling changes accuracy? ::: No — argmax is preserved Minimum expected log-loss for random ::: the entropy , not 0

Related: Parent: Log-loss & Calibration · Brier Score · ROC-AUC · Focal Loss · Temperature Scaling · Reliability Diagrams · Proper Scoring Rules · Information Theory · Cross-Entropy Loss · Bayesian Neural Networks