Maximum likelihood estimation
Set up a likelihood, maximize its log, and estimate parameters from data.
Distributions come with parameters: the rate of a Poisson, the mean of a Gaussian. Given data and a chosen distribution family, maximum likelihood picks the parameter that makes the observed data as probable as possible[1]. It is the default estimator behind logistic regression, Gaussian fits, and the cross-entropy loss of classification.
The likelihood function
Suppose the data are i.i.d. from a distribution with PMF or PDF . The likelihood is the probability of the data viewed as a function of the parameter[2]:
The data are fixed; varies. The maximum likelihood estimate (MLE) is the maximizer
Why the log
Products of many small probabilities underflow and are awkward to differentiate. Because is strictly increasing, it does not move the maximizer, and it turns the product into a sum:
Maximize the log-likelihood instead. At an interior maximum its gradient vanishes:
The summand is the score; the MLE sets the total score to zero.
Worked example: the Gaussian
For ,
Summing and differentiating in :
the sample mean. Differentiating in :
the (biased) sample variance with divisor . The MLE recovers the natural plug-in estimators.
Properties
Under regularity conditions, the MLE is consistent (converges to the true parameter as , by the law of large numbers applied to the score), asymptotically normal, and asymptotically efficient (it achieves the smallest possible variance, set by the Fisher information of a later lesson). It can be biased at finite , as shows: dividing by instead removes that bias.
Check it on arrays
import numpy as np
rng = np.random.default_rng(11)
N = 100_000
# Gaussian MLE: closed form equals sample mean and (1/n) sample variance
true_mu, true_sigma = 2.0, 1.5
data = rng.normal(true_mu, true_sigma, N)
mu_hat = data.mean()
sigma2_hat = ((data - mu_hat)**2).mean() # divisor n (MLE)
print("mu_hat:", round(mu_hat, 4), " true", true_mu)
print("sigma_hat:", round(np.sqrt(sigma2_hat), 4), " true", true_sigma)
# Poisson MLE for lambda is the sample mean: maximize sum(x log L - L)
true_lam = 4.0
counts = rng.poisson(true_lam, N)
lam_hat = counts.mean()
print("lambda_hat:", round(lam_hat, 4), " true", true_lam)
# Verify by a grid search on the log-likelihood
grid = np.linspace(3.5, 4.5, 2001)
# sum over data of (x log lam - lam), constant log(x!) dropped
ll = counts.sum() * np.log(grid) - N * grid
print("grid argmax:", round(grid[np.argmax(ll)], 4))The closed-form Gaussian and Poisson estimators recover the true parameters, and the grid search confirms the Poisson MLE is the sample mean.
Exercises
Exercise 1. A coin lands heads times in independent flips, with unknown head probability . Derive the MLE by maximizing the log-likelihood.
Solution
Step 1. Each flip is Bernoulli(), so the likelihood of heads and tails is . Take the log:
Step 2. Set the derivative to zero:
Step 3. Cross-multiply: , so , giving and
the observed head fraction. The MLE is the empirical proportion.
Exercise 2. For data i.i.d. Exponential with rate (density for ), derive .
Solution
Step 1. The log of one term is . Summing,
Step 2. Differentiate and set to zero:
Step 3. Solve:
The MLE of the rate is the reciprocal of the sample mean, matching the fact that an Exponential() has mean .
Exercise 3 (code). Generate Exponential samples with true rate . Verify that the closed-form MLE from Exercise 2 matches the maximizer of the log-likelihood found by a grid search.
Solution
import numpy as np
rng = np.random.default_rng(7)
N = 200_000
true_lam = 3.0
x = rng.exponential(scale=1 / true_lam, size=N) # numpy scale = 1/rate
lam_closed = 1 / x.mean() # MLE from Exercise 2
# Grid search on the log-likelihood ell(lam) = N log(lam) - lam * sum(x)
grid = np.linspace(2.5, 3.5, 4001)
ll = N * np.log(grid) - grid * x.sum()
lam_grid = grid[np.argmax(ll)]
assert np.isclose(lam_closed, lam_grid, atol=1e-3)
print("closed form:", round(lam_closed, 4), " grid:", round(lam_grid, 4))The closed-form reciprocal-mean estimator and the grid maximizer agree to grid resolution, both landing near the true rate .
The next lesson adds a prior on top of the likelihood, turning estimation into Bayesian inference.
References
- [1]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §7.2, maximum likelihood estimators and their derivation.↩
- [2]Wasserman, L. (2004). All of Statistics. Springer. Ch. 9, the likelihood function and MLE. doi:10.1007/978-0-387-21736-9↩