Maximum likelihood
The likelihood and the log-likelihood, estimates for the coin, the Poisson, normal and uniform families, losses as negative log-likelihoods, the link to cross-entropy and KL, Fisher information and asymptotic normality, and the cases where the method breaks.
The likelihood
The topic Estimators: bias and variance taught us to judge an estimator once someone hands it to us: its bias, its variance, its MSE, whether it is consistent. It did not say where estimators come from. The share of successes for a probability and the sample mean for a mean look natural, but a new model needs a recipe. Maximum likelihood is that recipe: choose the parameter under which the observed data are most probable.
Take the coin with an unknown probability of heads . In 10 independent flips it shows 7 heads and 3 tails. By independence, the probability of this particular sequence is a product of ten factors:
In general, let be an i.i.d. sample from a pmf or density . The likelihood is
It is the joint probability of the data, read the other way round: the data are fixed and varies.
is not a distribution over . For the coin, , not . Its size alone says little: is small only because any particular sequence of ten flips is unlikely. Ratios carry the meaning. , so a fair coin explains the data about half as well as . To talk about the probability of itself we need a prior, the subject of the topic Bayesian inference.
The maximum likelihood estimate (MLE) is the value with the largest likelihood:
The logarithm and the derivative
A product of many numbers below 1 is awkward. A thousand flips of a fair coin give , at the edge of what a computer's floating-point numbers can hold. The logarithm turns the product into a sum, the log-likelihood:
The logarithm is increasing, so and peak at the same . Nothing is lost.
To find the peak we need derivatives. A one-line reminder: the derivative is the slope of , and at a maximum inside an interval a smooth function has slope zero. We also need and . For more, see the topic Derivatives and Taylor series in Mathematics for ML.
For the coin, . Setting the derivative to zero gives
The derivative of the log-likelihood is called the score. A zero of the score could also be a minimum, or the maximum could sit at an end of the range. Here as and as , so the only zero is the maximum.
With heads in flips the same steps give , the share of heads. The recipe reproduces the obvious answer. That is a good sign: its value is that it keeps working where no answer is obvious.
Familiar families
The same steps work for the families of the topics Discrete distributions and Continuous distributions.
Poisson. Let be the numbers of requests to a server in separate minutes, modelled as with . Then
Normal. has two parameters. We set the derivative in each one to zero while holding the other fixed. The log-density is , and the two equations give
The divisor is , not . This is the biased variance estimate from the topic Estimators: bias and variance: . Maximum likelihood does not promise unbiasedness. For it gives , which is biased upward for small .
Uniform. For the density is on and zero outside. If some , that point has density zero and . Otherwise , which decreases in . So the maximum sits at the smallest allowed value, . The derivative is not zero there: the peak is on the edge of the allowed region. Setting is a tool; the definition is the .
One more property is free. The MLE is invariant: the MLE of is . So the MLE of is , and the MLE of the odds is .
Loss functions are likelihoods
Most losses in machine learning are negative log-likelihoods in disguise.
Regression. A model with weights predicts a number . Assume with noise , independent across examples. Then given is , with density
The negative log-likelihood of a training set is
The second term does not depend on . The first is the sum of squared errors times a positive constant. So minimizing squared error is maximum likelihood under Gaussian noise. The value of does not change the fitted , but it matters as soon as the model must report its own uncertainty.
Classification. A classifier outputs a categorical distribution over the classes . It gives the observed label the probability , so
This is the cross-entropy loss. A language model is the same formula over tokens: the multiplication rule from the topic Conditional probability and Bayes' formula splits the probability of a text into next-token probabilities.
A different noise model gives a different loss. Laplace noise has heavier tails and leads to absolute error, which is less sensitive to outliers. Not every loss reads this way: must normalize to a distribution with a constant that does not depend on the parameters. The hinge loss of support vector machines does not.
Likelihood and KL
Divide the log-likelihood by and group equal values. For the coin with 7 heads and 3 tails,
The weights and are the frequencies in the data. In general, the empirical distribution puts mass on each observation, and repeated values add up. Then
Two quantities from information theory name this sum. For distributions and on the same values, the cross-entropy is the average of when comes from :
The entropy is . The Kullback–Leibler divergence is the excess of the first over the second:
It is never negative and equals zero only when . The topic Entropy, cross-entropy and KL in Generative Models explains why.
Write for the model's distribution. Then . The entropy depends only on the data. Hence
The MLE is the model closest to the data in KL, with the data in the first argument. For the coin, the divergence is zero at : the fitted model reproduces the observed frequencies exactly.
As grows, the average log-likelihood is a sample mean, so by the law of large numbers it tends to under the true distribution . The MLE then heads for the that minimizes . If is in the family, that is the true . If not, it is the member closest to in KL. The same holds for densities, with integrals in place of sums.
Fisher information
How precise is ? Compare 7 heads in 10 flips with 70 in 100. Both give . But is after 10 flips and after 100: the peak of the likelihood is much sharper. Sharpness is the curvature of at its peak. The second derivative (the derivative of the derivative) measures it: it is negative at a peak, and the sharper the peak, the larger its size.
The expected curvature per observation is the Fisher information. With the derivative in with held fixed,
The second form, the variance of the score, is equal to the first; we state this without proof. A sample of i.i.d. observations carries .
For one Bernoulli observation, . Its second derivative is . Taking the expectation with gives .
The main result, stated without proof: under regularity conditions, for large ,
The conditions include a correct model, different giving different distributions, a support that does not depend on (the uniform fails this), a smooth log-likelihood, and a true away from the boundary. For the coin the standard error is : after 10 flips and after 100. In practice we plug in for the unknown .
The Cramér–Rao bound adds that no unbiased estimator has variance below . The MLE reaches it as grows, so on large samples no reasonable estimator is more precise. With parameters, becomes a matrix.
Where it breaks
The guarantees above are for large samples and well-behaved models. Outside them, maximum likelihood can fail.
Small samples. After 3 heads in 3 flips, : tails are declared impossible. If the next flip is tails, the model gives it probability and its log-likelihood becomes . An n-gram language model, which predicts a word from counts of the words before it, has the same problem: a word never seen in training gets probability zero, which is why such models are smoothed.
Unbounded likelihoods. Take a mixture of two Gaussians, a density that averages two normal densities. Centre one component on a data point and shrink its towards zero. Its density at that point grows without limit, while the other component keeps the remaining points finite. For five points the log-likelihood is , and for component widths , and . The maximum does not exist, and fitting procedures must bound the variances from below.
Too many parameters. When the number of parameters grows with the data, the MLE can be inconsistent. Overparametrized models without regularization fit the noise.
The wrong model. If no is the true distribution, the MLE converges to the member closest in KL. That member can describe badly what matters, for example the tails.
Behind the first failure is a deeper limit. The MLE returns one point and uses only the data. We know that coins are almost always close to fair, yet three heads are enough to make it declare . The next topic, Bayesian inference, treats the parameter as a random variable with a prior distribution. Bayes' formula turns the prior into a posterior given the data, and the prior's pseudo-counts keep three heads in three flips well away from .
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.