Cross-Entropy and Log Loss
Cross-entropy scores a probability forecast by how surprised it would have been by what actually happened — and the penalty for a confident wrong call grows without bound, which is exactly the behavior you want from a loss function.
Prerequisites: Objective Functions vs Evaluation Metrics
Suppose a model outputs "there is a 95% chance this loan does not default." The loan defaults. How bad was that prediction? Simple accuracy has no answer beyond "wrong" — it cannot distinguish a model that said 51% from one that said 99%, even though the second one should be punished far more severely for being so confidently mistaken. Cross-entropy, also called log loss, is the standard way to score a probability forecast rather than just a label, and it is built around one simple idea: punish confidence that turns out to be misplaced, and punish it harder the more confident it was.
The analogy: a bookmaker who bets his own odds
Imagine a bookmaker who is required to actually stake money according to the odds he quotes. If he says a horse has a 90% chance of winning and it loses, he loses a lot — he was staking heavily on being right. If he says 55% and it loses, he loses only a little, because he was barely committing to that outcome at all. A bookmaker who is well calibrated — right roughly as often as his stated probabilities imply — loses only a small, steady amount over time. One who is overconfident and wrong gets wiped out fast, because his losses on bad calls scale up faster than his gains on good ones.
Cross-entropy scores a model exactly like that bookmaker's ledger. Every prediction is treated as a bet sized by the stated probability, and the penalty for being wrong grows the more sure the model claimed to be.
The formula, one symbol at a time
For a single binary outcome, let be the true label (1 if the event happened, 0 if it did not) and be the model's predicted probability that it happens. The loss for that one prediction is
Read it in two pieces, only one of which is ever active for a given example. If the event actually happened (), the second term vanishes and the loss is just — the model is graded only on how much probability it put on the thing that actually occurred. If the event did not happen (), the first term vanishes and the loss is — graded on how much probability it put on the correct outcome, "no." The symbol is the natural logarithm; the reason it appears at all is that is the standard measure of "surprise" for an event with probability — it is exactly zero when (an event you were certain of causes no surprise when it happens) and it grows without bound as (an event you thought was nearly impossible causes unbounded surprise if it happens anyway). Cross-entropy is simply the average surprise, across all your predictions, at the outcomes that actually occurred. For possible classes with one-hot true label vector and predicted probabilities , the same idea generalizes to — in words, look up the probability the model assigned to whichever class actually happened, and take the negative log of just that one number; the other classes' probabilities don't enter the formula for that example at all.
Worked example 1: four loans, one bad miss
A credit model scores four loans. For each, the loan's true outcome and the model's predicted default probability:
| Loan | Outcome | P(default) predicted | Loss |
|---|---|---|---|
| 1 | no default () | 0.10 | |
| 2 | default () | 0.80 | |
| 3 | default () | 0.05 | |
| 4 | no default () | 0.02 |
Loan 3 alone contributes more than three times the sum of the other three losses combined. The model was confidently wrong — it said only a 5% chance of default on a loan that then defaulted — and log loss punishes exactly that mistake severely, on purpose. An accuracy score would have simply recorded "wrong" for Loan 3, identical in magnitude to any other wrong call; log loss instead tells you this was the single worst call in the batch, by a wide margin.
Worked example 2: three-class regime prediction
A model predicts tomorrow's market regime as one of "up," "flat," "down" using a softmax output — three probabilities that sum to 1.
Day 1: true regime is "up" (one-hot ), model predicts . Only the probability on the true class matters: .
Day 2: true regime is "down" (one-hot ), model predicts — it put most of its mass on "up," which did not happen, and only 10% on "down," which did.
Notice what the loss ignores on Day 2: it does not matter that the model also put 30% on "flat" rather than "down" — only the probability mass on the class that actually happened enters the formula. This is why cross-entropy rewards concentrating probability on the right answer, not merely avoiding a specific wrong one.
The explorer above plots a logarithmic curve; drag its parameters and notice how flat it is near and how steeply it drops as — that steepness is the mechanism that makes log loss punish overconfidence so much harder than it punishes underconfidence.
Cross-entropy is the average "surprise" — — at whatever actually happened, using the probability the model assigned to it. It rewards putting probability mass on the right answer and punishes confident wrong answers without bound, which plain accuracy cannot do at all.
What this means in practice
Cross-entropy is the standard training objective for classifiers precisely because it is smooth (a real gradient exists everywhere, unlike accuracy) and because minimizing it is mathematically equivalent to maximum-likelihood estimation — finding the model parameters that make the observed data least surprising. In trading applications where a model's output feeds directly into position sizing (bet bigger when more confident), log loss is also the right metric to monitor even after training, because a model that is well calibrated by log loss will size its bets sensibly, while a model that is merely "accurate" by a fixed threshold can still be dangerously overconfident on the calls that matter most.
Practice
- A model predicts 99% probability of "up" and the market goes down. Compute the log loss for that single prediction and compare it to a model that predicted 55% and was also wrong.
- Why does log loss have no upper bound, while accuracy is always between 0% and 100%?
- Two models have identical accuracy, but Model X's average log loss is 0.30 and Model Y's is 0.65. What does that difference tell you about how the two models are wrong when they are wrong?
Never let a raw predicted probability of exactly 0 or 1 reach the log-loss formula — is infinite, and a single such prediction, if wrong, will blow up the whole average and can crash training. Production implementations clip probabilities to a small margin away from 0 and 1 (for example ) for exactly this reason. The second common confusion: a lower average log loss does not automatically mean higher accuracy, and vice versa — they are correlated, not identical, which is the same trap explored in Objective Functions vs Evaluation Metrics.
Related concepts
Practice in interviews
Further reading
- Shannon, A Mathematical Theory of Communication (1948)
- Bishop, Pattern Recognition and Machine Learning, ch. 4.3