Estimation and Maximum Likelihood

Estimation assumes a probabilistic mechanism generated the data and uses the data to infer its unknown parameters. Under the i.i.d. assumption, maximum likelihood picks the parameter that makes the observed data most probable: the fraction of heads for a coin, the sample mean and the (1/n) sample variance for a Gaussian.

Machine Learning Techniques

PCA and k-means never asked where the data came from. They described its shape directly. This module takes a different stance: assume the data was produced by a random mechanism with some unknown settings, then use the data to work out those settings. That shift, from describing data to modelling how it was generated, is what lets machine learning reason about uncertainty, and it will carry straight over to supervised learning.

A box with a coin inside

Imagine a sealed box with a button. Inside is a coin, not necessarily a fair one: it lands heads with some probability pp that we do not know. Each press flips the coin and shows us the result, 1 for heads and 0 for tails. After ten presses we see

1, 1, 0, 1, 0, 1, 1, 1, 0, 1.1,\ 1,\ 0,\ 1,\ 0,\ 1,\ 1,\ 1,\ 0,\ 1 .

Two things are now separate.

  • What we observe: the ten outcomes.
  • What we assume: that a coin with some bias pp produced them. This is a model, a story we choose in order to explain the data. It may be wrong; it is useful if it is roughly right.

The unknown pp is called a parameter. Estimation is the job of inferring the parameter from the observations. Notice the compression again: a thousand coin flips are explained by a single number. Once we know pp, we could build our own box and generate as much data like this as we liked; the data held no information beyond pp.

Formally, each outcome is a random variable XX with

P(X=1)=p,P(X=0)=1−p,P(X = 1) = p, \qquad P(X = 0) = 1 - p,

a Bernoulli distribution.

Any guess could be right

Seven of the ten flips are heads, so p=0.7p = 0.7 feels like the natural guess. But consider other values.

  • Could p=0.5p = 0.5 have produced this data? Certainly: a fair coin gives 7 heads in 10 flips about 12% of the time.
  • Could p=0.05p = 0.05? Yes, though it would take extraordinary luck.
  • Could p=0p = 0 or p=1p = 1? No. A coin that never lands heads cannot produce a 1, and one that always does cannot produce a 0.

So every value strictly between 0 and 1 remains possible. Once probability enters, we can never be certain of the true parameter. What we need is a principled way to prefer one guess over another, and a principled reason for the intuitive answer 0.7.

The i.i.d. assumption

Two assumptions sit quietly behind the intuitive guess.

  • Independent: knowing the outcome of one flip tells us nothing about another. P(Xi=x∣Xj=x′)=P(Xi=x)P(X_i = x \mid X_j = x') = P(X_i = x) for i≠ji \ne j.
  • Identically distributed: every flip uses the same coin, so every XiX_i has the same distribution, P(Xi=1)=pP(X_i = 1) = p for all ii.

Together they are called i.i.d.. Two different coins flipped once each would give independent but not identically distributed outcomes.

The likelihood

Under the i.i.d. assumption, the probability of the whole observed sequence factorises into a product. For a coin with bias pp,

L(p)=P(x1,…,xn;p)=∏i=1npxi(1−p)1−xi=pk(1−p)n−k,L(p) = P(x_1, \dots, x_n ; p) = \prod_{i=1}^{n} p^{x_i}(1 - p)^{1 - x_i} = p^{k}(1 - p)^{n - k},

where kk is the number of heads. Read as a function of the parameter, with the data held fixed, this is the likelihood. It measures how probable our actual observations would be if the parameter were pp. For our data (k=7k = 7, n=10n = 10):

ppL(p)=p7(1−p)3L(p) = p^7(1 - p)^3
0.50.00098
0.60.00179
0.70.00222
0.80.00168
0.90.00048

The value p=0.7p = 0.7 makes the data more probable than any of its neighbours.

Maximum likelihood estimation

The maximum likelihood estimate (MLE) is the parameter that makes the observed data most probable:

p^ML=arg⁡max⁡p  L(p).\hat p_{\text{ML}} = \arg\max_{p}\; L(p) .

Products are awkward to differentiate, and the logarithm is increasing, so maximise the log-likelihood instead; it has the same maximiser:

ℓ(p)=ln⁡L(p)=kln⁡p+(n−k)ln⁡(1−p).\ell(p) = \ln L(p) = k \ln p + (n - k)\ln(1 - p) .

Set the derivative to zero:

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

The intuitive answer, the fraction of heads, is exactly the maximum likelihood estimate. Now we know why it is a sensible answer, and we have a recipe that works far beyond coins.

The likelihood p to the 7 times 1 minus p to the 3 plotted for p from 0 to 1, a single hump peaking at p equals 0.7, with zero at both ends
The likelihood of 7 heads in 10 flips as a function of the coin's bias. It is zero at p = 0 and p = 1 (impossible) and peaks at the maximum likelihood estimate, p = 0.7.

The Gaussian case

Many measurements, such as heights, sensor readings and exam marks, are modelled as draws from a Gaussian (normal) distribution with unknown mean μ\mu and variance σ2\sigma^2:

f(x;μ,σ2)=12πσ2exp⁡ ⁣(−(x−μ)22σ2).f(x; \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\Big(-\frac{(x - \mu)^2}{2\sigma^2}\Big).

For i.i.d. observations x1,…,xnx_1, \dots, x_n, the log-likelihood is

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

Setting the partial derivatives to zero gives

μ^ML=1n∑i=1nxi,σ^ML2=1n∑i=1n(xi−μ^ML)2.\hat\mu_{\text{ML}} = \frac{1}{n}\sum_{i=1}^{n} x_i, \qquad \hat\sigma^2_{\text{ML}} = \frac{1}{n}\sum_{i=1}^{n} (x_i - \hat\mu_{\text{ML}})^2 .

The sample mean and the sample variance (with 1n\tfrac1n, not the 1n−1\tfrac{1}{n-1} of many statistics courses). Notice that maximising the likelihood over μ\mu is the same as minimising the sum of squared distances ∑i(xi−μ)2\sum_i (x_i - \mu)^2: the same quantity that k-means minimises within each cluster. This link between Gaussians and squared error will appear again in regression.

How good is the estimate?

The MLE is itself random: a different set of flips gives a different p^\hat p. Two useful properties hold for the coin.

  • Unbiased: on average over repeated experiments, E[p^]=p\mathbb{E}[\hat p] = p.
  • Consistent: its variance is p(1−p)/np(1 - p)/n, which shrinks as nn grows, so with enough data the estimate settles on the truth.

The Gaussian variance estimate σ^ML2\hat\sigma^2_{\text{ML}} is slightly biased: its expected value is n−1nσ2\tfrac{n - 1}{n}\sigma^2, because it measures spread around the sample mean, which sits a little closer to the data than the true mean does. Dividing by n−1n - 1 instead removes the bias; for large nn the difference is negligible.

The weakness of maximum likelihood shows up with little data. Flip a coin three times, see three heads, and the MLE is p^=1\hat p = 1: it declares tails impossible. The next chapter fixes this by adding what we believed before seeing the data.

MediumMLE

A die-like spinner lands on one of three colours with probabilities θ₁, θ₂, θ₃. In 20 spins you see red 9 times, green 6 times and blue 5 times. What is the maximum likelihood estimate?

EasyMLE

Why do we maximise the log-likelihood instead of the likelihood?

MediumMLEGaussian

Show that the maximum likelihood estimate of a Gaussian mean does not depend on the variance.