Tutorials › Statistical Learning (Graduate) › Log Loss and Kullback–Leibler Regret

Bayes Rules and Conditional Risk · Tutorial 63 of 989

Log Loss and Kullback–Leibler Regret

See how log loss turns a probability report’s excess conditional risk into an exact Kullback–Leibler divergence.

Beginner 10 min read

What You'll Learn

  • Define log loss for a reported probability vector over finitely many classes.
  • Express conditional log-loss risk as entropy plus a divergence term.
  • Prove that Kullback–Leibler divergence is nonnegative and identify when it is zero.
  • Derive the unique conditional risk-minimizing probability report under positive class probabilities.
  • Calculate log-loss regret for binary and multiclass probability reports.
  • Interpret conditional regret when the probabilities and reports depend on observed features.

Why Probability Reports Need a Loss

A classifier can report only its most likely class, or it can report a probability for every class. The latter carries more information: a report such as “class 1 has probability 0.8” distinguishes strong confidence from a near tie. To assess such reports, we need a loss that evaluates the probability assigned to the class that actually occurs. Log loss does exactly this, and its regret has a precise interpretation: it measures the Kullback–Leibler divergence between the true conditional probabilities and the reported probabilities.

Throughout, there are \(K\) possible labels, indexed by \(k=1,\ldots,K\). Logarithms are natural logarithms. We focus on probability vectors whose coordinates are all strictly positive, so every loss and divergence below is finite.

Definition: The open probability simplex is \(\Delta_K^\circ=\{q\in\mathbb{R}^K:q_k>0\text{ for every }k,\ \sum_{k=1}^K q_k=1\}\). A report \(q\in\Delta_K^\circ\) assigns probability \(q_k\) to label \(k\). The log loss when label \(y\) occurs is \(L(q,y)=-\log q_y\).

Let \(p=(p_1,\ldots,p_K)\in\Delta_K^\circ\) be the true probability vector for the label. The risk of reporting \(q\) is the expected loss when the label has distribution \(p\). The true vector \(p\) is fixed for this calculation; the randomness is in the label that is observed.

$$ R_p(q)=E_p[L(q,Y)]=-\sum_{k=1}^K p_k\log q_k. $$

The risk is small when the report assigns high probability to labels that are likely to occur. But this intuitive description does not yet show which vector minimizes risk or how much worse a particular report is. The key is to compare the risk with the risk of reporting \(p\) itself.

Entropy and the Risk–Regret Identity

The risk of the truthful report \(q=p\) is the entropy of the true probability vector. The Kullback–Leibler divergence, in the direction needed here, compares the true probabilities in the first argument with the reported probabilities in the second.

Definition: For \(p,q\in\Delta_K^\circ\), the entropy of \(p\) and the Kullback–Leibler divergence from \(p\) to \(q\) are \(H(p)=-\sum_{k=1}^K p_k\log p_k\) and \(D_{\mathrm{KL}}(p\|q)=\sum_{k=1}^K p_k\log(p_k/q_k)\), respectively.
Theorem: For \(p,q\in\Delta_K^\circ\), \[ R_p(q)=H(p)+D_{\mathrm{KL}}(p\|q). \] Consequently, the regret of report \(q\), relative to the truthful report \(p\), is exactly \(D_{\mathrm{KL}}(p\|q)\).

Proof. By the definitions,

$$ \begin{aligned} R_p(q)-H(p) &=-\sum_{k=1}^K p_k\log q_k+\sum_{k=1}^K p_k\log p_k\\ &=\sum_{k=1}^K p_k\bigl(\log p_k-\log q_k\bigr)\\ &=\sum_{k=1}^K p_k\log\left(\frac{p_k}{q_k}\right)\\ &=D_{\mathrm{KL}}(p\|q). \end{aligned} $$

The risk at \(p\) is \(R_p(p)=-\sum_k p_k\log p_k=H(p)\). Substituting this into the identity gives \(R_p(q)-R_p(p)=D_{\mathrm{KL}}(p\|q)\), which is the claimed regret. \(\square\)

The identity reduces minimization of log-loss risk to a basic property of the divergence: it is nonnegative and vanishes only when the two probability vectors agree. Here is a proof under the stated positivity assumptions.

Result: If \(p,q\in\Delta_K^\circ\), then \(D_{\mathrm{KL}}(p\|q)\geq 0\), with equality if and only if \(p=q\).

Proof. For every \(u>0\), \(\log u\leq u-1\), with equality only at \(u=1\). Apply this inequality to \(u=q_k/p_k\), multiply by \(p_k>0\), and change signs:

$$ p_k\log\left(\frac{p_k}{q_k}\right) \geq p_k-q_k. $$

Summing over \(k\) gives

$$ D_{\mathrm{KL}}(p\|q) \geq \sum_{k=1}^K(p_k-q_k) =1-1=0. $$

If equality holds in the sum, equality must hold in each of the \(K\) inequalities, since each difference between the left and right sides is nonnegative. Thus \(q_k/p_k=1\) for every \(k\), so \(q=p\). Conversely, if \(q=p\), every term in the divergence is zero. \(\square\)

Conclusion: The unique risk-minimizing report in \(\Delta_K^\circ\) is \(q=p\). Log loss rewards truthful probabilities, and the excess risk of any other report is exactly its Kullback–Leibler divergence from the truth.

Worked Calculations

Worked Example: A Binary Report with Incorrect Probabilities

Suppose the true probabilities of labels 1 and 2 are \(p=(0.8,0.2)\), but the report is \(q=(0.6,0.4)\). Calculate the truthful risk, the reported risk, and the regret.

The truthful risk is the entropy:

$$ \begin{aligned} H(p) &=-0.8\log(0.8)-0.2\log(0.2)\\ &=0.8(0.223143551314)+0.2(1.609437912434)\\ &\approx 0.500402424. \end{aligned} $$

The reported risk is

$$ \begin{aligned} R_p(q) &=-0.8\log(0.6)-0.2\log(0.4)\\ &=0.8(0.510825623)+0.2(0.916290732)\\ &\approx 0.591918645. \end{aligned} $$

The regret can be calculated directly as the risk difference, or from the divergence:

$$ \begin{aligned} D_{\mathrm{KL}}(p\|q) &=0.8\log(0.8/0.6)+0.2\log(0.2/0.4)\\ &=0.8\log(4/3)+0.2\log(1/2)\\ &\approx 0.230145658-0.138629436\\ &\approx 0.091516222. \end{aligned} $$

Indeed, \(0.591918645-0.500402424\approx0.091516222\), up to rounding. Notice the order of the divergence arguments: the truth is first, and the report is second.

Worked Example: Three-Class Probability Report

Let \(p=(0.2,0.5,0.3)\) and \(q=(0.25,0.4,0.35)\). Compute the risk and verify that the regret is the divergence from \(p\) to \(q\).

First, the truthful risk is

$$ \begin{aligned} H(p) &=-0.2\log(0.2)-0.5\log(0.5)-0.3\log(0.3)\\ &\approx 0.321887582+0.346573590+0.361191841\\ &\approx 1.029653014. \end{aligned} $$

The risk of \(q\) is

$$ \begin{aligned} R_p(q) &=-0.2\log(0.25)-0.5\log(0.4)-0.3\log(0.35)\\ &\approx 0.277258872+0.458145366+0.314946637\\ &\approx 1.050350876. \end{aligned} $$

The risk difference is approximately \(1.050350876-1.029653014=0.020697862\). Direct calculation of the divergence gives

$$ \begin{aligned} D_{\mathrm{KL}}(p\|q) &=0.2\log(0.2/0.25)+0.5\log(0.5/0.4)+0.3\log(0.3/0.35)\\ &=0.2\log(0.8)+0.5\log(1.25)+0.3\log(6/7)\\ &\approx -0.044628710+0.111571776-0.046245204\\ &\approx 0.020697861. \end{aligned} $$

The two calculations agree to the displayed precision; the final digits can differ by one unit because the risks and divergence terms were rounded separately. The unrounded identity holds exactly.

Conditional Probabilities and Bayes Reports

In prediction, the true label distribution usually depends on observed features \(X\). For each class \(k\), let \(\eta_k(X)=P(Y=k\mid X)\) be a version of the conditional probability, and write \(\eta(X)=(\eta_1(X),\ldots,\eta_K(X))\). Assume \(\eta_k(X)>0\) for every class almost surely. A probability-valued prediction is a measurable vector \(q(X)\in\Delta_K^\circ\). At a feature value \(X=x\), its conditional risk is

$$ E[L(q(X),Y)\mid X=x] =-\sum_{k=1}^K \eta_k(x)\log q_k(x). $$

Apply the risk–regret identity with \(p=\eta(x)\). This yields the conditional-risk decomposition, for almost every \(x\):

$$ E[L(q(X),Y)\mid X=x] =H(\eta(x))+D_{\mathrm{KL}}\bigl(\eta(x)\|q(x)\bigr). $$

The first term does not depend on the report. The second is nonnegative and equals zero only when \(q(x)=\eta(x)\). Thus the conditional probability vector is the unique pointwise minimizer. If the reports are measurable functions of \(X\), choosing \(q(X)=\eta(X)\) is itself measurable, so it provides the minimizing rule under the assumptions above. This is the log-loss version of pointwise Bayes-risk minimization.

Worked Example: Regret Across Two Feature Groups

Suppose a feature \(X\) takes values \(a\) and \(b\), each with probability \(1/2\). The true conditional probabilities are \(\eta(a)=(0.25,0.75)\) and \(\eta(b)=(0.4,0.6)\). A model reports \(q(a)=(0.4,0.6)\) and \(q(b)=(0.5,0.5)\). Find its expected excess risk over the truthful conditional report.

At \(X=a\), the conditional regret is

$$ \begin{aligned} D_{\mathrm{KL}}\bigl(\eta(a)\|q(a)\bigr) &=0.25\log(0.25/0.4)+0.75\log(0.75/0.6)\\ &=0.25\log(0.625)+0.75\log(1.25)\\ &\approx -0.117500907+0.167357663\\ &\approx 0.049856756. \end{aligned} $$

At \(X=b\), it is

$$ \begin{aligned} D_{\mathrm{KL}}\bigl(\eta(b)\|q(b)\bigr) &=0.4\log(0.4/0.5)+0.6\log(0.6/0.5)\\ &=0.4\log(0.8)+0.6\log(1.2)\\ &\approx -0.089257421+0.109392934\\ &\approx 0.020135514. \end{aligned} $$

Averaging over the two equally likely feature values gives expected excess risk \(\tfrac12(0.049856756)+\tfrac12(0.020135514)\approx0.034996135\). The group-specific regrets are nonnegative, and their average is the overall regret. A model can therefore have different probability errors in different parts of feature space, even though its overall excess risk is a single average.

Interpretation and Common Pitfalls

The divergence quantifies the cost of a probability report under the true distribution. It is not a distance in the ordinary metric sense: in general, \(D_{\mathrm{KL}}(p\|q)\) is not equal to \(D_{\mathrm{KL}}(q\|p)\). The orientation matters because the risk averages \(-\log q_k\) using the true probabilities \(p_k\). This averaging produces \(D_{\mathrm{KL}}(p\|q)\), not the reversed expression.

  • Confusing the report with the observed label. The loss is \(-\log q_y\): it evaluates the probability assigned to the label that actually occurred, not the probability of the most likely label.
  • Reversing the divergence. Starting with risk under the true distribution gives \(D_{\mathrm{KL}}(p\|q)\). Swapping \(p\) and \(q\) changes the quantity and generally changes its value.
  • Calling entropy the regret. Entropy \(H(p)\) is the minimum risk itself. Regret is the excess \(R_p(q)-H(p)\), which equals the divergence.
  • Assuming every probability report has zero regret. Only the truthful report has zero regret under strictly positive \(p\). A report that differs from \(p\) has strictly positive regret.

In applications, the identity makes log loss useful for evaluating the quality of probability forecasts, not merely their hard classifications. Two reports can select the same most likely class and still have different log-loss risks because they assign different probabilities to the realized outcomes. The divergence decomposition explains exactly how those probability differences translate into excess risk.

Check Your Understanding

Use the definitions and risk–regret identity to answer the following.

  1. For a true vector \(p=(0.7,0.3)\) and report \(q=(0.5,0.5)\), write the risk and divergence expressions whose difference is the regret.
  2. Why does the nonnegativity proof require the coordinates of the probability vectors to be positive in the version presented here?
  3. State the unique conditional-risk-minimizing report when the conditional class probabilities at \(X=x\) are \((0.1,0.2,0.7)\).
  4. If a report differs from the true vector, what does strictness in \(\log u\leq u-1\) imply about its regret?
  5. Explain why conditional regrets for feature values must be averaged using the distribution of \(X\) to obtain overall expected excess risk.
Clinical Biostats