Distributions and MLE
Probability distributions as models of data, and maximum likelihood estimation: fit a Gaussian analytically, then numerically, and see why both agree.
A model in machine learning is often a probability distribution with knobs. Training means turning the knobs until the data you observed looks as probable as possible. That idea, maximum likelihood estimation (MLE), is behind linear regression, logistic regression, and the cross-entropy loss used to train language models.
The normal distribution
moves the bell left and right, and makes it wider and flatter. The area under the curve is always 1, so a wider curve must also be lower. Try it:
Likelihood
Suppose we observe independent data points and believe they came from for some unknown and . The likelihood of a choice of parameters is the probability density of the whole sample under those parameters:
Products of many small numbers underflow, and sums are easier to differentiate, so we work with the log-likelihood instead:
Since is increasing, the parameters that maximize also maximize .
Solving it analytically
Set the derivatives to zero. For :
Doing the same for gives the second half of the result:
Here is the log-likelihood of that sample as a function of , with held at . It is a downward parabola with its peak exactly at the sample mean:
Solving it numerically
Most models don’t have a closed-form MLE, so in practice we minimize the negative log-likelihood with an optimizer. Doing it here, where we know the answer, is a good check that the machinery works:
import numpy as np
from scipy.optimize import minimize
x = np.array([1.2, 0.4, 2.1, 1.7, 0.9])
def neg_log_lik(params):
mu, log_sigma = params # optimize log(sigma) so sigma stays positive
sigma = np.exp(log_sigma)
return 0.5 * len(x) * np.log(2 * np.pi * sigma**2) + np.sum((x - mu) ** 2) / (2 * sigma**2)
result = minimize(neg_log_lik, x0=[0.0, 0.0])
mu_hat, sigma_hat = result.x[0], np.exp(result.x[1])
print(mu_hat, sigma_hat) # ≈ 1.26, 0.595
print(x.mean(), x.std(ddof=0)) # 1.26, 0.5953 (the analytic answer)
Optimizing instead of is a common trick: it turns a constrained problem () into an unconstrained one.
Exercise
Draw 1,000 points from with a fixed seed. Fit and analytically and with
scipy.optimize.minimize, and show that the two agree to at least four decimal places. Then repeat with only 10
points and compare using and . Which one is closer to 2 on average over 1,000 repetitions?
Key ideas
- A distribution with parameters is a model; the likelihood scores how well parameters explain the data.
- Maximize the log-likelihood, not the likelihood: same answer, better numerics.
- For a Gaussian, the MLE is the sample mean and the (divide-by-) sample variance.
- When there’s no closed form, minimize the negative log-likelihood with an optimizer.