TL;DR
Maximum likelihood estimation (MLE) chooses the parameters of a model so that the data you actually observed would have been as probable as possible under that model. You write down the probability of the data as a function of the parameters, called the likelihood, and find the parameters that make it largest. In practice you maximise the logarithm of the likelihood, which gives the same answer and is easier to work with.
Most of the familiar estimators are MLEs in disguise. The sample mean is the MLE of a Gaussian's centre, and minimising squared error is maximum likelihood under Gaussian noise.
Where you'll see it: in fitting a Gaussian to measurements, in logistic regression, and in every neural network trained with cross-entropy loss.
Intuition
Ask which setting explains the data best
Suppose you flip a coin ten times and get seven heads. You do not know the chance that the coin lands heads. Which value of makes "seven heads in ten flips" most believable? Intuitively it is . A fair coin with makes that outcome fairly common, but makes it almost impossible. Maximum likelihood formalises this: it scores each candidate by how probable the observed data is under it, and keeps the best one.
The same idea applies to any model. You have a family of probability distributions indexed by parameters , and some observed data. Each gives the data a probability. The MLE is the that gives the data the highest probability.
flowchart LR A[Observed data] --> B[Likelihood: probability of the data for each theta] B --> C[Take the log] C --> D[Find the theta that maximises it] D --> E[Maximum likelihood estimate]
The method does not say the model is true. It says: among the models in this family, this one fits the data best in the sense of making it most probable.
The math
Likelihood
Let be observations, and let be the probability density (or mass) function of one observation under parameter . Assume the observations are independent and identically distributed. The probability density of the whole dataset is then a product:
The likelihood of given the data is
The log-likelihood is
The maximum likelihood estimate is .
Two points matter here. The likelihood is a function of , with the data held fixed. It is not a probability distribution over , and its values do not need to sum to 1. And the log is used because it is monotonic: the that maximises also maximises . The log turns a product into a sum, which is easier to differentiate. It also stops the product from underflowing to zero when is large, since multiplying a thousand numbers below 1 quickly gives something a computer rounds to 0.
How to find the maximum
For a smooth likelihood, set the derivative of with respect to each parameter to zero, and check that the point is a maximum (the second derivative is negative). Sometimes this has a closed form. When it does not, numerical methods such as gradient ascent on do the job.
Worked derivation: the coin
For flips with heads, each flip has probability of heads, so
Differentiate and set to zero:
The MLE of a coin's heads probability is the observed fraction of heads. The second derivative is negative, so this is a maximum.
- likelihood of p, scaled so the peak is 1
Worked derivation: a Gaussian
For observations from , the log-likelihood is
Setting the derivative with respect to to zero gives , so
Setting the derivative with respect to to zero, and writing , gives , so
The MLE of the mean is the sample average. The MLE of the variance divides by , not . That makes it slightly biased low; the unbiased sample variance divides by . For large the difference is negligible.
Where the familiar losses come from
Maximum likelihood explains two training objectives that look like arbitrary choices.
Least squares. If each observation is with Gaussian noise , then the negative log-likelihood is a constant plus . Maximising the likelihood over is the same as minimising the sum of squared errors. This is why Linear Regression and Mean Squared Error go together. Change the noise model and the loss changes with it: Laplace noise gives absolute error.
Cross-entropy. For a classifier that outputs a probability for each class, the likelihood of the true labels is a product of the predicted probabilities of the correct classes. Taking the negative log gives the Cross-Entropy Loss. Minimising cross-entropy is maximum likelihood for a categorical model.
So "minimise this loss" is often a statement about a probability model you have already assumed.
Worked examples
Ten flips give seven heads. The likelihood is .
- Log-likelihood: .
- Derivative: .
- Set to zero: , so .
- Solution: .
- Compare with other values. . . So makes the data about 2.3 times more probable than a fair coin.
Interpretation: the estimate is the observed frequency, and the likelihood ratio tells you how much better it explains the data than the fair coin.
Four measurements are . Assume they come from a Gaussian with unknown mean and variance.
- Sample mean, the MLE of : .
- Deviations from 4: .
- Squares: , with sum .
- MLE of the variance: . The standard deviation estimate is .
- For comparison, the unbiased sample variance is .
Interpretation: the model says the measurements centre on 4 with spread about 2.55. The unbiased version is larger because it corrects for the fact that the mean was estimated from the same data. With four points, that correction matters.
Tradeoffs and failure modes
MLE is a strong default with large datasets and a reasonable model. It is less reliable elsewhere:
| Situation | Problem | Better choice |
|---|---|---|
| Very small samples | The estimate can be far off and high-variance, and MLE of variance is biased | Unbiased estimators, or a Bayesian estimate with a prior |
| Flexible models with many parameters | The likelihood can be maximised by memorising the data, which is overfitting | Regularisation, which is maximum a posteriori (MAP) estimation with a prior |
| Wrong model family | The maximum is the best fit within the family, which may still be poor | Check the model with residuals, or change the family |
| Some mixture models and degenerate cases | The likelihood can be unbounded. For example, a Gaussian centred exactly on one data point with variance going to 0 has density going to infinity | Constrain the variance from below, or use a prior |
| Parameters that cannot be told apart | Many parameter values give the same likelihood, so there is no unique maximum | Reparameterise, or use a different model |
Large-sample theory helps in the good case. Under regularity conditions the MLE is consistent (it converges to the true value), asymptotically normal, and asymptotically efficient, meaning no other reasonable estimator has smaller variance in the limit. The precise variance of the estimate is governed by the Fisher Information.
History
Carl Friedrich Gauss and Pierre-Simon Laplace worked with least squares and Gaussian error models in the early 1800s, which is the special case that MLE generalises. The general principle was formalised by Ronald Fisher in the early 1920s. In his papers from that period he introduced "likelihood" as a term distinct from probability and argued that it should be the basis for estimation. The method caught on because it applies to almost any model, gives a single recipe, and has good large-sample behaviour. It is now the default training principle in statistics and in most of machine learning.
Check yourself
QuestionTen flips of a coin give four heads. What is the maximum likelihood estimate of the heads probability, and why does the estimate not depend on the order of the flips?Show answer
The MLE is . The order does not matter because the likelihood is a product of identical factors for each head and for each tail. The product depends only on how many of each there are, not on their order.
QuestionWhy do we maximise the log-likelihood rather than the likelihood itself?Show answer
The logarithm is strictly increasing, so the maximiser is the same. It turns the product over independent observations into a sum, which is easier to differentiate. It also avoids numerical underflow, since a product of many probabilities below 1 quickly rounds to zero in floating point, while the sum of their logs stays a usable number.
QuestionSuppose you fit a Gaussian to a dataset of ten values and report the variance as . A colleague says this is wrong and you should divide by . Who is right, and what is the difference?Show answer
Neither is wrong; they are different estimators. Dividing by gives the maximum likelihood estimate, which is the value that maximises the likelihood. Dividing by gives the unbiased sample variance, whose average over repeated samples equals the true variance. The MLE is biased low by a factor of , which is 0.9 for . For large the difference is small.
QuestionA classifier outputs probability 0.9 for the correct class in one example and 0.1 in another, with equal weight. Which of these has the larger contribution to the cross-entropy loss, and why does this match the likelihood view?Show answer
The second, because the loss is of the probability of the correct class. For 0.9 it is , and for 0.1 it is . Maximising the likelihood means maximising the product of these probabilities, which is the same as minimising the sum of their negative logs. The loss is large exactly when the model gave the observed labels low probability, which is what the likelihood view penalises.
Sources
- Maximum likelihood estimation (Wikipedia): https://en.wikipedia.org/wiki/Maximum_likelihood_estimation
- Bishop, Pattern Recognition and Machine Learning, §1.2.5: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/
- Goodfellow, Bengio and Courville, Deep Learning, Chapter 5.5: https://www.deeplearningbook.org/