Mathematical Statistics & Data Inference Module

Maximum Likelihood Estimation (MLE)

Likelihood functions, score equations, Fisher information matrix, the Cramér-Rao lower bound, and asymptotic efficiency in machine learning.

1. The Principle of Maximum Likelihood

In parameter estimation, we assume data $\mathbf{x} = (x_1, x_2, \dots, x_n)$ was generated by a probability distribution with unknown parameters $\theta \in \Theta$.
Maximum Likelihood Estimation (MLE) asks: Which parameter value $\theta$ makes the observed data most probable?
text
+-----------------------------------------------------------------------------------------+
|                               PROBABILITY VS LIKELIHOOD                                 |
+-----------------------------------------------------------------------------------------+
|                                                                                         |
|  PROBABILITY P(x; theta):                                                               |
|  - theta is FIXED.                                                                      |
|  - Function of data x.                                                                  |
|  - Integrates/sums to 1 over all possible outcomes x.                                    |
|                                                                                         |
|  LIKELIHOOD L(theta; x):                                                                |
|  - Observed data x is FIXED.                                                            |
|  - Function of parameter theta.                                                         |
|  - Does NOT integrate to 1 over parameter space Theta!                                  |
+-----------------------------------------------------------------------------------------+

2. Mathematical Formalism: Likelihood & Log-Likelihood

For independent and identically distributed (i.i.d.) observations $x_1, x_2, \dots, x_n$ drawn from density $f(x; \theta)$:
$$L(\theta; \mathbf{x}) = \prod_{i=1}^n f(x_i; \theta)$$

2.1 The Log-Likelihood Function $\ell(\theta)$

Because multiplying hundreds of probabilities between $0$ and $1$ causes catastrophic floating-point underflow ($10^{-300} \to 0$), we take the natural logarithm:
$$\ell(\theta) = \ln L(\theta; \mathbf{x}) = \sum_{i=1}^n \ln f(x_i; \theta)$$
Because the natural logarithm is a strictly monotonically increasing function, the parameter that maximizes $\ell(\theta)$ is identical to the parameter that maximizes $L(\theta)$:
$$\hat{\theta}{\text{MLE}} = \arg\max{\theta \in \Theta} L(\theta; \mathbf{x}) = \arg\max_{\theta \in \Theta} \ell(\theta)$$

3. Step-by-Step Analytical Derivations

3.1 Gaussian (Normal) Distribution MLE

Let $x_1, \dots, x_n \sim \mathcal{N}(\mu, \sigma^2)$ where $\theta = (\mu, \sigma^2)$.
  1. Joint Density Product: $$L(\mu, \sigma^2; \mathbf{x}) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi \sigma^2}} \exp\left( -\frac{(x_i - \mu)^2}{2\sigma^2} \right) = (2\pi \sigma^2)^{-n/2} \exp\left( -\frac{1}{2\sigma^2} \sum_{i=1}^n (x_i - \mu)^2 \right)$$
  2. Log-Likelihood: $$\ell(\mu, \sigma^2) = -\frac{n}{2} \ln(2\pi) - \frac{n}{2} \ln(\sigma^2) - \frac{1}{2\sigma^2} \sum_{i=1}^n (x_i - \mu)^2$$
  3. Optimizing $\mu$: $$\frac{\partial \ell}{\partial \mu} = \frac{1}{\sigma^2} \sum_{i=1}^n (x_i - \mu) = 0 \implies \sum_{i=1}^n x_i - n\mu = 0 \implies \hat{\mu}{\text{MLE}} = \frac{1}{n} \sum{i=1}^n x_i = \bar{x}$$
  4. Optimizing $\sigma^2$: Let $v = \sigma^2$. Differentiating with respect to $v$: $$\frac{\partial \ell}{\partial v} = -\frac{n}{2v} + \frac{1}{2v^2} \sum_{i=1}^n (x_i - \mu)^2 = 0$$ Multiplying by $2v^2$: $$-nv + \sum_{i=1}^n (x_i - \mu)^2 = 0 \implies \hat{\sigma}^2_{\text{MLE}} = \frac{1}{n} \sum_{i=1}^n (x_i - \hat{\mu})^2$$
Key Insight: The MLE estimator for variance is the naive estimator with denominator $n$. MLE estimators are not guaranteed to be unbiased in finite samples ($E[\hat{\sigma}^2_{\text{MLE}}] = \frac{n-1}{n}\sigma^2$), but they are asymptotically unbiased ($n \to \infty$).

3.2 Bernoulli Distribution MLE

For binary outcomes $x_i \in {0, 1}$ with success parameter $p$: $$\ell(p) = \sum_{i=1}^n \left[ x_i \ln p + (1 - x_i) \ln(1 - p) \right] = \left(\sum_{i=1}^n x_i\right) \ln p + \left(n - \sum_{i=1}^n x_i\right) \ln(1 - p)$$ Setting derivative to zero: $$\frac{d\ell}{dp} = \frac{\sum x_i}{p} - \frac{n - \sum x_i}{1 - p} = 0 \implies \hat{p}{\text{MLE}} = \frac{1}{n} \sum{i=1}^n x_i$$ The MLE is the empirical sample proportion.

4. The Score Function & Fisher Information

4.1 The Score Function $S(\theta)$

The gradient of the log-likelihood:
$$S(\theta) = \nabla_\theta \ell(\theta)$$

Fundamental Lemma: Expected Score is Always Zero

$$E_{\mathbf{x}}[S(\theta)] = \mathbf{0}$$

4.2 Fisher Information $I(\theta)$

Measures the amount of information that observed data carries about unknown parameter $\theta$. It is the curvature (negative Hessian) of the log-likelihood:
$$I(\theta) = - E\left[ \frac{\partial^2 \ell(\theta)}{\partial \theta^2} \right] = \text{Var}(S(\theta))$$
text
Geometric Meaning of Fisher Information:
High Information I(theta): Sharp peak.         Low Information I(theta): Flat curvature.
Parameter is tightly constrained by data.    High uncertainty, wide variance.
ell(theta)                                  ell(theta)
    ^        /\                                 ^       .--------.
    |       /  \                                |     .'          '.
    |      /    \                               |    /              \
    +-----+------+-----> theta                  +---+----------------+---> theta

5. The Cramér-Rao Lower Bound (CRLB)

How accurate can an unbiased estimator possibly be? Is there a theoretical floor on variance?

Theorem (Cramér-Rao)

Let $\hat{\theta}$ be any unbiased estimator of $\theta$ ($E[\hat{\theta}] = \theta$). Then:
$$\text{Var}(\hat{\theta}) \ge \frac{1}{I_n(\theta)}$$
where $I_n(\theta) = n I_1(\theta)$ is the Fisher Information of $n$ i.i.d. observations.
An estimator that achieves the Cramér-Rao Lower Bound is called efficient.

6. Asymptotic Properties of MLE

Under standard regularity conditions, as sample size $n \to \infty$:
  1. Consistency: $\hat{\theta}_{\text{MLE}} \xrightarrow{P} \theta_0$ (converges to the true parameter).
  2. Asymptotic Normality: $$\sqrt{n}\left(\hat{\theta}_{\text{MLE}} - \theta_0\right) \xrightarrow{d} \mathcal{N}\left(0, I_1(\theta_0)^{-1}\right)$$
  3. Asymptotic Efficiency: The MLE achieves the Cramér-Rao Lower Bound asymptotically: $$\lim_{n \to \infty} n \text{Var}(\hat{\theta}_{\text{MLE}}) = \frac{1}{I_1(\theta_0)}$$
No other consistent estimator has lower asymptotic variance than the MLE!

7. Deep Learning Connection: Negative Log-Likelihood (NLL)

In deep learning, training a neural network with parameters $\mathbf{w}$ to classify images into classes $y \in {1, \dots, C}$ is framed as minimizing Cross-Entropy Loss:
$$\mathcal{L}(\mathbf{w}) = - \sum_{i=1}^n \ln P(y_i \mid \mathbf{x}_i; \mathbf{w}) = \text{Negative Log-Likelihood (NLL)}$$
Minimizing NLL using Stochastic Gradient Descent (SGD) is mathematically identical to finding the Maximum Likelihood Estimator of the network weights!

8. Python Implementation: Numerical MLE via SciPy

python
import numpy as np
from scipy.optimize import minimize
import scipy.stats as stats

def fit_mle_gamma_distribution(data: np.ndarray) -> tuple[float, float]:
    """
    Find Maximum Likelihood Estimates for Gamma distribution parameters
    (shape k, scale theta) using numerical optimization of Negative Log-Likelihood.
    """
    # Negative Log-Likelihood function
    def negative_log_likelihood(params):
        shape, scale = params
        if shape <= 0 or scale <= 0:
            return 1e12  # Penalty for non-positive parameters
        # scipy.stats.gamma uses (a=shape, scale=scale)
        nll = -np.sum(stats.gamma.logpdf(data, a=shape, scale=scale))
        return nll

    # Initial guess via method of moments
    mean = np.mean(data)
    var = np.var(data)
    init_shape = (mean ** 2) / var
    init_scale = var / mean

    res = minimize(negative_log_likelihood, x0=[init_shape, init_scale], method='L-BFGS-B', bounds=[(1e-4, None), (1e-4, None)])
    return res.x[0], res.x[1]

def demonstrate_mle():
    true_shape = 3.5
    true_scale = 2.0
    rng = np.random.default_rng(42)
    sample_data = rng.gamma(shape=true_shape, scale=true_scale, size=50_000)

    est_shape, est_scale = fit_mle_gamma_distribution(sample_data)
    print(f"=== MLE Numerical Fit for Gamma Distribution ===")
    print(f"True  Params: shape = {true_shape:.4f}, scale = {true_scale:.4f}")
    print(f"MLE   Params: shape = {est_shape:.4f}, scale = {est_scale:.4f}")

if __name__ == "__main__":
    demonstrate_mle()

9. Key Takeaways

  1. Log-Likelihood Transforms Products into Sums: Maximizing $\ell(\theta) = \sum \ln f(x_i)$ prevents underflow and simplifies gradients.
  2. Fisher Information Governs Variance: Curvature of the log-likelihood dictates estimator precision; sharper curvature means lower variance.
  3. Cramér-Rao Optimality: MLE asymptotically achieves the theoretical minimum possible variance ($\text{Var} \to 1/I(\theta)$).