Skip to content
BIASED

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 pp. 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:

L(p)=p7(1−p)3.L(p) = p^7 (1 - p)^3 .

In general, let x1,…,xnx_1, \dots, x_n be an i.i.d. sample from a pmf or density pθp_\theta. The likelihood is

L(θ)=∏i=1npθ(xi).L(\theta) = \prod_{i=1}^n p_\theta(x_i) .

It is the joint probability of the data, read the other way round: the data are fixed and θ\theta varies.

LL is not a distribution over θ\theta. For the coin, ∫01p7(1−p)3 dp=1/1320\int_0^1 p^7(1-p)^3\,dp = 1/1320, not 11. Its size alone says little: L(0.7)≈0.0022L(0.7) \approx 0.0022 is small only because any particular sequence of ten flips is unlikely. Ratios carry the meaning. L(0.5)/L(0.7)≈0.44L(0.5)/L(0.7) \approx 0.44, so a fair coin explains the data about half as well as p=0.7p = 0.7. To talk about the probability of pp itself we need a prior, the subject of the topic Bayesian inference.

The maximum likelihood estimate (MLE) is the value with the largest likelihood:

θ^=arg⁡max⁡θL(θ).\hat\theta = \arg\max_\theta L(\theta) .

The logarithm and the derivative

A product of many numbers below 1 is awkward. A thousand flips of a fair coin give L=0.51000≈10−301L = 0.5^{1000} \approx 10^{-301}, at the edge of what a computer's floating-point numbers can hold. The logarithm turns the product into a sum, the log-likelihood:

ℓ(θ)=log⁡L(θ)=∑i=1nlog⁡pθ(xi).\ell(\theta) = \log L(\theta) = \sum_{i=1}^n \log p_\theta(x_i) .

The logarithm is increasing, so ℓ\ell and LL peak at the same θ\theta. Nothing is lost.

To find the peak we need derivatives. A one-line reminder: the derivative f′(x)f'(x) is the slope of ff, and at a maximum inside an interval a smooth function has slope zero. We also need (log⁡x)′=1/x(\log x)' = 1/x and (log⁡(1−x))′=−1/(1−x)(\log(1 - x))' = -1/(1 - x). For more, see the topic Derivatives and Taylor series in Mathematics for ML.

For the coin, ℓ(p)=7log⁡p+3log⁡(1−p)\ell(p) = 7 \log p + 3 \log(1 - p). Setting the derivative to zero gives

ℓ′(p)=7p−31−p=0⟹7(1−p)=3p⟹p^=710.\ell'(p) = \frac{7}{p} - \frac{3}{1 - p} = 0 \quad\Longrightarrow\quad 7(1 - p) = 3p \quad\Longrightarrow\quad \hat p = \frac{7}{10} .

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 ℓ(p)→−∞\ell(p) \to -\infty as p→0p \to 0 and as p→1p \to 1, so the only zero is the maximum.

With kk heads in nn flips the same steps give p^=k/n\hat p = k/n, 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 xix_i be the numbers of requests to a server in nn separate minutes, modelled as Pois⁡(λ)\operatorname{Pois}(\lambda) with pλ(x)=λxe−λ/x!p_\lambda(x) = \lambda^x e^{-\lambda}/x!. Then

ℓ(λ)=∑i=1n(xilog⁡λ−λ−log⁡xi!),ℓ′(λ)=1λ∑i=1nxi−n=0⟹λ^=xˉ.\ell(\lambda) = \sum_{i=1}^n \big(x_i \log\lambda - \lambda - \log x_i!\big), \qquad \ell'(\lambda) = \frac{1}{\lambda}\sum_{i=1}^n x_i - n = 0 \quad\Longrightarrow\quad \hat\lambda = \bar x .

Normal. N(μ,σ2)\mathcal N(\mu, \sigma^2) has two parameters. We set the derivative in each one to zero while holding the other fixed. The log-density is log⁡p(x)=−(x−μ)22σ2−12log⁡(2πσ2)\log p(x) = -\frac{(x - \mu)^2}{2\sigma^2} - \frac12 \log(2\pi\sigma^2), and the two equations give

μ^=xˉ,σ^2=1n∑i=1n(xi−xˉ)2.\hat\mu = \bar x, \qquad \hat\sigma^2 = \frac1n \sum_{i=1}^n (x_i - \bar x)^2 .

The divisor is nn, not n−1n - 1. This is the biased variance estimate from the topic Estimators: bias and variance: E[σ^2]=n−1nσ2\mathbb E[\hat\sigma^2] = \frac{n-1}{n}\sigma^2. Maximum likelihood does not promise unbiasedness. For Exp⁡(λ)\operatorname{Exp}(\lambda) it gives λ^=1/xˉ\hat\lambda = 1/\bar x, which is biased upward for small nn.

Uniform. For U(0,θ)U(0, \theta) the density is 1/θ1/\theta on [0,θ][0, \theta] and zero outside. If some xi>θx_i > \theta, that point has density zero and L(θ)=0L(\theta) = 0. Otherwise L(θ)=θ−nL(\theta) = \theta^{-n}, which decreases in θ\theta. So the maximum sits at the smallest allowed value, θ^=max⁡ixi\hat\theta = \max_i x_i. The derivative is not zero there: the peak is on the edge of the allowed region. Setting ℓ′=0\ell' = 0 is a tool; the definition is the arg⁡max⁡\arg\max.

One more property is free. The MLE is invariant: the MLE of g(θ)g(\theta) is g(θ^)g(\hat\theta). So the MLE of σ\sigma is σ^2\sqrt{\hat\sigma^2}, and the MLE of the odds p/(1−p)p/(1 - p) is p^/(1−p^)\hat p/(1 - \hat p).

Loss functions are likelihoods

Most losses in machine learning are negative log-likelihoods in disguise.

Regression. A model fw(x)f_w(x) with weights ww predicts a number yy. Assume y=fw(x)+εy = f_w(x) + \varepsilon with noise ε∼N(0,σ2)\varepsilon \sim \mathcal N(0, \sigma^2), independent across examples. Then yy given xx is N(fw(x),σ2)\mathcal N(f_w(x), \sigma^2), with density

pw(y∣x)=1σ2πexp⁡(−(y−fw(x))22σ2).p_w(y \mid x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\Big(-\frac{(y - f_w(x))^2}{2\sigma^2}\Big) .

The negative log-likelihood of a training set (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) is

−ℓ(w)=∑i=1n(yi−fw(xi))22σ2+nlog⁡(σ2π).-\ell(w) = \sum_{i=1}^n \frac{(y_i - f_w(x_i))^2}{2\sigma^2} + n \log\big(\sigma\sqrt{2\pi}\big) .

The second term does not depend on ww. 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 σ2\sigma^2 does not change the fitted ww, but it matters as soon as the model must report its own uncertainty.

Classification. A classifier outputs a categorical distribution qw(k∣x)q_w(k \mid x) over the classes kk. It gives the observed label yiy_i the probability qw(yi∣xi)q_w(y_i \mid x_i), so

−ℓ(w)=−∑i=1nlog⁡qw(yi∣xi).-\ell(w) = -\sum_{i=1}^n \log q_w(y_i \mid x_i) .

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: e−losse^{-\text{loss}} 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 nn and group equal values. For the coin with 7 heads and 3 tails,

1nℓ(p)=0.7log⁡p+0.3log⁡(1−p).\frac1n \ell(p) = 0.7 \log p + 0.3 \log(1 - p) .

The weights 0.70.7 and 0.30.3 are the frequencies in the data. In general, the empirical distribution P^n\hat P_n puts mass 1/n1/n on each observation, and repeated values add up. Then

1nℓ(θ)=∑xP^n(x)log⁡pθ(x).\frac1n \ell(\theta) = \sum_x \hat P_n(x) \log p_\theta(x) .

Two quantities from information theory name this sum. For distributions PP and QQ on the same values, the cross-entropy is the average of −log⁡Q(x)-\log Q(x) when xx comes from PP:

H(P,Q)=−∑xP(x)log⁡Q(x).H(P, Q) = -\sum_x P(x) \log Q(x) .

The entropy is H(P)=H(P,P)H(P) = H(P, P). The Kullback–Leibler divergence is the excess of the first over the second:

DKL(P ∥ Q)=H(P,Q)−H(P)=∑xP(x)log⁡P(x)Q(x).D_{\mathrm{KL}}(P \,\|\, Q) = H(P, Q) - H(P) = \sum_x P(x) \log\frac{P(x)}{Q(x)} .

It is never negative and equals zero only when Q=PQ = P. The topic Entropy, cross-entropy and KL in Generative Models explains why.

Write PθP_\theta for the model's distribution. Then 1nℓ(θ)=−H(P^n,Pθ)=−DKL(P^n ∥ Pθ)−H(P^n)\frac1n \ell(\theta) = -H(\hat P_n, P_\theta) = -D_{\mathrm{KL}}(\hat P_n \,\|\, P_\theta) - H(\hat P_n). The entropy H(P^n)H(\hat P_n) depends only on the data. Hence

θ^=arg⁡min⁡θDKL(P^n ∥ Pθ).\hat\theta = \arg\min_\theta D_{\mathrm{KL}}(\hat P_n \,\|\, P_\theta) .

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 p=0.7p = 0.7: the fitted model reproduces the observed frequencies exactly.

As nn grows, the average log-likelihood is a sample mean, so by the law of large numbers it tends to E[log⁡pθ(X)]\mathbb E[\log p_\theta(X)] under the true distribution PP. The MLE then heads for the θ\theta that minimizes DKL(P ∥ Pθ)D_{\mathrm{KL}}(P \,\|\, P_\theta). If PP is in the family, that is the true θ\theta. If not, it is the member closest to PP in KL. The same holds for densities, with integrals in place of sums.

Fisher information

How precise is θ^\hat\theta? Compare 7 heads in 10 flips with 70 in 100. Both give p^=0.7\hat p = 0.7. But L(0.5)/L(0.7)L(0.5)/L(0.7) is 0.440.44 after 10 flips and 0.000270.00027 after 100: the peak of the likelihood is much sharper. Sharpness is the curvature of ℓ\ell 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 ∂/∂θ\partial/\partial\theta the derivative in θ\theta with xx held fixed,

I(θ)=−Eθ[∂2∂θ2log⁡pθ(X)]=Var⁡θ(∂∂θlog⁡pθ(X)).I(\theta) = -\mathbb E_\theta\Big[\frac{\partial^2}{\partial\theta^2} \log p_\theta(X)\Big] = \operatorname{Var}_\theta\Big(\frac{\partial}{\partial\theta} \log p_\theta(X)\Big) .

The second form, the variance of the score, is equal to the first; we state this without proof. A sample of nn i.i.d. observations carries nI(θ)n I(\theta).

For one Bernoulli observation, log⁡pp(x)=xlog⁡p+(1−x)log⁡(1−p)\log p_p(x) = x \log p + (1 - x)\log(1 - p). Its second derivative is −x/p2−(1−x)/(1−p)2-x/p^2 - (1 - x)/(1 - p)^2. Taking the expectation with E[X]=p\mathbb E[X] = p gives I(p)=1/p+1/(1−p)=1/(p(1−p))I(p) = 1/p + 1/(1 - p) = 1/(p(1 - p)).

The main result, stated without proof: under regularity conditions, for large nn,

θ^≈N(θ,1nI(θ)).\hat\theta \approx \mathcal N\Big(\theta, \frac{1}{n I(\theta)}\Big) .

The conditions include a correct model, different θ\theta giving different distributions, a support that does not depend on θ\theta (the uniform fails this), a smooth log-likelihood, and a true θ\theta away from the boundary. For the coin the standard error is p(1−p)/n\sqrt{p(1 - p)/n}: 0.1450.145 after 10 flips and 0.0460.046 after 100. In practice we plug in p^\hat p for the unknown pp.

The Cramér–Rao bound adds that no unbiased estimator has variance below 1/(nI(θ))1/(n I(\theta)). The MLE reaches it as nn grows, so on large samples no reasonable estimator is more precise. With dd parameters, I(θ)I(\theta) becomes a d×dd \times d 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, p^=1\hat p = 1: tails are declared impossible. If the next flip is tails, the model gives it probability 00 and its log-likelihood becomes −∞-\infty. 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 σ\sigma 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 −7.64-7.64, −3.06-3.06 and 3.843.84 for component widths 10−110^{-1}, 10−310^{-3} and 10−610^{-6}. 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 pθp_\theta 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 p=1p = 1. 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 p=1p = 1.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.