Maximum Likelihood Estimation › Example

Michael BrenndoerferOctober 25, 20266 min read
Machine Learning from ScratchAll chapters
Part 3 of 15Model Fundamentals and Diagnostics
Chapter 29Maximum Likelihood Estimation › Example
Reading section 6 of 10 · Example · 6 minSection 6 of 10 · 6 min

Section 6 of 10 · 6 min read

Maximum Likelihood Estimation › ExampleSection view · Sections in this chapter6 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

We now work through the likelihood calculations for two small examples. The first example is a Bernoulli problem with a count. The second is a Gaussian problem with five observations. In both cases, the same pattern appears. Build the likelihood, take its logarithm, differentiate, solve the score equation, then inspect curvature.

Bernoulli: twelve clicks in twenty visits

Suppose twenty visitors arrive, and twelve click. We model each visit as an independent Bernoulli trial with unknown click probability pp. The observed count is k=12k=12 and the number of trials is n=20n=20. The likelihood is

L(p)=p12(1−p)8.L(p)=p^{12}(1-p)^{8}.

where:

  • L(p)L(p): the likelihood of the click probability pp given the observed counts
  • pp: the unknown click probability
  • the exponents 1212 and 88: the numbers of clicks and non-clicks, so the terms carry the success and failure counts

The log-likelihood is

ℓ(p)=12log⁡p+8log⁡(1−p).\ell(p)=12\log p+8\log(1-p).

The score is

ℓ′(p)=12p−81−p.\ell'(p)=\frac{12}{p}-\frac{8}{1-p}.

Setting the score to zero gives

12p=81−p.\frac{12}{p}=\frac{8}{1-p}.

Cross-multiplying gives 12(1−p)=8p12(1-p)=8p, so 12−12p=8p12-12p=8p, and therefore 12=20p12=20p. Hence

p^=1220=0.6.\hat p=\frac{12}{20}=0.6.

The second derivative is

ℓ′′(p)=−12p2−8(1−p)2.\ell''(p)=-\frac{12}{p^2}-\frac{8}{(1-p)^2}.

At p^=0.6\hat p=0.6, this is

ℓ′′(0.6)=−120.62−8(1−0.6)2(substitute p^=0.6)=−120.36−80.16(square the denominators)=−33.3‾−50(divide each term)=−83.3‾(add).\begin{aligned} \ell''(0.6) &= -\frac{12}{0.6^2} - \frac{8}{(1-0.6)^2} && \text{(substitute } \hat p = 0.6 \text{)} \\ &= -\frac{12}{0.36} - \frac{8}{0.16} && \text{(square the denominators)} \\ &= -33.\overline{3} - 50 && \text{(divide each term)} \\ &= -83.\overline{3} && \text{(add)}. \end{aligned}

where:

  • ℓ′′(0.6)\ell''(0.6): the second derivative of the Bernoulli log-likelihood evaluated at p^=0.6\hat p = 0.6
  • 120.62\frac{12}{0.6^2}: the magnitude of the negative second-derivative contribution from the 12 successes
  • 8(1−0.6)2\frac{8}{(1-0.6)^2}: the magnitude of the negative second-derivative contribution from the 8 failures

The observed information is therefore J(0.6)=83.3‾J(0.6) = 83.\overline{3}, so the approximate standard error is

se^(p^)=183.333≈0.1095.\widehat{\mathrm{se}}(\hat p) =\sqrt{\frac{1}{83.333}} \approx 0.1095.

where:

  • se^(p^)\widehat{\mathrm{se}}(\hat p): the approximate standard error of the Bernoulli estimate
  • 83.33383.333: the observed information J(0.6)J(0.6) rounded

This matches the formula p^(1−p^)/n=0.6⋅0.4/20=0.012≈0.1095\sqrt{\hat p(1-\hat p)/n}=\sqrt{0.6\cdot0.4/20}=\sqrt{0.012}\approx0.1095.

We can compare the log-likelihood at a few candidate values. At p=0.5p=0.5,

ℓ(0.5)=12log⁡(0.5)+8log⁡(0.5)(substitute p=0.5)=20log⁡(0.5)(combine like terms)≈−13.863(evaluate).\begin{aligned} \ell(0.5) &= 12\log(0.5) + 8\log(0.5) && \text{(substitute } p = 0.5 \text{)} \\ &= 20\log(0.5) && \text{(combine like terms)} \\ &\approx -13.863 && \text{(evaluate)}. \end{aligned}

At p=0.6p=0.6,

ℓ(0.6)=12log⁡(0.6)+8log⁡(0.4)(substitute p=0.6)≈−13.460(evaluate).\begin{aligned} \ell(0.6) &= 12\log(0.6) + 8\log(0.4) && \text{(substitute } p = 0.6 \text{)} \\ &\approx -13.460 && \text{(evaluate)}. \end{aligned}

At p=0.7p=0.7,

ℓ(0.7)=12log⁡(0.7)+8log⁡(0.3)(substitute p=0.7)≈−13.912(evaluate).\begin{aligned} \ell(0.7) &= 12\log(0.7) + 8\log(0.3) && \text{(substitute } p = 0.7 \text{)} \\ &\approx -13.912 && \text{(evaluate)}. \end{aligned}

The largest value is at p=0.6p=0.6, as expected. The differences in log-likelihood are modest, which is another way of seeing that p=0.5p=0.5 is not absurdly implausible with only twenty observations. A rough 95% Wald interval is 0.6±1.96(0.1095)0.6\pm1.96(0.1095), or approximately (0.385,0.815)(0.385,0.815). The interval is symmetric around 0.60.6 and can be unreliable near the boundaries, but it gives a useful first summary of uncertainty.

Gaussian: five sensor readings

Suppose five readings from a sensor are

y=(9.8, 10.3, 9.9, 10.2, 9.8).y=(9.8,\ 10.3,\ 9.9,\ 10.2,\ 9.8).

We model them as independent draws from N(μ,σ2)N(\mu,\sigma^2). The sample mean is

μ^=yˉ=9.8+10.3+9.9+10.2+9.85=10.0.\hat\mu=\bar y=\frac{9.8+10.3+9.9+10.2+9.8}{5}=10.0.

where:

  • μ^\hat\mu: the maximum likelihood estimate of the mean
  • yˉ\bar y: the sample mean
  • the numerator: the sum of the five observed readings
  • the denominator 55: the number of observations nn

The deviations from the mean are −0.2,0.3,−0.1,0.2,−0.2-0.2,0.3,-0.1,0.2,-0.2. Squaring and summing gives

SSE=(y1−yˉ)2+(y2−yˉ)2+(y3−yˉ)2+(y4−yˉ)2+(y5−yˉ)2(definition)=(−0.2)2+(0.3)2+(−0.1)2+(0.2)2+(−0.2)2(substitute deviations)=0.04+0.09+0.01+0.04+0.04(square each term)=0.22.(sum)\begin{aligned} \mathrm{SSE} &= (y_1-\bar y)^2 + (y_2-\bar y)^2 + (y_3-\bar y)^2 + (y_4-\bar y)^2 + (y_5-\bar y)^2 && \text{(definition)} \\ &= (-0.2)^2 + (0.3)^2 + (-0.1)^2 + (0.2)^2 + (-0.2)^2 && \text{(substitute deviations)} \\ &= 0.04 + 0.09 + 0.01 + 0.04 + 0.04 && \text{(square each term)} \\ &= 0.22. && \text{(sum)} \end{aligned}

where:

  • SSE\mathrm{SSE}: the sum of squared deviations of the readings from the sample mean
  • each squared term: one observation's deviation from the mean, squared

The maximum likelihood variance is therefore

σ^2=SSEn=0.225=0.044.\hat\sigma^2=\frac{\mathrm{SSE}}{n}=\frac{0.22}{5}=0.044.

The maximum likelihood standard deviation is

σ^=0.044≈0.2098.\hat\sigma=\sqrt{0.044}\approx0.2098.

where:

  • σ^\hat\sigma: the maximum likelihood standard deviation
  • 0.0440.044: the maximum likelihood variance estimate

For comparison, the unbiased sample variance divides by n−1=4n-1=4:

s2=SSEn−1=0.224=0.055,s=s2=0.055≈0.2345.\begin{aligned} s^2 &= \frac{\mathrm{SSE}}{n-1} = \frac{0.22}{4} = 0.055, \\ s &= \sqrt{s^2} = \sqrt{0.055} \approx 0.2345. \end{aligned}

where:

  • s2s^2: the unbiased sample variance, which divides by n−1n-1 rather than nn
  • ss: the square root of the unbiased sample variance; ss itself is generally biased for σ\sigma
  • n−1=4n-1 = 4: the degrees of freedom, one less than the number of observations

The maximum likelihood variance is smaller because it divides by nn. On average, it underestimates the true variance by a factor of (n−1)/n=4/5(n-1)/n=4/5. With only five observations, the bias is not negligible.

The bias is easiest to see as a ratio. The maximum likelihood variance has expected value (n−1)σ2/n(n-1)\sigma^2/n, while the unbiased sample variance has expected value σ2\sigma^2. The gap closes as nn increases.

PythonIn [11]
CollapseExpand
variance_bias_n = np.arange(2, 51)
variance_bias_mle_ratio = (variance_bias_n - 1) / variance_bias_n
variance_bias_unbiased_ratio = np.ones_like(variance_bias_n)
VisualizationOut [12]
CollapseExpand
Expected MLE variance divided by true variance rises toward one as sample size grows.
Expected maximum likelihood variance divided by the true variance for sample sizes from 2 to 50, compared with the unbiased sample variance ratio. The maximum likelihood ratio (n-1)/n approaches one as n grows, but the downward bias is substantial in small samples.

The score for μ\mu at the estimate is

∂ℓ∂μ∣μ=10=1σ^2∑i=1n(yi−10)(score formula at μ=10)=10.044(−0.2+0.3−0.1+0.2−0.2)(substitute deviations)=10.044(0)(sum the deviations)=0(simplify).\begin{aligned} \left.\frac{\partial\ell}{\partial\mu}\right|_{\mu=10} &= \frac{1}{\hat\sigma^2}\sum_{i=1}^{n}(y_i-10) && \text{(score formula at } \mu=10\text{)} \\ &= \frac{1}{0.044}\left(-0.2+0.3-0.1+0.2-0.2\right) && \text{(substitute deviations)} \\ &= \frac{1}{0.044}(0) && \text{(sum the deviations)} \\ &= 0 && \text{(simplify)}. \end{aligned}

where:

  • ∂ℓ∂μ∣μ=10\left.\frac{\partial\ell}{\partial\mu}\right|_{\mu=10}: the score evaluated at μ=10\mu = 10
  • σ^2\hat\sigma^2: the maximum likelihood variance estimate, 0.0440.044
  • yi−10y_i-10: each observation's deviation from the candidate mean

The score is zero at μ^=yˉ=10\hat\mu = \bar y = 10, which is exactly the first-order condition for the maximum.

The approximate standard error of the mean, using σ^2\hat\sigma^2 as the variance, is

se^(μ^)=σ^2n(information n/σ^2)=0.0445(substitute values)≈0.0088(divide)≈0.0938.(square root)\begin{aligned} \widehat{\mathrm{se}}(\hat\mu) &= \sqrt{\frac{\hat\sigma^2}{n}} && \text{(information } n/\hat\sigma^2 \text{)} \\ &= \sqrt{\frac{0.044}{5}} && \text{(substitute values)} \\ &\approx \sqrt{0.0088} && \text{(divide)} \\ &\approx 0.0938. && \text{(square root)} \end{aligned}

where:

  • se^(μ^)\widehat{\mathrm{se}}(\hat\mu): the approximate standard error of the estimated mean
  • σ^2\hat\sigma^2: the maximum likelihood variance estimate
  • nn: the number of observations

A rough 95% interval using the normal approximation is 10.0±1.96(0.0938)10.0\pm1.96(0.0938), or approximately (9.816,10.184)(9.816,10.184). With only five observations, a tt interval based on four degrees of freedom would be wider, which is a reminder that the curvature approximation is asymptotic. The likelihood still gives us the point estimate and a sense of precision, but the finite-sample interval should be chosen with care.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy