Log Loss
Log loss (logarithmic loss, cross-entropy loss) scores predicted probabilities against the true labels. Take binary labels and predicted probabilities . Log loss is the average negative log of the probability given to the true label:
Only one term in the bracket is nonzero for each point. A positive costs , and a negative costs . With classes, one-hot labels and predicted probabilities , the loss becomes
where is the probability given to the true class of point .
- Range: LL lies in . It is 0 only when every true label gets probability 1. It grows without bound as a true label’s probability goes to 0.
- Chance level: predicting the prevalence for every point gives the binary entropy . Predicting everywhere gives , and a uniform guess over classes gives .
- Likelihood: LL is the negative log-likelihood of the labels under a Bernoulli model (categorical for classes), divided by . Minimizing it over a model’s parameters is maximum likelihood estimation.
- Cross-entropy: LL is the cross-entropy between the label distribution and the predicted distribution. Its expected value is the entropy of the true label plus a KL divergence. The KL term vanishes only at the true probability.
- Units: the natural log gives nats, and dividing by gives bits.
- Computation: some libraries clip to to avoid the log of 0. Others compute the loss from the logit as .
Log loss and the Brier score are the common classification metrics that judge the probability values themselves. Accuracy and read one threshold, and ROC-AUC and average precision read only the order of the scores.
Not Taught in Lecture
CS 182 uses cross-entropy as the standard training loss (lecture 1; Prince, sections 5.4, 5.5 and 5.7). Calibration, reliability diagrams, proper scoring rules and the Brier score were not covered. Those parts follow Guo et al. (2017), Gneiting and Raftery (2007) and Brier (1950).
Intuition
The quantity is the surprisal of an outcome that was given probability , in the sense of Shannon entropy. Log loss averages the log of one over the probability given to each true label, hence the name. It is the model’s average surprise at the true labels. A forecaster who says 90% rain and sees rain pays . One who said 10% pays . The penalty grows slowly for mild mistakes and without limit for confident ones. A model keeps the loss low by putting high probability on what happens. When the outcome is unclear, it must admit uncertainty.
CS 182 lecture 1 separates the evaluation metric from the training surrogate, the differentiable loss that the optimizer minimizes. Accuracy cannot serve as a surrogate. It changes in jumps as the weights move, and its gradient is zero almost everywhere. Log loss is smooth, and its gradient with respect to a logit is . Logistic regression and neural network classifiers therefore train on log loss. People often report accuracy at the end, so the training surrogate and the evaluation metric differ. When the probabilities matter, log loss serves as both.
Properties
(One Confident Error Can Dominate) A single point whose true label received a probability near 0 can outweigh many good predictions.
Predicting 0.01 for an event that then happens costs nats. That equals the cost of 6.6 predictions of 0.5. The average therefore depends heavily on the worst few points. This sensitivity is useful when confident errors are costly, as in medical risk scores. It hurts when some labels are wrong, because a correct model is then punished for the noise in the labels. Clipping at caps each term at 16.1 nats. The clipping prevents an infinite loss but leaves the largest terms dominant.
(Reading the Number) A log loss value means little without the baseline of always predicting the prevalence.
With balanced classes the baseline is . With 1% positives, predicting 0.01 for everyone already gives 0.056. A fraud model with log loss 0.05 therefore removes only about 11% of the baseline loss. A model on balanced data with log loss 0.30 removes 57%. The normalized score measures this fraction. Statisticians call it McFadden’s pseudo-, by analogy with $R^2$ for regression.
The left panel shows that a positive predicted at costs . The right panel bins 800 predictions from two models that rank the points in the same order. The overconfident model has log loss , and the calibrated one has .
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{prp}{HTML}{8E6BD6}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw[->] (0,0) -- (6.30,0);
\draw[->] (0,0) -- (0,4.49);
\draw (0.00,-0.07) -- (0.00,0.07) node[pos=0, below] {\footnotesize $0$};
\draw (3.00,-0.07) -- (3.00,0.07) node[pos=0, below] {\footnotesize $0.5$};
\draw (6.00,-0.07) -- (6.00,0.07) node[pos=0, below] {\footnotesize $1$};
\draw (-0.07,0.00) -- (0.07,0.00) node[pos=0, left] {\footnotesize $0$};
\draw (-0.07,0.90) -- (0.07,0.90) node[pos=0, left] {\footnotesize $1$};
\draw (-0.07,1.80) -- (0.07,1.80) node[pos=0, left] {\footnotesize $2$};
\draw (-0.07,2.70) -- (0.07,2.70) node[pos=0, left] {\footnotesize $3$};
\draw (-0.07,3.60) -- (0.07,3.60) node[pos=0, left] {\footnotesize $4$};
\node at (3.00,-0.72) {predicted probability $p$ that $y = 1$};
\node[anchor=south west] at (-0.1,4.49) {loss};
\draw[hot, very thick] (0.06,4.14) -- (0.08,3.94) -- (0.10,3.73) -- (0.12,3.52) -- (0.15,3.32) -- (0.19,3.11) -- (0.24,2.90) -- (0.30,2.69) -- (0.38,2.49) -- (0.48,2.28) -- (0.60,2.07) -- (0.76,1.86) -- (0.95,1.66) -- (1.20,1.45) -- (1.65,1.16) -- (2.10,0.94) -- (2.55,0.77) -- (3.00,0.62) -- (3.45,0.50) -- (3.90,0.39) -- (4.35,0.29) -- (4.80,0.20) -- (5.05,0.16) -- (5.24,0.12) -- (5.40,0.10) -- (5.52,0.07) -- (5.62,0.06) -- (5.70,0.05) -- (5.76,0.04) -- (5.81,0.03) -- (5.85,0.02) -- (5.88,0.02) -- (5.90,0.01) -- (5.92,0.01) -- (5.94,0.01);
\draw[cold, very thick] (0.06,0.01) -- (0.08,0.01) -- (0.10,0.01) -- (0.12,0.02) -- (0.15,0.02) -- (0.19,0.03) -- (0.24,0.04) -- (0.30,0.05) -- (0.38,0.06) -- (0.48,0.07) -- (0.60,0.10) -- (0.76,0.12) -- (0.95,0.16) -- (1.20,0.20) -- (1.65,0.29) -- (2.10,0.39) -- (2.55,0.50) -- (3.00,0.62) -- (3.45,0.77) -- (3.90,0.94) -- (4.35,1.16) -- (4.80,1.45) -- (5.05,1.66) -- (5.24,1.86) -- (5.40,2.07) -- (5.52,2.28) -- (5.62,2.49) -- (5.70,2.69) -- (5.76,2.90) -- (5.81,3.11) -- (5.85,3.32) -- (5.88,3.52) -- (5.90,3.73) -- (5.92,3.94) -- (5.94,4.14);
\draw[hot, very thick] (0.75,3.3) -- (1.1,3.3); \draw[cold, very thick] (0.75,2.8) -- (1.1,2.8);
\node[anchor=west] at (0.2,4.14) {$-\log p$};
\node[anchor=east] at (5.75,4.14) {$-\log(1 - p)$};
\draw (8.1,0) rectangle (12.24,4.14);
\draw (8.10,-0.07) -- (8.10,0.07) node[pos=0, below] {\footnotesize $0$};
\draw (8.03,0.00) -- (8.17,0.00) node[pos=0, left] {\footnotesize $0$};
\draw (10.17,-0.07) -- (10.17,0.07) node[pos=0, below] {\footnotesize $0.5$};
\draw (8.03,2.07) -- (8.17,2.07) node[pos=0, left] {\footnotesize $0.5$};
\draw (12.24,-0.07) -- (12.24,0.07) node[pos=0, below] {\footnotesize $1$};
\draw (8.03,4.14) -- (8.17,4.14) node[pos=0, left] {\footnotesize $1$};
\draw (8.93,0) -- (8.93,0.12);
\draw (9.76,0) -- (9.76,0.12);
\draw (10.58,0) -- (10.58,0.12);
\draw (11.41,0) -- (11.41,0.12);
\node at (10.17,-0.72) {mean $p$ in each bin};
\node[anchor=south west] at (8.00,4.24) {share of $y = 1$ in each bin};
\draw[dashed] (8.1,0) -- (12.24,4.14);
\node[rotate=45, anchor=south] at (10.47,2.77) {\footnotesize perfect calibration};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.48s = "true", 30.28s = "true", repeats}}}]
\draw[dashed] (0.12,0) -- (0.12,3.52);
\filldraw[draw=black, fill=hot] (0.12,3.52) circle (3.6pt);
\filldraw[draw=black, fill=cold] (0.12,0.02) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.02$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.02 = 3.91$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.98 = 0.02$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.28s = "true", 30.28s = "true", repeats}}}]
\filldraw[draw=black, fill=prp] (12.81,3.45) circle (3pt); \node[anchor=west] at (12.99,3.45) {overconfident};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.78s = "true", 30.28s = "true", repeats}}}]
\fill (12.72,1.96) rectangle (12.90,2.14); \node[anchor=west] at (12.99,2.05) {calibrated};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.6s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (3.00,0) -- (3.00,0.62);
\filldraw[draw=black, fill=hot] (3.00,0.62) circle (2.6pt);
\filldraw[draw=black, fill=cold] (3.00,0.62) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.50$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.50 = 0.69$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.50 = 0.69$};
\node[anchor=west] at (-0.8,-1.45) {A prediction of $p = 0.5$ costs $\log 2 = 0.69$ whichever label is true.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 2.84s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (3.48,0) -- (3.48,0.78);
\filldraw[draw=black, fill=hot] (3.48,0.49) circle (2.6pt);
\filldraw[draw=black, fill=cold] (3.48,0.78) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.58$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.58 = 0.54$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.42 = 0.87$};
\node[anchor=west] at (-0.8,-1.45) {The model grows more confident that $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.84s = "true", 3.08s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (3.96,0) -- (3.96,0.97);
\filldraw[draw=black, fill=hot] (3.96,0.37) circle (2.6pt);
\filldraw[draw=black, fill=cold] (3.96,0.97) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.66$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.66 = 0.42$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.34 = 1.08$};
\node[anchor=west] at (-0.8,-1.45) {The model grows more confident that $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.08s = "true", 3.32s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (4.44,0) -- (4.44,1.21);
\filldraw[draw=black, fill=hot] (4.44,0.27) circle (2.6pt);
\filldraw[draw=black, fill=cold] (4.44,1.21) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.74$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.74 = 0.30$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.26 = 1.35$};
\node[anchor=west] at (-0.8,-1.45) {The model grows more confident that $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.32s = "true", 3.56s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (4.92,0) -- (4.92,1.54);
\filldraw[draw=black, fill=hot] (4.92,0.18) circle (2.6pt);
\filldraw[draw=black, fill=cold] (4.92,1.54) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.82$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.82 = 0.20$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.18 = 1.71$};
\node[anchor=west] at (-0.8,-1.45) {The model grows more confident that $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.56s = "true", 6.36s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (5.40,0) -- (5.40,2.07);
\filldraw[draw=black, fill=hot] (5.40,0.09) circle (2.6pt);
\filldraw[draw=black, fill=cold] (5.40,2.07) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.90$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.90 = 0.11$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.10 = 2.30$};
\node[anchor=west] at (-0.8,-1.45) {At $p = 0.9$, a right answer costs $0.11$ and a wrong one costs $2.30$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.36s = "true", 6.6s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (4.80,0) -- (4.80,1.45);
\filldraw[draw=black, fill=hot] (4.80,0.20) circle (2.6pt);
\filldraw[draw=black, fill=cold] (4.80,1.45) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.80$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.80 = 0.22$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.20 = 1.61$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.6s = "true", 6.84s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (3.60,0) -- (3.60,0.82);
\filldraw[draw=black, fill=hot] (3.60,0.46) circle (2.6pt);
\filldraw[draw=black, fill=cold] (3.60,0.82) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.60$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.60 = 0.51$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.40 = 0.92$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.84s = "true", 7.08s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (2.40,0) -- (2.40,0.82);
\filldraw[draw=black, fill=hot] (2.40,0.82) circle (2.6pt);
\filldraw[draw=black, fill=cold] (2.40,0.46) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.40$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.40 = 0.92$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.60 = 0.51$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.08s = "true", 7.32s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (1.50,0) -- (1.50,1.25);
\filldraw[draw=black, fill=hot] (1.50,1.25) circle (2.6pt);
\filldraw[draw=black, fill=cold] (1.50,0.26) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.25$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.25 = 1.39$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.75 = 0.29$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.32s = "true", 7.56s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (0.90,0) -- (0.90,1.71);
\filldraw[draw=black, fill=hot] (0.90,1.71) circle (2.6pt);
\filldraw[draw=black, fill=cold] (0.90,0.15) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.15$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.15 = 1.90$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.85 = 0.16$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.56s = "true", 7.8s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (0.54,0) -- (0.54,2.17);
\filldraw[draw=black, fill=hot] (0.54,2.17) circle (2.6pt);
\filldraw[draw=black, fill=cold] (0.54,0.08) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.09$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.09 = 2.41$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.91 = 0.09$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.8s = "true", 8.04s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (0.30,0) -- (0.30,2.70);
\filldraw[draw=black, fill=hot] (0.30,2.70) circle (2.6pt);
\filldraw[draw=black, fill=cold] (0.30,0.05) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.05$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.05 = 3.00$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.95 = 0.05$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.04s = "true", 8.28s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (0.18,0) -- (0.18,3.16);
\filldraw[draw=black, fill=hot] (0.18,3.16) circle (2.6pt);
\filldraw[draw=black, fill=cold] (0.18,0.03) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.03$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.03 = 3.51$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.97 = 0.03$};
\node[anchor=west] at (-0.8,-1.45) {Now the model moves toward $y = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.28s = "true", 11.48s = "false", 30.28s = "false", repeats}}}]
\draw[dashed] (0.12,0) -- (0.12,3.52);
\filldraw[draw=black, fill=hot] (0.12,3.52) circle (3.6pt);
\filldraw[draw=black, fill=cold] (0.12,0.02) circle (2.6pt);
\node[anchor=west] at (1.15,3.8) {$p = 0.02$};
\node[anchor=west] at (1.15,3.3) {if $y = 1$: \ $-\log 0.02 = 3.91$};
\node[anchor=west] at (1.15,2.8) {if $y = 0$: \ $-\log 0.98 = 0.02$};
\node[anchor=west] at (-0.8,-1.45) {For a true $y = 1$, the prediction $p = 0.02$ costs $-\log 0.02 = 3.91$. The loss has no upper limit.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.48s = "true", 14.28s = "false", 30.28s = "false", repeats}}}]
\node[anchor=west] at (-0.8,-1.45) {Sort the $800$ predictions into $5$ bins of $p$. Compare each bin's mean $p$ with how often $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.28s = "true", 15.18s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\node[anchor=west] at (-0.8,-1.45) {Overconfident, bin 1: mean $p = 0.05$. Of its $280$ points, $49$ ($0.18$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.18s = "true", 16.08s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\node[anchor=west] at (-0.8,-1.45) {Overconfident, bin 2: mean $p = 0.28$. Of its $80$ points, $32$ ($0.40$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.08s = "true", 16.98s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\node[anchor=west] at (-0.8,-1.45) {Overconfident, bin 3: mean $p = 0.50$. Of its $80$ points, $40$ ($0.50$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.98s = "true", 17.88s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\node[anchor=west] at (-0.8,-1.45) {Overconfident, bin 4: mean $p = 0.72$. Of its $80$ points, $48$ ($0.60$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.88s = "true", 18.78s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Overconfident, bin 5: mean $p = 0.95$. Of its $280$ points, $231$ ($0.83$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.78s = "true", 21.78s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Overconfident: low bins are too low and high bins too high. Its log loss is $0.62$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.78s = "true", 22.68s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\fill (8.42,0.32) rectangle (8.60,0.50);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Calibrated, bin 1: mean $p = 0.10$. Of its $160$ points, $16$ ($0.10$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 22.68s = "true", 23.58s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\draw[black, very thick] (8.51,0.41) -- (9.34,1.24);
\fill (8.42,0.32) rectangle (8.60,0.50);
\fill (9.25,1.15) rectangle (9.43,1.33);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Calibrated, bin 2: mean $p = 0.30$. Of its $160$ points, $48$ ($0.30$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 23.58s = "true", 24.48s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\draw[black, very thick] (8.51,0.41) -- (9.34,1.24) -- (10.17,2.07);
\fill (8.42,0.32) rectangle (8.60,0.50);
\fill (9.25,1.15) rectangle (9.43,1.33);
\fill (10.08,1.98) rectangle (10.26,2.16);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Calibrated, bin 3: mean $p = 0.50$. Of its $160$ points, $80$ ($0.50$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 24.48s = "true", 25.38s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\draw[black, very thick] (8.51,0.41) -- (9.34,1.24) -- (10.17,2.07) -- (11.00,2.90);
\fill (8.42,0.32) rectangle (8.60,0.50);
\fill (9.25,1.15) rectangle (9.43,1.33);
\fill (10.08,1.98) rectangle (10.26,2.16);
\fill (10.91,2.81) rectangle (11.09,2.99);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (-0.8,-1.45) {Calibrated, bin 4: mean $p = 0.70$. Of its $160$ points, $112$ ($0.70$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 25.38s = "true", 26.28s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\draw[black, very thick] (8.51,0.41) -- (9.34,1.24) -- (10.17,2.07) -- (11.00,2.90) -- (11.83,3.73);
\fill (8.42,0.32) rectangle (8.60,0.50);
\fill (9.25,1.15) rectangle (9.43,1.33);
\fill (10.08,1.98) rectangle (10.26,2.16);
\fill (10.91,2.81) rectangle (11.09,2.99);
\fill (11.74,3.64) rectangle (11.92,3.82);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (12.69,1.55) {log loss $= 0.50$};
\node[anchor=west] at (-0.8,-1.45) {Calibrated, bin 5: mean $p = 0.90$. Of its $160$ points, $144$ ($0.90$) have $y = 1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.28s = "true", 30.28s = "false", 30.28s = "false", repeats}}}]
\draw[prp, thick] (8.29,0.72) -- (8.29,0.19);
\draw[prp, thick] (9.24,1.66) -- (9.24,1.14);
\draw[prp, thick] (10.17,2.07) -- (10.17,2.07);
\draw[prp, thick] (11.10,2.48) -- (11.10,3.00);
\draw[prp, thick] (12.05,3.42) -- (12.05,3.95);
\draw[prp, very thick] (8.29,0.72) -- (9.24,1.66) -- (10.17,2.07) -- (11.10,2.48) -- (12.05,3.42);
\filldraw[draw=black, fill=prp] (8.29,0.72) circle (3pt);
\filldraw[draw=black, fill=prp] (9.24,1.66) circle (3pt);
\filldraw[draw=black, fill=prp] (10.17,2.07) circle (3pt);
\filldraw[draw=black, fill=prp] (11.10,2.48) circle (3pt);
\filldraw[draw=black, fill=prp] (12.05,3.42) circle (3pt);
\draw[black, very thick] (8.51,0.41) -- (9.34,1.24) -- (10.17,2.07) -- (11.00,2.90) -- (11.83,3.73);
\fill (8.42,0.32) rectangle (8.60,0.50);
\fill (9.25,1.15) rectangle (9.43,1.33);
\fill (10.08,1.98) rectangle (10.26,2.16);
\fill (10.91,2.81) rectangle (11.09,2.99);
\fill (11.74,3.64) rectangle (11.92,3.82);
\node[anchor=west] at (12.69,2.95) {log loss $= 0.62$};
\node[anchor=west] at (12.69,1.55) {log loss $= 0.50$};
\node[anchor=west] at (-0.8,-1.45) {Both models rank the points in the same order. The calibrated one has the lower log loss.};
\end{scope}
\end{tikzpicture}
\end{document}(Calibration and Reliability Diagrams) A model is calibrated when, among the points given probability , a fraction are positive.
A reliability diagram groups the predictions into bins, such as , and so on. For each bin it plots the observed fraction of positives against the mean prediction. A calibrated model lies on the diagonal, and points below the diagonal at high signal overconfidence. The expected calibration error averages the gaps, weighted by bin size:
Here is the number of points in bin . The value is their fraction of positives, and is their mean prediction. Guo et al. (2017) found modern deep networks overconfident on test data. Temperature scaling fixes much of this. It divides all logits by one constant , fitted to minimize log loss on a validation set. An overconfident network gets . Dividing by keeps the order of the scores, so accuracy and ROC-AUC do not change.
(Proper Scoring Rules) Log loss is strictly proper, so a forecaster minimizes the expected loss only by reporting the true probability.
A scoring rule assigns a penalty to a probability forecast once the outcome is known. It is proper if reporting the true probability minimizes the expected penalty. It is strictly proper if no other report does equally well. The Derivation below proves that log loss is strictly proper. Accuracy at threshold 0.5 is proper but not strictly proper. For an event with true probability 0.7, every report above 0.5 earns the same expected accuracy. A model tuned only for accuracy therefore has no reason to output correct probabilities. Consider rules that depend only on the probability of the observed outcome. With three or more classes, log loss is the only proper one (Bernardo 1979). It ignores how the remaining probability spreads over the wrong classes.
(Brier Score) The Brier score is a bounded, strictly proper alternative to log loss.
The Brier score is the mean squared error between probabilities and labels, . Here is the predicted probability and the label. It lies in , and predicting everywhere gives . One confident error costs at most 1. A prediction of 0.01 on a positive costs 0.98 under the Brier score and 4.6 under log loss. The Brier score therefore tolerates rare label errors better and penalizes overconfidence less. When the predictions fall into bins with shared values, Murphy (1973) splits it exactly into three parts:
Here measures miscalibration, and rewards bins whose rates differ from . The term is the baseline. Brier (1950) proposed the score to grade weather forecasts.
Derivation
We derive log loss from maximum likelihood and then show that it is strictly proper. A model predicts . Under a Bernoulli model, the probability of the observed label is . For independent points the likelihood of all labels is the product of these terms. Taking of the product turns it into the sum in the definition. Minimizing log loss is therefore maximizing the likelihood (Prince, section 5.4). For logistic regression, with logit . One term of the loss is then , and its derivative with respect to is .
Now fix one input whose true probability of a positive label is , and suppose the model reports . The expected loss is
Adding and subtracting splits it into two parts:
The entropy does not depend on . The KL divergence is nonnegative and equals 0 only at . The expected loss therefore has its unique minimum at the true probability, so log loss is strictly proper. The minimum is the uncertainty in the label itself, which no model can remove. Prince (section 5.7) starts from the KL divergence instead. Minimizing the KL divergence from the empirical label distribution to the model gives the negative log-likelihood. Apply the same split to the points in one bin of a reliability diagram. Recalibrating the bin then lowers its loss by exactly the bin’s KL term.
Examples
(Easy) Log Loss of a Spam Filter
A spam filter gives four emails these spam probabilities: 0.9 (spam), 0.2 (legitimate), 0.6 (spam) and 0.05 (legitimate).
Compute the log loss in nats and in bits. , and each legitimate email costs . The four terms are , , and . Their mean is 0.223 nats, or bits. The hesitant 0.6 on a spam email costs more than the other three emails together. All four emails are classified correctly at threshold 0.5, so the accuracy of 1 hides the hesitation.
Each spam email costs
(Medium) Same Accuracy, Different Log Loss
Two disease-risk models score five patients. Patients 1 and 2 are sick, and patients 3 to 5 are healthy. Model A predicts 0.8, 0.45, 0.3, 0.2 and 0.1. Model B predicts 0.95, 0.02, 0.05, 0.05 and 0.05.
Compare accuracy at threshold 0.5, log loss and Brier score. for patient 2, with mean 0.823. Model B is closer to the truth on four patients. Its one confident miss still supplies 95% of its total loss. The Brier scores are 0.0965 for A and 0.194 for B. The Brier score also prefers A, by a factor of 2 instead of 2.4. A doctor who acts on the probability gains more from A's hedge than from B's 2% for a sick patient.
Both models misclassify patient 2 and get the other four right, so both have accuracy 0.8. Model A’s terms are 0.223, 0.799, 0.357, 0.223 and 0.105, with mean 0.341. Model B’s terms are 0.051 for each of the four correct patients and
(Hard) Recalibrating a Loan-Default Model
A lender’s model assigns 1,000 loans to three probability levels. It predicts 0.1 for 500 loans, and 60 of them default. It predicts 0.5 for 300 loans, and 120 default. It predicts 0.9 for 200 loans, and 140 default.
Compute the ECE, the log loss, and the log loss after each prediction is replaced by its bin's observed default rate. . The model is overconfident at the top, where loans given 0.9 default only 70% of the time. Its log loss is 0.545 nats. Replacing the predictions by 0.12, 0.40 and 0.70 lowers it to 0.508. The drop of 0.038 equals the bin-weighted KL divergence between observed and predicted rates, as the Derivation predicts. The 0.9 bin improves most, falling from 0.765 to 0.611 nats per loan. Predicting the overall default rate of 0.32 for every loan gives . The recalibrated model removes 19% of that baseline loss. The Brier score falls from 0.178 to 0.167, and the drop equals its reliability term . The ranking of the loans does not change, so the ROC-AUC stays the same. In practice the new rates come from a validation set, since rates fitted on the test set overstate the gain.
The observed rates are 0.12, 0.40 and 0.70. The ECE is
See Also
- 4.3. Logistic Regression, which minimizes log loss over a linear model of the logit.
- 4.3a. Softmax Regression, the multi-class version with categorical cross-entropy.
- 2. Neural Networks, which train on cross-entropy and are often evaluated on accuracy.
- 2.1.1. Mean Squared Error, the regression counterpart, which is the Gaussian negative log-likelihood.
- 2.2.2. ROC Curve, which measures the ranking that log loss combines with calibration.