Stop 04 · Probability & StatisticsTopics · 8/9

Maximum Likelihood Estimation

Part of the Machine Learning Roadmap

In short

Why do we minimise squared error for regression and cross-entropy for classification? Both come from one idea: choose the parameters that make the data you actually saw as likely as possible. That idea is maximum likelihood.

Maximum likelihood estimation picks the model parameters that make the observed data most probable. Many familiar losses come from it: minimising mean squared error and cross-entropy are both MLE under certain assumptions.

The jaadu watches a coin spin in the air at a colourful village mela stall, holding up a row of differently weighted coins to compare.

Think of it like this

Guessing the coin at the mela

A stall at the mela flips a coin 10 times and gets 7 heads. Is it a fair coin? You try different guesses for the chance of heads: 0.5 makes 7 heads fairly unlikely, 0.9 makes it unlikely too, and 0.7 makes it the most likely. You just did maximum likelihood estimation: the best guess is the one that best explains what happened.

Watch it happen

Likelihood of 7 heads in 10 flips for different guesses of p

p=0.3
0
p=0.5
1
p=0.7
2
p=0.9
3

1/5We saw 7 heads and 3 tails. Try four values for p, the chance of heads.

The big ideas

What you need to know

Likelihood flips the question

Probability asks: given p, how likely is the data? Likelihood asks: given the data, how good is each p?

Use the log

Multiplying many small probabilities underflows to zero. Taking logs turns products into sums, and the best p stays the same.

Often it's just the average

For a coin, the MLE is heads divided by flips. For a normal distribution, it's the sample mean and variance.

Losses come from MLE

Assume Gaussian noise and MLE becomes minimising squared error. Assume Bernoulli labels and it becomes minimising log loss.

How it works

MLE in four steps

  1. 1

    Pick a model

    Choose a distribution with parameters, like a coin with chance p.

  2. 2

    Write the likelihood

    Multiply the probability of each observation under that model.

  3. 3

    Take the log

    Sum the log-probabilities instead. Flip the sign to get a loss to minimise.

  4. 4

    Optimise

    Solve with calculus, or let gradient descent find the best parameters.

See it in code

Walk through the code

python
1import numpy as np2 3flips = np.array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1])   # 7 heads4ps = np.linspace(0.01, 0.99, 99)5 6def log_likelihood(p, data):7    return np.sum(data * np.log(p) + (1 - data) * np.log(1 - p))8 9scores = [log_likelihood(p, flips) for p in ps]10best_p = ps[np.argmax(scores)]11 12print(round(best_p, 2))      # 0.713print(flips.mean())          # 0.7, the closed-form MLE14 15nll = -log_likelihood(best_p, flips) / len(flips)16print(round(nll, 3))         # 0.611, this is binary cross-entropy

1/5The observed flips, and 99 candidate values of p to test.

A brute-force search over p finds the same answer as the formula: heads divided by flips.

Save yourself the pain

Common mistakes

Oops

Multiplying thousands of probabilities directly.

Do this instead

Work with the sum of log-probabilities. The product quickly becomes 0.0 in floating point.

Oops

Trusting an MLE from very little data.

Do this instead

Three heads in three flips gives p = 1.0. With small samples, add a prior or smoothing, as in MAP or Laplace smoothing.

Your mission

MLE for delivery times

~25 min

Quick check

If you assume regression errors follow a normal distribution, maximising likelihood is the same as minimising which loss?

Go deeper

  1. 1Estimate a coin's bias from 7 heads in 10 tosses by maximising the likelihood.
  2. 2Plot the log-likelihood for different values and find the peak.
  3. 3Read how logistic regression's loss is the negative log-likelihood.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge