Type at least 2 letters. Enter opens the first result, Esc closes.

    Probability and Statistics · Lesson 8 · Estimation

    Maximum likelihood, the exponential family and properties of estimators

    Intermediate 75 min 4 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 03 (Bernoulli, Poisson, exponential, Gaussian and gamma distributions), lesson 04 (covariance matrices are positive semidefinite; the sample variance S2S^2) and lesson 06 (the law of large numbers). From other tracks: gradients and Hessians (calculus 02, calculus 03); the KL divergence (information theory 03) is defined again in section 3. Time: about 75 minutes, plus the notebook.


    Why this matters #

    • Training losses are likelihoods. The mean squared error is the negative log-likelihood (NLL) of a Gaussian model. Binary and softmax cross-entropy are the NLLs of Bernoulli and categorical models. Minimising them is maximum likelihood (information theory 04).
    • Error bars and comparisons. Bias, variance and MSE are the language for comparing estimators: why the sample variance divides by n−1n - 1, why a regularised (shrunk) estimate can beat the MLE, why a model evaluated with few random seeds looks more stable than it is.
    • Generalised linear models. Linear, logistic and Poisson regression are maximum likelihood in an exponential family. This is why their losses are convex and why their gradients all look like "prediction minus target".
    • Science. Fitting a gamma distribution to rainfall amounts, an exponential distribution to the waiting times between events, or a Poisson rate to counts of photons or earthquakes is maximum likelihood.

    1. Predict first #

    2. The picture #

    The likelihood of a coin #

    You flip a coin n=10n = 10 times and see k=7k = 7 heads. Model: each flip is xi∼Bernoulli(p)x_i \sim \text{Bernoulli}(p), independent, with xi=1x_i = 1 for heads. The probability of the observed sequence is

    P(x1,…,xn∣p)=∏i=1npxi(1−p)1−xi=pk(1−p)n−k.P(x_1, \dots, x_n \mid p) = \prod_{i=1}^n p^{x_i}(1-p)^{1-x_i} = p^k (1-p)^{n-k}.

    Read this formula in two ways.

    • As a probability: fix pp and vary the data. For p=0.7p = 0.7, it gives the probability of each possible sequence of 10 flips. These numbers sum to 1 over all 2102^{10} sequences.
    • As a likelihood: fix the data (7 heads) and vary pp. This gives a curve over p∈[0,1]p \in [0, 1]:
    L(p)=p7(1−p)3.L(p) = p^7 (1-p)^3 .

    The likelihood L(θ)L(\theta) is the probability (or density) of the observed data, seen as a function of the parameter θ\theta. It is the same formula, with a different variable. The maximum likelihood estimate is the value of θ\theta where LL is largest.

    Some values: L(0.7)=0.77⋅0.33≈0.00222L(0.7) = 0.7^7 \cdot 0.3^3 \approx 0.00222 and L(0.5)=0.510≈0.00098L(0.5) = 0.5^{10} \approx 0.00098. So the data are about 2.3 times more probable under p=0.7p = 0.7 than under a fair coin. The number 0.002220.00222 itself has no meaning on its own; only ratios of likelihoods do.

    Interactive figure: loading…

    Flip the coin and watch the curve. Press Flip 10 a few times. The blue flips are the data. The top panel is the orange likelihood L(p)L(p), divided by its maximum so that the peak is at 1; the bottom panel is the log-likelihood ℓ(p)−ℓ(p^)\ell(p) - \ell(\hat p). The yellow line marks the MLE p^=k/n\hat p = k/n, the dashed grey line the true pp of the coin. Notice that the peak gets narrower as nn grows: more data pin pp down. The readout gives the curvature −ℓ′′(p^)-\ell''(\hat p); lesson 09 turns it into an error bar.

    Why we take logs. The likelihood of i.i.d. data is a product of nn numbers, often much smaller than 1. For 1000 flips with 700 heads, L(0.7)L(0.7) is about 10−26510^{-265}; with 2000 flips the product underflows to 0 in double precision. The log-likelihood ℓ(θ)=log⁡L(θ)\ell(\theta) = \log L(\theta) turns the product into a sum,

    ℓ(p)=klog⁡p+(n−k)log⁡(1−p),\ell(p) = k\log p + (n-k)\log(1-p),

    and has the same maximiser, because log⁡\log is increasing. Set the derivative to zero:

    ℓ′(p)=kp−n−k1−p=0⟹k(1−p)=(n−k) p⟹p^=kn.\ell'(p) = \frac{k}{p} - \frac{n-k}{1-p} = 0 \quad\Longrightarrow\quad k(1-p) = (n-k)\,p \quad\Longrightarrow\quad \hat p = \frac{k}{n}.

    Check that it is a maximum: ℓ′′(p)=−kp2−n−k(1−p)2<0\ell''(p) = -\frac{k}{p^2} - \frac{n-k}{(1-p)^2} < 0 for all p∈(0,1)p \in (0, 1). At p^=0.7\hat p = 0.7, ℓ′′(p^)=−70.49−30.09≈−47.6\ell''(\hat p) = -\frac{7}{0.49} - \frac{3}{0.09} \approx -47.6, which equals −np^(1−p^)=−100.21-\frac{n}{\hat p(1 - \hat p)} = -\frac{10}{0.21}. The size of this second derivative is the curvature of the peak. It grows in proportion to nn.

    The likelihood is not a density in pp. The area under L(p)L(p) is ∫01p7(1−p)3 dp=7! 3!11!=11320\int_0^1 p^7(1-p)^3\,dp = \frac{7!\,3!}{11!} = \frac{1}{1320}, not 1. To get a probability distribution over pp you need a prior and Bayes' rule; that is lesson 10.

    Quick check

    A coin gives 3 heads in 4 flips. Which statement is correct?

    Fitting a Gaussian: every point votes #

    Now the data are real numbers x1,…,xnx_1, \dots, x_n and the model is N(μ,σ2)\mathcal N(\mu, \sigma^2). Each point contributes its log-density:

    ℓ(μ,σ2)=∑i=1nlog⁡p(xi∣μ,σ2)=−n2log⁡(2πσ2)−12σ2∑i=1n(xi−μ)2.\ell(\mu, \sigma^2) = \sum_{i=1}^n \log p(x_i \mid \mu, \sigma^2) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^n (x_i - \mu)^2 .

    Think of −log⁡p(xi)-\log p(x_i) as the "cost" of point ii. A point far from μ\mu (measured in units of σ\sigma) costs a lot. A large σ\sigma makes far points cheap, but it makes every point pay the term log⁡σ\log\sigma. The MLE balances the two.

    Interactive figure: loading…

    Move μ\mu and σ\sigma to make the total cost small. The blue points are the data; the orange curve is the Gaussian model, and each stem shows the density p(xi)p(x_i) of a point. The magenta bars below are the costs −log⁡p(xi)-\log p(x_i); the readout adds them up to the negative log-likelihood. Try a tiny σ\sigma: the points near μ\mu become cheap, the far ones very expensive. Press Jump to MLE and compare with your best guess.

    The minimum of the total cost is at μ^=xˉ\hat\mu = \bar x and σ^2=1n∑i(xi−xˉ)2\hat\sigma^2 = \frac{1}{n}\sum_i (x_i - \bar x)^2 (section 3 proves it). In words: the fitted Gaussian has the same mean and the same average squared deviation as the data.

    Quick check

    You fit N(μ,σ2)\mathcal N(\mu, \sigma^2) by maximum likelihood to the data 1,2,61, 2, 6. What are μ^\hat\mu and σ^2\hat\sigma^2?

    3. Definitions and main results #

    Likelihood and the maximum likelihood estimate #

    Setting. A model is a family of PMFs or densities p(x∣θ)p(x \mid \theta), indexed by a parameter θ∈Θ\theta \in \Theta (a number or a vector). The data x1,…,xnx_1, \dots, x_n are i.i.d. from p(x∣θ)p(x \mid \theta) for one true value of θ\theta.

    • The likelihood is L(θ)=∏i=1np(xi∣θ)L(\theta) = \prod_{i=1}^n p(x_i \mid \theta), a function of θ\theta for fixed data.
    • The log-likelihood is ℓ(θ)=∑i=1nlog⁡p(xi∣θ)\ell(\theta) = \sum_{i=1}^n \log p(x_i \mid \theta).
    • The maximum likelihood estimate is θ^MLE=arg⁡max⁡θ∈Θℓ(θ)\hat\theta_{\text{MLE}} = \arg\max_{\theta \in \Theta} \ell(\theta), when the maximum exists.

    In practice, set the gradient ∇ℓ(θ)\nabla\ell(\theta) to zero (the likelihood equations) and check that the stationary point is a maximum: a negative second derivative, or a negative definite Hessian (calculus 03). Write xˉ=1n∑ixi\bar x = \frac{1}{n}\sum_i x_i.

    Data ℓ(θ)\ell(\theta), up to constants Solve ℓ′(θ)=0\ell'(\theta) = 0 MLE
    Bernoulli(pp) nxˉlog⁡p+n(1−xˉ)log⁡(1−p)n\bar x\log p + n(1-\bar x)\log(1-p) nxˉp=n(1−xˉ)1−p\frac{n\bar x}{p} = \frac{n(1-\bar x)}{1-p} p^=xˉ\hat p = \bar x
    Poisson(λ\lambda) nxˉlog⁡λ−nλn\bar x\log\lambda - n\lambda nxˉλ−n=0\frac{n\bar x}{\lambda} - n = 0 λ^=xˉ\hat\lambda = \bar x
    Exponential(λ\lambda), rate λ\lambda nlog⁡λ−λnxˉn\log\lambda - \lambda n\bar x nλ−nxˉ=0\frac{n}{\lambda} - n\bar x = 0 λ^=1/xˉ\hat\lambda = 1/\bar x
    N(μ,v)\mathcal N(\mu, v) with v=σ2v = \sigma^2 −n2log⁡v−12v∑i(xi−μ)2-\frac{n}{2}\log v - \frac{1}{2v}\sum_i (x_i - \mu)^2 see below μ^=xˉ\hat\mu = \bar x, v^=1n∑i(xi−xˉ)2\hat v = \frac{1}{n}\sum_i (x_i - \bar x)^2

    The second derivatives are negative in all four cases: −nxˉλ2-\frac{n\bar x}{\lambda^2} (Poisson, when xˉ>0\bar x > 0), −nλ2-\frac{n}{\lambda^2} (exponential), and −nxˉp2−n(1−xˉ)(1−p)2-\frac{n\bar x}{p^2} - \frac{n(1-\bar x)}{(1-p)^2} (Bernoulli).

    Gaussian, step by step. We maximise over μ\mu and over the variance v=σ2v = \sigma^2 (not σ\sigma; the answer is the same by invariance, below).

    1. ∂μℓ=1v∑i(xi−μ)\partial_\mu \ell = \frac{1}{v}\sum_i (x_i - \mu). It is zero when ∑ixi=nμ\sum_i x_i = n\mu, so μ^=xˉ\hat\mu = \bar x, for every vv.
    2. ∂vℓ=−n2v+12v2∑i(xi−μ^)2\partial_v \ell = -\frac{n}{2v} + \frac{1}{2v^2}\sum_i (x_i - \hat\mu)^2. Multiply by 2v22v^2: −nv+∑i(xi−xˉ)2=0-nv + \sum_i (x_i - \bar x)^2 = 0, so v^=1n∑i(xi−xˉ)2\hat v = \frac{1}{n}\sum_i (x_i - \bar x)^2.
    3. It is a maximum: for fixed vv, ℓ\ell is a downward parabola in μ\mu. With μ=xˉ\mu = \bar x and D=∑i(xi−xˉ)2>0D = \sum_i (x_i - \bar x)^2 > 0, the function −n2log⁡v−D2v-\frac{n}{2}\log v - \frac{D}{2v} increases for v<D/nv < D/n and decreases after, so v=D/nv = D/n is its maximum. (If all xix_i are equal, D=0D = 0 and ℓ→∞\ell \to \infty as v→0v \to 0: no MLE exists.)

    Invariance of the MLE. Let ϕ=g(θ)\phi = g(\theta) for a one-to-one map gg. The likelihood in the new parameter is L~(ϕ)=L(g−1(ϕ))\tilde L(\phi) = L(g^{-1}(\phi)). It takes the same values, only relabelled, so its maximum is at ϕ^=g(θ^)\hat\phi = g(\hat\theta). Examples: the MLE of σ\sigma is v^\sqrt{\hat v}; the MLE of the log-odds log⁡p1−p\log\frac{p}{1-p} is log⁡p^1−p^\log\frac{\hat p}{1-\hat p}; the MLE of the mean waiting time 1/λ1/\lambda is 1/λ^=xˉ1/\hat\lambda = \bar x.

    Estimators are random variables #

    An estimator is a rule θ^=θ^(X1,…,Xn)\hat\theta = \hat\theta(X_1, \dots, X_n) that turns data into a guess. Before the data are drawn, θ^\hat\theta is a random variable with its own distribution, the sampling distribution. For a true value θ\theta:

    • the bias is bias⁡(θ^)=Eθ[θ^]−θ\operatorname{bias}(\hat\theta) = \mathbb{E}_\theta[\hat\theta] - \theta; the estimator is unbiased if the bias is 0 for every θ\theta;
    • the variance is Var⁡θ(θ^)\operatorname{Var}_\theta(\hat\theta), and its square root is the standard error;
    • the mean squared error is MSE(θ^)=Eθ[(θ^−θ)2]\text{MSE}(\hat\theta) = \mathbb{E}_\theta[(\hat\theta - \theta)^2].

    A dartboard is the usual picture: the bias is how far the centre of the darts is from the bull's-eye, the variance is how spread out the darts are, and the MSE is the average squared distance to the bull's-eye.

    Theorem (bias–variance decomposition). MSE(θ^)=bias⁡(θ^)2+Var⁡(θ^)\text{MSE}(\hat\theta) = \operatorname{bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta).

    Proof.

    1. Write m=E[θ^]m = \mathbb{E}[\hat\theta] and b=m−θb = m - \theta (a constant). Split the error: θ^−θ=(θ^−m)+b\hat\theta - \theta = (\hat\theta - m) + b.
    2. Square: (θ^−θ)2=(θ^−m)2+2b (θ^−m)+b2(\hat\theta - \theta)^2 = (\hat\theta - m)^2 + 2b\,(\hat\theta - m) + b^2.
    3. Take expectations. E[θ^−m]=0\mathbb{E}[\hat\theta - m] = 0, so the middle term vanishes, and E[(θ^−θ)2]=Var⁡(θ^)+b2\mathbb{E}[(\hat\theta - \theta)^2] = \operatorname{Var}(\hat\theta) + b^2. ■\blacksquare

    Example: three ways to estimate a variance. Let X1,…,XnX_1, \dots, X_n be i.i.d. N(μ,σ2)\mathcal N(\mu, \sigma^2) and D=∑i(Xi−Xˉ)2D = \sum_i (X_i - \bar X)^2. Lesson 04 showed E[D]=(n−1)σ2\mathbb{E}[D] = (n-1)\sigma^2. For Gaussian data, D/σ2D/\sigma^2 has a chi-squared distribution with n−1n - 1 degrees of freedom, so Var⁡(D)=2(n−1)σ4\operatorname{Var}(D) = 2(n-1)\sigma^4. Consider the estimators D/cD/c:

    Divide by Name Bias Variance MSE at n=10n = 10, σ2=1\sigma^2 = 1
    c=n−1c = n - 1 S2S^2, unbiased 00 2σ4n−1\frac{2\sigma^4}{n-1} 0.2220.222
    c=nc = n σ^MLE2\hat\sigma^2_{\text{MLE}} −σ2n-\frac{\sigma^2}{n} 2(n−1)σ4n2\frac{2(n-1)\sigma^4}{n^2} 0.1900.190
    c=n+1c = n + 1 −2σ2n+1-\frac{2\sigma^2}{n+1} 2(n−1)σ4(n+1)2\frac{2(n-1)\sigma^4}{(n+1)^2} 0.1820.182

    The bias of the MLE comes from E[D/n]=n−1nσ2\mathbb{E}[D/n] = \frac{n-1}{n}\sigma^2. But the biased estimators have a smaller MSE: they pay a little bias for a larger cut in variance. For Gaussian data, c=n+1c = n + 1 gives the smallest MSE of all estimators of the form D/cD/c. Unbiased is not the same as best.

    Interactive figure: loading…

    Throw darts: draw samples and compare three estimators. Each press of Draw 500 simulates 500 samples of size nn from N(0,1)\mathcal N(0, 1), so the true variance is σ2=1\sigma^2 = 1 (dashed orange line). Each blue dot is one estimate; the black tick is the average of the dots in its row. The table compares the simulated bias, variance and MSE with the exact formulas. Notice that the unbiased row is centred on 1 but has the widest spread. Then move nn: all three rows shrink toward 1 (consistency), and their differences vanish.

    Another bias: a nonlinear function. For exponential data, Xˉ\bar X is unbiased for the mean 1/λ1/\lambda. But λ^=1/Xˉ\hat\lambda = 1/\bar X is not unbiased for λ\lambda. The sum ∑iXi\sum_i X_i has a Gamma(n,λn, \lambda) distribution (lesson 03), and a short integral gives, for n≥3n \ge 3,

    E[λ^]=nn−1λ,Var⁡(λ^)=n2λ2(n−1)2(n−2),MSE(λ^)=(n+2)λ2(n−1)(n−2).\mathbb{E}[\hat\lambda] = \frac{n}{n-1}\lambda, \qquad \operatorname{Var}(\hat\lambda) = \frac{n^2\lambda^2}{(n-1)^2(n-2)}, \qquad \text{MSE}(\hat\lambda) = \frac{(n+2)\lambda^2}{(n-1)(n-2)}.

    At n=10n = 10 and λ=1\lambda = 1: mean 1.1111.111, variance 0.1540.154, MSE 0.1670.167. The bias is upward, as Jensen's inequality predicts: t↦1/tt \mapsto 1/t is convex, so E[1/Xˉ]≥1/E[Xˉ]=λ\mathbb{E}[1/\bar X] \ge 1/\mathbb{E}[\bar X] = \lambda.

    Consistency. A sequence of estimators θ^n\hat\theta_n (one for each sample size) is consistent if θ^n→θ\hat\theta_n \to \theta in probability for every θ\theta: P(∣θ^n−θ∣>ε)→0P(|\hat\theta_n - \theta| > \varepsilon) \to 0 for every ε>0\varepsilon > 0.

    • MSE → 0 is enough. By Markov's inequality applied to (θ^n−θ)2(\hat\theta_n - \theta)^2 (lesson 06), P(∣θ^n−θ∣>ε)≤MSE(θ^n)/ε2→0P(|\hat\theta_n - \theta| > \varepsilon) \le \text{MSE}(\hat\theta_n)/\varepsilon^2 \to 0.
    • Xˉ\bar X is consistent (law of large numbers). σ^MLE2\hat\sigma^2_{\text{MLE}} is biased but consistent: its bias −σ2/n-\sigma^2/n and its variance both go to 0. And 1/Xˉ1/\bar X is consistent for λ\lambda, because t↦1/tt \mapsto 1/t is continuous at 1/λ1/\lambda (continuous mapping).
    • Unbiased does not imply consistent: X1X_1 alone is an unbiased estimate of the mean, but its spread never shrinks.
    • Under regularity conditions, the MLE is consistent. The KL view below explains why.
    Quick check

    For a coin with true p=0.6p = 0.6 and n=10n = 10 flips, compare the MLE p^=k/n\hat p = k/n with the silly estimator p~=0.5\tilde p = 0.5 that ignores the data. Which has the smaller MSE?

    Why maximum likelihood: the KL view #

    The Kullback–Leibler divergence between distributions pp and qq is

    KL(p ∥ q)=EX∼p[log⁡p(X)q(X)] ≥ 0,\mathrm{KL}(p \,\|\, q) = \mathbb{E}_{X \sim p}\Big[\log\frac{p(X)}{q(X)}\Big] \ \ge\ 0,

    with equality only if p=qp = q (Gibbs' inequality, a consequence of Jensen's inequality; information theory 03). Let pdatap_{\text{data}} be the true distribution of the data, and pθp_\theta the model.

    1. By the law of large numbers, 1nℓ(θ)=1n∑ilog⁡pθ(Xi)→Epdata[log⁡pθ(X)]\frac{1}{n}\ell(\theta) = \frac{1}{n}\sum_i \log p_\theta(X_i) \to \mathbb{E}_{p_{\text{data}}}[\log p_\theta(X)].
    2. Add and subtract log⁡pdata\log p_{\text{data}}: Epdata[log⁡pθ(X)]=Epdata[log⁡pdata(X)]−KL(pdata ∥ pθ)\mathbb{E}_{p_{\text{data}}}[\log p_\theta(X)] = \mathbb{E}_{p_{\text{data}}}[\log p_{\text{data}}(X)] - \mathrm{KL}(p_{\text{data}} \,\|\, p_\theta).
    3. The first term does not depend on θ\theta. So maximising the expected log-likelihood is the same as minimising KL(pdata ∥ pθ)\mathrm{KL}(p_{\text{data}} \,\|\, p_\theta), and the same as minimising the cross-entropy −Epdata[log⁡pθ(X)]-\mathbb{E}_{p_{\text{data}}}[\log p_\theta(X)].
    4. For finite nn the same identity holds exactly with the empirical distribution p^n\hat p_n (mass 1/n1/n on each data point) in place of pdatap_{\text{data}}: the MLE minimises the cross-entropy between p^n\hat p_n and pθp_\theta.

    Three consequences:

    • Consistency. If the model is correct (pdata=pθ0p_{\text{data}} = p_{\theta_0}) and identifiable (different θ\theta give different distributions), the KL is zero only at θ0\theta_0. So the limit of 1nℓ\frac{1}{n}\ell has its unique maximum at the truth. (Turning this into θ^n→θ0\hat\theta_n \to \theta_0 needs a uniform law of large numbers or concavity.)
    • Misspecification. If no θ\theta fits exactly, the MLE still converges, to the θ∗\theta^* that minimises KL(pdata ∥ pθ)\mathrm{KL}(p_{\text{data}} \,\|\, p_\theta): the closest model in this sense.
    • Losses are NLLs. The average negative log-likelihood is the training loss:
    Model for the target yy given input xx Negative log-likelihood of one example Name of the loss
    y∼N(f(x),σ2)y \sim \mathcal N(f(x), \sigma^2), σ\sigma fixed (y−f(x))22σ2+const\frac{(y - f(x))^2}{2\sigma^2} + \text{const} mean squared error
    y∼Bernoulli(σ(z(x)))y \sim \text{Bernoulli}(\sigma(z(x))), logit zz −ylog⁡σ(z)−(1−y)log⁡(1−σ(z))-y\log\sigma(z) - (1-y)\log(1 - \sigma(z)) binary cross-entropy
    y∼Categorical(softmax⁡(z(x)))y \sim \text{Categorical}(\operatorname{softmax}(z(x))) −log⁡softmax⁡(z)y-\log \operatorname{softmax}(z)_y softmax cross-entropy
    y∼N(μ(x),σ(x)2)y \sim \mathcal N(\mu(x), \sigma(x)^2), both predicted 12log⁡σ2+(y−μ)22σ2+const\frac12\log\sigma^2 + \frac{(y - \mu)^2}{2\sigma^2} + \text{const} Gaussian NLL
    Quick check

    A regression network minimises the mean absolute error 1n∑i∣yi−f(xi)∣\frac{1}{n}\sum_i |y_i - f(x_i)|. For which noise model is this maximum likelihood?

    The exponential family #

    Start with the coin. Rewrite the Bernoulli PMF with exp⁡\exp and log⁡\log:

    p(x∣p)=px(1−p)1−x=exp⁡(xlog⁡p+(1−x)log⁡(1−p))=exp⁡(xlog⁡p1−p+log⁡(1−p)).\begin{aligned} p(x \mid p) = p^x(1-p)^{1-x} &= \exp\Big(x\log p + (1-x)\log(1-p)\Big) \\ &= \exp\Big(x \log\frac{p}{1-p} + \log(1-p)\Big). \end{aligned}

    The log-probability is linear in the data xx, with the coefficient η=log⁡p1−p\eta = \log\frac{p}{1-p}, the log-odds. The rest, log⁡(1−p)\log(1-p), does not depend on xx. Solve for pp: p=eη1+eη=σ(η)p = \frac{e^\eta}{1 + e^\eta} = \sigma(\eta), the sigmoid (foundations 05). So log⁡(1−p)=−log⁡(1+eη)\log(1 - p) = -\log(1 + e^\eta), and

    p(x∣η)=exp⁡(ηx−A(η)),A(η)=log⁡(1+eη).p(x \mid \eta) = \exp\big(\eta x - A(\eta)\big), \qquad A(\eta) = \log(1 + e^\eta).

    Differentiate AA: A′(η)=eη1+eη=p=E[X]A'(\eta) = \frac{e^\eta}{1 + e^\eta} = p = \mathbb{E}[X], and A′′(η)=p(1−p)=Var⁡(X)A''(\eta) = p(1-p) = \operatorname{Var}(X). The derivatives of the normaliser are the mean and the variance. This is not a coincidence.

    Definition. A family of densities (or PMFs) is an exponential family if it can be written as

    p(x∣η)=h(x) exp⁡(η⊤T(x)−A(η)),A(η)=log⁡∫h(x) eη⊤T(x) dxp(x \mid \eta) = h(x)\, \exp\big(\eta^\top T(x) - A(\eta)\big), \qquad A(\eta) = \log \int h(x)\, e^{\eta^\top T(x)}\, dx

    (a sum instead of the integral for discrete xx). The parts:

    • η∈Rd\eta \in \mathbb{R}^d is the natural parameter;
    • T(x)∈RdT(x) \in \mathbb{R}^d is the sufficient statistic;
    • h(x)≥0h(x) \ge 0 is the base measure; it does not depend on η\eta;
    • A(η)A(\eta) is the log-partition function; it makes the total mass 1.

    Because hh does not depend on η\eta, the support (where p>0p > 0) is the same for every η\eta.

    Distribution η\eta T(x)T(x) h(x)h(x) A(η)A(\eta)
    Bernoulli(pp) log⁡p1−p\log\frac{p}{1-p} xx 11 log⁡(1+eη)\log(1 + e^\eta)
    Poisson(λ\lambda) log⁡λ\log\lambda xx 1/x!1/x! eηe^\eta
    Exponential(λ\lambda) −λ-\lambda xx 11 on x≥0x \ge 0 −log⁡(−η)-\log(-\eta)
    N(μ,σ2)\mathcal N(\mu, \sigma^2) (μσ2, −12σ2)\big(\frac{\mu}{\sigma^2},\, -\frac{1}{2\sigma^2}\big) (x, x2)(x,\, x^2) 12π\frac{1}{\sqrt{2\pi}} −η124η2−12log⁡(−2η2)-\frac{\eta_1^2}{4\eta_2} - \frac12\log(-2\eta_2)
    Gamma(k,βk, \beta), rate β\beta (k−1, −β)(k - 1,\, -\beta) (log⁡x, x)(\log x,\, x) 11 on x>0x > 0 log⁡Γ(η1+1)−(η1+1)log⁡(−η2)\log\Gamma(\eta_1 + 1) - (\eta_1 + 1)\log(-\eta_2)
    • Gaussian. Expand the square: −(x−μ)22σ2=μσ2x−12σ2x2−μ22σ2-\frac{(x-\mu)^2}{2\sigma^2} = \frac{\mu}{\sigma^2}x - \frac{1}{2\sigma^2}x^2 - \frac{\mu^2}{2\sigma^2}. The normaliser is μ22σ2+log⁡σ\frac{\mu^2}{2\sigma^2} + \log\sigma; written in η\eta it is the entry of the table.
    • Gamma (rate form, lesson 03): βkxk−1e−βxΓ(k)=exp⁡((k−1)log⁡x−βx−(log⁡Γ(k)−klog⁡β))\frac{\beta^k x^{k-1} e^{-\beta x}}{\Gamma(k)} = \exp\big((k-1)\log x - \beta x - (\log\Gamma(k) - k\log\beta)\big). The exponential is the case k=1k = 1.
    • Not exponential families: Uniform(0,θ0, \theta) (its support depends on θ\theta), the Student-t and the Cauchy with an unknown centre, and mixtures.

    Theorem (the log-partition function gives the moments). For η\eta in the interior of the set where A(η)<∞A(\eta) < \infty,

    ∇A(η)=Eη[T(X)],∇2A(η)=Cov⁡η[T(X)].\nabla A(\eta) = \mathbb{E}_\eta[T(X)], \qquad \nabla^2 A(\eta) = \operatorname{Cov}_\eta[T(X)].
    Proof: differentiate the normaliser
    1. Write Z(η)=eA(η)=∫h(x) eη⊤T(x) dxZ(\eta) = e^{A(\eta)} = \int h(x)\, e^{\eta^\top T(x)}\, dx. In the interior of the parameter set you may differentiate under the integral: ∇Z(η)=∫T(x) h(x) eη⊤T(x) dx\nabla Z(\eta) = \int T(x)\, h(x)\, e^{\eta^\top T(x)}\, dx.
    2. ∇A=∇log⁡Z=∇ZZ=∫T(x) h(x) eη⊤T(x)−A(η) dx=Eη[T(X)]\nabla A = \nabla \log Z = \frac{\nabla Z}{Z} = \int T(x)\, h(x)\, e^{\eta^\top T(x) - A(\eta)}\, dx = \mathbb{E}_\eta[T(X)].
    3. For the second derivative, note that ∇η p(x∣η)=p(x∣η) (T(x)−∇A(η))\nabla_\eta\, p(x \mid \eta) = p(x \mid \eta)\,\big(T(x) - \nabla A(\eta)\big). Differentiate step 2 once more:
    ∇2A=∫T(x) (T(x)−∇A(η))⊤p(x∣η) dx=E[TT⊤]−E[T] E[T]⊤=Cov⁡η[T(X)].■\nabla^2 A = \int T(x)\,\big(T(x) - \nabla A(\eta)\big)^\top p(x \mid \eta)\, dx = \mathbb{E}[TT^\top] - \mathbb{E}[T]\,\mathbb{E}[T]^\top = \operatorname{Cov}_\eta[T(X)]. \quad\blacksquare

    Consequences.

    • AA is convex. A covariance matrix is positive semidefinite (lesson 04), so ∇2A⪰0\nabla^2 A \succeq 0.
    • The log-likelihood is concave in η\eta. For i.i.d. data, ℓ(η)=∑ilog⁡h(xi)+η⊤∑iT(xi)−nA(η)\ell(\eta) = \sum_i \log h(x_i) + \eta^\top \sum_i T(x_i) - nA(\eta): a linear function minus nn times a convex one. So any stationary point is a global maximum.
    • The MLE matches moments. ∇ℓ(η)=∑iT(xi)−n∇A(η)=0\nabla \ell(\eta) = \sum_i T(x_i) - n\nabla A(\eta) = 0 gives
    Eη^[T(X)]=1n∑i=1nT(xi).\mathbb{E}_{\hat\eta}[T(X)] = \frac{1}{n}\sum_{i=1}^n T(x_i).

    The fitted model reproduces the sample averages of the sufficient statistic. For the Gaussian, T=(x,x2)T = (x, x^2): the fitted model has the data's mean and mean square, which is exactly μ^=xˉ\hat\mu = \bar x, σ^2=x2‾−xˉ2\hat\sigma^2 = \overline{x^2} - \bar x^2. All four rows of the MLE table above are this rule.

    Gamma: matching needs a computer. For the gamma, ∇A=(ψ(k)−log⁡β, k/β)=(E[log⁡X], E[X])\nabla A = (\psi(k) - \log\beta,\ k/\beta) = (\mathbb{E}[\log X],\ \mathbb{E}[X]), where ψ=Γ′/Γ\psi = \Gamma'/\Gamma is the digamma function. So the MLE solves

    ψ(k^)−log⁡β^=1n∑ilog⁡xi,k^β^=xˉ.\psi(\hat k) - \log\hat\beta = \frac{1}{n}\sum_i \log x_i, \qquad \frac{\hat k}{\hat\beta} = \bar x .

    Put β^=k^/xˉ\hat\beta = \hat k/\bar x into the first equation: log⁡k^−ψ(k^)=log⁡xˉ−1n∑ilog⁡xi\log\hat k - \psi(\hat k) = \log\bar x - \frac{1}{n}\sum_i\log x_i. The right side is ≥0\ge 0 by Jensen's inequality (log⁡\log is concave). There is no closed form; Newton's method solves it in a few steps (notebook exercise 3).

    Interactive figure: loading…

    Fit a gamma by matching two averages. The blue histogram is a data set of 200 positive numbers; the orange curve is the gamma density with your shape kk and rate β\beta. The two readout rows compare the model's E[log⁡X]\mathbb{E}[\log X] and E[X]\mathbb{E}[X] with the data's averages of log⁡x\log x and xx. Move the sliders until both rows match, then press Jump to MLE: the MLE is the one point where both match, and there the average log-likelihood is largest.

    Sufficiency. A statistic T(X)T(X) is sufficient for θ\theta if the conditional distribution of the data given T(X)T(X) does not depend on θ\theta. Once you know TT, the rest of the data tells you nothing more about θ\theta.

    Theorem (factorisation, Fisher–Neyman). TT is sufficient for θ\theta if and only if p(x∣θ)=g(T(x),θ) h(x)p(x \mid \theta) = g(T(x), \theta)\, h(x) for some functions gg and hh.

    Proof (discrete case)
    1. (⇐) Fix xx and let t=T(x)t = T(x). Then Pθ(X=x∣T=t)=g(t,θ) h(x)∑x′:T(x′)=tg(t,θ) h(x′)=h(x)∑x′:T(x′)=th(x′)P_\theta(X = x \mid T = t) = \dfrac{g(t,\theta)\,h(x)}{\sum_{x' : T(x') = t} g(t,\theta)\,h(x')} = \dfrac{h(x)}{\sum_{x' : T(x') = t} h(x')}, which does not depend on θ\theta.
    2. (⇒) Take g(t,θ)=Pθ(T=t)g(t, \theta) = P_\theta(T = t) and h(x)=P(X=x∣T=T(x))h(x) = P(X = x \mid T = T(x)), which is free of θ\theta by sufficiency. Then p(x∣θ)=Pθ(T=T(x)) P(X=x∣T=T(x))=g(T(x),θ) h(x)p(x \mid \theta) = P_\theta(T = T(x))\, P(X = x \mid T = T(x)) = g(T(x), \theta)\, h(x). ■\blacksquare

    For i.i.d. data from an exponential family, p(x1,…,xn∣η)=(∏ih(xi))exp⁡(η⊤∑iT(xi)−nA(η))p(x_1, \dots, x_n \mid \eta) = \big(\prod_i h(x_i)\big)\exp\big(\eta^\top \sum_i T(x_i) - nA(\eta)\big), so ∑iT(xi)\sum_i T(x_i) (with nn) is sufficient. Its size dd does not grow with nn. Coin: the number of heads. Gaussian: (∑ixi,∑ixi2)(\sum_i x_i, \sum_i x_i^2). Gamma: (∑ilog⁡xi,∑ixi)(\sum_i \log x_i, \sum_i x_i). The same structure gives every exponential family a conjugate prior (lesson 10).

    Quick check

    You fit a Poisson model to counts by maximum likelihood. Which equation does λ^\hat\lambda solve, according to the exponential-family theorem?

    When maximum likelihood fails #

    • Zero counts. A new web page variant has k=0k = 0 conversions in n=20n = 20 visits. The MLE is p^=0\hat p = 0: "this page never converts". In the natural parameter, η^=log⁡01=−∞\hat\eta = \log\frac{0}{1} = -\infty: the maximum is not attained. The same happens for all-zero Poisson counts and for logistic regression on data that a line separates perfectly (the weights grow without bound). A prior fixes this (lesson 10).
    • No unique maximum. If the model is not identifiable, a whole set of θ\theta values gives the same likelihood.
    • Small samples. The MLE can be biased (the variance by (n−1)/n(n-1)/n, the rate by n/(n−1)n/(n-1)); the bias shrinks like 1/n1/n.
    An unbounded likelihood: Gaussian mixtures

    Fit the mixture 12N(μ1,σ12)+12N(μ2,σ22)\frac12\mathcal N(\mu_1, \sigma_1^2) + \frac12\mathcal N(\mu_2, \sigma_2^2) to data x1,…,xnx_1, \dots, x_n with at least two different values. Put μ1=x1\mu_1 = x_1 and let σ1→0\sigma_1 \to 0. The density at x1x_1 contains the term 122πσ1\frac{1}{2\sqrt{2\pi}\sigma_1}, which goes to infinity. Every other point keeps a density of at least 12N(xi;μ2,σ22)>0\frac12\mathcal N(x_i; \mu_2, \sigma_2^2) > 0 from the second component. So L→∞L \to \infty: the "MLE" is a spike on one data point, a useless model. In practice, EM for Gaussian mixtures needs a floor on the variances or a prior on them (this is MAP estimation, lesson 10). The KL view does not apply here, because no θ\theta achieves the supremum.

    Answers to section 1. (1) Too small, by the factor (n−1)/n=0.9(n-1)/n = 0.9: E[σ^2]=0.9 σ2\mathbb{E}[\hat\sigma^2] = 0.9\,\sigma^2. The 1/n1/n variance of 10 seeds is 10% low on average. Divide by n−1=9n - 1 = 9 for an unbiased estimate. (2) By invariance, the MLE of θ=1/λ\theta = 1/\lambda is 1/λ^=xˉ1/\hat\lambda = \bar x. But λ^=1/xˉ\hat\lambda = 1/\bar x is biased upward: E[λ^]=nn−1λ\mathbb{E}[\hat\lambda] = \frac{n}{n-1}\lambda. The MLE keeps its form under a change of parameter; unbiasedness does not survive a nonlinear map.

    4. Worked example #

    (a) A kitchen scale. You weigh a 1 kg reference weight four times. The readings minus 1000 g are x=(2.1, −0.3, 3.0, 0.4)x = (2.1,\ -0.3,\ 3.0,\ 0.4) g. Model: xi∼N(μ,σ2)x_i \sim \mathcal N(\mu, \sigma^2) i.i.d., where μ\mu is the scale's offset and σ\sigma its noise.

    1. Sufficient statistics. ∑ixi=2.1−0.3+3.0+0.4=5.2\sum_i x_i = 2.1 - 0.3 + 3.0 + 0.4 = 5.2 and ∑ixi2=4.41+0.09+9.00+0.16=13.66\sum_i x_i^2 = 4.41 + 0.09 + 9.00 + 0.16 = 13.66. For this model, nothing else in the data matters.
    2. MLE of the offset. μ^=5.2/4=1.3\hat\mu = 5.2/4 = 1.3 g.
    3. MLE of the variance by moment matching on x2x^2. μ^2+σ^2=13.66/4=3.415\hat\mu^2 + \hat\sigma^2 = 13.66/4 = 3.415, so σ^2=3.415−1.69=1.725\hat\sigma^2 = 3.415 - 1.69 = 1.725 g².
    4. Check with the deviations. xi−xˉ=(0.8, −1.6, 1.7, −0.9)x_i - \bar x = (0.8,\ -1.6,\ 1.7,\ -0.9). Their squares are 0.64,2.56,2.89,0.810.64, 2.56, 2.89, 0.81, with sum 6.96.9. And 6.9/4=1.7256.9/4 = 1.725 ✓.
    5. Bias. E[σ^2]=34σ2\mathbb{E}[\hat\sigma^2] = \frac{3}{4}\sigma^2: with only 4 readings, the MLE is 25% low on average. The unbiased estimate is S2=6.9/3=2.3S^2 = 6.9/3 = 2.3 g².
    6. Invariance. The MLE of the noise standard deviation is σ^=1.725≈1.313\hat\sigma = \sqrt{1.725} \approx 1.313 g.

    (b) Waiting times. A help desk records the times between 5 calls: their sum is S=10S = 10 minutes. Model: i.i.d. Exponential(λ\lambda).

    1. ℓ(λ)=nlog⁡λ−λS=5log⁡λ−10λ\ell(\lambda) = n\log\lambda - \lambda S = 5\log\lambda - 10\lambda.
    2. ℓ′(λ)=5/λ−10=0\ell'(\lambda) = 5/\lambda - 10 = 0 gives λ^=0.5\hat\lambda = 0.5 calls per minute. ℓ′′(λ)=−5/λ2<0\ell''(\lambda) = -5/\lambda^2 < 0: a maximum.
    3. Invariance: the MLE of the mean waiting time is 1/λ^=21/\hat\lambda = 2 minutes =xˉ= \bar x.
    4. Bias: E[λ^]=nn−1λ=1.25 λ\mathbb{E}[\hat\lambda] = \frac{n}{n-1}\lambda = 1.25\,\lambda. With 5 observations, the rate estimate is 25% too high on average. n−1nλ^=n−1S=0.4\frac{n-1}{n}\hat\lambda = \frac{n-1}{S} = 0.4 is unbiased.

    (c) The coin in natural form. For k=7k = 7 heads in n=10n = 10 flips, the log-likelihood in the log-odds η\eta is ℓ(η)=7η−10log⁡(1+eη)\ell(\eta) = 7\eta - 10\log(1 + e^\eta).

    1. ℓ′(η)=7−10 σ(η)=0\ell'(\eta) = 7 - 10\,\sigma(\eta) = 0 gives σ(η^)=0.7\sigma(\hat\eta) = 0.7: the model mean matches the sample mean.
    2. η^=log⁡0.70.3≈0.847\hat\eta = \log\frac{0.7}{0.3} \approx 0.847, which is log⁡p^1−p^\log\frac{\hat p}{1-\hat p} (invariance).
    3. ℓ′′(η)=−10 σ(η)(1−σ(η))=−2.1\ell''(\eta) = -10\,\sigma(\eta)(1 - \sigma(\eta)) = -2.1 at η^\hat\eta, negative for every η\eta: ℓ\ell is concave in η\eta.

    5. In code #

    NumPy and SciPy check the worked example, the bias of the variance estimators and the gamma moment matching. The notebook has these cells and the exercises.

    import numpy as np
    from scipy import stats, special
    
    # (a) kitchen scale: MLE mean, MLE variance (ddof=0) and the unbiased S^2 (ddof=1)
    x = np.array([2.1, -0.3, 3.0, 0.4])
    print(x.mean(), x.var(), round(x.var(ddof=1), 6))      # 1.3 1.725 2.3
    
    # predict-first 1: 10 seeds; the 1/n estimate averages 0.9 * sigma^2
    rng = np.random.default_rng(1)
    runs = rng.normal(0.80, 0.02, size=(200_000, 10))      # accuracy 0.80, sd 0.02
    print(round(runs.var(axis=1).mean() / 0.02**2, 3))    # 0.898 (exact: 0.9)
    
    # bias of 1/xbar for exponential data, n = 10, lambda = 1 (exact: mean 1.111, MSE 0.167)
    lam_hat = 1 / rng.exponential(1.0, size=(400_000, 10)).mean(axis=1)
    print(round(lam_hat.mean(), 3), round(((lam_hat - 1) ** 2).mean(), 3))   # 1.111 0.166
    
    # gamma MLE = moment matching on T = (log x, x); NumPy and SciPy use scale = 1 / rate
    g = np.random.default_rng(3).gamma(shape=2.0, scale=1 / 0.5, size=200)
    k_hat, _, scale_hat = stats.gamma.fit(g, floc=0)
    b_hat = 1 / scale_hat
    print(np.isclose(special.digamma(k_hat) - np.log(b_hat), np.log(g).mean()))   # True
    print(np.isclose(k_hat / b_hat, g.mean()))                                     # True
    

    The printed Monte Carlo values move a little with the seed; their errors are about ±0.002\pm 0.002 here. The exact values come from the formulas of section 3.

    6. Where it shows up #

    • Every supervised loss. Mean squared error, binary and softmax cross-entropy are Gaussian, Bernoulli and categorical NLLs. For the categorical, the logits zz are the natural parameters, A(z)=log⁡∑kezkA(z) = \log\sum_k e^{z_k} and ∇A=softmax⁡(z)\nabla A = \operatorname{softmax}(z). So the gradient of the loss with respect to the logits is "softmax minus one-hot", the rule of calculus 06.
    • Generalised linear models. With the canonical link, ηi=w⊤xi\eta_i = w^\top x_i, and the NLL is ∑i[A(w⊤xi)−yi w⊤xi]+const\sum_i \big[A(w^\top x_i) - y_i\, w^\top x_i\big] + \text{const}: convex in ww, with gradient ∑i(y^i−yi) xi\sum_i (\hat y_i - y_i)\, x_i, where y^i=A′(w⊤xi)\hat y_i = A'(w^\top x_i). Linear, logistic and Poisson regression have the same form.
    • Language models. Pretraining minimises the average NLL of the next token: the MLE of a categorical model. Perplexity is eaverage NLLe^{\text{average NLL}}.
    • Models that predict their own uncertainty. Heteroscedastic regression, probabilistic weather models and surrogates of simulators predict a mean and a variance and minimise the Gaussian NLL.
    • Science. Gamma fits of rainfall amounts (the Standardized Precipitation Index), exponential fits of the magnitudes of earthquakes above a threshold (Aki's estimator of the Gutenberg–Richter bb-value is an exponential MLE), Poisson rates of photon counts in astronomy.
    • Evaluation. Reporting the variance of a metric over a handful of seeds or folds with the 1/n1/n formula underestimates it; with 3 seeds the factor is 2/32/3.

    7. Common mistakes #

    • Treating the likelihood as a probability of θ\theta. L(θ)L(\theta) is the probability of the data. It does not integrate to 1 over θ\theta, and "the probability that p=0.7p = 0.7" needs a prior (lesson 10).
    • Thinking unbiased means best. Compare MSEs. The 1/n1/n and 1/(n+1)1/(n+1) variance estimators beat the unbiased S2S^2 in MSE for Gaussian data.
    • Expecting unbiasedness to survive a transformation. S2S^2 is unbiased for σ2\sigma^2, but SS is biased for σ\sigma (Jensen). The MLE does survive transformations (invariance).
    • Differentiating AA in the wrong parameters. ∇A=E[T]\nabla A = \mathbb{E}[T] holds for the natural parameter η\eta, not for (μ,σ)(\mu, \sigma) or (k,β)(k, \beta).
    • Mixing up rate and scale. These lessons write the exponential and gamma with the rate. NumPy and SciPy use the scale 1/β1/\beta.
    • Assuming the MLE always exists. It does not for all-zero counts, separable data in logistic regression, constant data in a Gaussian or gamma fit, or Gaussian mixtures.
    • Maximising the likelihood with a product. Multiply 1000 probabilities and you get 0 in floating point. Always sum log-probabilities.

    8. Check yourself #

    1. Write the exponential distribution in exponential-family form, and get its mean and variance from AA.
      Answerp(x)=λe−λx=exp⁡(−λx+log⁡λ)p(x) = \lambda e^{-\lambda x} = \exp(-\lambda x + \log\lambda) on x≥0x \ge 0. So η=−λ\eta = -\lambda, T(x)=xT(x) = x, h(x)=1h(x) = 1 on x≥0x \ge 0, and A(η)=−log⁡λ=−log⁡(−η)A(\eta) = -\log\lambda = -\log(-\eta). Then A′(η)=−1/η=1/λ=E[X]A'(\eta) = -1/\eta = 1/\lambda = \mathbb{E}[X] and A′′(η)=1/η2=1/λ2=Var⁡(X)A''(\eta) = 1/\eta^2 = 1/\lambda^2 = \operatorname{Var}(X).
    2. A website gets x=(5,7,4,6,3)x = (5, 7, 4, 6, 3) sign-ups in five hours, modelled as i.i.d. Poisson(λ\lambda). Give the MLE, check that it is a maximum, and give the MLE of P(no sign-up in an hour)=e−λP(\text{no sign-up in an hour}) = e^{-\lambda}.
      Answerℓ(λ)=25log⁡λ−5λ+const\ell(\lambda) = 25\log\lambda - 5\lambda + \text{const}, so ℓ′(λ)=25/λ−5=0\ell'(\lambda) = 25/\lambda - 5 = 0 gives λ^=5\hat\lambda = 5. ℓ′′(λ)=−25/λ2<0\ell''(\lambda) = -25/\lambda^2 < 0: a maximum. By invariance, the MLE of e−λe^{-\lambda} is e−5≈0.0067e^{-5} \approx 0.0067.
    3. S2S^2 is unbiased for σ2\sigma^2. Is SS unbiased for σ\sigma?
      AnswerNo. The square root is strictly concave, so by Jensen E[S]<E[S2]=σ\mathbb{E}[S] < \sqrt{\mathbb{E}[S^2]} = \sigma (strict, because SS is not constant). For Gaussian data with n=5n = 5, E[S]≈0.94 σ\mathbb{E}[S] \approx 0.94\,\sigma.
    4. Which of these estimators of a mean are consistent? (a) X1X_1; (b) Xˉ+5/n\bar X + 5/n; (c) Xˉ\bar X for data from a Cauchy distribution centred at mm, as an estimator of mm.
      Answer(a) No: it is unbiased, but its distribution does not change with nn. (b) Yes: the bias 5/n→05/n \to 0 and the variance σ2/n→0\sigma^2/n \to 0, so the MSE goes to 0. (c) No: the mean of nn i.i.d. Cauchy variables has the same Cauchy distribution for every nn. The Cauchy has no mean, so the law of large numbers does not apply. The sample median is consistent for mm.
    5. Show that the binary cross-entropy of a logistic model, −∑i[yilog⁡σ(zi)+(1−yi)log⁡(1−σ(zi))]-\sum_i [y_i\log\sigma(z_i) + (1 - y_i)\log(1 - \sigma(z_i))], is the negative Bernoulli log-likelihood, and give its derivative with respect to one logit ziz_i.
      AnswerThe Bernoulli PMF with pi=σ(zi)p_i = \sigma(z_i) is piyi(1−pi)1−yip_i^{y_i}(1 - p_i)^{1 - y_i}; its log is yilog⁡pi+(1−yi)log⁡(1−pi)y_i\log p_i + (1 - y_i)\log(1 - p_i). Sum and negate: that is the loss. In natural form, the term is −(yizi−log⁡(1+ezi))-(y_i z_i - \log(1 + e^{z_i})), so the derivative is σ(zi)−yi\sigma(z_i) - y_i: prediction minus target.
    6. What are the sufficient statistics of a gamma sample? Why does a gamma fit fail for constant data?
      AnswerT(x)=(log⁡x,x)T(x) = (\log x, x), so (∑ilog⁡xi,∑ixi)(\sum_i \log x_i, \sum_i x_i), or the geometric and the arithmetic mean. The shape equation is log⁡k−ψ(k)=log⁡xˉ−log⁡x‾\log k - \psi(k) = \log\bar x - \overline{\log x}. For constant data the right side is 0 (the two means are equal), and log⁡k−ψ(k)>0\log k - \psi(k) > 0 for every k>0k > 0, decreasing to 0 only as k→∞k \to \infty. So no MLE exists: the fit wants a spike of infinite shape.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. What is the likelihood L(θ)L(\theta)?

    2. MLE of a Bernoulli pp from kk successes in nn trials?

    3. Gaussian MLEs of μ\mu and σ2\sigma^2?

    4. State the bias–variance decomposition of the MSE.

    5. What is E[σ^MLE2]\mathbb{E}[\hat\sigma^2_{\text{MLE}}] for nn Gaussian observations?

    6. What does consistency of an estimator mean, and what is a sufficient condition?

    7. Why does maximum likelihood minimise a KL divergence?

    8. Write the general exponential-family form.

    9. What are ∇A(η)\nabla A(\eta) and ∇2A(η)\nabla^2 A(\eta) in an exponential family?

    10. What is the invariance property of the MLE?

    Open the review →

    9. Interview angle #

    • "Derive the MLE of a Gaussian. Why does the unbiased variance divide by n−1n - 1?" — μ^=xˉ\hat\mu = \bar x and σ^2=1n∑i(xi−xˉ)2\hat\sigma^2 = \frac1n\sum_i (x_i - \bar x)^2; because E∑i(Xi−Xˉ)2=(n−1)σ2\mathbb{E}\sum_i (X_i - \bar X)^2 = (n - 1)\sigma^2: the deviations are measured from Xˉ\bar X, which is fitted to the same data.
    • "Mean squared error and cross-entropy: which likelihoods?" — Gaussian with a fixed variance; Bernoulli or categorical. Minimising them is maximum likelihood, which minimises KL(pdata ∥ pθ)\mathrm{KL}(p_{\text{data}} \,\|\, p_\theta).
    • "Is an unbiased estimator always better?" — No. MSE=bias2+variance\text{MSE} = \text{bias}^2 + \text{variance}; shrinkage (ridge, MAP, James–Stein) trades bias for variance.
    • "Why is the logistic-regression loss convex?" — It is an exponential-family NLL with a natural parameter linear in ww: the convex A(w⊤xi)A(w^\top x_i) minus a linear term.
    • "Your logistic regression weights blow up. Why?" — The data are linearly separable, so the likelihood keeps increasing as ∥w∥→∞\|w\| \to \infty; the MLE does not exist. Add L2 regularisation (a prior).
    • "What is a sufficient statistic? Give one for a Gaussian." — A summary that carries all the information about θ\theta; (∑ixi,∑ixi2)(\sum_i x_i, \sum_i x_i^2).

    10. Practice #

    The notebook for this lesson has five exercises, each with a check cell and a folded solution:

    1. The MLE of a Poisson rate by a grid search over the log-likelihood, compared with the closed form xˉ\bar x.
    2. Bias, variance and MSE by simulation (mc_bias_variance), applied to the Gaussian variance estimators with 1/(n−1)1/(n-1), 1/n1/n and 1/(n+1)1/(n+1).
    3. The gamma MLE by Newton's method on log⁡k−ψ(k)=log⁡xˉ−log⁡x‾\log k - \psi(k) = \log\bar x - \overline{\log x}, compared with scipy.stats.gamma.fit(x, floc=0).
    4. Bias, variance and MSE of 1/xˉ1/\bar x for exponential data with n=10n = 10, against the closed form.
    5. The cross-entropy loss of a logistic model equals the Bernoulli NLL; check it and its gradient numerically.

    11. Watch and read #

    The idea of the likelihood as a curve over the parameter, with a fitted distribution moving over the data.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The Gaussian MLE derived slowly, with the log-likelihood and both partial derivatives.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The MLE as an estimate of the KL divergence, with Bernoulli, Poisson and Gaussian examples.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play