Search

Search papers and lessons

Maximum Likelihood Estimation

Level 3 · 9 min read

Maximum likelihood estimation picks the parameter values that make the observed data most probable, found by maximising the log-likelihood.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Inference
  • Sign in to see your progress

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 pp that the coin lands heads. Which value of pp makes "seven heads in ten flips" most believable? Intuitively it is p=0.7p = 0.7. A fair coin with p=0.5p = 0.5 makes that outcome fairly common, but p=0.1p = 0.1 makes it almost impossible. Maximum likelihood formalises this: it scores each candidate pp 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 θ\theta, and some observed data. Each θ\theta gives the data a probability. The MLE is the θ\theta 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 x1,…,xnx_1, \dots, x_n be observations, and let f(x;θ)f(x; \theta) be the probability density (or mass) function of one observation under parameter θ\theta. Assume the observations are independent and identically distributed. The probability density of the whole dataset is then a product:

Definition: Likelihood and log-likelihood

The likelihood of θ\theta given the data is

L(θ)=∏i=1nf(xi;θ).L(\theta) = \prod_{i=1}^{n} f(x_i; \theta).

The log-likelihood is

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

The maximum likelihood estimate is θ^=arg⁡max⁡θℓ(θ)\hat{\theta} = \arg\max_{\theta} \ell(\theta).

Two points matter here. The likelihood is a function of θ\theta, with the data held fixed. It is not a probability distribution over θ\theta, and its values do not need to sum to 1. And the log is used because it is monotonic: the θ\theta that maximises LL also maximises log⁡L\log L. The log turns a product into a sum, which is easier to differentiate. It also stops the product from underflowing to zero when nn 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 ℓ\ell 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 ℓ\ell do the job.

Worked derivation: the coin

For nn flips with kk heads, each flip has probability pp of heads, so

ℓ(p)=klog⁡p+(n−k)log⁡(1−p).\ell(p) = k \log p + (n - k) \log (1 - p).

Differentiate and set to zero:

dℓdp=kp−n−k1−p=0⟹p^=kn.\frac{d\ell}{dp} = \frac{k}{p} - \frac{n-k}{1-p} = 0 \quad\Longrightarrow\quad \hat{p} = \frac{k}{n}.

The MLE of a coin's heads probability is the observed fraction of heads. The second derivative −k/p2−(n−k)/(1−p)2-k/p^2 - (n-k)/(1-p)^2 is negative, so this is a maximum.

00.20.40.60.8100.20.40.60.81p (probability of heads)relative likelihood
  • likelihood of p, scaled so the peak is 1
Seven heads in ten flips. The likelihood peaks at p = 0.7, the observed fraction. Values far from it make the data much less probable.

Worked derivation: a Gaussian

For observations from N(μ,σ2)\mathcal{N}(\mu, \sigma^2), the log-likelihood is

ℓ(μ,σ2)=−n2log⁡(2πσ2)−12σ2∑i=1n(xi−μ)2.\ell(\mu, \sigma^2) = -\frac{n}{2} \log(2\pi\sigma^2) - \frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i - \mu)^2.

Setting the derivative with respect to μ\mu to zero gives ∑i(xi−μ)=0\sum_i (x_i - \mu) = 0, so

μ^=1n∑i=1nxi.\hat{\mu} = \frac{1}{n} \sum_{i=1}^{n} x_i.

Setting the derivative with respect to σ2\sigma^2 to zero, and writing S=∑i(xi−μ^)2S = \sum_i (x_i - \hat{\mu})^2, gives −n2σ2+S2σ4=0-\frac{n}{2\sigma^2} + \frac{S}{2\sigma^4} = 0, so

σ^2=1n∑i=1n(xi−μ^)2.\hat{\sigma}^2 = \frac{1}{n} \sum_{i=1}^{n} (x_i - \hat{\mu})^2.

The MLE of the mean is the sample average. The MLE of the variance divides by nn, not n−1n - 1. That makes it slightly biased low; the unbiased sample variance divides by n−1n - 1. For large nn 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 yi=w⊤xi+εiy_i = w^\top x_i + \varepsilon_i with Gaussian noise εi∼N(0,σ2)\varepsilon_i \sim \mathcal{N}(0, \sigma^2), then the negative log-likelihood is a constant plus 12σ2∑i(yi−w⊤xi)2\frac{1}{2\sigma^2} \sum_i (y_i - w^\top x_i)^2. Maximising the likelihood over ww 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

Example
A coin, by hand

Ten flips give seven heads. The likelihood is L(p)=p7(1−p)3L(p) = p^7 (1 - p)^3.

  1. Log-likelihood: ℓ(p)=7log⁡p+3log⁡(1−p)\ell(p) = 7 \log p + 3 \log (1 - p).
  2. Derivative: 7p−31−p\dfrac{7}{p} - \dfrac{3}{1 - p}.
  3. Set to zero: 7(1−p)=3p7(1 - p) = 3p, so 7=10p7 = 10p.
  4. Solution: p^=0.7\hat{p} = 0.7.
  5. Compare with other values. L(0.5)=0.510≈0.000977L(0.5) = 0.5^{10} \approx 0.000977. L(0.7)=0.77×0.33≈0.002224L(0.7) = 0.7^7 \times 0.3^3 \approx 0.002224. So p=0.7p = 0.7 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.

Example
Fitting a Gaussian to four measurements

Four measurements are 1,3,4,81, 3, 4, 8. Assume they come from a Gaussian with unknown mean and variance.

  1. Sample mean, the MLE of μ\mu: (1+3+4+8)/4=4(1 + 3 + 4 + 8) / 4 = 4.
  2. Deviations from 4: −3,−1,0,4-3, -1, 0, 4.
  3. Squares: 9,1,0,169, 1, 0, 16, with sum S=26S = 26.
  4. MLE of the variance: σ^2=26/4=6.5\hat{\sigma}^2 = 26 / 4 = 6.5. The standard deviation estimate is 6.5≈2.55\sqrt{6.5} \approx 2.55.
  5. For comparison, the unbiased sample variance is 26/3≈8.6726 / 3 \approx 8.67.

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:

SituationProblemBetter choice
Very small samplesThe estimate can be far off and high-variance, and MLE of variance is biasedUnbiased estimators, or a Bayesian estimate with a prior
Flexible models with many parametersThe likelihood can be maximised by memorising the data, which is overfittingRegularisation, which is maximum a posteriori (MAP) estimation with a prior
Wrong model familyThe maximum is the best fit within the family, which may still be poorCheck the model with residuals, or change the family
Some mixture models and degenerate casesThe likelihood can be unbounded. For example, a Gaussian centred exactly on one data point with variance going to 0 has density going to infinityConstrain the variance from below, or use a prior
Parameters that cannot be told apartMany parameter values give the same likelihood, so there is no unique maximumReparameterise, 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 p^=k/n=4/10=0.4\hat{p} = k / n = 4 / 10 = 0.4. The order does not matter because the likelihood is a product of identical factors pp for each head and (1−p)(1 - p) 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 σ^2=1n∑(xi−μ^)2\hat{\sigma}^2 = \frac{1}{n}\sum(x_i - \hat{\mu})^2. A colleague says this is wrong and you should divide by n−1n - 1. Who is right, and what is the difference?Show answer

Neither is wrong; they are different estimators. Dividing by nn gives the maximum likelihood estimate, which is the value that maximises the likelihood. Dividing by n−1n - 1 gives the unbiased sample variance, whose average over repeated samples equals the true variance. The MLE is biased low by a factor of (n−1)/n(n-1)/n, which is 0.9 for n=10n = 10. For large nn 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 −log⁡-\log of the probability of the correct class. For 0.9 it is −log⁡0.9≈0.105-\log 0.9 \approx 0.105, and for 0.1 it is −log⁡0.1≈2.303-\log 0.1 \approx 2.303. 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