Skip to content

Probability and Statistics

Maximum likelihood estimation

Choosing parameters that best explain the data

You have data, and you have a model with some knobs. Maximum likelihood estimation asks one question and answers it precisely: which knob settings make the data you actually observed the least surprising? Turn that question into arithmetic and something remarkable falls out - the loss functions you have been minimizing all along, mean squared error and cross-entropy, are not arbitrary. They are the exact scores you get when you write down "make this data likely" and simplify. Training a model is maximum likelihood estimation wearing a disguise.

Likelihood: probability, read backwards

A probability model normally answers a forward question: given the parameters , how probable is an outcome ? Maximum likelihood flips it. Now the data is fixed - you already saw it - and is the unknown you get to choose. The same expression, read with data locked and parameters free, is called the likelihood. If the observations are independent, the likelihood of a whole dataset is the product of each point's probability:

Likelihood, and why we log it

A product of tiny probabilities underflows to zero and is a nightmare to differentiate. So take the logarithm. Because is monotonic - it never changes which is largest - the maximizer is identical, but the product becomes a friendly sum, the log-likelihood:

Maximizing is fitting the model: you are sliding the parameters until the model assigns as much total probability as possible to the exact points you observed. Everything else - gradient descent, loss functions, training - is machinery for climbing this one hill.

Steer the bell curve to fit the data

Here is the whole idea in one picture. Six data points sit fixed on the axis. Above them floats a Gaussian whose center and width you control. Each point sends up a stem to the curve - the stem's height is that point's probability density . Multiply the six heights to get the likelihood; add their logs to get the log-likelihood. Drag the sliders: notice that pushing the peak over the data's center and tuning the width to match its spread makes every stem tall at once. Then hit Maximize and watch the curve snap to the exact place gradient descent would climb to - the MLE.

Six data points sit on the axis. Drag mu and sigma to make every stem tall at once - that raises the likelihood. Then hit Maximize and watch the curve snap to the MLE.
Gaussian p(x)stem height = p(xᵢ)observed datamu (curve center)
center mu3.00
width sigma2.50
likelihood ℓ1.69e-6
log-lik ℓ-13.29

Low likelihood - the curve doesn't explain the data yet. Keep climbing.

The MLE for a Gaussian has a closed form you can read off the data: is just the mean of the points, and is their average squared distance from that mean. No search required - but gradient descent would find the very same summit by walking uphill on . That equivalence is the bridge to every model too complex for a formula.

Where MSE and cross-entropy come from

Now the payoff. Suppose your model predicts and you assume the real target is that prediction plus Gaussian noise: with . Write the log-likelihood of the targets and expand the Gaussian - the constants fall away and one term is left holding the parameters:

The two famous losses are MLE in disguise

Regression - MSE. Under Gaussian noise the log-likelihood is

Only the last sum depends on the model's parameters, and it carries a minus sign - so maximizing is exactly minimizing , the mean squared error.

Classification - cross-entropy. If instead the model outputs a probability over classes and the truth is a one-hot label, the likelihood of one example is the probability it placed on the correct class. Its negative log-likelihood, summed over the data, is precisely

Minimizing cross-entropy is maximizing categorical likelihood.

So the loss was never a design choice pulled from thin air. Pick a noise assumption - Gaussian for real-valued targets, categorical for labels - write "make the data likely," take the log, and the standard loss drops out of the algebra. Change the assumption and you change the loss; assume Laplace noise instead of Gaussian and MLE hands you mean absolute error. Loss functions are downstream of a probability story.

import numpy as np
data = np.array([2.0, 3.5, 4.0, 5.0, 5.5, 7.0])

def neg_log_likelihood(mu, sigma):
    z     = (data - mu) / sigma
    log_p = -0.5 * z**2 - np.log(sigma * np.sqrt(2 * np.pi))
    return -log_p.sum()          # NLL - the thing we minimize

mu_hat    = data.mean()             # 4.50  - MLE of the mean
sigma_hat = data.std()              # 1.58  - MLE of the std (ddof=0)
print(neg_log_likelihood(mu_hat, sigma_hat))  # 11.26 - the smallest possible NLL
MLE for a Gaussian is a two-line closed form

Check yourself

Why maximize the log-likelihood instead of the raw likelihood?

Minimizing mean squared error is exactly the same as:

State the MLE recipe, and the two losses it produces.

The recipe is the spine; the two losses are the payoff. Try to state it, then check.

Lock it in

  • Likelihood is the same formula as probability, read with data fixed and parameters free.
  • The log turns the awkward product into an easy sum without moving the peak.
  • The MLE is the parameter setting that makes the observed data most likely under the model.
  • MSE drops out of MLE under Gaussian noise; cross-entropy drops out of MLE under categorical output.
  • Loss functions are not arbitrary - they are downstream of a probability story.

Primary source

StatQuest's Maximum Likelihood playlist builds the intuition step by step with the clearest visuals anywhere. For the rigorous treatment - likelihoods, log-likelihoods, and MLE as an optimization problem - see the free textbook Mathematics for Machine Learning (Deisenroth, Faisal & Ong), Chapter 8.

Ask your teacher

This is the hidden spine of the whole course. Back in lesson 17 we minimized MSE and in lesson 63 we minimize cross-entropy - and both feel like they were handed down from on high. They weren't. Training a model is maximizing the likelihood of the data under that model; MSE and cross-entropy are just what "maximize the likelihood" becomes once you commit to a noise assumption. Ask me why a language model's next-token loss is categorical MLE, or what happens to the loss if you swap Gaussian noise for Laplace or Student-t.