Contents
02Maximum likelihood: what makes the data most likely?
2.1The idea: try candidate explanations
Flip a coin ten times and get seven heads. Which coin was it? Maximum likelihood answers like a detective: consider each candidate explanation and ask how likely the evidence is under it. Figure 1(a) tries four candidate coins. The probability of exactly 7 heads in 10 flips is \binom{10}{7}\theta^7(1-\theta)^3. A coin with \theta=0.3 almost never does this (0.9\%), a fair coin sometimes (11.7\%), a coin with \theta=0.7 most often (26.7\%), and a coin with \theta=0.9 less often again (5.7\%). Letting \theta range over all values in [0,1] gives the curve in Figure 1(b), and its peak, \theta=0.7, is the maximum likelihood estimate.
2.2Likelihood and log-likelihood
Definition 1 (Likelihood). For fixed data D, the likelihood is the probability (or density) of the data, viewed as a function of the parameter:
where the product uses independence. The maximum likelihood estimate is \hat\theta_{\mathrm{MLE}}=\arg\max_\theta L(\theta).
In practice we maximize the log-likelihood
Two reasons. First, the logarithm is strictly increasing, so it does not move the peak. Second, it turns a product of many small numbers, which underflows on a computer and is awkward to differentiate, into a sum, which is easy to differentiate term by term.
One subtle point: the likelihood is not a probability distribution over \theta. It is a function of \theta with the data held fixed, and it does not integrate to one. For the coin, \int_0^1\theta^7(1-\theta)^3\,d\theta=1/1320\approx0.00076.
2.3Two worked examples
Worked example 1 (The coin). With h heads in n independent flips, L(\theta)=\theta^h(1-\theta)^{n-h} and
Setting \ell'(\theta)=0 gives h(1-\theta)=(n-h)\theta, so h=n\theta and
The second derivative \ell''(\theta)=-h/\theta^2-(n-h)/(1-\theta)^2 is negative, so this is a maximum. The estimate is simply the fraction of heads: 7/10=0.7, exactly what intuition says.
Worked example 2 (The Gaussian). For x_i\sim\mathcal N(\mu,\sigma^2),
The derivative in \mu is \frac{1}{\sigma^2}\sum_i(x_i-\mu), which is zero at \hat\mu=\bar x=\frac1n\sum_ix_i, the sample mean. The derivative in \sigma^2 is -\frac{n}{2\sigma^2}+\frac{1}{2\sigma^4}\sum_i(x_i-\mu)^2, which is zero at
This estimate is biased. Since \sum_i(x_i-\bar x)^2=\sum_i(x_i-\mu)^2-n(\bar x-\mu)^2 and \E[(\bar x-\mu)^2]=\sigma^2/n, we get \E\big[\sum_i(x_i-\bar x)^2\big]=n\sigma^2-\sigma^2=(n-1)\sigma^2, so \E[\hat\sigma^2_{\mathrm{MLE}}]=\frac{n-1}{n}\sigma^2. With n=5 it is only 80\% of the true variance on average; dividing by n-1 removes the bias (Bishop, 2006, §1.2.4). The bias vanishes as n grows: maximum likelihood is consistent, not always unbiased.
2.4Maximum likelihood is hiding in your loss functions
Most training objectives in machine learning are negative log-likelihoods.
Regression. Assume the target is the model’s prediction plus Gaussian noise, y_i=f(x_i;w)+\varepsilon_i with \varepsilon_i\sim\mathcal N(0,\sigma^2). Then
and only the second term depends on w. Maximizing the likelihood is minimizing the sum of squared errors: least squares is maximum likelihood under Gaussian noise.
Classification. Assume each label y_i\in\{0,1\} is a coin flip that comes up 1 with probability \hat y_i=P(y_i=1\mid x_i;w), so p(y_i\mid x_i)=\hat y_i^{\,y_i}(1-\hat y_i)^{1-y_i}. Then
which is the binary cross-entropy, or log loss. With K classes the same steps give the categorical cross-entropy -\sum_i\log\hat y_{i,y_i}.
Remark 1. More generally, as n\to\infty the average log-likelihood \frac1n\sum_i\log p(x_i\mid\theta) tends to \E_{p_{\mathrm{data}}}[\log p(x\mid\theta)]=-H(p_{\mathrm{data}})-\mathrm{KL}(p_{\mathrm{data}}\,\|\,p_\theta). The entropy does not depend on \theta, so maximum likelihood chooses the model closest to the data distribution in Kullback–Leibler divergence.
Pause and think. A friend fits a Gaussian to five measurements and reports the maximum likelihood variance 2.0. What is the unbiased estimate?
Show the answer
Multiply by n/(n-1)=5/4: 2.5. Maximum likelihood divides the sum of squares by n=5; the unbiased estimate divides by n-1=4.