Phase 1 · Math foundations · Topic 1.13

Maximum likelihood estimation

Maximum likelihood estimation chooses the parameter values that make the data you actually observed as probable as possible.

Not started

Step 1 Understand it

The problem it solves. A has unknown : the of a coin, the of a , the weights of a . You need a principled rule for choosing them from data. is that rule, and it explains where the standard come from: and are both maximum likelihood in disguise.

New words in this topic (tap one to see what it means)

Watch it happen

AnimationMaximum likelihood: pick what makes your data most probable
Read the animation as text
  1. 10 coin flips: 7 heads and 3 tails. Which value of p, the chance of heads, explains this data best?
  2. Try p = 0.3. The chance of exactly this sequence is p⁷(1 − p)³ = 0.00008: tiny. 7 heads would be surprising.
  3. p = 0.5: 0.00098. Better.
  4. p = 0.9: 0.00048. Worse again: with p = 0.9 you would expect more than 7 heads.
  5. Try every p and draw the likelihood curve. It peaks at p = 0.7, the fraction of heads you saw. That is the maximum likelihood estimate.

The idea

Maximum likelihood: choose the model parameters that make the data you actually saw most probable.

How it works, step by step

  1. Write the probability of the data as a function of the parameter: the . For 7 heads and 3 tails, L(p) = p⁷(1 − p)³.
  2. Try some values: L(0.5) ≈ 0.00098, L(0.7) ≈ 0.00222, L(0.9) ≈ 0.00048. The data is most probable at p = 0.7.
  3. Take the log, which turns products into sums and keeps the same peak: log L = 7 log p + 3 log(1 − p).
  4. Set the to zero: 7/p − 3/(1 − p) = 0, so p = 7/10. The estimate is just the fraction of heads.
  5. In you minimise the negative log-likelihood instead of maximising the likelihood. That is your loss.
  6. For a Gaussian with fixed spread, maximising the likelihood is the same as minimising squared error, so the best mean is the mean. For , it is the same as minimising cross-entropy.

Picture it

xychart-beta
  title "Likelihood of 7 heads in 10 flips, for each p"
  x-axis "p (chance of heads)" [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
  y-axis "likelihood × 1000" 0 --> 2.5
  bar [0.0, 0.007, 0.075, 0.354, 0.977, 1.792, 2.224, 1.678, 0.478]
Maximum likelihood picks the p that makes the data you saw most probable: the tallest bar, at p = 0.7.

Analogy

A detective picks the story that best explains all the clues.

Where the analogy breaks

A detective also uses common sense. MLE uses only the data, so with little data it can be overconfident: 3 heads in 3 flips gives 'this coin always lands heads'.

Worked example

7 heads in 10 flips. The likelihood p⁷(1 − p)³ is highest at p = 0.7. Two facts to remember: minimising cross-entropy loss is MLE for classification, and minimising squared error is MLE when the is Gaussian.

python
p = np.linspace(0.01, 0.99, 99)
lik = p**7 * (1 - p)**3
p[lik.argmax()]   # ≈ 0.7

Must-know points

  • Likelihood L(θ) = P(data | θ), seen as a function of the parameters θ.
  • Maximise log L instead: products become sums, and tiny numbers don't to 0.
  • The negative log-likelihood is the loss you minimise.
  • Squared error = Gaussian maximum likelihood. Cross-entropy = maximum likelihood for categories.
  • For a coin, the estimate is the fraction of heads; for a Gaussian mean, it is the sample average.
  • adds a and maximises likelihood × prior. is MAP with a Gaussian prior on the weights.

Common mistakes

  • Reading the likelihood as "the probability that the parameter is right". It is the probability of the data, given the parameter.
  • Multiplying thousands of small probabilities directly. They underflow to 0; add their logs instead.
  • Trusting maximum likelihood on tiny data: 3 heads out of 3 flips gives p = 1.

Step 2 Go further (optional)

Optional. The lesson above is written to be enough on its own. If you want a second explanation, start with the one marked Worth it.

Step 3 Practise

Try each exercise yourself first: on paper, out loud or in code. Then open the answer and compare it with yours.

  1. ExplainSay it in plain words, with no notes.

    Explain, in plain words, why minimising cross-entropy is the same as maximum likelihood.

    Show answer

    Maximum likelihood means choosing the model that makes the as probable as possible: the probability the model gives to the right of every example, all multiplied together.

    Multiplying thousands of small numbers is awkward, so take the log, which turns the product into a sum, and maximising a sum of logs is the same as minimising minus that sum. Minus the average log-probability of the right label is exactly the cross-entropy loss. So when you a classifier by minimising cross-entropy, you are doing maximum likelihood: pushing up the probability the model gives to the right answers.

  2. DeriveWrite the maths, or the steps, from memory.

    Derive the MLE of p for coin flips: write the log-likelihood, differentiate, set it to 0. Then do the same for the mean of a Gaussian.

    Show answer

    Coin: h heads and t tails, with P(heads) = p.
    Likelihood: L(p) = pʰ·(1 − p)ᵗ.
    Log-likelihood: ℓ(p) = h·log p + t·log(1 − p).
    Derivative: h/p − t/(1 − p). Set it to 0: h(1 − p) = t·p, so h = p(h + t), and p = h / (h + t).
    The MLE is simply the fraction of heads: 7 heads in 10 flips gives 0.7.

    Gaussian mean, with the σ² fixed:
    ℓ(μ) = −Σ(xᵢ − μ)² / (2σ²) + a constant.
    Derivative: Σ(xᵢ − μ) / σ². Set it to 0: Σxᵢ = n·μ, so μ = (1/n)·Σxᵢ, the sample mean.
    Notice that maximising this log-likelihood is the same as minimising the squared error Σ(xᵢ − μ)²: that is why MSE goes with Gaussian noise.

  3. BuildCode it from scratch in NumPy, with no ML library.

    Fit a Gaussian to real data by maximising the log-likelihood with your own . Compare with the formula answer.

    Show answer

    Minimise the negative log-likelihood of a Gaussian with gradient descent. Learn log σ instead of σ, so σ can never go negative.

    On Iris sepal lengths it converges to μ ≈ 5.843 and σ ≈ 0.825, the same as the formula answers: the sample mean and the that divides by n (np.std with its default ddof = 0).

    python
    import numpy as np
    from sklearn.datasets import load_iris      # only to load the data
    
    x = load_iris().data[:, 0]                   # sepal length, 150 values
    mu, log_sigma, lr = 0.0, 0.0, 0.05
    for _ in range(5000):
        sigma = np.exp(log_sigma)
        # gradients of the mean negative log-likelihood
        grad_mu = -np.mean(x - mu) / sigma**2
        grad_log_sigma = 1 - np.mean((x - mu) ** 2) / sigma**2
        mu -= lr * grad_mu
        log_sigma -= lr * grad_log_sigma
    
    print(round(mu, 3), round(np.exp(log_sigma), 3))   # 5.843 0.825
    print(round(x.mean(), 3), round(x.std(), 3))       # the formula answers
  4. BreakShow when it fails, and why.

    Fit the coin with only 3 flips, all heads. What does MLE say? Now pretend you also saw 1 extra head and 1 extra tail, and compare. That is MAP with a simple prior.

    Show answer

    3 heads out of 3: MLE says p = 3/3 = 1. The model is now certain that tails is impossible, after only three flips. That is overconfidence from too little data: MLE fits the data you saw exactly, however little of it there is.

    Add one imaginary head and one imaginary tail: p = (3 + 1) / (3 + 2) = 0.8. Much more sensible. Those extra counts are a prior belief that coins are usually fairly balanced, and using them is MAP estimation (with a Beta(2, 2) prior), also called Laplace .

    With lots of data the prior hardly matters: 700 heads in 1,000 flips gives 0.700 by MLE and 0.6996 with the extra counts. It matters most exactly when data is scarce.

Step 4 Prove it

Check you understood

You saw 2 heads in 8 flips. What is the maximum likelihood estimate of p?

2/8 = 0.25.

Why maximise the log-likelihood instead of the likelihood?

The log has the same peak, turns products into sums (easier to differentiate), and avoids numbers too small for a computer.

Which loss is maximum likelihood for with Gaussian noise?

Mean squared error.

How does MAP differ from maximum likelihood?

MAP also multiplies by a prior over the parameters, which pulls estimates towards sensible values when data is scarce.

Tick each check when you can do it without notes

A topic is finished when you pass all four checks, not when you finish reading. Be strict: if you needed your notes, it is not ticked yet.

  • Explain Say it in plain words, with no notes.
  • Derive Write the maths, or the steps, from memory.
  • Build Code it from scratch in NumPy, with no ML library.
  • Break Show when it fails, and why.

0 of 4 checks ticked.

Saved in this browser. Back it up or move it to another device.