SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.9.1 Likelihood and the maximum likelihood estimatorConcept · 35 min
      2. 5.9.2 Fisher information and standard errorsConcept · 35 min
      3. 5.9.3 Practice set: maximum likelihoodPractice set · 25 min
      4. 5.9.4 Case study: fitting a Student t to daily returnsCase study · 35 min

Level 5, Probability and statistics 5.9 Maximum likelihood Lesson 1 of 4

ConceptLesson 5.9.1 in Level 5, Probability and statistics. About 35 minutes.

Likelihood and the maximum likelihood estimator

The likelihood

A coin lands heads with an unknown probability pp. You toss it 10 times and see H H T H H H T H T H: 7 heads and 3 tails. For a given pp the tosses are independent, so the chance of this exact sequence is a product of 10 factors (lesson 5.1.3): p7(1−p)3p^7(1 - p)^3.

Before the tosses, pp is fixed and the outcome is random. Afterwards the outcome is fixed and pp is what you do not know. Read the same formula as a function of pp and it is called the likelihood, L(p)L(p).

pp0.30.50.60.70.80.9
L(p)=p7(1−p)3L(p) = p^7(1 - p)^30.0000750.0009770.0017920.0022240.0016780.000478

The data are most likely when p=0.7p = 0.7. They are 2.28 times as likely under p=0.7p = 0.7 as under a fair coin, and 29.6 times as likely as under p=0.3p = 0.3.

Rule: Likelihood

L(θ)=f(x1;θ) f(x2;θ)⋯f(xn;θ)data x1,…,xn fixed, θ varies\begin{gathered} L(\theta) = f(x_1; \theta)\, f(x_2; \theta) \cdots f(x_n; \theta) \\[4pt] \text{data } x_1, \ldots, x_n \text{ fixed, } \theta \text{ varies} \end{gathered}

θ\theta (theta) stands for the unknown parameter, and f(x;θ)f(x; \theta) is the pmf or density of one observation when the parameter is θ\theta. The product needs independent observations. For a continuous model the factors are densities (lesson 5.4.1), not probabilities.

It is not a distribution for the parameter

A pmf or density must have total probability 1. The likelihood need not: the area under L(p)L(p) from 0 to 1 is 11320\frac{1}{1320}. It says how well each value of pp explains the data, not how probable pp is.

Only comparisons between values of θ\theta mean something, for the same data. That is why the table quotes ratios.

Try it

L is the likelihood of 7 heads and 3 tails. The loop prints the table, then the ratios to the peak.

quad integrates L(p)L(p) from 0 to 1, beside 11320\frac{1}{1320}. The last line scans a grid of 99 values of pp and keeps the one with the largest likelihood.

⌘/Ctrl + Enter runs the cell

The log-likelihood

A product of many numbers below 1 shrinks fast. With 1,000 tosses and 700 heads, L(0.7)=0.7700 0.3300L(0.7) = 0.7^{700}\,0.3^{300} is about 5.07×10−2665.07 \times 10^{-266}. With 2,000 tosses Python gives 0, because the number is below the smallest a computer stores.

Logs fix this. The log of a product is the sum of the logs (lesson 1.4.3), so the log-likelihood is a sum of moderate numbers. And ln⁡\ln is increasing, so LL and ln⁡L\ln L peak at the same θ\theta.

Rule: Log-likelihood

ℓ(θ)=ln⁡L(θ)=∑i=1nln⁡f(xi;θ)\ell(\theta) = \ln L(\theta) = \sum_{i=1}^{n} \ln f(x_i; \theta)

ℓ\ell is the script letter l. It has the same maximiser as LL. A difference ℓ(θ1)−ℓ(θ2)\ell(\theta_1) - \ell(\theta_2) is the log of the ratio L(θ1)L(θ2)\frac{L(\theta_1)}{L(\theta_2)}.

For the coin, ℓ(p)=7ln⁡p+3ln⁡(1−p)\ell(p) = 7\ln p + 3\ln(1 - p). Then ℓ(0.7)=−6.1086\ell(0.7) = -6.1086 and ℓ(0.5)=−6.9315\ell(0.5) = -6.9315. The difference, 0.8228, is ln⁡2.2769\ln 2.2769: the ratio from the table.

The likelihood and the log-likelihood of 7 heads in 10 tosses. Taking logs changes the heights, not the place of the peak: both are highest at p=0.7p = 0.7.

Try it

The first loop computes the likelihood of 70% heads directly, then its log: 100 and 1,000 tosses still fit, 2,000 gives 0. The log-likelihood stays an ordinary number.

The last line prints ℓ(0.7)\ell(0.7), ℓ(0.5)\ell(0.5) and ee to their difference, the likelihood ratio.

⌘/Ctrl + Enter runs the cell

The maximum likelihood estimator

The maximum likelihood estimate θ^\hat{\theta} is the value of θ\theta that makes the observed data most likely: the maximiser of LL, or of ℓ\ell. It is computed from the data, so before the data arrive it is a random variable, an estimator in the sense of lesson 5.8.1.

A coin, by calculus

Differentiate ℓ(p)=7ln⁡p+3ln⁡(1−p)\ell(p) = 7\ln p + 3\ln(1 - p), using ddpln⁡(1−p)=−11−p\frac{d}{dp}\ln(1 - p) = -\frac{1}{1 - p} (the chain rule, lesson 2.3.3):

ℓ′(p)=7p−31−p\ell'(p) = \frac{7}{p} - \frac{3}{1 - p}

Setting ℓ′(p)=0\ell'(p) = 0 gives 7(1−p)=3p7(1 - p) = 3p, so p^=0.7\hat{p} = 0.7. With kk heads in nn tosses the same steps give p^=kn\hat{p} = \frac{k}{n}: the share of heads.

The second derivative is ℓ′′(p)=−7p2−3(1−p)2\ell''(p) = -\frac{7}{p^2} - \frac{3}{(1 - p)^2}, negative for every pp. By lesson 2.4.2's test the critical point is a maximum. At p^=0.7\hat{p} = 0.7 it is −47.62-47.62, a number lesson 5.9.2 puts to use.

Rule: Maximum likelihood, one parameter

ℓ′(θ^)=0ℓ′′(θ^)<0\begin{gathered} \ell'(\hat{\theta}) = 0 \\[4pt] \ell''(\hat{\theta}) < 0 \end{gathered}

The slope ℓ′(θ)\ell'(\theta) is called the score. A zero score is needed at a peak inside the range of θ\theta; a negative second derivative makes it a maximum.

Waiting times

Five waits for a bus are 2, 0.5, 1.5, 4 and 2 minutes. Model each as exponential with rate λ\lambda per minute (lesson 5.4.2), with density f(x;λ)=λe−λxf(x; \lambda) = \lambda e^{-\lambda x}. The log of each factor is ln⁡λ−λx\ln\lambda - \lambda x, so

ℓ(λ)=5ln⁡λ−λ (2+0.5+1.5+4+2)=5ln⁡λ−10λ\begin{aligned} \ell(\lambda) &= 5\ln\lambda - \lambda\,(2 + 0.5 + 1.5 + 4 + 2) \\ &= 5\ln\lambda - 10\lambda \end{aligned}

Then ℓ′(λ)=5λ−10=0\ell'(\lambda) = \frac{5}{\lambda} - 10 = 0 gives λ^=0.5\hat{\lambda} = 0.5 per minute, and ℓ′′(λ)=−5λ2<0\ell''(\lambda) = -\frac{5}{\lambda^2} < 0. In general λ^=1xˉ\hat{\lambda} = \frac{1}{\bar{x}}: one over the mean wait, 2 minutes.

λ\lambda0.250.40.50.61
ℓ(λ)\ell(\lambda)−9.4315-9.4315−8.5815-8.5815−8.4657-8.4657−8.5541-8.5541−10-10

Try it

Central differences (lesson 2.2.2) check the coin's score: 0 at 0.7, and 8 at 0.5, where 70.5−30.5=8\frac{7}{0.5} - \frac{3}{0.5} = 8. The second difference of lesson 2.4.2 gives ℓ′′(0.7)\ell''(0.7).

Then the waits: ℓ(λ)\ell(\lambda) by the formula and by summing expon.logpdf, which takes scale =1λ= \frac{1}{\lambda}.

⌘/Ctrl + Enter runs the cell

Two parameters: the normal

Five measurements, 2, 4, 5, 6 and 8, come from N(μ,σ2)\mathcal{N}(\mu, \sigma^2) with both μ\mu and σ\sigma unknown. The log of the normal density (lesson 5.4.3) is −12ln⁡(2π)−ln⁡σ−(x−μ)22σ2-\frac{1}{2}\ln(2\pi) - \ln\sigma - \frac{(x - \mu)^2}{2\sigma^2}. Adding over the nn points:

ℓ(μ,σ)=−n2ln⁡(2π)−nln⁡σ−12σ2∑i=1n(xi−μ)2\begin{aligned} \ell(\mu, \sigma) ={}& -\tfrac{n}{2}\ln(2\pi) - n\ln\sigma \\ &- \frac{1}{2\sigma^2}\sum_{i=1}^{n}(x_i - \mu)^2 \end{aligned}

The conditions of lesson 4.4.1

The highest point of ℓ\ell is the lowest point of −ℓ-\ell, so lesson 4.4.1's conditions apply to −ℓ-\ell. First order: the gradient of ℓ\ell is zero. Second order: the Hessian of −ℓ-\ell is positive definite, so the Hessian of ℓ\ell is negative definite.

The two partial derivatives (lesson 4.2.1) are

∂ℓ∂μ=1σ2∑i(xi−μ)∂ℓ∂σ=−nσ+1σ3∑i(xi−μ)2\begin{gathered} \frac{\partial \ell}{\partial \mu} = \frac{1}{\sigma^2}\sum_{i}(x_i - \mu) \\[6pt] \frac{\partial \ell}{\partial \sigma} = -\frac{n}{\sigma} + \frac{1}{\sigma^3}\sum_{i}(x_i - \mu)^2 \end{gathered}

The first is zero when μ\mu is the mean xˉ=5\bar{x} = 5. The second is then zero when σ2=1n∑(xi−xˉ)2=9+1+0+1+95=4\sigma^2 = \frac{1}{n}\sum(x_i - \bar{x})^2 = \frac{9 + 1 + 0 + 1 + 9}{5} = 4, so σ^=2\hat{\sigma} = 2.

Rule: Maximum likelihood for a normal

μ^=xˉσ^2=1n∑i=1n(xi−xˉ)2\begin{gathered} \hat{\mu} = \bar{x} \\[4pt] \hat{\sigma}^2 = \frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2 \end{gathered}

σ^2\hat{\sigma}^2 divides by nn, not n−1n - 1: here 4, against the sample variance 5. It is the biased estimator of lesson 5.8.1, whose bias shrinks as nn grows. Case study 5.3.5's volatility, which divides by nn, is this estimate.

Differentiating again gives the Hessian of ℓ\ell (lesson 4.3.1). At (μ^,σ^)(\hat{\mu}, \hat{\sigma}) the cross term −2σ3∑(xi−μ)-\frac{2}{\sigma^3}\sum(x_i - \mu) is zero, and

H=(−n/σ^200−2n/σ^2)=(−1.2500−2.5)\begin{gathered} H = \begin{pmatrix} -n/\hat{\sigma}^2 & 0 \\ 0 & -2n/\hat{\sigma}^2 \end{pmatrix} \\[6pt] = \begin{pmatrix} -1.25 & 0 \\ 0 & -2.5 \end{pmatrix} \end{gathered}

Both eigenvalues are negative, so −H-H is positive definite and (5,2)(5, 2) is a strict local maximum. Far away ℓ\ell falls without limit: as σ→0\sigma \to 0, as σ→∞\sigma \to \infty and as μ\mu moves off. So by lesson 4.4.1 this peak is the global maximum.

A grid of ℓ(μ,σ)\ell(\mu, \sigma) agrees: every neighbour of (5,2)(5, 2) is lower.

σ=1.5\sigma = 1.5σ=2\sigma = 2σ=2.5\sigma = 2.5
μ=4\mu = 4−12.1776-12.1776−11.1854-11.1854−11.1761-11.1761
μ=5\mu = 5−11.0665-11.0665−10.5604-10.5604−10.7761-10.7761
μ=6\mu = 6−12.1776-12.1776−11.1854-11.1854−11.1761-11.1761

Maximising over σ2\sigma^2 instead of σ\sigma gives the same point: the estimate of σ\sigma is the square root of the estimate of σ2\sigma^2. In general the maximum likelihood estimate of a function g(θ)g(\theta) is g(θ^)g(\hat{\theta}).

Try it

ell sums norm.logpdf, which takes the standard deviation as scale. The first lines print the estimates and the grid.

hessian is lesson 4.3.1's central-difference Hessian. At (5,2)(5, 2) it returns the matrix above, with two negative eigenvalues.

⌘/Ctrl + Enter runs the cell

By computer: minimise the negative log-likelihood

Most models have no formula for θ^\hat{\theta}; case study 5.9.4 fits one, the Student t. Then a computer searches for the peak. Optimisers minimise, so hand them the negative log-likelihood −ℓ(θ)-\ell(\theta), as lesson 4.4.1 turned a maximum of ff into a minimum of −f-f.

Rule: Numerical maximum likelihood

θ^=the minimiser of −ℓ(θ)\hat{\theta} = \text{the minimiser of } -\ell(\theta)

In SciPy: minimize(nll, x0), with nll the negative log-likelihood and x0 a starting guess. The result holds the minimiser res.x, the minimum res.fun =−ℓ(θ^)= -\ell(\hat{\theta}), and res.success.

A standard deviation must stay positive, but an optimiser does not know that: norm.logpdf with a negative scale returns nan. The cure is to search over a=ln⁡σa = \ln\sigma, which can be any number, and report σ^=ea^\hat{\sigma} = e^{\hat{a}}. By the rule for g(θ^)g(\hat{\theta}) above, this is still the maximum likelihood estimate.

Try it

nll takes (μ,a)(\mu, a) with σ=ea\sigma = e^a. From the start (0,0)(0, 0), minimize lands on μ=5\mu = 5 and σ=2\sigma = 2, with −ℓ=10.5604-\ell = 10.5604, as the calculus found.

The last lines fit 500 seeded draws from N(10,32)\mathcal{N}(10, 3^2). The estimates match the mean and std(), which divides by nn, to 4 decimals.

⌘/Ctrl + Enter runs the cell

Where the method needs care

A peak at the edge. Toss a coin 5 times and see no heads. Then L(p)=(1−p)5L(p) = (1 - p)^5, largest at p=0p = 0, where its slope is not zero.

The score −51−p-\frac{5}{1 - p} is never 0, so setting it to zero finds nothing. Check the ends of the range, as lesson 2.4.1 did on a closed interval: p^=0\hat{p} = 0, a coin that never lands heads.

Several peaks. Two points, −3-3 and 3, come from a t with ν=1\nu = 1 (lesson 5.4.4) shifted by an unknown θ\theta, with density 1π(1+(x−θ)2)\frac{1}{\pi(1 + (x - \theta)^2)}. Its log-likelihood has two equal peaks, at θ=±8≈±2.8284\theta = \pm\sqrt{8} \approx \pm 2.8284, and a dip at 0.

θ\theta−4-4−2.8284-2.8284−2-2022.82844
ℓ(θ)\ell(\theta)−6.8946-6.8946−5.873-5.873−6.2407-6.2407−6.8946-6.8946−6.2407-6.2407−5.873-5.873−6.8946-6.8946

An optimiser climbs the hill nearest its start. From 0.5 it finds 2.8284, from −0.5-0.5 it finds −2.8284-2.8284, and from 0 it stays at the dip, where the score is also zero. Check the second-order condition and try several starts, as case study 4.4.5 did.

Parameters with limits. A standard deviation, a rate or a probability has a range. Search over a transformed parameter that can take any value, such as ln⁡σ\ln\sigma, or tell the optimiser the bounds.

Try it

ell is the two-point log-likelihood, from t.logpdf with 1 degree of freedom and location θ\theta. The loop prints the table.

Then minimize on −ℓ-\ell from three starts. The last line is ℓ′′(0)\ell''(0) by a second difference: 0.32, positive, so 0 is a dip, not a peak.

⌘/Ctrl + Enter runs the cell

Lesson 5.9.2 reads how sharp the peak is, which says how far θ^\hat{\theta} may be from the truth. Case study 5.9.4 fits a Student t to daily returns this way.

Common mistakes

  1. The likelihood is not the probability of θ\theta. For 7 heads in 10 its area over pp is 11320\frac{1}{1320}. Only ratios for the same data mean something.
  2. Multiply only independent factors. The product rule for LL needs independent observations.
  3. Use logs in code. The product for 2,000 tosses underflows to 0; the log-likelihood does not.
  4. Hand the optimiser −ℓ-\ell, not ℓ\ell. Minimising ℓ\ell itself runs away from the peak.
  5. The normal's σ^2\hat{\sigma}^2 divides by nn. It is biased (lesson 5.8.1), and it is the maximum likelihood estimate.
  6. A zero score is not enough. It misses a peak at the edge, such as p^=0\hat{p} = 0, and it also holds at a dip. Check ℓ′′\ell'', or the Hessian, and try several starts.
  7. Keep parameters in range. Search over ln⁡σ\ln\sigma, not σ\sigma.

Likelihood and the maximum likelihood estimator

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • The likelihood L(θ)=∏if(xi;θ)L(\theta) = \prod_i f(x_i; \theta) is the probability, or density, of the observed data, read as a function of the parameter. It is not a distribution for θ\theta: only ratios for the same data mean something.
  • The log-likelihood ℓ(θ)=∑iln⁡f(xi;θ)\ell(\theta) = \sum_i \ln f(x_i; \theta) has the same peak and does not underflow.
  • The maximum likelihood estimate θ^\hat{\theta} maximises ℓ\ell: a zero score ℓ′(θ^)=0\ell'(\hat{\theta}) = 0 and ℓ′′(θ^)<0\ell''(\hat{\theta}) < 0. With several parameters: a zero gradient and a negative definite Hessian (lesson 4.4.1 applied to −ℓ-\ell).
  • Coin: p^=kn\hat{p} = \frac{k}{n}. Exponential: λ^=1xˉ\hat{\lambda} = \frac{1}{\bar{x}}. Normal: μ^=xˉ\hat{\mu} = \bar{x} and σ^2=1n∑(xi−xˉ)2\hat{\sigma}^2 = \frac{1}{n}\sum(x_i - \bar{x})^2, dividing by nn. The estimate of g(θ)g(\theta) is g(θ^)g(\hat{\theta}).
  • Numerically, minimise the negative log-likelihood with minimize, over parameters that can take any value (such as ln⁡σ\ln\sigma). Check for peaks at the edge and for several peaks.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.