The likelihood
A coin lands heads with an unknown probability . 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 the tosses are independent, so the chance of this exact sequence is a product of 10 factors (lesson 5.1.3): .
Before the tosses, is fixed and the outcome is random. Afterwards the outcome is fixed and is what you do not know. Read the same formula as a function of and it is called the likelihood, .
| 0.3 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | |
|---|---|---|---|---|---|---|
| 0.000075 | 0.000977 | 0.001792 | 0.002224 | 0.001678 | 0.000478 |
The data are most likely when . They are 2.28 times as likely under as under a fair coin, and 29.6 times as likely as under .
Rule: Likelihood
(theta) stands for the unknown parameter, and is the pmf or density of one observation when the parameter is . 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 from 0 to 1 is . It says how well each value of explains the data, not how probable is.
Only comparisons between values of 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 from 0 to 1, beside . The last line scans a grid of 99 values of and keeps the one with the largest likelihood.
The log-likelihood
A product of many numbers below 1 shrinks fast. With 1,000 tosses and 700 heads, is about . 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 is increasing, so and peak at the same .
Rule: Log-likelihood
is the script letter l. It has the same maximiser as . A difference is the log of the ratio .
For the coin, . Then and . The difference, 0.8228, is : 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 .
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 , and to their difference, the likelihood ratio.
The maximum likelihood estimator
The maximum likelihood estimate is the value of that makes the observed data most likely: the maximiser of , or of . 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 , using (the chain rule, lesson 2.3.3):
Setting gives , so . With heads in tosses the same steps give : the share of heads.
The second derivative is , negative for every . By lesson 2.4.2's test the critical point is a maximum. At it is , a number lesson 5.9.2 puts to use.
Rule: Maximum likelihood, one parameter
The slope is called the score. A zero score is needed at a peak inside the range of ; 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 per minute (lesson 5.4.2), with density . The log of each factor is , so
Then gives per minute, and . In general : one over the mean wait, 2 minutes.
| 0.25 | 0.4 | 0.5 | 0.6 | 1 | |
|---|---|---|---|---|---|
Try it
Central differences (lesson 2.2.2) check the coin's score: 0 at 0.7, and 8 at 0.5, where . The second difference of lesson 2.4.2 gives .
Then the waits: by the formula and by summing expon.logpdf, which takes scale .
Two parameters: the normal
Five measurements, 2, 4, 5, 6 and 8, come from with both and unknown. The log of the normal density (lesson 5.4.3) is . Adding over the points:
The conditions of lesson 4.4.1
The highest point of is the lowest point of , so lesson 4.4.1's conditions apply to . First order: the gradient of is zero. Second order: the Hessian of is positive definite, so the Hessian of is negative definite.
The two partial derivatives (lesson 4.2.1) are
The first is zero when is the mean . The second is then zero when , so .
Rule: Maximum likelihood for a normal
divides by , not : here 4, against the sample variance 5. It is the biased estimator of lesson 5.8.1, whose bias shrinks as grows. Case study 5.3.5's volatility, which divides by , is this estimate.
Differentiating again gives the Hessian of (lesson 4.3.1). At the cross term is zero, and
Both eigenvalues are negative, so is positive definite and is a strict local maximum. Far away falls without limit: as , as and as moves off. So by lesson 4.4.1 this peak is the global maximum.
A grid of agrees: every neighbour of is lower.
Maximising over instead of gives the same point: the estimate of is the square root of the estimate of . In general the maximum likelihood estimate of a function is .
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 it returns the matrix above, with two negative eigenvalues.
By computer: minimise the negative log-likelihood
Most models have no formula for ; 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 , as lesson 4.4.1 turned a maximum of into a minimum of .
Rule: Numerical maximum likelihood
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 , 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 , which can be any number, and report . By the rule for above, this is still the maximum likelihood estimate.
Try it
nll takes with . From the start , minimize lands on and , with , as the calculus found.
The last lines fit 500 seeded draws from . The estimates match the mean and std(), which divides by , to 4 decimals.
Where the method needs care
A peak at the edge. Toss a coin 5 times and see no heads. Then , largest at , where its slope is not zero.
The score 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: , a coin that never lands heads.
Several peaks. Two points, and 3, come from a t with (lesson 5.4.4) shifted by an unknown , with density . Its log-likelihood has two equal peaks, at , and a dip at 0.
| 0 | 2 | 2.8284 | 4 | ||||
|---|---|---|---|---|---|---|---|
An optimiser climbs the hill nearest its start. From 0.5 it finds 2.8284, from it finds , 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 , 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 . The loop prints the table.
Then minimize on from three starts. The last line is by a second difference: 0.32, positive, so 0 is a dip, not a peak.
Lesson 5.9.2 reads how sharp the peak is, which says how far may be from the truth. Case study 5.9.4 fits a Student t to daily returns this way.
Common mistakes
- The likelihood is not the probability of . For 7 heads in 10 its area over is . Only ratios for the same data mean something.
- Multiply only independent factors. The product rule for needs independent observations.
- Use logs in code. The product for 2,000 tosses underflows to 0; the log-likelihood does not.
- Hand the optimiser , not . Minimising itself runs away from the peak.
- The normal's divides by . It is biased (lesson 5.8.1), and it is the maximum likelihood estimate.
- A zero score is not enough. It misses a peak at the edge, such as , and it also holds at a dip. Check , or the Hessian, and try several starts.
- Keep parameters in range. Search over , not .
Likelihood and the maximum likelihood estimator
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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 is the probability, or density, of the observed data, read as a function of the parameter. It is not a distribution for : only ratios for the same data mean something.
- The log-likelihood has the same peak and does not underflow.
- The maximum likelihood estimate maximises : a zero score and . With several parameters: a zero gradient and a negative definite Hessian (lesson 4.4.1 applied to ).
- Coin: . Exponential: . Normal: and , dividing by . The estimate of is .
- Numerically, minimise the negative log-likelihood with
minimize, over parameters that can take any value (such as ). Check for peaks at the edge and for several peaks.