Negative Log-Likelihood as a Loss Function
Negative log-likelihood turns "how probable did the model say the true outcome was" into a number to minimize, punishing confident wrong answers far more harshly than uncertain ones — it is the loss quietly hiding behind logistic regression, softmax classifiers, and most of deep learning.
Prerequisites: Maximum Likelihood Estimation (MLE), Cross-Entropy and Log Loss
A weather forecaster who says "90% chance of rain" and it rains should be scored better than one who says "55%" and it rains — both got the direction right, but the first was more confidently right. And a forecaster who says "95% chance of sun" when it pours should score much worse than one who hedged at "55% sun" — confidently wrong deserves a harsher penalty than cautiously wrong. Negative log-likelihood is exactly this scoring rule, made precise: it rewards confident correctness, tolerates cautious mistakes, and punishes confident mistakes severely.
Building the formula from the idea
A model that outputs probabilities assigns some probability to the outcome that actually happened. The likelihood of the data, given the model, is just that probability — how likely the model said the true outcome was, before it was known. Training wants to make the true outcomes look as probable as possible under the model, so naturally you'd want to maximize likelihood. Optimization algorithms are built to minimize, so the negative sign flips the direction: minimizing is the same problem as maximizing likelihood.
The logarithm is there for a specific, important reason. With independent data points, the joint likelihood is a product of probabilities, — a number that shrinks toward zero fast as grows and becomes numerically unworkable. Taking the log turns that product into a sum, , which is numerically stable and lets each data point contribute independently and additively to the total.
In words: for every training example, look up the probability the model assigned to the actual observed outcome , take its logarithm, and sum those logarithms across all examples, then flip the sign. A model that assigns high probability to what actually happened, every time, gets a small NLL. A model that confidently assigns low probability to what actually happened gets a large NLL, because of a small number is a large negative number, and the outer negative sign turns that into a large positive penalty.
This is the shape doing all the work: stays small and gentle as approaches (barely any penalty for being confidently right), but rises steeply — without bound — as approaches (an enormous, unbounded penalty for confidently assigning near-zero probability to something that then happens). No other simple penalty shape has this specific asymmetry built in.
Worked example 1: three forecasts scored
A binary classifier predicts, for three days that all actually ended "up": forecast probabilities of "up" equal to , , and (that last one confidently wrong). Compute for each: , , . Average NLL across the three: .
Notice how lopsided the contributions are: the confidently correct forecast () contributes almost nothing to the total, the middling forecast () contributes a moderate amount, and the single confidently wrong forecast () contributes nearly three times as much as the other two combined. One bad, overconfident call can dominate the entire loss — which is deliberate: NLL is specifically designed to punish miscalibrated confidence, not just wrong direction.
Worked example 2: comparing two models with the same accuracy
Model A and Model B both correctly classify 8 of 10 days (80% accuracy). Model A makes correct predictions with modest confidence (), and wrong ones modestly too ( to the right class). Model B makes correct predictions very confidently (), but its wrong predictions are also very confident and very wrong ( to the correct class).
Model A's NLL: 8 terms at , 2 at : average . Model B's NLL: 8 terms at , 2 at : average .
Both models are 80% accurate, but Model B's NLL is substantially worse, because its confident mistakes are punished heavily while its confident correct calls barely help. Accuracy alone would call these models equal; NLL correctly flags Model B as more dangerous to trade on, since its wrong calls carry false conviction.
Negative log-likelihood does not just ask "was the prediction right or wrong" — it asks "how much probability did the model stake on the truth," which is why a badly overconfident wrong prediction is punished far more than a cautious one, even when both count as one "miss" under plain accuracy.
What this means in practice
NLL (often called log loss or, for classification, cross-entropy — the same object under different names) is the loss function actually being minimized inside logistic regression, most neural network classifiers, and any model that outputs calibrated probabilities rather than just a hard label. Choosing to optimize NLL instead of plain accuracy is what forces a model to output well-calibrated probabilities in the first place — a model trained purely to maximize accuracy has no incentive to get its confidence level right, only its label, whereas NLL directly penalizes miscalibration.
A model can achieve excellent NLL on a validation set purely by learning to be well-calibrated and modestly confident, without necessarily being more accurate than a less-calibrated competitor — NLL and accuracy are correlated but not the same objective, and optimizing hard for one can occasionally trade away a little of the other. The other classic trap: a model that outputs a literal probability of exactly or for an outcome that turns out to be otherwise makes NLL mathematically infinite for that point, since is undefined in the limit — production systems clip predicted probabilities away from the extremes specifically to avoid this blow-up.
Related concepts
Practice in interviews
Further reading
- Bishop, Pattern Recognition and Machine Learning, ch. 1.5, 4.3
- Murphy, Machine Learning: A Probabilistic Perspective, ch. 2