Estimation & maximum likelihood

Step 17 of 18 in Foundations · Concept · Medium

Maximum likelihood picks the parameters that make the observed data most probable. Most ML loss functions are negative log-likelihoods in disguise.

The lesson

The likelihood L(θ) is the probability of the data you actually saw, read as a function of the parameters θ. Maximum likelihood picks the θ that makes it largest. For independent data the likelihood is a product, so take the log to get a sum. Maximising the log-likelihood is the same as minimising the negative log-likelihood, the NLL.

Worked example: a coin lands heads 7 times in 10
  1. L(p) = p⁷(1 − p)³
  2. log L = 7·log p + 3·log(1 − p)
  3. Set the derivative to zero: 7/p = 3/(1 − p), so p = 0.7

For a Gaussian the answers are the ones you would guess: μ̂ is the sample mean and σ̂² is the mean squared deviation from it.

This is where loss functions come from. Assume Gaussian noise round a model's prediction and the NLL is squared error plus a constant: least squares is maximum likelihood. Assume yes/no labels and the NLL is cross-entropy.

Common mistake. The MLE of the variance divides by n and comes out slightly too small; the unbiased estimate divides by n − 1 (ddof=1 in NumPy). With very little data MLE also overfits: three heads in three flips gives p = 1.

Check yourself

  1. An ad gets 12 clicks in 80 impressions. What is the MLE of the click probability?

    Show the answer

    0.15. For a Bernoulli the MLE is the observed fraction: 12 / 80 = 0.15.

  2. Minimising squared error is maximum likelihood under the assumption of:

    • uniform noise
    • Gaussian noise
    • no assumption at all
    Show the answer

    Gaussian noise. The log of a Gaussian density is −(y − ŷ)²/2σ² plus a constant, so maximising it minimises squared error.

  3. The data are 2, 4 and 6. What is the MLE of σ²?

    Show the answer

    2.6667. Mean 4. Squared deviations 4, 0, 4. Divide by n = 3: 8/3 ≈ 2.67. (The unbiased version divides by 2 and gives 4.)

Build it

  1. MLE of a Gaussian

    Derive the MLE for μ and σ² by hand, then minimize the negative log-likelihood numerically on 1,000 samples.

    Done when: Numeric optimum matches the closed form to 1e-4.

    Runs in your browser: write the code on the map and the tests mark it.

Resources

Earn this star

On the map, your answers are marked and you log what you built. Finish the step and its star lights.

Open this step on the map