2.6.11Model Evaluation & Selection

Log-loss and calibration

2,756 words13 min readdifficulty · medium

Overview

Log-loss (logarithmic loss, cross-entropy loss) measures how well predicted probabilities match true outcomes. Calibration measures whether predicted probabilities reflect true frequencies: if you say "70% confident" for 100 events, roughly 70 should occur.

Why this matters: A model can have high accuracy but terrible probabilities. Log-loss + calibration tell you if your uncertainties are trustworthy—critical for medical diagnosis, finance, any decision under uncertainty.


Core Intuition


Log-loss: The Math from Scratch

Binary Classification

You have true label y{0,1}y \in \{0,1\} and predicted probability p^[0,1]\hat{p} \in [0,1] for class 1.

Step 1: Define the likelihood.
If y=1y=1, the probability you assigned was p^\hat{p}.
If y=0y=0, the probability you assigned was 1p^1-\hat{p}.
Combined: P(datamodel)=p^y(1p^)1yP(\text{data}|\text{model}) = \hat{p}^y \cdot (1-\hat{p})^{1-y}

Why this step? This is the Bernoulli likelihood—basic probability of observing outcome yy given your prediction.

Step 2: Take the negative log.
We want to maximize likelihood = minimize negative log-likelihood: Log-loss=logP(datamodel)=[ylogp^+(1y)log(1p^)]\text{Log-loss} = -\log P(\text{data}|\text{model}) = -\big[y \log \hat{p} + (1-y)\log(1-\hat{p})\big]

Why negative log?

  1. Numerical stability: probabilities near 0 cause underflow; logs keep numbers manageable.
  2. Additive: logs turn products into sums over multiple samples.
  3. Convex: easier optimization (gradient descent works cleanly).

Step 3: Average over dataset.
For NN samples:

Multi-class Classification

True label: one-hot vector y=[y1,y2,,yK]\mathbf{y} = [y_1, y_2, \ldots, y_K] where exactly one yk=1y_k=1.
Predicted probabilities: p^=[p^1,,p^K]\hat{\mathbf{p}} = [\hat{p}_1, \ldots, \hat{p}_K] with p^k=1\sum \hat{p}_k = 1.

Derivation:
The likelihood of observing the true class is k=1Kp^kyk\prod_{k=1}^K \hat{p}_k^{y_k} (only the true class contributes since others have yk=0y_k=0).
Negative log-likelihood:

Why this step? The sum over kk collapses to a single term (the true class) because yi,k=0y_{i,k}=0 for all incorrect classes.


Calibration: The Math

Definition

Measuring Calibration: Expected Calibration Error (ECE)

Step 1: Bin predictions.
Divide [0,1][0,1] into MM bins (typically 10-20): B1,B2,,BMB_1, B_2, \ldots, B_M.

Step 2: Compute accuracy and confidence per bin.
For bin BmB_m:

  • Average predicted probability: conf(Bm)=1BmiBmp^i\text{conf}(B_m) = \frac{1}{|B_m|}\sum_{i \in B_m} \hat{p}_i
  • Actual accuracy: acc(Bm)=1BmiBmyi\text{acc}(B_m) = \frac{1}{|B_m|}\sum_{i \in B_m} y_i

Step 3: Weight by bin size and average.

Why this step? We weight by bin size because a bin with 2 samples shouldn't dominate a bin with 200. The absolute difference accconf|\text{acc} - \text{conf}| is the calibration gap.

Reliability Diagrams

Plot predicted confidence (x-axis) vs. observed accuracy (y-axis). Perfect calibration = diagonal line y=xy=x.

Common patterns:

  • Overconfident: curve below diagonal (you say 80%, reality is 60%)
  • Underconfident: curve above diagonal (you say 60%, reality is 80%)

Worked Examples


Common Mistakes


Calibration Fixes

Temperature Scaling

Problem: Neural net outputs zz (logits). Softmax σ(z)\sigma(z) is overconfident.

Solution: Add temperature TT: p^k=ezk/Tjezj/T\hat{p}_k = \frac{e^{z_k/T}}{\sum_j e^{z_j/T}}

How to find TT: Freeze model weights. Tune TT on validation set to minimize NLL or ECE. Typically T[1,5]T \in [1, 5].

Why this works: Dividing logits by T>1T>1 "softens" the distribution—probabilities move away from 0 and 1 toward uniform. This corrects overconfidence without retraining.

Platt Scaling (Binary)

Fit a logistic regression on top of model scores: P(y=1)=σ(as+b)P(y=1) = \sigma(a \cdot s + b) where ss is the model's output and a,ba, b are learned on validation data.

Why this works: Learns an affine transformation to map arbitrary scores to calibrated probabilities.

Isotonic Regression (Non-parametric)

Fit a piecewise-constant, monotonically increasing function from scores to probabilities. Flexible but can overfit on small validation sets.


Connections to Other Concepts

  • Cross-Entropy Loss: Log-loss is cross-entropy between true distribution (one-hot) and predicted distribution.
  • Information Theory: Log-loss measures "surprise"—average bits needed to encode outcomes given predictions.
  • Brier Score: Alternative to log-loss: 1N(p^iyi)2\frac{1}{N}\sum (\hat{p}_i - y_i)^2. Less sensitive to confident errors, but doesn't decompose nicely.
  • ROC-AUC: Measures ranking quality (discrimination). A model can have perfect AUC but terrible calibration.
  • Focal Loss: Modifies log-loss to focus on hard examples: (1p^)γlogp^-(1-\hat{p})^\gamma \log \hat{p}. Used in imbalanced settings.
  • Bayesian Neural Networks: Produce uncertainty estimates. Calibration measures if those uncertainties are honest.
  • Proper Scoring Rules: Log-loss is "proper"—maximized when predictions = true probabilities. Encourages honest forecasting.
  • Temperature Scaling: Post-hoc calibration method. Scales logits to fix overconfidence.
  • Reliability Diagrams: Visual tool for calibration. Plot predicted vs. observed frequencies.

Feynman Technique

Recall Explain to a 12-year-old

Imagine you're a weather forecaster. You say "70% chance of rain." If you say that 100 times, it should rain about 70 times. If it only rains 50 times, your probabilities are bad—you're overconfident. Calibration checks if your percentages are honest.

Now, imagine every time you're wrong, you lose points. But not all mistakes are equal. If you say "99% no rain" and it pours, you lose TONS of points—you were super confident and super wrong. If you said "60% no rain" and it rained, you lose fewer points because you weren't sure. That's log-loss: it punishes cocky mistakes way more than humble ones.

Why? Because in real life (medicine, driving, investing), being overconfident when wrong is dangerous. Log-loss makes your AI admit when it's unsure, and calibration makes sure its "I'm 80% sure" actually means something.


Mnemonic


Active Recall Flashcards

#flashcards/ai-ml

What is log-loss and why do we use logarithms?
Log-loss (cross-entropy) is 1N[ylogp^+(1y)log(1p^)]-\frac{1}{N}\sum [y\log\hat{p} + (1-y)\log(1-\hat{p})]. We use logarithms because: (1) probabilities multiply, logs make them add; (2) numerical stability near 0; (3) measures information-theoretic "surprise"; (4) convex for optimization.
Binary log-loss formula
L=1Ni=1N[yilogp^i+(1yi)log(1p^i)]\mathcal{L} = -\frac{1}{N}\sum_{i=1}^N [y_i \log \hat{p}_i + (1-y_i)\log(1-\hat{p}_i)] where yi{0,1}y_i \in \{0,1\}, p^i[0,1]\hat{p}_i \in [0,1]. Range: [0,)[0,\infty).
Multi-class log-loss formula
L=1Ni=1Nk=1Kyi,klogp^i,k\mathcal{L} = -\frac{1}{N}\sum_{i=1}^N \sum_{k=1}^K y_{i,k} \log \hat{p}_{i,k} where yi\mathbf{y}_i is one-hot, p^i\hat{\mathbf{p}}_i sums to 1. Equivalent to 1Nilogp^i,ci-\frac{1}{N}\sum_i \log \hat{p}_{i,c_i} (true class).
What does it mean for a model to be calibrated?
A model is calibrated if, among all predictions with confidence pp, the true positive rate is pp. E.g., of all "70% confident" predictions, 70% should be correct. Perfect calibration: observed frequency = predicted probability.
Expected Calibration Error (ECE) formula
ECE=m=1MBmNacc(Bm)conf(Bm)\text{ECE} = \sum_{m=1}^M \frac{|B_m|}{N} |\text{acc}(B_m) - \text{conf}(B_m)| where BmB_m are bins of predictions, acc\text{acc} is actual accuracy in bin, conf\text{conf} is average predicted probability. Range: [0,1][0,1].
Why can a model have low log-loss but poor calibration?
Log-loss measures fit to the training distribution. A model can minimize log-loss on training data but be overconfident on test/OOD data, or extrapolate poorly. Always measure calibration separately with ECE or reliability diagrams.
Why are softmax outputs not automatically calibrated probabilities?
Softmax produces values in [0,1][0,1] summing to 1, but the logits' scale is arbitrary. Neural nets are trained to classify (minimize loss), not to be calibrated. They often output overconfident scores. Fix: temperature scaling.
What is temperature scaling and how does it fix calibration?
Temperature scaling replaces softmax(z)\text{softmax}(z) with softmax(z/T)\text{softmax}(z/T) where T>1T>1 is tuned on validation data. Dividing logits by TT "softens" the distribution, moving probabilities away from extremes (0 or 1), correcting overconfidence without retraining.
Log-loss vs. accuracy: which is more informative?
Log-loss is more informative because it evaluates probability quality, not just binary decisions. A model predicting 0.51 for all positives has 100% accuracy but useless probabilities. Log-loss would be high, revealing the problem.
How does log-loss penalize confident wrong predictions?
Log-loss is log(p^)-\log(\hat{p}) when correct, log(1p^)-\log(1-\hat{p}) when wrong. As p^0\hat{p} \to 0 for a true positive, log(p^)-\log(\hat{p}) \to \infty. A confident wrong prediction (say 0.01 for true class) yields massive loss (~4.6), while a timid wrong prediction (0.4) yields smaller loss (~0.92). Exponential penalty.
What is a reliability diagram?
A plot of predicted confidence (x-axis) vs. observed accuracy (y-axis) across bins. Perfect calibration = diagonal line y=xy=x. Curve below diagonal = overconfident. Curve above = underconfident.
Why is log-loss called "cross-entropy"?
Cross-entropy H(p,q)=p(x)logq(x)H(p,q) = -\sum p(x)\log q(x) measures the average bits needed to encode data from distribution pp using code optimized for qq. For classification, pp is the one-hot true label, qq is the predicted distribution. Binary log-loss is exactly H(p,q)H(p,q) for Bernoulli distributions.

Concept Map

derived from

take neg log

averaged over N

generalizes to

log turns products to sums

behavior

checks

together with

enables

enables

Log-loss cross-entropy

Calibration

Bernoulli likelihood

Negative log-likelihood

Binary log-loss

Multi-class log-loss

Information theory bits of surprise

Trustworthy uncertainties

Penalizes confident wrong preds

Predicted probs match true frequencies

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo isko simple tarike se samajhte hain. Socho tumhara model sirf haan/naa nahi bolta, balki confidence bhi deta hai - jaise "70% chance hai ki barish hogi". Ab log-loss ka kaam yeh check karna hai ki yeh probabilities kitni sahi hain. Agar tum bade confidence se (P=0.9) galat prediction karte ho, toh log-loss tumhe bahut zyada penalty deta hai, aur agar timid ho ke (P=0.6) galat hue toh kam penalty. Logarithm isliye use karte hain kyun ki probabilities multiply hoti hain, aur log unhe addition mein badal deta hai - isse math aasan ho jaata hai aur gradient descent smoothly kaam karta hai. Binary case ka formula bas Bernoulli likelihood ka negative log hai, aur multi-class mein sirf true class wala term bachta hai baaki sab zero ho jaate hain.

Ab calibration ka intuition alag hai lekin equally important. Ek model ki accuracy high ho sakti hai, par uski probabilities jhoothi ho sakti hain. Perfect calibration ka matlab hai ki jab tum 100 events ke liye "70% confident" bolte ho, toh actually kareeb 70 hone chahiye - na kam, na zyada. Ise measure karne ke liye hum predictions ko bins mein baant dete hain (jaise 0-0.1, 0.1-0.2...), phir har bin mein dekhte hain ki jitni confidence claim ki thi, utna actual accuracy mila ya nahi. In dono ka difference hi ECE (Expected Calibration Error) deta hai.

Yeh cheez matter kyun karti hai? Kyunki real world mein decisions probabilities pe based hote hain - medical diagnosis mein doctor ko pata hona chahiye ki "80% cancer chance" ka matlab sach mein 80% hai, ya finance mein risk estimate trustworthy hona chahiye. Sirf accuracy dekhna kaafi nahi, kyunki wahan tumhe bas final label sahi chahiye, lekin log-loss aur calibration tumhe batate hain ki tumhare model ki uncertainty bharosemand hai ya nahi. Yaad rakho - ek achha model wahi hai jo apni confidence ke saath honest ho.

Go deeper — visual, from zero

Test yourself — Model Evaluation & Selection

Connections