Point estimation — theory with the campus weather station, the method of moments and maximum likelihood (and the α I mistook for the mean wind speed)

The first day I downloaded the data from the weather station on the roof, I drew a Rayleigh over the wind histogram setting α equal to the mean. The curve came out squashed to the left, peaking at 3.5 km/h, while the histogram had its hump at 12. I had written alpha <- mean(wind), which gives 12.165, and I had assumed that the parameter of a distribution “is its mean”. In the normal it almost is (μ is the mean); in the Poisson too (λ is the mean); in the Rayleigh it is not: its mean is απ/2 \sqrt{\alpha \pi / 2} , and if you want it to be 12.165, α has to be about 94. That day I learnt the question of this topic: with the data in front of you, what value do you give the parameter?

Until now, in every topic, we were given the distribution with its parameters (X∼N(40,6) X \sim N(40, 6) , exp(0.1) \text{exp}(0.1) …) and we computed probabilities. In real life it is the other way round: you have the data and you do not know the parameter. Today we look at the two classic methods for getting it out of the sample: the method of moments (set the population mean equal to the sample mean) and maximum likelihood (pick the parameter that makes the data you have seen most likely). We carry on from sampling distributions: there Xˉ \bar X and S2 S^2 were statistics with their distribution; here they become estimators. The full derivations, step by step, are in the point estimation proofs.

Before you start. For this topic you need three things you already have: the distributions from earlier topics (Bernoulli, Poisson, gamma, uniform) with their mean and variance; the idea from the previous topic that Xˉ \bar X is a random variable, because it changes with the sample; and knowing how to differentiate a logarithm ((log⁡x)′=1/x (\log x)' = 1/x ) with the chain rule. Nothing else. The topic goes in this order: first, three words that need to be kept apart (parameter, estimator, estimate); then the method of moments, which is setting the model’s mean equal to the data’s mean and solving; then the likelihood, which measures how likely it was to see your data with each value of the parameter; and finally, how to find its maximum with logs and derivatives, and what to do when you cannot (the uniform and the gamma). All with the same data: the rooftop station. If you get lost, go back to the 9 rainy days out of 30: with them you can understand the whole topic.

The rooftop station

The example for the whole topic is the weather station on the roof of the Tafira campus. It records many things, and we will use five series (made up, but shaped like the real ones):

  • Wind: 40 readings of the mean wind speed (10-minute average), in km/h. The classic model for wind is the Rayleigh, with density f(x∣α)=xαe−x2/(2α) f(x \mid \alpha) = \frac{x}{\alpha} e^{-x^2/(2\alpha)} for x≥0 x \ge 0 and a parameter α>0 \alpha > 0 .
  • Gusts: how many gusts above 40 km/h there were in each of 24 hours. A count: Poisson(λ).
  • Rain or not: 30 days in December, 1 if it rained and 0 if not. Bernoulli(p).
  • Rain: the litres per square metre (mm) on 20 rainy days. Positive and skewed right: gamma(α, β).
  • Delay: how long the station takes to upload each reading to the server, in seconds. The manufacturer says it is “between 0 and a maximum”: uniform(0, θ).
wind  <- c(7.9, 12.2, 11.5, 11.8, 7.0, 11.7, 4.6, 20.3, 9.4, 10.6,
           12.9, 3.1, 9.2, 27.3, 12.3, 5.5, 14.2, 14.2, 7.4, 21.1,
           14.6, 6.4, 25.4, 8.2, 14.8, 4.1, 7.1, 18.0, 5.6, 16.4,
           13.1, 19.7, 12.1, 11.0, 9.1, 20.8, 21.7, 18.6, 1.2, 4.5)  # km/h
gusts <- c(2, 6, 4, 4, 9, 4, 4, 1, 3, 3, 1, 4,
           1, 1, 3, 2, 2, 3, 4, 3, 1, 4, 6, 2)                      # per hour
rains <- c(0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1,
           0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1)             # 30 days
rain  <- c(5.0, 8.8, 6.8, 6.1, 0.8, 8.7, 3.7, 10.0, 7.4, 1.6,
           7.1, 1.3, 7.5, 6.6, 12.0, 4.0, 7.2, 12.2, 14.8, 6.6)     # mm
delay <- c(1.34, 1.18, 2.95, 3.36, 3.51, 1.17, 3.42, 3.74,
           1.62, 0.91, 0.05, 1.01, 3.35, 0.52, 1.37)                # seconds
sapply(list(wind = wind, gusts = gusts, rains = rains,
            rain = rain, delay = delay),
       function(x) round(c(n = length(x), mean = mean(x)), 2))

Output:

      wind gusts rains  rain delay
n    40.00 24.00  30.0 20.00 15.00
mean 12.16  3.21   0.3  6.91  1.97

Each column is a series: how many data points it has and its mean. Notice two things that will keep coming up: the mean wind speed is 12.165 km/h (R rounds it to 12.16), and the mean of rains is 0.3, the proportion of rainy days (9 out of 30). In all five cases we know the family of the distribution (Rayleigh, Poisson, Bernoulli, gamma, uniform), but not the parameter. That is parametric estimation: the shape is assumed; the number comes from the data.

Parameter, estimator and estimate

Three words that look the same and are not:

  • The parameter θ is a fixed number of the population that you do not know: the α of the Rayleigh of the Tafira wind. It does not change if you change sample.
  • An estimator θ^=θ^(X1,…,Xn) \hat\theta = \hat\theta(X_1, \dots, X_n) is a formula, a function of the sample: “the sum of squares over 2n”. Because it depends on the sample, it is a random variable (what we saw in the previous topic: it has its sampling distribution).
  • An estimate is the number that formula gives with your actual data: 93.14.
Parameter, estimator and estimateDiagram from left to right. The population, all the wind in Tafira, is a Rayleigh with a parameter alpha that cannot be seen. From it comes a sample of 40 readings. The sample goes into the estimator, the formula sum of squares over 2n, and out comes the estimate, the number 93.14.Populationall the wind in TafiraRayleigh(α)α = ? (unseen)Sample7.912.211.511.87.011.74.620.39.410.612.93.1… 40 readingsEstimatorΣ Xᵢ²2na formulaEstimate93.14a numberAnother sample of 40 readings: the same formula, another number.
The parameter is fixed and unknown; the estimator, a formula; the estimate, the number it gives with your data.

In the picture: the parameter lives in the population and cannot be seen; the sample goes through the formula (the estimator) and out comes a number (the estimate). With another sample of 40 readings you would get another number, 89 or 101, but the formula would be the same. In the exam they ask for both: “obtain the estimator” wants a formula with capital Xi X_i ; “give an estimate with these data” wants a number.

Loosely, the notes and the exam say “maximum likelihood estimator” both for the formula and for the number. If you are asked for “the MLE of α”, give the formula; if they add “for these data”, give the number too.

One formula, two samples of wind

The difference between estimator and estimate shows up in an experiment you can do with the 40 wind readings. Split them in two: the first 20, as if they were one afternoon, and the last 20, as if they were another. Apply the same formula to both, the maximum likelihood one that comes further down, α^=12n∑Xi2 \hat\alpha = \frac{1}{2n}\sum X_i^2 :

What you look atThe first 20The last 20What it is
Formula140∑Xi2 \frac{1}{40}\sum X_i^2 140∑Xi2 \frac{1}{40}\sum X_i^2 the estimator: it does not change
Sum of squares3393.344058.20depends on which 20 readings you got
Result84.83101.46the estimate: it changes with the sample
True α??the parameter: the same in both columns, and you cannot see it

Look at the last row. The α of the Tafira wind is a single number; both afternoons are estimating it at the same time and they give different numbers. Neither is “wrong”: that is what always happens when you estimate from a sample. If you put the 40 readings together, the same formula (now over 2n=80 2n = 80 ) gives 93.14, between the two. That is why an estimator is a random variable: before you take the sample you do not know what number it will give, just as you did not know what mean would come out in the previous topic.

Check you’ve got it: in “the mean wind is 12.165”, what is the parameter, what is the estimator and what is the estimate?

The estimator is Xˉ \bar X , the formula “add up the readings and divide by n”, written in capitals because it works for any sample. The estimate is 12.165, the number that formula gives with these 40 readings (with the first 20 it would give 11.71; with the last 20, 12.62). The parameter would be the true mean of the wind on the roof, E(X)=απ/2 E(X) = \sqrt{\alpha\pi/2} , which you do not know. It is not 12.165: with another 40 readings you would get another number, and the true mean would still be the same.

The sample moments

The method of moments rests on something you already know from descriptive statistics: moments. The population moment of order k is μk=E(Xk) \mu_k = E(X^k) ; the sample one, mk=1n∑i=1nXik m_k = \frac{1}{n} \sum_{i=1}^n X_i^k . And the central ones, around the mean:

PopulationSample
μ1=E(X) \mu_1 = E(X) , the meanm1=Xˉ m_1 = \bar X , the sample mean
μ2=E(X2) \mu_2 = E(X^2) m2=1n∑Xi2 m_2 = \frac{1}{n}\sum X_i^2
μ2′=Var(X) \mu'_2 = \text{Var}(X) m2′=S′2=1n∑(Xi−Xˉ)2 m'_2 = S'^2 = \frac{1}{n}\sum (X_i – \bar X)^2

Careful with the last row: the second central moment of the sample divides by n, not by n − 1. We write it S′2 S'^2 (with a prime) so as not to confuse it with the sample variance S2 S^2 of the previous topic, the one var() computes. You go from one to the other with

S′2=n−1n S2 S'^2 = \frac{n-1}{n}\, S^2

Using var(x) as S′2 S'^2 . var() divides by n − 1. For the method of moments you need (n - 1) / n * var(x) or, directly, mean((x - mean(x))^2). With 20 rain values the difference is 5 %, and in the exam it changes the second decimal.

The method of moments

The idea is very simple: if the data come from an f(x∣θ) f(x \mid \theta) , choose the θ that makes the population mean equal to the sample mean. With two parameters one equation is not enough: you also match the second moment (or the variance). With k parameters, k equations:

μ1(θ)=m1,μ2(θ)=m2,…,μk(θ)=mk \mu_1(\theta) = m_1, \quad \mu_2(\theta) = m_2, \quad \dots, \quad \mu_k(\theta) = m_k

The method of moments estimator is the solution of that system. We will write it with a tilde, θ~ \tilde\theta , to tell it apart from the maximum likelihood one, θ^ \hat\theta .

One parameter: gusts and wind

In the Poisson, E(X)=λ E(X) = \lambda . Set it equal to the sample mean and you are done: λ~=Xˉ \tilde\lambda = \bar X .

In the Rayleigh it takes a bit more work, because the mean is not α: it is E(X)=απ/2 E(X) = \sqrt{\alpha\pi/2} (the integral, with a change t=x2 t = x^2 that turns it into a gamma, is in proof 1). Set them equal and solve:

απ2=Xˉ  ⟹  απ2=Xˉ2  ⟹  α~=2Xˉ2π \sqrt{\frac{\alpha\pi}{2}} = \bar X \;\Longrightarrow\; \frac{\alpha\pi}{2} = \bar X^2 \;\Longrightarrow\; \tilde\alpha = \frac{2\bar X^2}{\pi}
The method of moments with the windTwo boxes joined by an equals sign. On the left, what the population says: the mean of a Rayleigh is the root of alpha times pi over 2. On the right, what the data say: the mean of the 40 readings is 12.165. Below, solving for alpha gives alpha tilde equal to 2 times x bar squared over pi, 94.21.What the population saysE(X) = √(απ/2)the mean of a Rayleigh(proof 1)=What the data sayx̄ = 12.165the mean of the 40 readingsα̃ = 2x̄² / π = 94.21solve for α
Set the population mean equal to the sample mean and solve: that is the whole method.

The picture shows the idea: on the left, what the population says (the mean of a Rayleigh is απ/2 \sqrt{\alpha\pi/2} ); on the right, what the data say (12.165); in the middle, the equals sign. Solving for α is all that is left.

The wind, step by step

The calculation above is three arrows. Slowly, and with the wind numbers:

StepWhat you doResultWhy
1Count the parametersone, αone parameter, one unknown: a single equation is enough, the one for the mean
2Write the population mean as a function of αE(X)=απ/2 E(X) = \sqrt{\alpha\pi/2} it comes from the integral in proof 1; it is what the model “says”
3Compute the sample meanxˉ=12.165 \bar x = 12.165 it is what the data say: the sum of the 40 readings over 40
4Set them equalαπ/2=12.165 \sqrt{\alpha\pi/2} = 12.165 the whole method is this equals sign
5Square both sidesαπ/2=147.99 \alpha\pi/2 = 147.99 to get rid of the root; both sides are positive, so nothing is lost
6Multiply by 2 and divide by πα~=2⋅147.99/π=94.21 \tilde\alpha = 2 \cdot 147.99/\pi = 94.21 solving for α is all that is left

Look at step 2: it is the only one where you need to know something about the distribution. If the model’s mean is the parameter itself (Poisson, Bernoulli), steps 5 and 6 disappear and the estimator is Xˉ \bar X as it is. If it is a function of the parameter, as here, you have to solve for it, and that is where anyone who writes alpha <- mean(wind) goes wrong, as I did on the first day.

Check the result by going the other way: with α = 94.21, the mean of the Rayleigh is 94.21⋅π/2=12.16 \sqrt{94.21 \cdot \pi/2} = 12.16 , the data’s mean. If undoing the calculation does not give you xˉ \bar x , you made a mistake when solving.

Two parameters: the rain

The gamma(α, β) has two parameters, so you need two equations. The handy ones are the mean and the variance: E(X)=α/β E(X) = \alpha/\beta and Var(X)=α/β2 \text{Var}(X) = \alpha/\beta^2 . Setting them equal to Xˉ \bar X and S′2 S'^2 :

αβ=Xˉ,αβ2=S′2 \frac{\alpha}{\beta} = \bar X, \qquad \frac{\alpha}{\beta^2} = S'^2

Divide the first by the second: β=Xˉ/S′2 \beta = \bar X / S'^2 . And substitute in the first: α=βXˉ=Xˉ2/S′2 \alpha = \beta \bar X = \bar X^2 / S'^2 .

α~=Xˉ2S′2,β~=XˉS′2 \tilde\alpha = \frac{\bar X^2}{S'^2}, \qquad \tilde\beta = \frac{\bar X}{S'^2}

With the rain numbers, before the code, so you can see where each one comes from:

StepCalculationResultWhy
1xˉ=120∑xi \bar x = \frac{1}{20}\sum x_i 6.91 mmthe first sample moment
2S′2=120∑(xi−xˉ)2 S'^2 = \frac{1}{20}\sum (x_i – \bar x)^2 12.67the second central moment: over 20, not over 19
3β~=xˉ/S′2=6.91/12.67 \tilde\beta = \bar x / S'^2 = 6.91 / 12.67 0.55dividing the two equations, α goes away and only β is left
4α~=xˉ2/S′2=6.912/12.67 \tilde\alpha = \bar x^2 / S'^2 = 6.91^2 / 12.67 3.77you substitute β into the first equation, α=βxˉ \alpha = \beta \bar x

Why divide? Because both equations have α on top and they only differ in the power of β. When you divide them, α cancels and β is left on its own in one go. It is the trick behind almost every two-parameter method-of-moments system: look for the operation that leaves a single unknown.

Pairing the moments wrongly: setting E(X2) E(X^2) equal to the sample variance, or Var(X) \text{Var}(X) equal to 1n∑Xi2 \frac{1}{n}\sum X_i^2 . The two good pairs are E(X2) E(X^2) with m2=1n∑Xi2 m_2 = \frac{1}{n}\sum X_i^2 (60.42 with the rain) and Var(X) \text{Var}(X) with S′2 S'^2 (12.67). They give the same estimator, because 60.42−6.912=12.67 60.42 – 6.91^2 = 12.67 .

Check you’ve got it: if you use var(rain) instead of S’², what shape and rate do you get?

var(rain) divides by 19 and gives 13.34 instead of 12.67. With that number, the rate would be 6.91/13.34=0.52 6.91 / 13.34 = 0.52 and the shape 6.912/13.34=3.58 6.91^2 / 13.34 = 3.58 , instead of 0.55 and 3.77. The shape already changes in the first decimal. It is not a method-of-moments estimator: the method’s second equation asks for the sample’s central moment, which divides by n.

n <- length(rain)
s2prime <- (n - 1) / n * var(rain)   # central moment of order 2: divides by n
lambda_mm <- mean(gusts)             # Poisson: E(X) = lambda
alpha_mm  <- 2 * mean(wind)^2 / pi   # Rayleigh: E(X) = sqrt(alpha * pi / 2)
a_mm <- mean(rain)^2 / s2prime       # gamma: shape
b_mm <- mean(rain) / s2prime         # gamma: rate
round(c(lambda = lambda_mm, alpha = alpha_mm, s2prime = s2prime,
        shape = a_mm, rate = b_mm), 2)

Output:

 lambda   alpha s2prime   shape    rate 
   3.21   94.21   12.67    3.77    0.55 

How to read it

  • lambda 3.21: we estimate that there are, on average, 3.21 strong gusts per hour. It is the mean of the 24 counts.
  • alpha 94.21: the parameter of the wind’s Rayleigh. It is not in units of speed: it is in speed squared (km²/h²). Its root, 9.71 km/h, is the most frequent wind speed (the mode of the Rayleigh is α \sqrt\alpha ).
  • s2prime 12.67: the variance of the rain dividing by 20. var(rain) would give 13.34.
  • shape 3.77 and rate 0.55: the gamma of the rain by moments. Its mean, 3.77/0.55=6.91 3.77 / 0.55 = 6.91 mm, is exactly xˉ \bar x : that is what the method is built for.

The moments estimator is not unique

In the Poisson we also know that Var(X)=λ \text{Var}(X) = \lambda . If instead of the mean I match the variance, λ=S′2 \lambda = S'^2 , I get another method of moments estimator, λ~2=S′2 \tilde\lambda_2 = S'^2 , which with the gusts gives 3.50 instead of 3.21. Both are “moments” estimators. In practice you choose the simplest (the mean one), and in the next topic we will see the criterion for deciding which is better. For now keep this: the method of moments does not give a single estimator; it depends on which moments you match.

The likelihood function

The second method is Fisher’s (1922) and has a very appealing idea: of all the possible values of the parameter, keep the one that makes the data you have seen most likely.

Before writing any formula, let’s build it with very little data. Keep only the first three days of December: dry, rain, dry (0, 1, 0). If the probability of rain were p, how likely is it to see exactly that?

  • The first day is dry: that happens with probability 1−p 1 – p .
  • On the second it rains: probability p p .
  • The third, dry again: 1−p 1 – p .
  • The days are independent, so the probability of all three together is the product: (1−p) p (1−p)=p(1−p)2 (1-p)\,p\,(1-p) = p(1-p)^2 .

Now we give p some values, like trying out candidates:

pp(1−p)2 p(1-p)^2 What it says
0.10.1⋅0.92=0.081 0.1 \cdot 0.9^2 = 0.081 with so little rain, rain on one day out of three is odd
0.30.3⋅0.72=0.147 0.3 \cdot 0.7^2 = 0.147 quite a bit better
1/313⋅(23)2=0.148 \frac{1}{3} \cdot \left(\frac{2}{3}\right)^2 = 0.148 the best of all: it is the proportion of rainy days in this sample
0.60.6⋅0.42=0.096 0.6 \cdot 0.4^2 = 0.096 too much rain to see two dry days out of three

What you have just done is the whole method. The data (0, 1, 0) do not move; what changes is p, and for each p you work out how likely it was to see your data. The p that gives the highest number is the maximum likelihood estimate. Now, the same with the whole month.

Check you’ve got it: with the first five days (0, 1, 0, 0, 0), what function of p comes out and which p makes it largest?

One rainy day and four dry ones, independent: p(1−p)4 p(1-p)^4 . Trying candidates: with 0.1 you get 0.0656; with 0.2, 0.0819; with 0.3, 0.0720. The maximum is at p = 0.2, which is 1/5, the proportion of rainy days among those five. It is the same pattern as with three days (1/3): the likelihood of a Bernoulli always has its maximum at the sample proportion.

Think of the 30 days in December: 9 rainy and 21 dry. If p (the probability of rain) were 0.1, seeing 9 rainy days would be odd. If it were 0.6, too: you would expect about 18. With p = 0.3, seeing 9 out of 30 is what you would expect. The function that measures “how likely my sample is if the parameter is θ” is the likelihood:

L(θ)=L(x∣θ)=f(x1,…,xn∣θ)=∏i=1nf(xi∣θ) L(\theta) = L(x \mid \theta) = f(x_1, \dots, x_n \mid \theta) = \prod_{i=1}^n f(x_i \mid \theta)

The product comes from the independence of the sample (a simple random sample): the probability, or density, of all of them happening together is the product. Three things to be clear about:

  1. The data are fixed and θ is the variable. It is the same formula as the joint density, but you look at it as a function of the parameter.
  2. In the discrete case it is a real probability (that of seeing exactly your sample); in the continuous case it is a product of densities, which is not a probability, but compares just as well.
  3. It does not add up to 1 and does not have to: it is not a distribution of θ.

With the rain: each day is pxi(1−p)1−xi p^{x_i}(1-p)^{1-x_i} , and multiplying the 30 the exponents add up: L(p)=p9(1−p)21 L(p) = p^{9}(1-p)^{21} , where 9 is the sum of the data.

L <- function(p) p^9 * (1 - p)^21          # likelihood of the 30 days
L(c(0.1, 0.3, 0.6))                         # three candidates
curve(L, 0, 1, xlab = "p", ylab = "L(p)")   # the whole curve

Output:

[1] 1.094190e-10 1.099386e-08 4.432218e-11
The likelihood of the 30 December daysCurve of L of p equal to p to the 9 times 1 minus p to the 21, between 0 and 1. It is a hump with the maximum at p equal to 0.3, with height 1.1 times 10 to the minus 8. Three candidates marked: with p equal to 0.1 the height is 1.1 times 10 to the minus 10, with 0.3 it is 1.1 times 10 to the minus 8 and with 0.6, 4.4 times 10 to the minus 11.04.0·10⁻⁹8.0·10⁻⁹1.2·10⁻⁸0.00.10.20.30.40.50.60.70.80.91.0pL(p)L(0.1) = 1.1·10⁻¹⁰L(0.3) = 1.1·10⁻⁸L(0.6) = 4.4·10⁻¹¹maximum: p = 9/30
What curve(L, 0, 1) draws. With p = 0.3 the 30 days we saw are about 100 times more likely than with p = 0.1.

How to read it

  • The three numbers are the probability of that particular sequence of 30 days (not “9 out of 30” in any order) for p = 0.1, 0.3 and 0.6. They are tiny because any particular sequence of 30 days is always unlikely; what matters is comparing them: with p = 0.3 the sample is about 100 times more likely than with p = 0.1 and about 250 times more than with p = 0.6.
  • The picture is what curve draws: a hump with its maximum at p = 0.3. That is the maximum likelihood estimate of p with these data.
  • Look at the vertical axis: it reaches 1.1×10−8 1.1 \times 10^{-8} . With 1000 days the numbers would be so small that the computer would round them to 0. That is why we work with the logarithm.

The maximum likelihood estimator

The maximum likelihood estimate is the value θ^ \hat\theta that maximises L(θ) L(\theta) ; the maximum likelihood estimator (MLE) is the formula that gives that value for each sample:

L(x∣θ^)=max⁡θL(x∣θ) L(x \mid \hat\theta) = \max_\theta L(x \mid \theta)

The procedure, in four steps

  1. Write the likelihood: L(θ)=∏f(xi∣θ) L(\theta) = \prod f(x_i \mid \theta) .
  2. Take logs: ℓ(θ)=log⁡L(θ)=∑i=1nlog⁡f(xi∣θ) \ell(\theta) = \log L(\theta) = \sum_{i=1}^n \log f(x_i \mid \theta) . The product becomes a sum, much easier to differentiate.
  3. Differentiate and set to 0: ℓ′(θ)=0 \ell'(\theta) = 0 . The solution is θ^ \hat\theta .
  4. Check that it is a maximum: ℓ′′(θ^)<0 \ell''(\hat\theta) < 0 .

Why can you maximise the log instead of L? Because the log is increasing: if L(θ1)>L(θ2) L(\theta_1) > L(\theta_2) , then log⁡L(θ1)>log⁡L(θ2) \log L(\theta_1) > \log L(\theta_2) too. It changes the height of the curve, but not where the maximum is.

The likelihood and its log peak at the same placeTwo panels with alpha between 50 and 200. On the left, the wind likelihood divided by its maximum: a narrow spike peaking at 93.14. On the right, the log-likelihood: a gentle hill, between minus 136 and minus 126, with its top at the same alpha, 93.14, where it is minus 127.83.0.00.51.050100150200Likelihood (scaled)α̂ = 93.14−136−131−12650100150200Log-likelihood−127.83αα
The logarithm changes the height and the shape, not where the maximum is.

In the picture, the wind likelihood and its logarithm, one above the other. The top curve is a very tall, narrow spike; the bottom one, a gentle hill. Both peak at the same α: 93.14.

Log and derivative, step by step with the rain

The four steps above fit in four lines, but each one hides a rule. With the likelihood of the 30 days, L(p)=p9(1−p)21 L(p) = p^9(1-p)^{21} :

StepResultWhy
Logarithmℓ(p)=9log⁡p+21log⁡(1−p) \ell(p) = 9\log p + 21\log(1-p) the log of a product is the sum of the logs, and the log of a power brings the exponent down in front: log⁡(ak)=klog⁡a \log(a^k) = k\log a
Derivativeℓ′(p)=9p−211−p \ell'(p) = \frac{9}{p} – \frac{21}{1-p} the derivative of log⁡p \log p is 1/p 1/p ; that of log⁡(1−p) \log(1-p) is −1/(1−p) -1/(1-p) : the minus comes from the chain rule, because the inside, 1−p 1-p , has derivative −1
Set to 09p=211−p \frac{9}{p} = \frac{21}{1-p} at the top of the hill the slope is 0: it neither rises nor falls
Solve9(1−p)=21p⇒9=30p⇒p^=0.3 9(1-p) = 21p \Rightarrow 9 = 30p \Rightarrow \hat p = 0.3 cross-multiply and gather the p terms on one side
Second derivativeℓ′′(p)=−9p2−21(1−p)2 \ell''(p) = -\frac{9}{p^2} – \frac{21}{(1-p)^2} both terms are negative for any p, so ℓ′′(0.3)=−142.86<0 \ell''(0.3) = -142.86 < 0 : it is a maximum

Three things the table shows that hold for every example in the topic:

  • Without the logarithm, you would have to differentiate p9(1−p)21 p^9(1-p)^{21} with the product rule and the chain rule at the same time. It can be done, but you get a long expression that you then have to factorise to set it to 0. With the logarithm, each factor becomes a term and is differentiated on its own.
  • The slope tells you where the maximum is. At p = 0.1, ℓ′(0.1)=90−23.33=66.67>0 \ell'(0.1) = 90 – 23.33 = 66.67 > 0 : the hill is rising and the maximum is to the right. At p = 0.6, ℓ′(0.6)=15−52.5=−37.5<0 \ell'(0.6) = 15 – 52.5 = -37.5 < 0 : it is falling, and the maximum is to the left. Only at 0.3 is it zero.
  • The logarithm also fixes the numbers. L(0.3) L(0.3) was 1.1×10−8 1.1 \times 10^{-8} ; on the other hand, ℓ(0.3)=9log⁡0.3+21log⁡0.7=−18.33 \ell(0.3) = 9\log 0.3 + 21\log 0.7 = -18.33 , a number you can handle with no trouble (it is the natural logarithm, R’s log).
Check you’ve got it: the 24 hours of gusts add up to 77; what is the MLE of λ and what happens to the factorial term when you differentiate?

The Poisson log-likelihood is ℓ(λ)=−24λ+77log⁡λ−∑log⁡xi! \ell(\lambda) = -24\lambda + 77\log\lambda – \sum \log x_i! . The last term has no λ: it is a fixed number, and its derivative is 0. It is in ℓ \ell , but it disappears when you differentiate. That leaves ℓ′(λ)=−24+77/λ \ell'(\lambda) = -24 + 77/\lambda ; setting it to 0, λ^=77/24=3.21 \hat\lambda = 77/24 = 3.21 , the mean of the gusts. The second derivative, −77/λ2 -77/\lambda^2 , is negative: it is a maximum.

The station’s three, with the derivative

The full working is in proofs 3 and 4; here, the result and one line of the idea.

  • Rain or not (Bernoulli). ℓ(p)=slog⁡p+(n−s)log⁡(1−p) \ell(p) = s \log p + (n – s)\log(1-p) , with s the number of rainy days. Differentiating, s/p−(n−s)/(1−p)=0 s/p – (n-s)/(1-p) = 0 , giving p^=s/n=Xˉ \hat p = s/n = \bar X : the proportion of rainy days.
  • Gusts (Poisson). ℓ(λ)=−nλ+slog⁡λ−∑log⁡xi! \ell(\lambda) = -n\lambda + s\log\lambda – \sum \log x_i! . Differentiating, −n+s/λ=0 -n + s/\lambda = 0 : λ^=Xˉ \hat\lambda = \bar X . The same as by moments.
  • Wind (Rayleigh). ℓ(α)=∑log⁡xi−nlog⁡α−12α∑xi2 \ell(\alpha) = \sum \log x_i – n\log\alpha – \frac{1}{2\alpha}\sum x_i^2 . Differentiating, −n/α+∑xi2/(2α2)=0 -n/\alpha + \sum x_i^2 / (2\alpha^2) = 0 :
α^=12n∑i=1nXi2 \hat\alpha = \frac{1}{2n}\sum_{i=1}^n X_i^2

Different from the moments one, α~=2Xˉ2/π \tilde\alpha = 2\bar X^2/\pi . The two methods do not have to agree.

The Rayleigh, without skipping anything

The wind line is the one that trips people up most, because α appears in two places in the density. Step by step:

StepResultWhy
Log of one densitylog⁡f(xi∣α)=log⁡xi−log⁡α−xi22α \log f(x_i \mid \alpha) = \log x_i – \log\alpha – \frac{x_i^2}{2\alpha} log⁡(x/α)=log⁡x−log⁡α \log(x/\alpha) = \log x – \log\alpha , and the log undoes the exponential: log⁡eu=u \log e^{u} = u
Add up the 40ℓ(α)=∑log⁡xi−nlog⁡α−12α∑xi2 \ell(\alpha) = \sum \log x_i – n\log\alpha – \frac{1}{2\alpha}\sum x_i^2 −log⁡α -\log\alpha is the same for all 40 readings, so it appears n times; 12α \frac{1}{2\alpha} is common and comes out of the sum
Differentiate with respect to αℓ′(α)=−nα+12α2∑xi2 \ell'(\alpha) = -\frac{n}{\alpha} + \frac{1}{2\alpha^2}\sum x_i^2 ∑log⁡xi \sum\log x_i has no α: derivative 0. That of −nlog⁡α -n\log\alpha is −n/α -n/\alpha . And 1/α 1/\alpha has derivative −1/α2 -1/\alpha^2 , which with the minus in front gives a plus
Set to 0 and multiply by 2α2 2\alpha^2 −2nα+∑xi2=0 -2n\alpha + \sum x_i^2 = 0 2α2 2\alpha^2 is positive: it clears the denominators without changing the solution
Solveα^=∑xi22n=7451.5480=93.14 \hat\alpha = \frac{\sum x_i^2}{2n} = \frac{7451.54}{80} = 93.14 with the data: the sum of the squares of the 40 readings, over 80
Second derivativeℓ′′(α^)=−nα^2=−4093.142<0 \ell''(\hat\alpha) = -\frac{n}{\hat\alpha^2} = -\frac{40}{93.14^2} < 0 ℓ′′(α)=nα2−∑xi2α3 \ell''(\alpha) = \frac{n}{\alpha^2} – \frac{\sum x_i^2}{\alpha^3} , and at α^ \hat\alpha we have ∑xi2=2nα^ \sum x_i^2 = 2n\hat\alpha : it becomes nα^2−2nα^2 \frac{n}{\hat\alpha^2} – \frac{2n}{\hat\alpha^2}

Differentiating with respect to x instead of α. In the likelihood the data are fixed numbers (the 40 readings) and the variable is α. If a 1/xi 1/x_i shows up when you differentiate, you have differentiated with respect to the data. You can cross out the ∑log⁡xi \sum \log x_i term from the start (it has no α), but do not confuse it with −nlog⁡α -n\log\alpha , which does.

p_ml      <- mean(rains)                       # Bernoulli: s / n
lambda_ml <- mean(gusts)                       # Poisson: the mean
alpha_ml  <- sum(wind^2) / (2 * length(wind))  # Rayleigh
round(c(p = p_ml, lambda = lambda_ml, alpha_ml = alpha_ml, alpha_mm = alpha_mm), 2)

Output:

       p   lambda alpha_ml alpha_mm 
    0.30     3.21    93.14    94.21 

How to read it

  • p 0.3: we estimate that in December it rains on 30 % of the days on campus.
  • lambda 3.21: the same as by moments, 3.21 gusts per hour.
  • alpha_ml 93.14 against alpha_mm 94.21: the two methods almost agree for the wind. It does not always happen; with the rain (further down) they split a lot.

Does the Rayleigh fit the wind? Checked as in every topic: density-scale histogram with the curve on top.

drayleigh <- function(x, alpha) x / alpha * exp(-x^2 / (2 * alpha))  # R does not have it
hist(wind, freq = FALSE, breaks = seq(0, 30, by = 3),
     main = "Wind on the roof", xlab = "km/h")
curve(drayleigh(x, alpha_ml), add = TRUE, lwd = 2)
The rooftop wind and its RayleighDensity-scale histogram of the 40 wind readings, from 0 to 30 km/h in classes of 3, with the tallest bar between 12 and 15. On top, in coral, the Rayleigh with alpha 93.14, peaking near 10 with a right tail, which follows the histogram. In dashed grey, the Rayleigh with alpha equal to the mean, 12.165, peaking at 3.5 and very tall: outside the histogram.0.000.020.040.060.08036912151821242730km/hdensityRayleigh(93.14)← my α = 12.16540 readingsα̂ = 93.14α = mean (wrong)
What hist and curve draw. The grey curve (cut at the top) is the one from my mistake in the hook.

In the picture, the histogram of the 40 readings and the Rayleigh with α^=93.14 \hat\alpha = 93.14 . The curve rises fast, peaks at 93.14=9.65 \sqrt{93.14} = 9.65 km/h and falls with a long tail to the right, like the histogram. The tallest bar (12-15 km/h) sits a little above the curve: with 40 data points, each bar wobbles. What matters is the shape: with the right α, the curve goes with the data; with my α = 12.165 from the hook, the peak would be at 3.5 km/h, outside the histogram.

Checking it without differentiating: optimize

In the exam you differentiate by hand. In R you can check it by maximising the log-likelihood directly. For one parameter, optimize:

loglik <- function(a) sum(log(drayleigh(wind, a)))   # l(alpha) with the data
optimize(loglik, interval = c(10, 500), maximum = TRUE)

Output:

$maximum
[1] 93.14425

$objective
[1] -127.8304

How to read it

  • $maximum 93.14: the α where the log-likelihood is largest. It matches the formula ∑xi2/80 \sum x_i^2 / 80 to the last decimal: the derivative was right.
  • $objective -127.83: the value of ℓ(α^) \ell(\hat\alpha) , the height of the hill at the top. It is negative because it is the log of a number below 1; it is not read on its own, it is for comparing.
  • interval = c(10, 500): the interval to search. It has to contain the maximum; if you use c(1, 50), it will return 50, the edge.
  • maximum = TRUE: without it, optimize minimises.

When you cannot differentiate: the uniform

Sometimes the four-step recipe does not work. The upload delay is a uniform(0, θ): f(x∣θ)=1/θ f(x \mid \theta) = 1/\theta if 0≤x≤θ 0 \le x \le \theta . The likelihood of 15 delays is

L(θ)=1θ15if θ≥max⁡(x1,…,x15),L(θ)=0 if θ<max⁡(xi) L(\theta) = \frac{1}{\theta^{15}} \quad \text{if } \theta \ge \max(x_1, \dots, x_{15}), \qquad L(\theta) = 0 \text{ if } \theta < \max(x_i)

The second part is the key: if θ were smaller than the largest delay observed (3.74 s), that data point would be impossible and L would be 0. And where it is not 0, 1/θ15 1/\theta^{15} always goes down: its derivative, −15/θ16 -15/\theta^{16} , is never zero. So the maximum is at the edge, at the first allowed θ:

θ^=max⁡(X1,…,Xn)=X(n) \hat\theta = \max(X_1, \dots, X_n) = X_{(n)}
The uniform(0, θ) likelihood with the 15 delaysGraph of L of theta between 3 and 6 seconds. Up to 3.74 it is 0, a line on the floor. At 3.74, the largest delay, it jumps to its maximum and from there falls steadily, like 1 over theta to the 15. The maximum is at the jump. A green line marks the moments estimator, 3.93, on the falling part.01.0·10⁻⁹2.0·10⁻⁹3.0·10⁻⁹3.03.54.04.55.05.56.0θ (s)L(θ)maximum: θ̂ = max(xᵢ) = 3.74L = 0: a data point would be impossiblemoments: 2x̄ = 3.93
The derivative is never zero: the maximum is a corner, at the largest data point.

In the picture: to the left of 3.74 the likelihood is 0 (a flat line on the floor); exactly at 3.74 it jumps up and from there it falls. The maximum is the jump. By moments, E(X)=θ/2 E(X) = \theta/2 gives θ~=2Xˉ=3.93 \tilde\theta = 2\bar X = 3.93 : this time it lands above the maximum, but with another sample it could land below, and then it would say the maximum delay is smaller than a delay you have already seen. In the practice we count how often with 10,000 samples.

With numbers it is even clearer. The likelihood of the 15 delays at four values of θ:

θL(θ) L(\theta) Why
3.500there is a delay of 3.74 s, impossible if the maximum were 3.50
3.741/3.7415=2.55×10−9 1/3.74^{15} = 2.55 \times 10^{-9} the first θ that allows all the data
41/415=9.31×10−10 1/4^{15} = 9.31 \times 10^{-10} allowed, but each data point falls in a wider interval and is less likely
51/515=3.28×10−11 1/5^{15} = 3.28 \times 10^{-11} wider still, lower still

With θ = 3.74 the sample is (4/3.74)15=2.74 (4/3.74)^{15} = 2.74 times more likely than with θ = 4. That is why the MLE is the largest data point: stretching θ always makes it worse, and shrinking it below 3.74 makes it impossible.

Differentiating log⁡L(θ)=−nlog⁡θ \log L(\theta) = -n\log\theta , setting it to 0 and getting stuck at −n/θ=0 -n/\theta = 0 . There is no solution because the maximum is not where the derivative is zero: it is at an edge. When the support depends on the parameter (uniform(0, θ), shifted exponential…), draw L before differentiating.

When you cannot solve it: the gamma with optim

With the rain, the gamma has two parameters. The log-likelihood is

ℓ(α,β)=nαlog⁡β−nlog⁡Γ(α)+(α−1)∑log⁡xi−β∑xi \ell(\alpha, \beta) = n\alpha\log\beta – n\log\Gamma(\alpha) + (\alpha – 1)\sum\log x_i – \beta\sum x_i

Differentiating with respect to β gives β=α/xˉ \beta = \alpha / \bar x , easy. But the derivative with respect to α brings in Γ′(α)/Γ(α) \Gamma'(\alpha)/\Gamma(\alpha) , the digamma function, and the equation cannot be solved by hand. It is solved numerically, and that is what optim is for:

loglik2 <- function(par, x) sum(dgamma(x, shape = par[1], rate = par[2], log = TRUE))
mle <- optim(par = c(a_mm, b_mm), fn = loglik2, x = rain,
             method = "L-BFGS-B", lower = c(0.01, 0.01),
             control = list(fnscale = -1))
mle

Output:

$par
[1] 2.6089050 0.3775559

$value
[1] -54.64027

$counts
function gradient 
      12       12 

$convergence
[1] 0

$message
[1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

How to read it

  • $par 2.61 0.38: the MLEs, α^=2.61 \hat\alpha = 2.61 (shape) and β^=0.38 \hat\beta = 0.38 (rate). By moments they were 3.77 and 0.55: here the methods do split. Their mean is the same, 2.61/0.378=6.91 2.61/0.378 = 6.91 mm; what changes is the shape of the curve.
  • $value -54.64: the value of ℓ \ell at the maximum.
  • $counts: how many times it evaluated the function and the gradient. A curiosity, not commented on in the exam.
  • $convergence 0: the first thing to look at. 0 means the algorithm finished fine; another number (1, 51, 52) is a warning that the result may not be the maximum.
  • $message: the reason it stopped, in the algorithm’s words.

What matters in the code:

  • par = c(a_mm, b_mm): the starting values. Starting from the moments ones is a good habit: they are close and the algorithm gets there sooner.
  • fn = loglik2: the function to optimise, with the parameters in a vector (par[1], par[2]) as first argument.
  • x = rain: the data, which optim passes as they are to loglik2.
  • control = list(fnscale = -1): essential. optim minimises; with fnscale = -1 it maximises. Without it, it would look for the minimum of the log-likelihood.
  • method = "L-BFGS-B", lower = c(0.01, 0.01): stops the parameters becoming negative (a gamma with shape −1 does not exist and dgamma would return NaN). The notes use the default method; both get to the same place.

fitdist: the shortcut

The fitdistrplus package does all this in one line, and it is the one used in the exam:

suppressMessages(library(fitdistrplus))   # load the package without its messages
rain <- c(5.0, 8.8, 6.8, 6.1, 0.8, 8.7, 3.7, 10.0, 7.4, 1.6,
          7.1, 1.3, 7.5, 6.6, 12.0, 4.0, 7.2, 12.2, 14.8, 6.6)
fitdist(rain, "gamma", method = "mle")

Output:

Fitting of the distribution ' gamma ' by maximum likelihood 
Parameters:
       estimate Std. Error
shape 2.6089268  0.7780414
rate  0.3775861  0.1241446
  • estimate: the MLEs, the same as with optim (2.61 and 0.38) except in the third decimal, because each algorithm stops a little earlier or later.
  • Std. Error: an estimate of the standard deviation of each estimator: how much the estimate would change from one sample of 20 days to another. The shape (2.61 with an error of 0.78) is much less well pinned down than the rate. We will use it in confidence intervals.
  • method = "mle" is maximum likelihood (the default); method = "mme" would be the method of moments.

In the picture, the two gammas over the rain histogram:

hist(rain, freq = FALSE, breaks = seq(0, 16, by = 2),
     main = "Rain on rainy days", xlab = "mm")
curve(dgamma(x, mle$par[1], mle$par[2]), add = TRUE, lwd = 2)    # ML
curve(dgamma(x, a_mm, b_mm), add = TRUE, lwd = 2, lty = 2)       # moments
The rain with the two gammasDensity-scale histogram of the rain on 20 days, from 0 to 16 mm in classes of 2, with a strong peak between 6 and 8. On top, the maximum likelihood gamma, shape 2.61 and rate 0.38, in solid coral, wider; and the moments one, shape 3.77 and rate 0.55, in dashed violet, more peaked in the center. Both have mean 6.91.0.000.050.100.150.200246810121416mmdensity20 rainy daysML: gamma(2.61, 0.38)moments: (3.77, 0.55)
What hist and the two curve calls draw: same mean, different shape.

The maximum likelihood one (solid) is wider and gives more probability to days of 1 or 2 mm and to those of 14; the moments one (dashed) is more peaked at 6-8 mm. The histogram, with a strong peak at 6-8 and three days below 2, does not clearly choose between them: with 20 data points, that is normal.

The invariance principle

Once you have θ^ \hat\theta , often you are not interested in θ itself but in something that depends on it: the probability of a very windy reading, the mean rainfall, the median… The invariance principle says you do not have to start again:

Invariance principle of the MLE. If θ^ \hat\theta is the MLE of θ, then the MLE of any function τ(θ) \tau(\theta) is τ(θ^) \tau(\hat\theta) .

Two examples from the station:

  • Probability that the wind goes above 20 km/h. In the Rayleigh, P(X>x)=e−x2/(2α) P(X > x) = e^{-x^2/(2\alpha)} . Its MLE is e−400/(2α^) e^{-400/(2\hat\alpha)} .
  • Probability that a rainy day goes above 10 mm. Its MLE is 1 - pgamma(10, 2.61, 0.38).
round(c(wind_20 = exp(-20^2 / (2 * alpha_ml)),
        rain_10 = 1 - pgamma(10, mle$par[1], mle$par[2]),
        wind_mean = sqrt(alpha_ml * pi / 2)), 3)

Output:

  wind_20   rain_10 wind_mean 
    0.117     0.201    12.096 
  • wind_20 0.117: we estimate that 11.7 % of readings are above 20 km/h. In the sample, 6 of 40 (15 %): the Rayleigh smooths out what a sample of 40 exaggerates or misses.
  • rain_10 0.201: 20 % of rainy days go above 10 mm.
  • wind_mean 12.096: the MLE of the mean wind speed, α^π/2 \sqrt{\hat\alpha\pi/2} . Almost the sample mean (12.165), but not exactly: it is the mean of the fitted Rayleigh.

Invariance belongs to MLEs. There is no such theorem for the method of moments: the “moments estimate” of e−400/(2α) e^{-400/(2\alpha)} would have to be derived again by matching moments.

Moments or maximum likelihood: which one I use

How I get the maximum likelihood estimatorDecision diagram. First question: if the support depends on the parameter, do not differentiate, draw L and the estimator is the sample maximum or minimum. If not, second question: if the equation derivative equal to zero can be solved, use the derivative with a negative second derivative, as in the Poisson, the exponential or the Rayleigh. If not, solve it numerically with optim or fitdist, with the moments estimator as the starting point.Does the support dependon the parameter?No derivative: draw Lθ̂ = max(xᵢ) or min(xᵢ)yesnoCan you solveℓ'(θ) = 0 ?Derivative and ℓ'' < 0Poisson, exp, Rayleigh…yesnoNumericaloptim · fitdistMomentsto start optimstarting point
Two questions decide the path; moments give the starting point.

The map sums up how you decide in an exam. And the table, the comparison:

Method of momentsMaximum likelihood
IdeaMatch population and sample momentsChoose the parameter that makes the data most likely
What you needE(X) E(X) (and E(X2) E(X^2) or the variance with two parameters)The density or mass, f(x∣θ) f(x \mid \theta)
WorkingSolve a systemDifferentiate the log-likelihood (or optim)
Unique?No: depends on which moments you matchYes, if the maximum is unique
Can give impossible valuesYes (2x̄ below the maximum in the uniform)No: it never picks a θ that makes a data point impossible
InvarianceNoYes
In RFormulas with mean and (n-1)/n*varoptimize, optim(..., fnscale = -1), fitdist
We use it forStarting values for optim, quick sumsThe “good” estimate (it is the one usually asked for)

What gets proved in this topic

All of it, step by step and checked in R, in the point estimation proofs:

  • The mean of the Rayleigh, απ/2 \sqrt{\alpha\pi/2} , and its moments estimator.
  • Moments with two parameters: the gamma and the uniform(a, b); and why the moments estimator is not unique.
  • MLE of the Bernoulli and the Poisson, with the second derivative.
  • MLE of the exponential and the Rayleigh.
  • MLE of the normal with μ and σ unknown (the system of two equations) and why the gamma needs optim.
  • MLE of the uniform(0, θ) without differentiating.
  • Invariance and the distribution of the Rayleigh MLE with the moment generating function.

Common mistakes

Confusing the parameter with the mean. They only coincide in some families (Poisson, Bernoulli, the μ of the normal). In the Rayleigh, the exponential (mean 1/λ) or the gamma (mean α/β), the estimator comes from solving.

Forgetting the logarithm and differentiating the product. It can be done, but the product rule with 30 factors is hell. The logarithm turns ∏ \prod into ∑ \sum and does not move the maximum.

Not checking the second derivative. It is marked in the exam: without it you do not know whether it is a maximum or a minimum.

optim without control = list(fnscale = -1). It looks for the minimum of the log-likelihood and ends up at an edge, with a meaningless result and convergence 0. In the exercises we see it live.

Confusing rate and scale in the gamma. In R, dgamma(x, shape, rate); if you write dgamma(x, 2.61, 0.38) without names, the third argument is rate. In the notes, gamma(α, β) is shape and rate, just like R.

Try it in the R playground

Open the R playground on the site (or RStudio) and paste this script, which brings together the essentials of the article with the wind data:

wind <- c(7.9, 12.2, 11.5, 11.8, 7.0, 11.7, 4.6, 20.3, 9.4, 10.6,
          12.9, 3.1, 9.2, 27.3, 12.3, 5.5, 14.2, 14.2, 7.4, 21.1,
          14.6, 6.4, 25.4, 8.2, 14.8, 4.1, 7.1, 18.0, 5.6, 16.4,
          13.1, 19.7, 12.1, 11.0, 9.1, 20.8, 21.7, 18.6, 1.2, 4.5)
n <- length(wind)
alpha_mm <- 2 * mean(wind)^2 / pi          # moments
alpha_ml <- sum(wind^2) / (2 * n)          # maximum likelihood
loglik <- function(a) sum(log(wind / a) - wind^2 / (2 * a))
curve(sapply(x, loglik), 50, 200, xlab = "alpha", ylab = "log-likelihood")
abline(v = c(alpha_mm, alpha_ml), lty = c(2, 1))
c(moments = alpha_mm, ml = alpha_ml,
  optimize = optimize(loglik, c(10, 500), maximum = TRUE)$maximum)

Things to look at:

  1. In the Environment panel, alpha_mm and alpha_ml: 94.21 and 93.14.
  2. In Plots, the log-likelihood hill with its top on the solid line. The dashed one (moments) is almost in the same place.
  3. Why the sapply: curve passes the function a vector of x, and loglik is meant for a single α (it does a sum inside). Remove the sapply and you will see a meaningless straight line.
  4. Change the optimize interval to c(10, 50): it returns almost 50, the edge. The interval has to contain the maximum.

Summary

ConceptWhat it isIn R
EstimatorFormula with the sample, θ^(X1,…,Xn) \hat\theta(X_1, \dots, X_n) A function of x
EstimateThe number with your dataThat function’s result
S′2 S'^2 Variance dividing by n(n - 1) / n * var(x)
MomentsSet E(Xk) E(X^k) equal to mk m_k and solvemean, s2prime
LikelihoodL(θ)=∏f(xi∣θ) L(\theta) = \prod f(x_i \mid \theta) , with θ as the variableprod(dxxx(x, θ))
MLEThe θ that maximises L (or ℓ=log⁡L \ell = \log L )optimize, optim(fnscale = -1), fitdist
Uniform(0, θ)θ^=max⁡(Xi) \hat\theta = \max(X_i) , without differentiatingmax(x)
InvarianceMLE of τ(θ) \tau(\theta) = τ(θ^) \tau(\hat\theta) Plug θ^ \hat\theta into the formula
DistributionMomentsMaximum likelihood
Bernoulli(p)Xˉ \bar X Xˉ \bar X
Poisson(λ)Xˉ \bar X Xˉ \bar X
exp(λ)1/Xˉ 1/\bar X 1/Xˉ 1/\bar X
Rayleigh(α)2Xˉ2/π 2\bar X^2/\pi ∑Xi2/(2n) \sum X_i^2 / (2n)
normal(μ, σ)Xˉ \bar X , S′2 S'^2 Xˉ \bar X , S′2 S'^2
gamma(α, β)Xˉ2/S′2 \bar X^2/S'^2 , Xˉ/S′2 \bar X/S'^2 With optim or fitdist
uniform(0, θ)2Xˉ 2\bar X max⁡(Xi) \max(X_i)

Three ideas to take away:

  1. Estimating is giving the parameter a value from the data. The estimator is a formula (a random variable); the estimate, a number.
  2. Moments match; maximum likelihood maximises. Sometimes they agree (Poisson, normal) and sometimes not (Rayleigh, gamma, uniform). Maximum likelihood is the one asked for almost always, and it has invariance.
  3. First the log, then the derivative, then the second derivative. And if it cannot be solved or the support depends on the parameter, optim or a picture of L.

Now it is time to do it on the computer: in the point estimation practice in R you estimate the station’s five series by both methods, draw likelihoods and fit the rain with optim and fitdist. And to check you have got it, the point estimation exercises go from easy to hard as chained exam-style exercises, with commented solutions.

Next step: proofsPoint estimation — step-by-step proofs with the campus station, the log-likelihood and the second derivative (and the uniform derivative that never hit zero)

Did this article help you? Here is how you can support SergioLearns →