Maximum Likelihood and Cross-Entropy
Choose the parameters that make the data you actually saw as probable as possible. Taking logs turns the product over examples into a sum without moving the peak, and the negative log-likelihood turns out to be exactly cross-entropy for a classifier and exactly squared error for regression with Gaussian noise.
What the symbols mean
- Likelihood
How probable the observed data is under the parameters θ, read as a function of θ.
- Parameters
The model's adjustable numbers: a coin's bias, a network's weights.
- Data point
One observed example; there are n of them, assumed independent.
What the symbols mean
- MLE
The parameter value that makes the observed data most probable.
What the symbols mean
- Label
1 or 0: what example i actually was.
- Predicted probability
The model's probability that example i is a 1.
What the symbols mean
- Model prediction
What the regression model predicts for input x.
- Noise standard deviation
How far the real y typically scatters around the prediction.
Explanation
Maximum likelihood is simply a rule for fitting a model: choose the parameters that make the data you actually saw as probable as possible. The likelihood is the probability your model assigns to the whole dataset, : the probability of each example (that's , "the probability of if the parameters are "), all multiplied together (the is a product, the way is a sum), because independent events multiply. The name is chosen on purpose. It's the same formula as a probability, read backwards: the data is fixed, and we ask how "likely" each value of makes it look. Then we take logs, flip the sign and minimize. That's the whole recipe, and it turns out to be where the two most common loss functions in machine learning come from.
A coin to start. You flip a coin 10 times and see 7 heads. If the coin's chance of heads is , the probability of that particular sequence is
Try a few values. gives , gives and gives . The data looks most at home near . To find the peak exactly, take the log first:
Set the derivative to zero: , so and . In general heads in flips gives , which is reassuring: the fancy method agrees with common sense.
Why take logs? Three reasons, and each one matters in practice.
- The peak doesn't move. is increasing, so whatever maximizes also maximizes .
- Products become sums. , so the log-likelihood is a sum over examples, and the derivative of a sum is the sum of the derivatives. That's exactly the "gradient is an average over examples" structure that mini-batch gradient descent relies on.
- No underflow. A dataset of 10,000 examples, each given probability 0.5, has likelihood . The smallest positive 64-bit float is about , so the computer stores 0. The log-likelihood, , is a perfectly ordinary number.
Optimizers like to minimize, so we flip the sign and minimize the negative log-likelihood (NLL). Dividing by to get an average doesn't move the minimum either.
Minimizing NLL is cross-entropy. Now take a classifier that outputs , its probability that example is a 1. There's a neat trick for writing the probability of the true label (which is 1 or 0) in one expression: . When that's , and when it's . Take and add over the examples:
That is exactly the binary cross-entropy from Loss Functions. With classes and a softmax output, the probability of the true class is , and is the categorical cross-entropy from Softmax and Logits. So cross-entropy isn't an arbitrary choice someone happened to like: training a classifier with it is maximum likelihood.
Least squares is maximum likelihood too. Suppose a regression model predicts and the real scatters around it with Gaussian noise of standard deviation (the normal distribution). The density of one observation is
Take . The log undoes the exponential, and the constant in front becomes its own term:
Now add over the data. The second term is the same constant for every example and doesn't depend on the model's weights at all, and is just a positive scale factor. Neither one can move the minimum. What's left to minimize is , the sum of squared errors. So least squares is what maximum likelihood tells you to do when you believe the noise is Gaussian. If you believed the noise had heavier tails, you'd get a different loss, which is a good way to think about choosing losses in general.
A common mistake. It's tempting to read as "the probability that is the true parameter". It isn't. It's the probability of the data given , and it doesn't even add up to 1 over (for the coin, ). Turning it into a probability about takes a prior and Bayes' rule, which is two lessons ahead in Bayes' Rule and Naive Bayes.
See it
Worked examples
A website shows a new button to 10 visitors, and 7 of them click it. Model each visit as an independent click with probability . Find the maximum likelihood estimate of .
- 1
Write the probability of what happened. Each click has probability and each non-click , and independent visits multiply: .
A sensor's reading is modeled as , where the noise is Gaussian with mean 0 and standard deviation . The data are . Find the maximum likelihood estimate of .
- 1
Each reading is normal with mean , so its density is .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A quality inspector tests 30 chips and 18 of them pass. Treating each chip as passing independently with probability , find the maximum likelihood estimate of .
Why do we maximize the log-likelihood instead of the likelihood itself?
A server logs the times between failures of a disk controller: 2, 3, 5 and 6 hours. Model them as independent exponential lifetimes with density . Find the maximum likelihood estimate of the failure rate , in failures per hour.
A spam classifier gives three emails the probabilities , and of being spam. Their true labels are spam, not spam and spam (). Find the total negative log-likelihood (binary cross-entropy with natural logs). Give 4 decimal places.
A model assigns probability to the correct label on each of 2000 test examples. The likelihood of the test set is . Find to 2 decimal places.
A bottling line overfills by 4, 7, 9 and 12 grams on four bottles, modeled as normal with unknown and . The maximum likelihood estimates are and . Find in grams squared.
A strain gauge's output is modeled as with Gaussian noise. Calibration loads of kN give readings of mV. Find the maximum likelihood estimate of in mV per kN. Give 4 decimal places.
The hardest one here. Get it, and you've got this lesson.
A logistic model predicts whether a user clicks an ad from one binary feature (1 if they've visited before, 0 if not): . In the training data, 2 of 10 new visitors () clicked and 8 of 10 returning visitors () clicked. Find the maximum likelihood estimate of . Give 4 decimal places.
Where it's used
Nearly every model trained today is fit by maximum likelihood, even when nobody says so. A language model's training loss is the average negative log-likelihood of the next token, and its "perplexity" is just raised to that average. Regression with squared error quietly assumes Gaussian noise; when the errors have outliers, engineers switch to the absolute error, which is maximum likelihood under a Laplace distribution with heavier tails.