AI Knowledge Tree

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 x1,…,xnx_1, \dots, x_n are i.i.d. from a distribution with PMF or PDF p(x∣θ)p(x \mid \theta). The likelihood is the probability of the data viewed as a function of the parameter[2]:

L(θ)=∏i=1np(xi∣θ).L(\theta) = \prod_{i=1}^n p(x_i \mid \theta).

The data are fixed; θ\theta varies. The maximum likelihood estimate (MLE) is the maximizer

θ^=arg⁡max⁡θL(θ).\hat\theta = \arg\max_\theta L(\theta).

Why the log

Products of many small probabilities underflow and are awkward to differentiate. Because log⁡\log is strictly increasing, it does not move the maximizer, and it turns the product into a sum:

ℓ(θ)=log⁡L(θ)=∑i=1nlog⁡p(xi∣θ).\ell(\theta) = \log L(\theta) = \sum_{i=1}^n \log p(x_i \mid \theta).

Maximize the log-likelihood ℓ\ell instead. At an interior maximum its gradient vanishes:

∇θℓ(θ)=∑i=1n∇θlog⁡p(xi∣θ)=0.\nabla_\theta \ell(\theta) = \sum_{i=1}^n \nabla_\theta \log p(x_i \mid \theta) = 0.

The summand ∇θlog⁡p(xi∣θ)\nabla_\theta \log p(x_i \mid \theta) is the score; the MLE sets the total score to zero.

Worked example: the Gaussian

For N(μ,σ2)\mathcal{N}(\mu, \sigma^2),

log⁡p(x∣μ,σ2)=−12log⁡(2πσ2)−(x−μ)22σ2.\log p(x \mid \mu, \sigma^2) = -\frac{1}{2}\log(2\pi\sigma^2) - \frac{(x - \mu)^2}{2\sigma^2}.

Summing and differentiating in μ\mu:

∂ℓ∂μ=∑ixi−μσ2=0  ⟹  μ^=1n∑ixi,\frac{\partial \ell}{\partial \mu} = \sum_i \frac{x_i - \mu}{\sigma^2} = 0 \implies \hat\mu = \frac{1}{n}\sum_i x_i,

the sample mean. Differentiating in σ2\sigma^2:

∂ℓ∂σ2=∑i(−12σ2+(xi−μ)22σ4)=0  ⟹  σ^2=1n∑i(xi−μ^)2,\frac{\partial \ell}{\partial \sigma^2} = \sum_i\left(-\frac{1}{2\sigma^2} + \frac{(x_i - \mu)^2}{2\sigma^4}\right) = 0 \implies \hat\sigma^2 = \frac{1}{n}\sum_i (x_i - \hat\mu)^2,

the (biased) sample variance with divisor nn. The MLE recovers the natural plug-in estimators.

Properties

Under regularity conditions, the MLE is consistent (converges to the true parameter as n→∞n \to \infty, 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 nn, as σ^2\hat\sigma^2 shows: dividing by n−1n - 1 instead removes that bias.

The Poisson log-likelihood is a single peak located exactly at the sample mean: the maximum-likelihood estimate λ̂ = k̄.
The Poisson log-likelihood is a single peak located exactly at the sample mean: the maximum-likelihood estimate λ̂ = k̄.

Check it on arrays

python · numpy
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 hh times in nn independent flips, with unknown head probability θ\theta. Derive the MLE θ^\hat\theta by maximizing the log-likelihood.

Solution

Step 1. Each flip is Bernoulli(θ\theta), so the likelihood of hh heads and n−hn - h tails is L(θ)=θh(1−θ)n−hL(\theta) = \theta^h (1 - \theta)^{n - h}. Take the log:

ℓ(θ)=hlog⁡θ+(n−h)log⁡(1−θ).\ell(\theta) = h\log\theta + (n - h)\log(1 - \theta).

Step 2. Set the derivative to zero:

ℓ′(θ)=hθ−n−h1−θ=0.\ell'(\theta) = \frac{h}{\theta} - \frac{n - h}{1 - \theta} = 0.

Step 3. Cross-multiply: h(1−θ)=(n−h)θh(1 - \theta) = (n - h)\theta, so h−hθ=nθ−hθh - h\theta = n\theta - h\theta, giving h=nθh = n\theta and

θ^=hn,\hat\theta = \frac{h}{n},

the observed head fraction. The MLE is the empirical proportion.

Exercise 2. For data x1,…,xnx_1, \dots, x_n i.i.d. Exponential with rate λ\lambda (density p(x∣λ)=λe−λxp(x \mid \lambda) = \lambda e^{-\lambda x} for x≥0x \ge 0), derive λ^\hat\lambda.

Solution

Step 1. The log of one term is log⁡λ−λxi\log\lambda - \lambda x_i. Summing,

ℓ(λ)=nlog⁡λ−λ∑ixi.\ell(\lambda) = n\log\lambda - \lambda\sum_i x_i.

Step 2. Differentiate and set to zero:

ℓ′(λ)=nλ−∑ixi=0.\ell'(\lambda) = \frac{n}{\lambda} - \sum_i x_i = 0.

Step 3. Solve:

λ^=n∑ixi=1xˉ.\hat\lambda = \frac{n}{\sum_i x_i} = \frac{1}{\bar x}.

The MLE of the rate is the reciprocal of the sample mean, matching the fact that an Exponential(λ\lambda) has mean 1/λ1/\lambda.

Exercise 3 (code). Generate n=200,000n = 200{,}000 Exponential samples with true rate λ=3\lambda = 3. Verify that the closed-form MLE 1/xˉ1/\bar x from Exercise 2 matches the maximizer of the log-likelihood found by a grid search.

Solution
python · numpy
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 33.

The next lesson adds a prior on top of the likelihood, turning estimation into Bayesian inference.


References

  1. [1]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §7.2, maximum likelihood estimators and their derivation.↩
  2. [2]Wasserman, L. (2004). All of Statistics. Springer. Ch. 9, the likelihood function and MLE. doi:10.1007/978-0-387-21736-9↩