Practice / Probability for ML

Maximum likelihood estimation: derivations by hand

Ten problems on maximum likelihood estimation: the Bernoulli, categorical, Poisson and Gaussian MLEs by hand, the curvature of the log-likelihood, why the variance MLE divides by N and is biased, least squares and cross-entropy as negative log-likelihoods, MAP with a Gaussian or Laplace prior as ridge and lasso with the exact λ, and the uniform MLE that no derivative finds, with worked solutions and the mistakes that maximise the wrong thing.

Before you start

Almost every loss in machine learning is a negative log-likelihood in disguise. Least squares is the likelihood of Gaussian noise, binary and softmax cross-entropy are the likelihoods of Bernoulli and categorical labels, and weight decay is a Gaussian prior on the weights. These ten problems derive the standard maximum likelihood estimates by hand, check that each stationary point really is a maximum, work out what the estimates get wrong on average, and then turn the regression losses of the previous pages back into the probability models they came from, priors included. The five mistakes at the end are the ones that give a plausible number: the unbiased variance passed off as the maximum likelihood one, the exponential distribution's answer used for the Poisson, a constraint forgotten, a prior strength off by a factor of NN, and a moment match used where the derivative has no zero.

  • The data x1,…,xNx_1, \dots, x_N are independent draws from a distribution with density or probability mass p(x;θ)p(x;\theta), where θ\theta is the parameter to estimate.
  • The likelihood is L(θ)=∏np(xn;θ)L(\theta) = \prod_n p(x_n;\theta), the probability of the data as a function of θ\theta. The log-likelihood is ℓ(θ)=log⁡L(θ)=∑nlog⁡p(xn;θ)\ell(\theta) = \log L(\theta) = \sum_n \log p(x_n;\theta), with the natural log. Because log⁡\log is strictly increasing, LL and ℓ\ell have the same maximiser, and the sum is easier to differentiate than the product.
  • The maximum likelihood estimate (MLE) is θ^=arg max⁡θℓ(θ)\hat\theta = \operatorname{arg\,max}_\theta \ell(\theta). A zero derivative only finds a stationary point; a negative second derivative (or a concave ℓ\ell) is what makes it a maximum.
  • xˉ=1N∑nxn\bar x = \tfrac1N\sum_n x_n is the sample mean.
  • N(μ,σ2)\mathcal N(\mu, \sigma^2) is the Gaussian with mean μ\mu and variance σ2\sigma^2, density (2πσ2)−1/2exp⁡(−(x−μ)2/(2σ2))(2\pi\sigma^2)^{-1/2}\exp\big(-(x - \mu)^2/(2\sigma^2)\big).
  • E\mathbb E is the expectation over a random sample. An estimator θ^\hat\theta is unbiased if E[θ^]=θ\mathbb E[\hat\theta] = \theta, and its bias is E[θ^]−θ\mathbb E[\hat\theta] - \theta. For independent variables the variance of a sum is the sum of the variances, and Var⁡(cX)=c2Var⁡(X)\operatorname{Var}(cX) = c^2\operatorname{Var}(X).
  • Regression uses the notation of the regression page: X∈RN×dX \in \mathbb{R}^{N \times d} has row nn equal to x(n)⊤x^{(n)\top}, y∈RNy \in \mathbb{R}^N holds the targets, w∈Rdw \in \mathbb{R}^d the weights, ∥v∥2=∑jvj2\|v\|^2 = \sum_j v_j^2 and ∥v∥1=∑j∣vj∣\|v\|_1 = \sum_j |v_j|.
  • Bayes' rule for the weights: p(w∣y)=p(y∣w) p(w)/p(y)p(w \mid y) = p(y \mid w)\,p(w)/p(y), where p(w)p(w) is the prior. The maximum a posteriori (MAP) estimate maximises log⁡p(y∣w)+log⁡p(w)\log p(y \mid w) + \log p(w); p(y)p(y) does not depend on ww.

Builds on: Regression gradients: linear, logistic and softmax

Problems

  1. ··

    A coin lands heads with probability θ\theta. In NN independent flips, xn=1x_n = 1 for heads and 00 for tails, and k=∑nxnk = \sum_n x_n is the number of heads, with 0<k<N0 < k < N. Find the MLE θ^\hat\theta, show it is a maximum, and compute ℓ′′(θ^)\ell''(\hat\theta).

  2. ··

    A categorical variable takes one of KK values, value ii with probability πi\pi_i, where πi>0\pi_i > 0 and ∑iπi=1\sum_i \pi_i = 1. In NN independent draws, value ii appears ni>0n_i > 0 times. Find the MLE π^\hat\pi with a Lagrange multiplier.

  3. ·

    Counts xn∈{0,1,2,… }x_n \in \{0, 1, 2, \dots\} are independent Poisson with rate λ>0\lambda > 0: P(x;λ)=λxe−λ/x!P(x;\lambda) = \lambda^{x}e^{-\lambda}/x!. Find the MLE λ^\hat\lambda, assuming not every count is 00.

  4. ··

    Let x1,…,xNx_1, \dots, x_N be independent draws from N(μ,σ2)\mathcal N(\mu, \sigma^2), with N≥2N \ge 2 and not all xnx_n equal. Find the joint MLE (μ^,σ^2)(\hat\mu, \hat\sigma^2) and show it is a maximum.

  5. ···

    The xnx_n are independent with mean μ\mu and variance σ2\sigma^2 (no Gaussian assumption needed). Show that the variance MLE of Problem 4 has E[σ^2]=N−1Nσ2\mathbb E[\hat\sigma^2] = \tfrac{N - 1}{N}\sigma^2.

  6. ··

    Linear regression with Gaussian noise: yn=x(n)⊤w+εny_n = x^{(n)\top}w + \varepsilon_n with the εn\varepsilon_n independent N(0,σ2)\mathcal N(0, \sigma^2). Write the negative log-likelihood, and find the MLE of ww and σ2\sigma^2 when the columns of XX are independent.

  7. ·

    Logistic regression models P(yn=1∣x(n))=σ(x(n)⊤w)P(y_n = 1 \mid x^{(n)}) = \sigma(x^{(n)\top}w), with labels yn∈{0,1}y_n \in \{0, 1\} independent given the inputs and σ(t)=1/(1+e−t)\sigma(t) = 1/(1 + e^{-t}). Show that −ℓ(w)/N-\ell(w)/N is the mean binary cross-entropy L(w)L(w) of the regression page.

  8. ···

    Keep the model of Problem 6 with σ2\sigma^2 known, and give the weights independent Gaussian priors wj∼N(0,τ2)w_j \sim \mathcal N(0, \tau^2). Show that the MAP estimate minimises the mean ridge loss 1N∥Xw−y∥2+λ∥w∥2\tfrac1N\|Xw - y\|^2 + \lambda\|w\|^2, find λ\lambda, and write wMAPw_{\text{MAP}}.

  9. ··

    Same model, but with independent Laplace priors p(wj)=12be−∣wj∣/bp(w_j) = \tfrac{1}{2b}e^{-|w_j|/b}, b>0b > 0. Show that the MAP estimate minimises 1N∥Xw−y∥2+λ∥w∥1\tfrac1N\|Xw - y\|^2 + \lambda\|w\|_1 and find λ\lambda.

  10. ···

    The xnx_n are independent and uniform on [0,θ][0, \theta], with θ>0\theta > 0 unknown and p(x;θ)=1/θp(x;\theta) = 1/\theta for 0≤x≤θ0 \le x \le \theta, 00 otherwise. Find the MLE θ^\hat\theta, explain why setting a derivative to zero cannot find it, and compute E[θ^]\mathbb E[\hat\theta].

Worked solutions

Problem 1

A coin lands heads with probability θ\theta. In NN independent flips, xn=1x_n = 1 for heads and 00 for tails, and k=∑nxnk = \sum_n x_n is the number of heads, with 0<k<N0 < k < N. Find the MLE θ^\hat\theta, show it is a maximum, and compute ℓ′′(θ^)\ell''(\hat\theta).

  1. p(x;θ)=θx(1−θ)1−xp(x;\theta) = \theta^{x}(1 - \theta)^{1 - x}, so ℓ(θ)=∑n[xnlog⁡θ+(1−xn)log⁡(1−θ)]=klog⁡θ+(N−k)log⁡(1−θ)\ell(\theta) = \sum_n\big[x_n\log\theta + (1 - x_n)\log(1 - \theta)\big] = k\log\theta + (N - k)\log(1 - \theta).One formula covers both outcomes: x=1x = 1 leaves θ\theta and x=0x = 0 leaves 1−θ1 - \theta. The log of the product is the sum of the logs, and ∑nxn=k\sum_n x_n = k, ∑n(1−xn)=N−k\sum_n(1 - x_n) = N - k.
  2. ℓ′(θ)=kθ−N−k1−θ\ell'(\theta) = \dfrac k\theta - \dfrac{N - k}{1 - \theta}.log⁡u\log u differentiates to 1/u1/u; the chain rule on log⁡(1−θ)\log(1 - \theta) supplies the minus sign.
  3. ℓ′(θ)=0  ⟺  k(1−θ)=(N−k) θ  ⟺  θ=k/N\ell'(\theta) = 0 \iff k(1 - \theta) = (N - k)\,\theta \iff \theta = k/N.Multiply by θ(1−θ)>0\theta(1 - \theta) > 0, which does not change where the expression is zero; the kθk\theta terms cancel.
  4. ℓ′′(θ)=−kθ2−N−k(1−θ)2<0\ell''(\theta) = -\dfrac{k}{\theta^2} - \dfrac{N - k}{(1 - \theta)^2} < 0 for every θ∈(0,1)\theta \in (0, 1).Both terms are negative because 0<k<N0 < k < N. So ℓ\ell is strictly concave on (0,1)(0, 1) and its one stationary point is the global maximum.
  5. At θ^=k/N\hat\theta = k/N: kθ^2=Nθ^\dfrac{k}{\hat\theta^2} = \dfrac{N}{\hat\theta} and N−k(1−θ^)2=N1−θ^\dfrac{N - k}{(1 - \hat\theta)^2} = \dfrac{N}{1 - \hat\theta}, whose sum is Nθ^(1−θ^)\dfrac{N}{\hat\theta(1 - \hat\theta)}.Substitute k=Nθ^k = N\hat\theta and N−k=N(1−θ^)N - k = N(1 - \hat\theta); over the common denominator θ^(1−θ^)\hat\theta(1 - \hat\theta) the numerator is N(1−θ^)+Nθ^=NN(1 - \hat\theta) + N\hat\theta = N.
  6. θ^=k/N\hat\theta = k/N, and ℓ′′(θ^)=−Nθ^(1−θ^)\ell''(\hat\theta) = -\dfrac{N}{\hat\theta(1 - \hat\theta)}The curvature grows in proportion to NN: more flips make a sharper peak. Its reciprocal with the sign flipped, θ^(1−θ^)/N\hat\theta(1 - \hat\theta)/N, is the usual estimate of the variance of θ^\hat\theta, and here it is exactly Var⁡(k/N)=θ(1−θ)/N\operatorname{Var}(k/N) = \theta(1 - \theta)/N with θ\theta replaced by θ^\hat\theta. If k=0k = 0 or k=Nk = N, ℓ\ell is monotonic and the maximum sits at the boundary 00 or 11, which is still k/Nk/N but is not a stationary point.

Problem 2

A categorical variable takes one of KK values, value ii with probability πi\pi_i, where πi>0\pi_i > 0 and ∑iπi=1\sum_i \pi_i = 1. In NN independent draws, value ii appears ni>0n_i > 0 times. Find the MLE π^\hat\pi with a Lagrange multiplier.

  1. ℓ(π)=∑i=1Knilog⁡πi\ell(\pi) = \sum_{i=1}^K n_i\log\pi_i.Each draw of value ii contributes a factor πi\pi_i to the likelihood, and there are nin_i of them.
  2. L(π,ν)=∑inilog⁡πi+ν(1−∑iπi)\mathcal L(\pi, \nu) = \sum_i n_i\log\pi_i + \nu\big(1 - \sum_i\pi_i\big), with multiplier ν\nu.The πi\pi_i are not free: on its own ℓ\ell increases in every πi\pi_i and has no stationary point (Mistake 3). The multiplier term enforces ∑iπi=1\sum_i \pi_i = 1.
  3. ∂L/∂πi=ni/πi−ν=0\partial\mathcal L/\partial\pi_i = n_i/\pi_i - \nu = 0, so πi=ni/ν\pi_i = n_i/\nu.Only the ii-th terms of the two sums contain πi\pi_i.
  4. ∑ini/ν=1\sum_i n_i/\nu = 1, so ν=∑ini=N\nu = \sum_i n_i = N.Substitute step 3 into the constraint.
  5. ℓ\ell is a sum of concave functions nilog⁡πin_i\log\pi_i, and the set {π:∑iπi=1, πi>0}\{\pi : \sum_i \pi_i = 1,\ \pi_i > 0\} is convex.A stationary point of the Lagrangian of a concave function under a linear equality constraint is the constrained global maximum.
  6. π^i=ni/N\hat\pi_i = n_i/NThe estimate is the observed frequency. At π^\hat\pi every partial derivative ni/π^in_i/\hat\pi_i equals NN, so ∇ℓ\nabla\ell is parallel to (1,…,1)(1, \dots, 1): no move that keeps the sum at 11 can increase ℓ\ell to first order. With K=2K = 2 this is Problem 1.

Problem 3

Counts xn∈{0,1,2,… }x_n \in \{0, 1, 2, \dots\} are independent Poisson with rate λ>0\lambda > 0: P(x;λ)=λxe−λ/x!P(x;\lambda) = \lambda^{x}e^{-\lambda}/x!. Find the MLE λ^\hat\lambda, assuming not every count is 00.

  1. log⁡P(x;λ)=xlog⁡λ−λ−log⁡x!\log P(x;\lambda) = x\log\lambda - \lambda - \log x!, so ℓ(λ)=(∑nxn)log⁡λ−Nλ−∑nlog⁡xn!\ell(\lambda) = \big(\sum_n x_n\big)\log\lambda - N\lambda - \sum_n\log x_n!.The log turns the power into a product and the quotient into a difference; the last sum does not contain λ\lambda.
  2. ℓ′(λ)=∑nxnλ−N=0  ⟺  λ=1N∑nxn\ell'(\lambda) = \dfrac{\sum_n x_n}{\lambda} - N = 0 \iff \lambda = \tfrac1N\sum_n x_n.The constant ∑nlog⁡xn!\sum_n\log x_n! differentiates to 00.
  3. ℓ′′(λ)=−∑nxnλ2<0\ell''(\lambda) = -\dfrac{\sum_n x_n}{\lambda^2} < 0.∑nxn>0\sum_n x_n > 0 by assumption, so ℓ\ell is strictly concave and the stationary point is the maximum.
  4. λ^=xˉ\hat\lambda = \bar xThe mean of a Poisson variable is λ\lambda, so the MLE is the sample mean. The exponential distribution, which models the waiting times between Poisson events, also has a parameter called a rate, but its mean is 1/λ1/\lambda and its MLE is 1/xˉ1/\bar x (Mistake 2).

Problem 4

Let x1,…,xNx_1, \dots, x_N be independent draws from N(μ,σ2)\mathcal N(\mu, \sigma^2), with N≥2N \ge 2 and not all xnx_n equal. Find the joint MLE (μ^,σ^2)(\hat\mu, \hat\sigma^2) and show it is a maximum.

  1. ℓ(μ,σ2)=−N2log⁡(2πσ2)−12σ2∑n(xn−μ)2\ell(\mu, \sigma^2) = -\tfrac N2\log(2\pi\sigma^2) - \dfrac{1}{2\sigma^2}\sum_n(x_n - \mu)^2.The log of the Gaussian density, summed over the data.
  2. ∂ℓ∂μ=1σ2∑n(xn−μ)=0  ⟺  μ=xˉ\dfrac{\partial\ell}{\partial\mu} = \dfrac1{\sigma^2}\sum_n(x_n - \mu) = 0 \iff \mu = \bar x, whatever σ2\sigma^2 is.(xn−μ)2(x_n - \mu)^2 differentiates to −2(xn−μ)-2(x_n - \mu), and the −2-2 cancels the −12-\tfrac12. Since ∑n(xn−μ)=Nxˉ−Nμ\sum_n(x_n - \mu) = N\bar x - N\mu, the zero does not depend on σ2\sigma^2.
  3. With v=σ2v = \sigma^2 as the variable: ∂ℓ∂v=−N2v+12v2∑n(xn−μ)2=0  ⟺  v=1N∑n(xn−μ)2\dfrac{\partial\ell}{\partial v} = -\dfrac{N}{2v} + \dfrac{1}{2v^2}\sum_n(x_n - \mu)^2 = 0 \iff v = \tfrac1N\sum_n(x_n - \mu)^2.log⁡(2πv)\log(2\pi v) differentiates to 1/v1/v and 1/v1/v to −1/v2-1/v^2. Multiply by 2v2>02v^2 > 0 to solve.
  4. Put μ=xˉ\mu = \bar x from step 2 into step 3.The joint stationary point has to satisfy both equations, and step 2 fixes μ\mu independently of vv.
  5. For each vv, ℓ\ell is a concave quadratic in μ\mu with its peak at xˉ\bar x. With S=∑n(xn−xˉ)2>0S = \sum_n(x_n - \bar x)^2 > 0, the profile ℓ(xˉ,v)=−N2log⁡(2πv)−S/(2v)\ell(\bar x, v) = -\tfrac N2\log(2\pi v) - S/(2v) has derivative (S−Nv)/(2v2)(S - Nv)/(2v^2), positive for v<S/Nv < S/N and negative beyond.So ℓ(μ,v)≤ℓ(xˉ,v)≤ℓ(xˉ,S/N)\ell(\mu, v) \le \ell(\bar x, v) \le \ell(\bar x, S/N) for every (μ,v)(\mu, v): the stationary point is the global maximum. S>0S > 0 because the xnx_n are not all equal.
  6. μ^=xˉ\hat\mu = \bar x, σ^2=1N∑n(xn−xˉ)2\hat\sigma^2 = \tfrac1N\sum_n(x_n - \bar x)^2The variance estimate divides by NN, not N−1N - 1; Problem 5 shows what that costs. Differentiating with respect to σ\sigma instead of σ2\sigma^2 gives the same answer, because the maximiser does not depend on how the parameter is written.

Problem 5

The xnx_n are independent with mean μ\mu and variance σ2\sigma^2 (no Gaussian assumption needed). Show that the variance MLE of Problem 4 has E[σ^2]=N−1Nσ2\mathbb E[\hat\sigma^2] = \tfrac{N - 1}{N}\sigma^2.

  1. ∑n(xn−xˉ)2=∑n(xn−μ)2−N(xˉ−μ)2\sum_n(x_n - \bar x)^2 = \sum_n(x_n - \mu)^2 - N(\bar x - \mu)^2.Write xn−xˉ=(xn−μ)−(xˉ−μ)x_n - \bar x = (x_n - \mu) - (\bar x - \mu) and expand. The cross term is −2(xˉ−μ)∑n(xn−μ)=−2N(xˉ−μ)2-2(\bar x - \mu)\sum_n(x_n - \mu) = -2N(\bar x - \mu)^2, because ∑n(xn−μ)=N(xˉ−μ)\sum_n(x_n - \mu) = N(\bar x - \mu); adding the N(xˉ−μ)2N(\bar x - \mu)^2 from the squares leaves −N(xˉ−μ)2-N(\bar x - \mu)^2.
  2. E[∑n(xn−μ)2]=Nσ2\mathbb E\big[\sum_n(x_n - \mu)^2\big] = N\sigma^2.Each term has expectation Var⁡(xn)=σ2\operatorname{Var}(x_n) = \sigma^2, and expectation is linear.
  3. E[(xˉ−μ)2]=Var⁡(xˉ)=σ2/N\mathbb E\big[(\bar x - \mu)^2\big] = \operatorname{Var}(\bar x) = \sigma^2/N.E[xˉ]=μ\mathbb E[\bar x] = \mu, so this is the variance of xˉ\bar x. By independence Var⁡(∑nxn)=Nσ2\operatorname{Var}\big(\sum_n x_n\big) = N\sigma^2, and the factor 1/N1/N divides that by N2N^2.
  4. E[∑n(xn−xˉ)2]=Nσ2−N⋅σ2/N=(N−1)σ2\mathbb E\big[\sum_n(x_n - \bar x)^2\big] = N\sigma^2 - N\cdot\sigma^2/N = (N - 1)\sigma^2.Steps 1 to 3, by linearity of expectation.
  5. E[σ^2]=N−1Nσ2\mathbb E[\hat\sigma^2] = \tfrac{N - 1}{N}\sigma^2Divide step 4 by NN. The MLE is biased low: xˉ\bar x is fitted to the same data, and it minimises ∑n(xn−c)2\sum_n(x_n - c)^2 over cc, so the residuals about xˉ\bar x are smaller than those about the true μ\mu. Dividing by N−1N - 1 instead gives the unbiased sample variance; the two agree as N→∞N \to \infty.

Problem 6

Linear regression with Gaussian noise: yn=x(n)⊤w+εny_n = x^{(n)\top}w + \varepsilon_n with the εn\varepsilon_n independent N(0,σ2)\mathcal N(0, \sigma^2). Write the negative log-likelihood, and find the MLE of ww and σ2\sigma^2 when the columns of XX are independent.

  1. yn∼N(x(n)⊤w,σ2)y_n \sim \mathcal N\big(x^{(n)\top}w, \sigma^2\big).yny_n is the constant x(n)⊤wx^{(n)\top}w plus Gaussian noise, and adding a constant shifts the mean without changing the variance.
  2. −ℓ(w,σ2)=N2log⁡(2πσ2)+12σ2∥Xw−y∥2-\ell(w, \sigma^2) = \tfrac N2\log(2\pi\sigma^2) + \dfrac{1}{2\sigma^2}\|Xw - y\|^2.Problem 4, step 1, with μ\mu replaced by x(n)⊤wx^{(n)\top}w for example nn; ∑n(yn−x(n)⊤w)2=∥Xw−y∥2\sum_n(y_n - x^{(n)\top}w)^2 = \|Xw - y\|^2 because entry nn of XwXw is x(n)⊤wx^{(n)\top}w.
  3. For every fixed σ2\sigma^2, ww enters only through ∥Xw−y∥2\|Xw - y\|^2 with the positive coefficient 1/(2σ2)1/(2\sigma^2), so w^\hat w minimises ∥Xw−y∥2\|Xw - y\|^2: w^=(X⊤X)−1X⊤y\hat w = (X^\top X)^{-1}X^\top y.Minimising a positive multiple of a function has the same minimiser; the least-squares solution is the regression page, Problem 2. The noise level does not affect w^\hat w.
  4. With w=w^w = \hat w, setting ∂(−ℓ)/∂σ2=0\partial(-\ell)/\partial\sigma^2 = 0 gives σ2=1N∥Xw^−y∥2\sigma^2 = \tfrac1N\|X\hat w - y\|^2.This is Problem 4, step 3, with the residuals yn−x(n)⊤w^y_n - x^{(n)\top}\hat w in place of xn−μx_n - \mu.
  5. w^=(X⊤X)−1X⊤y\hat w = (X^\top X)^{-1}X^\top y and σ^2=1N∥Xw^−y∥2\hat\sigma^2 = \tfrac1N\|X\hat w - y\|^2Least squares is maximum likelihood under Gaussian noise, and the mean squared error at the optimum is the noise-variance MLE. Like Problem 5, it is biased low, now because dd parameters were fitted: dividing by N−dN - d removes the bias.

Problem 7

Logistic regression models P(yn=1∣x(n))=σ(x(n)⊤w)P(y_n = 1 \mid x^{(n)}) = \sigma(x^{(n)\top}w), with labels yn∈{0,1}y_n \in \{0, 1\} independent given the inputs and σ(t)=1/(1+e−t)\sigma(t) = 1/(1 + e^{-t}). Show that −ℓ(w)/N-\ell(w)/N is the mean binary cross-entropy L(w)L(w) of the regression page.

  1. P(yn∣x(n);w)=pnyn(1−pn)1−ynP(y_n \mid x^{(n)}; w) = p_n^{y_n}(1 - p_n)^{1 - y_n} with pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w).The Bernoulli of Problem 1, with its probability now different for each example.
  2. ℓ(w)=∑n[ynlog⁡pn+(1−yn)log⁡(1−pn)]\ell(w) = \sum_n\big[y_n\log p_n + (1 - y_n)\log(1 - p_n)\big].The log of the product of the step 1 factors, as in Problem 1, step 1.
  3. −ℓ(w)/N=−1N∑n[ynlog⁡pn+(1−yn)log⁡(1−pn)]=L(w)-\ell(w)/N = -\tfrac1N\sum_n\big[y_n\log p_n + (1 - y_n)\log(1 - p_n)\big] = L(w)So minimising the mean binary cross-entropy is maximum likelihood, and its gradient 1NX⊤(σ(Xw)−y)\tfrac1N X^\top(\sigma(Xw) - y) (regression page, Problem 6) is the negative score divided by NN. The 1/N1/N scales the loss without moving the minimiser. In the same way, softmax cross-entropy is the negative log-likelihood of Problem 2's categorical distribution with per-example probabilities.

Problem 8

Keep the model of Problem 6 with σ2\sigma^2 known, and give the weights independent Gaussian priors wj∼N(0,τ2)w_j \sim \mathcal N(0, \tau^2). Show that the MAP estimate minimises the mean ridge loss 1N∥Xw−y∥2+λ∥w∥2\tfrac1N\|Xw - y\|^2 + \lambda\|w\|^2, find λ\lambda, and write wMAPw_{\text{MAP}}.

  1. log⁡p(w∣y)=log⁡p(y∣w)+log⁡p(w)−log⁡p(y)\log p(w \mid y) = \log p(y \mid w) + \log p(w) - \log p(y).Bayes' rule; log⁡p(y)\log p(y) does not depend on ww.
  2. log⁡p(w)=∑j[−12log⁡(2πτ2)−wj2/(2τ2)]=const−∥w∥2/(2τ2)\log p(w) = \sum_j\big[-\tfrac12\log(2\pi\tau^2) - w_j^2/(2\tau^2)\big] = \text{const} - \|w\|^2/(2\tau^2).The prior is a product over jj, so its log is a sum of Gaussian log-densities with mean 00.
  3. −log⁡p(w∣y)=12σ2∥Xw−y∥2+12τ2∥w∥2+c-\log p(w \mid y) = \dfrac{1}{2\sigma^2}\|Xw - y\|^2 + \dfrac{1}{2\tau^2}\|w\|^2 + c, with cc free of ww.Problem 6, step 2, for −log⁡p(y∣w)-\log p(y \mid w); the log⁡(2πσ2)\log(2\pi\sigma^2) and log⁡(2πτ2)\log(2\pi\tau^2) terms and log⁡p(y)\log p(y) go into cc.
  4. Multiplying by 2σ2/N>02\sigma^2/N > 0: 1N∥Xw−y∥2+σ2Nτ2∥w∥2+c′\tfrac1N\|Xw - y\|^2 + \dfrac{\sigma^2}{N\tau^2}\|w\|^2 + c'.A positive multiple has the same minimiser, and this one makes the data term a mean.
  5. The mean ridge loss with λ\lambda has minimiser (X⊤X+NλI)−1X⊤y(X^\top X + N\lambda I)^{-1}X^\top y, and Nλ=σ2/τ2N\lambda = \sigma^2/\tau^2.Regression page, Problem 3, which exists for every XX.
  6. λ=σ2Nτ2\lambda = \dfrac{\sigma^2}{N\tau^2} and wMAP=(X⊤X+(σ2/τ2) I)−1X⊤yw_{\text{MAP}} = \big(X^\top X + (\sigma^2/\tau^2)\,I\big)^{-1}X^\top yWeight decay is a Gaussian prior. A wide prior (τ→∞\tau \to \infty) gives λ→0\lambda \to 0 and the MLE of Problem 6. For the mean loss, λ\lambda shrinks as NN grows: the prior's pull is fixed while the evidence accumulates.

Problem 9

Same model, but with independent Laplace priors p(wj)=12be−∣wj∣/bp(w_j) = \tfrac{1}{2b}e^{-|w_j|/b}, b>0b > 0. Show that the MAP estimate minimises 1N∥Xw−y∥2+λ∥w∥1\tfrac1N\|Xw - y\|^2 + \lambda\|w\|_1 and find λ\lambda.

  1. log⁡p(w)=−dlog⁡(2b)−∥w∥1/b\log p(w) = -d\log(2b) - \|w\|_1/b.The log of a product of dd Laplace densities: each contributes −log⁡(2b)−∣wj∣/b-\log(2b) - |w_j|/b.
  2. −log⁡p(w∣y)=12σ2∥Xw−y∥2+1b∥w∥1+c-\log p(w \mid y) = \dfrac{1}{2\sigma^2}\|Xw - y\|^2 + \dfrac1b\|w\|_1 + c.Problem 8, steps 1 and 3, with this prior in place of the Gaussian one; constants go into cc.
  3. Multiplying by 2σ2/N2\sigma^2/N: 1N∥Xw−y∥2+2σ2Nb∥w∥1+c′\tfrac1N\|Xw - y\|^2 + \dfrac{2\sigma^2}{Nb}\|w\|_1 + c'.The same positive rescaling as Problem 8, step 4.
  4. λ=2σ2Nb\lambda = \dfrac{2\sigma^2}{Nb}: the MAP estimate under a Laplace prior is the lassoThe factor is 2σ22\sigma^2 here and σ2\sigma^2 in Problem 8 because the Gaussian prior's exponent carries a 12\tfrac12 and the Laplace prior's does not. ∣wj∣|w_j| has a corner at 00, so there is no closed form in general; the corner in the prior is also what lets the lasso set weights exactly to zero.

Problem 10

The xnx_n are independent and uniform on [0,θ][0, \theta], with θ>0\theta > 0 unknown and p(x;θ)=1/θp(x;\theta) = 1/\theta for 0≤x≤θ0 \le x \le \theta, 00 otherwise. Find the MLE θ^\hat\theta, explain why setting a derivative to zero cannot find it, and compute E[θ^]\mathbb E[\hat\theta].

  1. L(θ)=∏np(xn;θ)=θ−NL(\theta) = \prod_n p(x_n;\theta) = \theta^{-N} if θ≥m\theta \ge m, and 00 if θ<m\theta < m, where m=max⁡nxnm = \max_n x_n.One factor is 00 as soon as any xnx_n lies above θ\theta, and every xnx_n lies in [0,θ][0, \theta] exactly when the largest one does.
  2. L′(θ)=−Nθ−N−1<0L'(\theta) = -N\theta^{-N-1} < 0 for every θ>m\theta > m, and L=0L = 0 below mm.Above mm the likelihood strictly decreases, so no θ\theta has a zero derivative; below mm it is zero, the smallest possible value.
  3. θ^=m\hat\theta = m.LL jumps from 00 to m−Nm^{-N} at mm and decreases afterwards, so its maximum is the left end of the region where it is positive. The maximum is at a discontinuity, where no derivative exists: the calculus recipe assumes a maximum in the interior of a smooth region.
  4. P(θ^≤t)=P(every xn≤t)=(t/θ)NP(\hat\theta \le t) = P(\text{every } x_n \le t) = (t/\theta)^N for 0≤t≤θ0 \le t \le \theta.The maximum is at most tt exactly when every point is, and by independence the NN probabilities t/θt/\theta multiply.
  5. E[θ^]=∫0θt⋅NtN−1θN dt=NθN⋅θN+1N+1=NθN+1\mathbb E[\hat\theta] = \int_0^\theta t\cdot\dfrac{Nt^{N-1}}{\theta^N}\,dt = \dfrac{N}{\theta^N}\cdot\dfrac{\theta^{N+1}}{N + 1} = \dfrac{N\theta}{N + 1}.Differentiating step 4 in tt gives the density NtN−1/θNNt^{N-1}/\theta^N; then integrate tNt^N.
  6. θ^=max⁡nxn\hat\theta = \max_n x_n, with E[θ^]=NθN+1\mathbb E[\hat\theta] = \dfrac{N\theta}{N + 1}The MLE can never exceed θ\theta, so it is biased low, by θ/(N+1)\theta/(N + 1); N+1Nmax⁡nxn\tfrac{N + 1}{N}\max_n x_n is unbiased. Problem 1 noted a boundary maximum at k=0k = 0; here the maximum is always at a boundary.

Where this goes wrong

1. Variance MLE divided by N − 1

pandas, spreadsheet VAR functions and most statistics courses report the sample variance with N−1N - 1 in the denominator, and that version is the one that sticks.

  1. ∂ℓ∂σ2=−N2σ2+12σ4∑n(xn−xˉ)2\dfrac{\partial\ell}{\partial\sigma^2} = -\dfrac{N}{2\sigma^2} + \dfrac{1}{2\sigma^4}\sum_n(x_n - \bar x)^2Right so far: Problem 4, step 3, at μ=xˉ\mu = \bar x.
  2. “The correct estimate of a variance divides by N−1N - 1, so the maximum likelihood one must too.”The shortcut that causes the mistake: “correct” there means unbiased (Problem 5), which is a different criterion from maximising the likelihood.
  3. σ^2=1N−1∑n(xn−xˉ)2\hat\sigma^2 = \tfrac{1}{N - 1}\sum_n(x_n - \bar x)^2At that value the derivative in step 1 is −1/(2σ2)-1/(2\sigma^2), not 00, so it is not the maximiser: the MLE divides by NN (Problem 4). NumPy's np.var divides by NN by default and pandas by N−1N - 1, so the same data give two answers in one notebook.

2. Poisson rate estimated as one over the mean

The exponential and the Poisson distributions both have a parameter called the rate λ\lambda, and the exponential's MLE is 1/xˉ1/\bar x.

  1. ℓ(λ)=(∑nxn)log⁡λ−Nλ+const\ell(\lambda) = \big(\sum_n x_n\big)\log\lambda - N\lambda + \text{const}Right so far: Problem 3, step 1.
  2. “A rate is events per unit time, the reciprocal of the mean, so λ^=1/xˉ\hat\lambda = 1/\bar x.”The analogy that causes the mistake: an exponential variable is a waiting time with mean 1/λ1/\lambda; a Poisson variable is a count with mean λ\lambda.
  3. λ^=1/xˉ\hat\lambda = 1/\bar xAt that value ℓ′(λ)=Nxˉ2−N\ell'(\lambda) = N\bar x^2 - N, which is zero only when xˉ=1\bar x = 1. The MLE is xˉ\bar x (Problem 3): counts averaging 44 per hour give λ^=4\hat\lambda = 4 per hour, not 0.250.25.

3. Categorical log-likelihood made stationary without the constraint

Problem 1 had a single free parameter, and with KK of them it is natural to set each partial derivative to zero in turn.

  1. ℓ(π)=∑inilog⁡πi\ell(\pi) = \sum_i n_i\log\pi_iRight so far: Problem 2, step 1.
  2. “Set ∂ℓ/∂πi=0\partial\ell/\partial\pi_i = 0 for every ii, as for any maximum of a function of several variables.”The shortcut that causes the mistake: the unconstrained condition applied to parameters that must sum to 11.
  3. ∂ℓ/∂πi=ni/πi=0\partial\ell/\partial\pi_i = n_i/\pi_i = 0This has no solution: ni/πi>0n_i/\pi_i > 0, so ℓ\ell increases in every πi\pi_i and, with nothing to stop it, every probability heads to 11. The constraint is what makes the classes compete for probability; with a multiplier the answer is π^i=ni/N\hat\pi_i = n_i/N (Problem 2).

4. Ridge strength set to σ²/τ² in a mean loss

For the summed loss the textbook correspondence is λ=σ2/τ2\lambda = \sigma^2/\tau^2, and libraries document their loss as a mean.

  1. −log⁡p(w∣y)=12σ2∥Xw−y∥2+12τ2∥w∥2+c-\log p(w \mid y) = \dfrac{1}{2\sigma^2}\|Xw - y\|^2 + \dfrac{1}{2\tau^2}\|w\|^2 + cRight so far: Problem 8, step 3.
  2. “Multiply by 2σ22\sigma^2: the penalty weight is σ2/τ2\sigma^2/\tau^2.”The shortcut that causes the mistake: that is right for ∥Xw−y∥2+λ∥w∥2\|Xw - y\|^2 + \lambda\|w\|^2, and the λ\lambda is then carried to a loss whose data term has a 1/N1/N.
  3. 1N∥Xw−y∥2+σ2τ2∥w∥2\tfrac1N\|Xw - y\|^2 + \dfrac{\sigma^2}{\tau^2}\|w\|^2 has the MAP estimate as its minimiserMultiplying step 1 by 2σ2/N2\sigma^2/N gives λ=σ2/(Nτ2)\lambda = \sigma^2/(N\tau^2) (Problem 8). With σ2/τ2\sigma^2/\tau^2 the prior is NN times too strong, and the more data there are, the further the estimate is from the posterior mode.

5. Uniform MLE taken as twice the mean

The derivative of θ−N\theta^{-N} never vanishes, and matching the mean θ/2\theta/2 to xˉ\bar x looks like a reasonable way to get an answer anyway.

  1. L(θ)=θ−NL(\theta) = \theta^{-N} for θ≥max⁡nxn\theta \ge \max_n x_n, and 00 otherwiseRight so far: Problem 10, step 1.
  2. “There is no stationary point, so match the mean instead: θ/2=xˉ\theta/2 = \bar x.”The shortcut that causes the mistake: this is the method of moments, a different estimator with no claim to maximise LL.
  3. θ^=2xˉ\hat\theta = 2\bar xFor the data 0.2,0.3,1.90.2, 0.3, 1.9, 2xˉ=1.62\bar x = 1.6, below the observation 1.91.9, so L(1.6)=0L(1.6) = 0: the estimate says the data were impossible. The MLE is max⁡nxn=1.9\max_n x_n = 1.9 (Problem 10), and even when 2xˉ2\bar x exceeds the maximum it has a smaller likelihood, because LL decreases there.

Print this set: maximum-likelihood-estimation.pdf (problems, answers, and worked solutions on separate pages).