Point estimation — exam-style exercises with the humidity, the lightning around the station and the pressure (and the var() that cost me half a point)

In the EC2 practice exam we did in class I was asked for the moments estimator of a gamma, I computed it with var() and lost half a point. My shape was 3.58; the right one, 3.77. I had matched the population variance to the sample variance, the one that divides by n − 1, when the method matches moments and the sample’s central moment divides by n. It was a one-line detail, in the first part, and since the parts of an exercise are chained, the mistake dragged into the next two. This article is so that it does not happen to you in the real one.

These are three chained exercises like those in the ME1 EC2 (one context per exercise and parts that need the previous one), with the weather station on the campus roof as the thread, plus a bonus. Before you start, it helps to have the theory, the proofs and the practice of point estimation.

How to use this article

  1. Do the exercises with the exam in mind. In the ME1 EC2 you have 4 hours for 50 points, with RStudio and the data open. These three exercises add up to 40 points plus a 2.5-point bonus: give them about 3 hours.
  2. Paper for what is proved, R for what is computed. The derivations of the estimators, on paper; the estimates and checks, in R, with 2 decimals as the exam asks. Keep the code in a script: at the end you have to upload the .R file with the code for each exercise.
  3. Before differentiating, look at the support. If the interval where the variable lives depends on the parameter, do not differentiate: draw the likelihood.
  4. Mark with the criteria. Each solution comes with “How it is marked” and a breakdown of the points. It is a guide, mine: not the official criteria.
  5. Finish with the cheat sheet and “For your script”. The cheat sheet only has results (it is what you can take, handwritten: two sheets, both sides, in the EC2) and “For your script” is the topic’s code, ready for your .R file. If a derivation gets stuck, the proofs have the step by step.

Exercise 1: midday humidity (10 points)

The station records the relative humidity at midday as a proportion between 0 and 1. On winter days, the model that suits it best has density

f(x∣θ)=θ xθ−1,0<x<1,θ>0 f(x \mid \theta) = \theta\, x^{\theta – 1}, \qquad 0 < x < 1, \quad \theta > 0

These are 12 middays:

humidity <- c(0.982, 0.708, 0.873, 0.976, 0.616, 0.610,
              0.841, 0.854, 0.837, 0.861, 0.975, 0.934)
  1. (2 points) Given a simple random sample X1,…,Xn X_1, \dots, X_n , obtain the method of moments estimator of θ, θ~ \tilde\theta .
  2. (3 points) Obtain the maximum likelihood estimator, θ^ \hat\theta , and check that it is a maximum.
  3. (2 points) Compute the two estimates with the data and compare them.
  4. (2 points) Check with optim the value of θ^ \hat\theta from part 3.
  5. (1 point) Give a maximum likelihood estimate of the probability that the midday humidity goes above 90 %.

Exercise 2: lightning around the station (15 points)

In a storm, the lightning strikes the station detects fall at random (uniformly) within a circle of radius θ km around it; θ, the detector’s range, is unknown. Let R be the distance from a strike to the station. These are the distances, in km, of 15 strikes:

strikes <- c(3.61, 7.22, 3.45, 2.87, 8.27, 5.76, 7.71, 8.50,
             3.85, 8.57, 8.80, 7.75, 6.59, 8.79, 5.21)
  1. (2 points) Justify that the density of R is f(r∣θ)=2r/θ2 f(r \mid \theta) = 2r/\theta^2 for 0≤r≤θ 0 \le r \le \theta .
  2. (2 points) Obtain the method of moments estimator of θ.
  3. (4 points) Obtain the maximum likelihood estimator of θ, giving the reasoning.
  4. (2 points) Compute the two estimates with the data and say which one you would use.
  5. (3 points) In R, simulate 10,000 storms of 15 strikes with θ = 9 and compute the mean of each estimator and the proportion of storms in which the moments one falls below the largest distance observed. Interpret the results.
  6. (2 points) Give maximum likelihood estimates of the area the detector watches and of the median distance of a strike.

Exercise 3: morning pressure (15 points)

The atmospheric pressure at 8:00 at the station, in hPa, is assumed normal N(μ,σ) N(\mu, \sigma) . These are 15 mornings:

pressure <- c(1018.7, 1015.3, 1009.3, 1007.0, 1009.1, 1014.8, 1015.3, 1011.6,
              1016.3, 1015.6, 1014.1, 1016.1, 1010.3, 1016.1, 1016.2)
  1. (4 points) With μ and σ unknown, obtain the maximum likelihood estimators of μ and σ².
  2. (3 points) Compute the estimates with the data. Does the one for σ² match var(pressure)? Explain why.
  3. (2 points) Obtain the estimates with fitdist and interpret the Std. Error column.
  4. (3 points) A pressure below 1010 hPa signals a low. Give a maximum likelihood estimate of the probability that a morning is below 1010 hPa and of the standard deviation σ. Justify the method.
  5. (3 points) Now assume μ = 1013 hPa is known. Obtain the MLE of σ² in this case and compute it with the data.

Bonus (2.5 points)

The strong gusts per hour are Poisson(λ) and, with n hours, the MLE is λ^=Xˉ \hat\lambda = \bar X . Prove with the moment generating function that nλ^∼Pois(nλ) n\hat\lambda \sim \text{Pois}(n\lambda) and, if the true λ were 3, compute in R the probability that with 24 hours λ^ \hat\lambda comes out above 3.5.

Solutions

Exercise 1

Part 1. First, the population mean:

E(X)=∫01x⋅θxθ−1dx=θ∫01xθdx=θ[xθ+1θ+1]01=θθ+1 E(X) = \int_0^1 x\cdot\theta x^{\theta-1}dx = \theta\int_0^1 x^{\theta}dx = \theta\Big[\frac{x^{\theta+1}}{\theta+1}\Big]_0^1 = \frac{\theta}{\theta+1}

Setting it equal to Xˉ \bar X : θθ+1=Xˉ⇒θ=Xˉθ+Xˉ⇒θ(1−Xˉ)=Xˉ \frac{\theta}{\theta+1} = \bar X \Rightarrow \theta = \bar X\theta + \bar X \Rightarrow \theta(1 – \bar X) = \bar X , and

θ~=Xˉ1−Xˉ \tilde\theta = \frac{\bar X}{1 – \bar X}

Why go through E(X) E(X) instead of setting θ equal to the mean. The method of moments does not say “θ is the mean”: it says “the mean the model predicts has to equal the mean of the data”. That is why the first step is to work out E(X) E(X) as a function of θ, and only then set it equal and solve. The shortcut θ~=Xˉ=0.84 \tilde\theta = \bar X = 0.84 fails the check: with θ = 0.84 the model would predict a mean of 0.84/1.84=0.46 0.84/1.84 = 0.46 , not 0.84.

Do not round Xˉ \bar X before solving. With 0.84 you get 0.84/0.16=5.25 0.84/0.16 = 5.25 ; with the full mean (0.8389…), 5.21. The 2 decimals go only on what you write at the end.

Part 2. L(θ)=∏θxiθ−1=θn(∏xi)θ−1 L(\theta) = \prod \theta x_i^{\theta-1} = \theta^n\big(\prod x_i\big)^{\theta-1} . With t=∑log⁡xi t = \sum\log x_i :

ℓ(θ)=nlog⁡θ+(θ−1) t,ℓ′(θ)=nθ+t=0  ⟹  θ^=−n∑i=1nlog⁡Xi \ell(\theta) = n\log\theta + (\theta – 1)\,t, \qquad \ell'(\theta) = \frac{n}{\theta} + t = 0 \;\Longrightarrow\; \hat\theta = -\frac{n}{\sum_{i=1}^n\log X_i}

Since the xi x_i are between 0 and 1, their logs are negative, t < 0 and θ^>0 \hat\theta > 0 . Second derivative: ℓ′′(θ)=−n/θ2<0 \ell''(\theta) = -n/\theta^2 < 0 for every θ: a maximum, and the only one.

Why the logarithm. Differentiating L(θ) L(\theta) as it stands forces the product rule with a power whose exponent varies. The logarithm turns the product into a sum and does not move the maximum, because it is increasing: the θ that makes L largest makes ℓ \ell largest. Notice too that the 12 data points only come in through one number, t=∑log⁡xi t = \sum\log x_i : before you open R you already know it is the only thing you need to compute.

Check you’ve got it: if t comes out as −2.25, why does 12/2.25 give 5.33 when R says 5.32?

Nobody is wrong: it is rounding. The unrounded sum of logs is −2.2546, and 12/2.2546=5.32 12/2.2546 = 5.32 . Keep the full value in a variable (like theta_ml) and round only when you write it down.

Part 3.

n <- length(humidity)
theta_mm <- mean(humidity) / (1 - mean(humidity))   # moments
theta_ml <- -n / sum(log(humidity))                 # maximum likelihood
round(c(mean = mean(humidity), sum_logs = sum(log(humidity)),
        moments = theta_mm, ml = theta_ml), 2)

Output:

    mean sum_logs  moments       ml 
    0.84    -2.25     5.21     5.32 

θ~=5.21 \tilde\theta = 5.21 and θ^=5.32 \hat\theta = 5.32 . They are close, but not the same: the methods give different estimators. A θ of 5 or more says the density piles up near 1: winter middays are humid.

Part 4.

loglik <- function(th) n * log(th) + (th - 1) * sum(log(humidity))
o <- optim(par = theta_mm, fn = loglik, method = "Brent", lower = 0.01, upper = 50,
           control = list(fnscale = -1))
round(c(optim = o$par, formula = theta_ml, convergence = o$convergence), 2)

Output:

      optim     formula convergence 
       5.32        5.32        0.00 

optim, starting at the moments one, reaches 5.32: the same value as the formula. convergence 0: it finished fine.

Why start at the moments estimate and use Brent. optim needs a starting point, and the best one you have is one that is already close: 5.21. With a single parameter, the default method (Nelder-Mead) warns that it is unreliable; Brent is the one for one dimension, but it needs an interval. The lower bound of 0.01 is not a whim: θ has to be positive and log⁡θ \log\theta does not exist at 0.

Part 5. P(X>0.9)=∫0.91θxθ−1dx=1−0.9θ P(X > 0.9) = \int_{0.9}^1 \theta x^{\theta-1}dx = 1 – 0.9^\theta . By the invariance principle, its MLE is 1−0.9θ^ 1 – 0.9^{\hat\theta} :

round(c(mle = 1 - 0.9^theta_ml, in_the_sample = mean(humidity > 0.9)), 2)

Output:

          mle in_the_sample 
         0.43          0.33 

43 % of middays above 90 % humidity. In the sample it was 4 of 12 (33 %): the model’s estimate does not have to match the proportion over 12 days.

The humidity log-likelihoodCurve of the log-likelihood of the 12 middays for theta between 1 and 12. It rises to its top at 5.32, the maximum likelihood estimator marked in coral, and falls slowly. A dashed green line marks the moments one, 5.21, very close to the top.0246810123456789101112θℓ(θ)θ̂ = 5.32 (ML)θ̃ = 5.21 (moments)
The top is flat: between 5.2 and 5.3 the log-likelihood hardly changes.

In the picture, the log-likelihood from part 4: the top is at 5.32 (solid line) and the moments one (dashed) lands just to its left, where the curve barely drops. That is why they give such similar values.

How it is marked. Part 1: E(X) E(X) (1), solving (1). Part 2: likelihood and log (1), differentiate and solve (1), second derivative (1). Part 3: the two numbers (1 + 1). Part 4: optim with fnscale = -1 (1) and comment (1). Part 5: invariance (0.5) and number (0.5).

Exercise 2

Part 1. “Uniformly in the circle” means that the probability of falling in a region is proportional to its area. A strike falling less than r km away means falling inside the circle of radius r:

F(r)=P(R≤r)=πr2πθ2=r2θ2,0≤r≤θ F(r) = P(R \le r) = \frac{\pi r^2}{\pi\theta^2} = \frac{r^2}{\theta^2}, \qquad 0 \le r \le \theta

Differentiating: f(r)=F′(r)=2r/θ2 f(r) = F'(r) = 2r/\theta^2 . It is increasing: there is more room far away than close by, so distant strikes are more frequent.

The 15 strikes around the detectorOn the left, a circle of radius theta with the station at the center and the 15 strikes drawn at their real distance, spread out in angle so they can be seen; almost half fall near the outer edge. A dashed circle marks half the radius. On the right, the density f of r equal to 2r over theta squared, a straight line rising from 0 to 2 over 9 at r equal to 9.θthe station at the center0.00.10.20369r (km)f(r)f(r) = 2r/θ²More area far away, more strikes far away
The outer ring has three times the area of the inner circle: that is why the density grows with r.

In the picture, the detector’s circle with the 15 strikes at their distance (the angle does not matter: they are spread out so you can see them). The outer ring has much more area than the inner circle of the same thickness: that is why the density grows with r.

Part 2.

E(R)=∫0θr⋅2rθ2dr=2θ2[r33]0θ=2θ3  ⟹  2θ3=Rˉ  ⟹  θ~=3Rˉ2 E(R) = \int_0^\theta r\cdot\frac{2r}{\theta^2}dr = \frac{2}{\theta^2}\Big[\frac{r^3}{3}\Big]_0^\theta = \frac{2\theta}{3} \;\Longrightarrow\; \frac{2\theta}{3} = \bar R \;\Longrightarrow\; \tilde\theta = \frac{3\bar R}{2}

Part 3. The support [0,θ] [0, \theta] depends on θ: do not differentiate blindly. The likelihood is

L(θ)=∏i=1n2riθ2=2n∏riθ2nif θ≥max⁡ri,L(θ)=0 if θ<max⁡ri L(\theta) = \prod_{i=1}^n \frac{2r_i}{\theta^2} = \frac{2^n\prod r_i}{\theta^{2n}} \quad\text{if } \theta \ge \max r_i, \qquad L(\theta) = 0 \text{ if } \theta < \max r_i

(if θ were smaller than some ri r_i , that strike would be impossible). For θ≥max⁡ri \theta \ge \max r_i , L is proportional to θ−2n \theta^{-2n} , which is decreasing (its derivative, −2nθ−2n−1 -2n\theta^{-2n-1} , is negative). So the maximum is at the smallest allowed θ:

θ^=max⁡(R1,…,Rn) \hat\theta = \max(R_1, \dots, R_n)

Why you do not set the derivative to 0 here. If you do it blindly, ℓ(θ)=nlog⁡2+∑log⁡ri−2nlog⁡θ \ell(\theta) = n\log 2 + \sum\log r_i – 2n\log\theta and ℓ′(θ)=−2n/θ \ell'(\theta) = -2n/\theta , which is never 0: there is no point with a horizontal tangent. That does not mean there is no maximum, but that it sits on an edge. The approach is to picture L in your head: it is 0 while θ is smaller than the farthest strike, jumps at 8.80 and from there only goes down. The highest point is the one at the jump.

Check you’ve got it: what is the likelihood at θ = 8.70 with these 15 strikes?

Zero. With a range of 8.70 km, the strikes at 8.80 and 8.79 km could not have been detected: their density is 0 and the whole product vanishes, however well the other 13 fit. That is why the MLE cannot go below the sample maximum.

Part 4.

round(c(moments = 1.5 * mean(strikes), ml = max(strikes)), 2)

Output:

moments      ml 
    9.7     8.8 

θ~=9.70 \tilde\theta = 9.70 km and θ^=8.80 \hat\theta = 8.80 km. This time the moments one lands above the farthest strike (8.80), so it is possible. I would use the maximum likelihood one: it never leaves a data point out and, as we will see in part 5, the moments one sometimes does.

Part 5. To simulate a uniform strike in the circle: F(r)=r2/θ2=u⇒r=θu F(r) = r^2/\theta^2 = u \Rightarrow r = \theta\sqrt u , with u uniform(0, 1).

Why the square root and not runif(15, 0, 9). The obvious way to get “15 random strikes up to 9 km” is to draw 15 uniform distances between 0 and 9. But what is uniform is the point in the circle, not the distance: as you saw in part 1, there is more room far away. With uniform distances the mean would be 4.5 km instead of 2θ/3=6 2\theta/3 = 6 km, and the simulation would be measuring a different model.

set.seed(9)
sim <- replicate(10000, {
  r <- 9 * sqrt(runif(15))             # 15 strikes with theta = 9
  c(ml = max(r), mm = 1.5 * mean(r))
})
round(c(mean_ml = mean(sim["ml", ]), mean_mm = mean(sim["mm", ]),
        mm_impossible = mean(sim["mm", ] < sim["ml", ])), 3)

Output:

      mean_ml       mean_mm mm_impossible 
        8.710         9.000         0.345 
  • The mean of the MLE, 8.71, is below θ = 9: the maximum of 15 strikes can never exceed 9 and hardly ever reaches it. It tends to fall short.
  • The mean of the moments one, 9.00, is right on θ: on average it hits.
  • But in 34 % of storms the moments one falls below the farthest strike: it says the range is shorter than the distance of a strike the detector has seen. Neither is perfect: one falls short on average and the other is sometimes impossible. Which is “better” is the question for the next topic.

Part 6. The watched area is πθ2 \pi\theta^2 and the median m satisfies F(m)=m2/θ2=1/2⇒m=θ/2 F(m) = m^2/\theta^2 = 1/2 \Rightarrow m = \theta/\sqrt2 . By invariance, their MLEs are πθ^2 \pi\hat\theta^2 and θ^/2 \hat\theta/\sqrt2 :

round(c(area = pi * max(strikes)^2, median = max(strikes) / sqrt(2),
        sample_median = median(strikes)), 2)

Output:

         area        median sample_median 
       243.28          6.22          7.22 

About 243 km² watched and a median of 6.22 km: half the strikes fall less than 6.22 km away. The sample median (7.22) is larger: with 15 strikes and an increasing density, the sample has quite a few distant strikes.

How it is marked. Part 1: F(r) F(r) with areas (1), differentiate (1). Part 2: E(R) E(R) (1), solve (1). Part 3: likelihood with the condition θ≥max⁡ri \theta \ge \max r_i (2), reasoning that it decreases (1), conclusion (1); differentiating and setting to 0 without reasoning about the support, 0. Part 4: the two numbers (1) and the choice (1). Part 5: correct simulation (1), the three numbers (1), interpretation (1). Part 6: invariance and the two values (1 + 1).

Exercise 3

Part 1. It is proof 5 of the topic. The log-likelihood:

ℓ(μ,σ)=−nlog⁡σ−n2log⁡(2π)−12σ2∑i=1n(xi−μ)2 \ell(\mu, \sigma) = -n\log\sigma – \frac n2\log(2\pi) – \frac{1}{2\sigma^2}\sum_{i=1}^n(x_i – \mu)^2

Differentiating with respect to μ: 1σ2∑(xi−μ)=0⇒μ^=Xˉ \frac{1}{\sigma^2}\sum(x_i – \mu) = 0 \Rightarrow \hat\mu = \bar X . With respect to σ: −nσ+1σ3∑(xi−μ)2=0⇒σ2=1n∑(xi−μ)2 -\frac n\sigma + \frac{1}{\sigma^3}\sum(x_i-\mu)^2 = 0 \Rightarrow \sigma^2 = \frac1n\sum(x_i – \mu)^2 , and substituting μ^ \hat\mu :

μ^=Xˉ,σ^2=1n∑i=1n(Xi−Xˉ)2=S′2 \hat\mu = \bar X, \qquad \hat\sigma^2 = \frac1n\sum_{i=1}^n(X_i – \bar X)^2 = S'^2

It is a maximum: ∂2ℓ/∂μ2=−n/σ2<0 \partial^2\ell/\partial\mu^2 = -n/\sigma^2 < 0 and, at μ=xˉ \mu = \bar x , ∂2ℓ/∂σ2=−2n/σ^2<0 \partial^2\ell/\partial\sigma^2 = -2n/\hat\sigma^2 < 0 at σ^ \hat\sigma .

The approach with two parameters. Two unknowns, two equations: one partial derivative per parameter, and you solve the system. The order matters: in the equation for μ the 1/σ2 1/\sigma^2 disappears when you set it to 0, so μ is solved on its own and then substituted into the one for σ. Differentiating with respect to σ rather than σ² is for convenience: by invariance, the MLE of σ² is the square of the MLE of σ.

Part 2.

n <- length(pressure)
mu_ml <- mean(pressure)
s2_ml <- mean((pressure - mu_ml)^2)          # divides by n
round(c(mu = mu_ml, sigma2_ml = s2_ml, var_R = var(pressure),
        ratio = s2_ml / var(pressure), fourteen_fifteenths = (n - 1) / n), 4)

Output:

                 mu           sigma2_ml               var_R 
          1013.7200             10.7336             11.5003 
              ratio fourteen_fifteenths 
             0.9333              0.9333 

μ^=1013.72 \hat\mu = 1013.72 hPa and σ^2=10.73 \hat\sigma^2 = 10.73 hPa². var(pressure) gives 11.50: it does not match, because var() divides by n − 1 = 14 and the MLE by n = 15. The ratio, 0.9333, is exactly 14/15 14/15 .

Part 3.

suppressMessages(library(fitdistrplus))
pressure <- c(1018.7, 1015.3, 1009.3, 1007.0, 1009.1, 1014.8, 1015.3, 1011.6,
              1016.3, 1015.6, 1014.1, 1016.1, 1010.3, 1016.1, 1016.2)
fitdist(pressure, "norm")

Output:

Fitting of the distribution ' norm ' by maximum likelihood 
Parameters:
        estimate Std. Error
mean 1013.720000  0.8459157
sd      3.276217  0.5981525

fitdist gives μ^=1013.72 \hat\mu = 1013.72 and σ^=3.28 \hat\sigma = 3.28 (the root of 10.73). The Std. Error column estimates the standard deviation of each estimator: that of μ^ \hat\mu , 0.85 hPa, is σ^/15 \hat\sigma/\sqrt{15} ; that of σ^ \hat\sigma , 0.60 hPa. They tell you how much the estimates would change with another 15 mornings: the mean, less than 1 hPa up or down.

Part 4. P(X<1010)=Φ(1010−μσ) P(X < 1010) = \Phi\big(\frac{1010 – \mu}{\sigma}\big) , a function of (μ, σ). By the invariance principle, its MLE is obtained by plugging in the MLEs; and the MLE of σ is σ^2 \sqrt{\hat\sigma^2} :

round(c(p_low = pnorm(1010, mu_ml, sqrt(s2_ml)), sigma_ml = sqrt(s2_ml),
        in_the_sample = mean(pressure < 1010)), 4)

Output:

        p_low      sigma_ml in_the_sample 
       0.1281        3.2762        0.2000 

12.8 % of mornings with low-pressure readings and σ^=3.28 \hat\sigma = 3.28 hPa. In the sample, 3 of 15 (20 %).

Using sd(pressure) in pnorm. sd() is the root of the sample variance (divides by n − 1) and is not the MLE of σ; with it you would get 0.1363 instead of 0.1281.

Part 5. With μ = μ₀ = 1013 known, only σ is left. In the derivative with respect to σ there is nothing to substitute:

σ^02=1n∑i=1n(Xi−1013)2 \hat\sigma^2_0 = \frac1n\sum_{i=1}^n(X_i – 1013)^2
round(c(sigma2_mu_known = mean((pressure - 1013)^2), sigma2_mu_estimated = s2_ml), 4)

Output:

    sigma2_mu_known sigma2_mu_estimated 
            11.2520             10.7336 

11.25 hPa², somewhat larger than 10.73. It always happens: ∑(xi−c)2 \sum(x_i – c)^2 is smallest when c=xˉ c = \bar x (the parabola from descriptive statistics), so with any other center the sum is larger.

What changes when μ is known. There is no system any more: one parameter, one derivative. The typical mistake is to carry over the previous formula with Xˉ \bar X inside; the center is the 1013 you are given, even if the data say something else. And the difference has an exact formula: σ^02=σ^2+(xˉ−1013)2=10.73+0.722=10.73+0.52=11.25 \hat\sigma_0^2 = \hat\sigma^2 + (\bar x – 1013)^2 = 10.73 + 0.72^2 = 10.73 + 0.52 = 11.25 .

Check you’ve got it: what if the known μ were 1013.72?

You would get 10.73, the same as with μ estimated: the known center would be exactly xˉ \bar x and the term (xˉ−μ0)2 (\bar x – \mu_0)^2 would be 0.

How it is marked. Part 1: log-likelihood (1), the two derivatives (1 + 1), system and second derivative (1). Part 2: the numbers (2), explaining n against n − 1 (1). Part 3: fitdist (1), interpreting Std. Error (1). Part 4: invariance justified (1), probability (1), σ (1). Part 5: formula (2), number (1).

Bonus

The mgf of a Poisson(λ) is MX(t)=eλ(et−1) M_X(t) = e^{\lambda(e^t – 1)} . Since the Xi X_i are independent, that of nλ^=∑Xi n\hat\lambda = \sum X_i is the product:

M∑Xi(t)=∏i=1neλ(et−1)=enλ(et−1) M_{\sum X_i}(t) = \prod_{i=1}^n e^{\lambda(e^t-1)} = e^{n\lambda(e^t – 1)}

which is the mgf of a Poisson(nλ). So nλ^∼Pois(nλ) n\hat\lambda \sim \text{Pois}(n\lambda) . With n = 24 and λ = 3, ∑Xi∼Pois(72) \sum X_i \sim \text{Pois}(72) , and λ^>3.5  ⟺  ∑Xi>84  ⟺  ∑Xi≥85 \hat\lambda > 3.5 \iff \sum X_i > 84 \iff \sum X_i \ge 85 :

c(p = 1 - ppois(84, 72))     # P(sum >= 85) = 1 - P(sum <= 84)

Output:

         p 
0.07318557 

7.3 %. With a true λ = 3, 24 hours would give an estimate above 3.5 about 7 times in 100. So a day with the estimate above 3.5 is nothing odd even if λ is still 3.

Why 84 in ppois and not 85. The approach is to turn the question about λ^ \hat\lambda into one about the sum, which is the one with a known distribution. The sum is a whole number, so “greater than 84” means “85 or more”, and since ppois gives P(S≤q) P(S \le q) , the complement of S≥85 S \ge 85 is S≤84 S \le 84 . With 1 - ppois(85, 72) you would get 0.06 (you lose the 85) and with 1 - ppois(83, 72), 0.09 (you count the 84, which leaves λ^ \hat\lambda at exactly 3.5, not above it).

How it is marked. The mgf and the product (1), recognising the Poisson (0.5), going from λ^>3.5 \hat\lambda > 3.5 to the sum (0.5) and the number (0.5).

What’s wrong here?

Three solutions by other people. Before reading the explanation, predict what goes wrong.

Fragment 1. Moments estimator of the rain gamma:

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)
c(shape = mean(rain)^2 / var(rain), rate = mean(rain) / var(rain))

Output:

    shape      rate 
3.5793461 0.5179951 

var() divides by n − 1: it is the sample variance S2 S^2 , not the central moment S′2 S'^2 . The shape comes out 3.58 and the rate 0.52, instead of 3.77 and 0.55. It is my half point from the practice exam.

In the method of moments, the sample variance divides by n. With 20 data points the error is 5 %; with 10, 10 %.

Define s2p <- function(x) mean((x - mean(x))^2) once at the top of the script and always use it in moments and in the MLE of σ².

Fragment 2. Checking the humidity MLE with optim:

humidity <- c(0.982, 0.708, 0.873, 0.976, 0.616, 0.610,
              0.841, 0.854, 0.837, 0.861, 0.975, 0.934)
loglik <- function(th) 12 * log(th) + (th - 1) * sum(log(humidity))
optim(par = 5, fn = loglik, method = "Brent", lower = 0.01, upper = 50)$par

Output:

[1] 50

Without control = list(fnscale = -1), optim minimises. The log-likelihood goes down the further θ moves from 5.32 on either side, so its minimum inside the interval is at an edge: here, 50 (with a large θ, the term (θ−1)∑log⁡xi (\theta – 1)\sum\log x_i , negative, drags it down). And it warns of nothing: convergence comes out 0, because it has found (correctly) a minimum.

Trusting an optim that returns exactly a limit (lower or upper). It is almost always a forgotten fnscale or an interval that does not contain the maximum.

Fragment 3. MLE of α of the wind Rayleigh:

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)
sum(wind)^2 / (2 * length(wind))

Output:

[1] 2959.745

(∑xi)2 (\sum x_i)^2 is not ∑xi2 \sum x_i^2 . The bracket is in the wrong place: sum(wind)^2 squares the sum (486.6), giving α = 2959.75 instead of 93.14. A Rayleigh with α ≈ 3000 would have its most frequent wind at 54 km/h.

Check the order of magnitude. α^ \sqrt{\hat\alpha} is the mode of the Rayleigh: it has to be inside the range of the data. 2959.75=54.4 \sqrt{2959.75} = 54.4 km/h is outside.

Practice quiz

Before moving on to the next topic, check you have got it. This quiz is here to consolidate what this article explains: the ME1 exam is not a multiple-choice test (you solve problems by hand and with R), so no mark here counts. There are 10 questions on point estimation: key ideas, typical mistakes and many «what does this R code print?», with the outputs from really running it. In the short answers, a decimal point or a decimal comma both work. Answer without running anything, press Check and read the explanation for each option; you can repeat it as many times as you like.

Question 1Single choice

In the method of moments for a gamma, a classmate uses var(rain) as the sample variance. What is wrong?

  1. var() divides by n − 1 (the quasi-variance S²) and the method of moments uses the central moment S’², which divides by n
  2. Nothing: var() is the sample variance
  3. They should use sd()
  4. The method of moments uses only the mean, not the variance
Solution

Answer: var() divides by n − 1 (the quasi-variance S²) and the method of moments uses the central moment S’², which divides by n

Define s2p <- function(x) mean((x - mean(x))^2) once at the top of the script and always use it for moments and for the MLE of σ². S’² = (n − 1)/n · S².

Question 2Single choice

To check an MLE, the log-likelihood is maximised with optim(par = 5, fn = loglik, method = "Brent", lower = 0.01, upper = 50) and it returns exactly 50. What happened?

  1. optim minimises by default: control = list(fnscale = -1) is missing to make it maximise
  2. The MLE is 50
  3. The Brent method does not work for one parameter
  4. You have to start at par = 50
Solution

Answer: optim minimises by default: control = list(fnscale = -1) is missing to make it maximise

Trusting an optim that returns exactly a limit is the mistake. It is almost always a forgotten fnscale or an interval that does not contain the maximum.

Question 3Single choice

The MLE of α for a Rayleigh is Σx²/(2n). A classmate writes sum(wind)^2 / (2 * length(wind)) and gets 2959.75. What is wrong?

  1. (Σx)² is not Σx²: the bracket is in the wrong place; the right one is sum(wind^2) / (2 * length(wind)), which gives 93.14
  2. It should divide by n − 1
  3. You have to take the root at the end
  4. Nothing: the wind can be that strong
Solution

Answer: (Σx)² is not Σx²: the bracket is in the wrong place; the right one is sum(wind^2) / (2 * length(wind)), which gives 93.14

Check the order of magnitude: √α̂ is the Rayleigh’s mode and must lie inside the data’s range.

Question 4Short answer

What does it print? (It is S’², the central moment, with 2 decimals.)

x <- c(2, 4, 6, 8)
round(mean((x - mean(x))^2), 2)
Solution

Answer: 5

Mean 5; squared deviations 9 + 1 + 1 + 9 = 20; divided by n = 4, S’² = 5. var(x) would give 20/3 = 6.67.

Question 5Order

Put in order the steps of the maximum likelihood estimator (support not depending on θ):

  1. Solve for θ
  2. Differentiate and set to zero: ℓ'(θ) = 0
  3. Check that ℓ”(θ) < 0 (it is a maximum)
  4. Write the likelihood L(θ) = Π f(x_i | θ)
  5. Take logs: ℓ(θ) = log L(θ)
Solution

Correct order:

  1. Write the likelihood L(θ) = Π f(x_i | θ)
  2. Take logs: ℓ(θ) = log L(θ)
  3. Differentiate and set to zero: ℓ'(θ) = 0
  4. Solve for θ
  5. Check that ℓ”(θ) < 0 (it is a maximum)

If the support depends on θ (for example, a uniform on (0, θ)), you do not differentiate: the MLE is an extreme of the sample and you reason about the boundary.

Question 6Single choice

X ~ uniform(0, θ). What is the MLE of θ?

  1. The sample maximum
  2. Twice the mean
  3. The mean
  4. It is obtained by differentiating ℓ(θ) and setting it to 0
Solution

Answer: The sample maximum

It is the cheat sheet’s rule: if the support depends on θ, the MLE is an extreme of the sample.

Question 7Short answer

The MLE of λ in a Poisson is the sample mean. With the data 3, 5, 2, 6, 4, what is the MLE of P(X = 0) = e^(−λ)? (4 decimals.)

x <- c(3, 5, 2, 6, 4)
round(exp(-mean(x)), 4)
Solution

Answer: 0.0183

By the MLE’s invariance: the MLE of τ(λ) is τ(λ̂). λ̂ = 20/5 = 4, so e^(−4) = 0.0183.

Question 8Match

Match each concept with its definition:

Method of moments
Maximum likelihood
MLE invariance
Estimator

Definitions:

  • Choose the θ that makes the observed sample most likely
  • The formula with the X_i
  • Set E(X^k) equal to the sample's k-th moment and solve
  • The MLE of τ(θ) is τ(θ̂)
  • The number obtained with the actual data
Solution
  • Method of moments → Set E(X^k) equal to the sample’s k-th moment and solve
  • Maximum likelihood → Choose the θ that makes the observed sample most likely
  • MLE invariance → The MLE of τ(θ) is τ(θ̂)
  • Estimator → The formula with the X_i

The number obtained with the data is the estimate. If you are asked for both, give the formula with X_i and then the number.

Question 9True or false

True or false?

  1. S’² = (n − 1)/n · S².
  2. In the method of moments with two parameters, the mean and the quasi-variance S² are matched.
  3. In a Rayleigh, E(X²) = 2α.
  4. The sum of n independent Poisson(λ) is Poisson(nλ).
Solution
  • T — S’² = (n − 1)/n · S².
  • F — In the method of moments with two parameters, the mean and the quasi-variance S² are matched.
  • T — In a Rayleigh, E(X²) = 2α.
  • T — The sum of n independent Poisson(λ) is Poisson(nλ).

With two parameters you match the mean and the central moment S’² (divided by n), not var().

Question 10Short answer

Data from a Rayleigh: 2, 4 and 4. What is the MLE of α = Σx²/(2n)?

x <- c(2, 4, 4)
sum(x^2) / (2 * length(x))
Solution

Answer: 6

Σx² = 4 + 16 + 16 = 36; 2n = 6; α̂ = 36/6 = 6. Check: √6 = 2.45 lies inside the data’s range, as the mode should.

Try it in the R playground

Open the R playground on the site (or RStudio) and paste this script, which solves the whole of exercise 2 and draws the lightning likelihood:

strikes <- c(3.61, 7.22, 3.45, 2.87, 8.27, 5.76, 7.71, 8.50,
             3.85, 8.57, 8.80, 7.75, 6.59, 8.79, 5.21)
n <- length(strikes)
L <- function(th) ifelse(th >= max(strikes), 2^n * prod(strikes) / th^(2 * n), 0)
curve(L, 8, 12, n = 1001, xlab = "theta", ylab = "L(theta)")
abline(v = c(max(strikes), 1.5 * mean(strikes)), lty = c(1, 2))
c(ml = max(strikes), moments = 1.5 * mean(strikes))

Things to look at:

  1. In Plots, the likelihood: 0 up to 8.80, a jump and then a drop. The maximum is the jump.
  2. The dashed line, the moments one (9.70), is on the falling part: its likelihood is much lower than the maximum’s.
  3. n = 1001 in curve: with the default 101 points, the jump is drawn as a ramp.
  4. Copy the simulation from part 5 below it and change the 9 in 9 * sqrt(runif(15)) to another range: does the percentage of impossible estimators change?

Cheat sheet: point estimation

The essentials in 30 seconds. Moments: E(Xk)=mk E(X^k) = m_k and solve (with two parameters, mean and S′2 S'^2 ). Maximum likelihood: L=∏f(xi∣θ) L = \prod f(x_i \mid \theta) , ℓ=log⁡L \ell = \log L , ℓ′=0 \ell' = 0 , ℓ′′<0 \ell'' < 0 . If the support depends on θ, the MLE is an extreme of the sample. Invariance: MLE of τ(θ) \tau(\theta) = τ(θ^) \tau(\hat\theta) .

DistributionMomentsMaximum likelihood
Bernoulli(p)Xˉ \bar X Xˉ \bar X
binom(N, p), N knownXˉ/N \bar X / N Xˉ/N \bar X / N
geom(p), failures before success1/(1+Xˉ) 1/(1+\bar X) 1/(1+Xˉ) 1/(1+\bar X)
Poisson(λ)Xˉ \bar X Xˉ \bar X
exp(λ)1/Xˉ 1/\bar X 1/Xˉ 1/\bar X
normal(μ, σ)Xˉ \bar X , S′2 S'^2 Xˉ \bar X , S′2 S'^2 ; with μ₀: 1n∑(Xi−μ0)2 \frac1n\sum(X_i-\mu_0)^2
gamma(α, β)Xˉ2/S′2 \bar X^2/S'^2 , Xˉ/S′2 \bar X/S'^2 optim or fitdist; α^/β^=Xˉ \hat\alpha/\hat\beta = \bar X
uniform(0, θ)2Xˉ 2\bar X max⁡Xi \max X_i
uniform(a, b)Xˉ∓3 S′ \bar X \mp \sqrt3\,S' min⁡Xi \min X_i , max⁡Xi \max X_i
Rayleigh(α)2Xˉ2/π 2\bar X^2/\pi ∑Xi2/(2n) \sum X_i^2/(2n)
θxθ−1 \theta x^{\theta-1} on (0, 1)Xˉ/(1−Xˉ) \bar X/(1-\bar X) −n/∑log⁡Xi -n/\sum\log X_i
2x/θ2 2x/\theta^2 on (0, θ)3Xˉ/2 3\bar X/2 max⁡Xi \max X_i

Useful results. S′2=n−1nS2 S'^2 = \frac{n-1}{n}S^2 . Rayleigh: E(X)=απ/2 E(X) = \sqrt{\alpha\pi/2} , E(X2)=2α E(X^2) = 2\alpha , X2∼exp(1/(2α)) X^2 \sim \text{exp}(1/(2\alpha)) , α^∼gamma(n,n/α) \hat\alpha \sim \text{gamma}(n, n/\alpha) . Poisson: ∑Xi∼Pois(nλ) \sum X_i \sim \text{Pois}(n\lambda) .

I want…CodeResult
S′2 S'^2 mean((x - mean(x))^2)Variance over n
Maximise one parameteroptimize(f, c(a, b), maximum = TRUE)$maximum, $objective
Maximise severaloptim(start, f, x = data, control = list(fnscale = -1))$par, $value, $convergence
One parameter with optimmethod = "Brent", lower = a, upper = bNo Nelder-Mead warning
Positive parametersmethod = "L-BFGS-B", lower = c(0.01, 0.01)Never tries impossible values
MLE with standard errorsfitdist(x, "gamma") (package fitdistrplus)estimate, Std. Error, loglik
Moments with fitdistfitdist(x, "gamma", method = "mme")The moments ones
Draw a log-likelihoodcurve(Vectorize(l)(x), a, b)The hill
SymptomLikely cause
optim returns exactly lower or upperfnscale = -1 missing or the interval does not contain the maximum
The likelihood is 0 for every θWritten with prod() and lots of data: use sum(log())
curve draws a straight line or gives a length errorThe function does sum(): Vectorize
Moments off by 5-10 %var() instead of S′2 S'^2
Moments estimator below the largest data pointHappens in the uniform and similar: use the MLE
dgamma with NaN and warnings in optimNegative parameters: L-BFGS-B with lower

Before you hand in, ask yourself:

  1. Have I written the estimator with Xi X_i (formula) and the estimate with the data (number), if both are asked for?
  2. Have I checked the second derivative or, if the support depends on θ, reasoned about the edge?
  3. Have I used S′2 S'^2 (over n) where it belongs and not var()?
  4. Does my optim have fnscale = -1 and is convergence 0?
ME1 exam coming up?The script for every topic, the printable cheat sheet and the pack for each exam, for free.See the pack

For your script

The topic’s code for your exam .R file, with functions tested in this article’s exercises. No statements: only what gets reused.

# ===== Point estimation (ME1, topic 9) =====
s2p <- function(x) mean((x - mean(x))^2)               # S'^2 (divides by n)

# --- Moments: closed formulas
mme_poisson  <- function(x) mean(x)
mme_exp      <- function(x) 1 / mean(x)
mme_gamma    <- function(x) c(shape = mean(x)^2 / s2p(x), rate = mean(x) / s2p(x))
mme_unif_ab  <- function(x) c(a = mean(x) - sqrt(3 * s2p(x)), b = mean(x) + sqrt(3 * s2p(x)))
mme_rayleigh <- function(x) 2 * mean(x)^2 / pi

# --- Maximum likelihood: closed formulas
mle_bernoulli <- function(x) mean(x)
mle_poisson   <- function(x) mean(x)
mle_exp       <- function(x) 1 / mean(x)
mle_normal    <- function(x) c(mu = mean(x), sigma2 = s2p(x))
mle_unif      <- function(x) max(x)                     # uniform(0, theta)
mle_rayleigh  <- function(x) sum(x^2) / (2 * length(x))
drayleigh <- function(x, alpha) x / alpha * exp(-x^2 / (2 * alpha))

# --- Numerical maximum likelihood
# One parameter: the log-likelihood as a function of theta (data inside)
mle_1par <- function(loglik, a, b) optimize(loglik, c(a, b), maximum = TRUE)$maximum
# Several parameters: loglik(par, x) with par a vector; start = moments estimator
mle_optim <- function(loglik, start, x, low = rep(0.01, length(start))) {
  o <- optim(start, loglik, x = x, method = "L-BFGS-B", lower = low,
             control = list(fnscale = -1))
  if (o$convergence != 0) warning("optim has not converged")
  o
}
loglik_gamma <- function(par, x) sum(dgamma(x, shape = par[1], rate = par[2], log = TRUE))
loglik_norm  <- function(par, x) sum(dnorm(x, mean = par[1], sd = par[2], log = TRUE))
# Example: mle_optim(loglik_gamma, mme_gamma(rain), rain)$par

# --- With fitdistrplus (installed before the exam)
# library(fitdistrplus)
# fitdist(x, "gamma"); fitdist(x, "norm"); fitdist(x, "exp")
# fitdist(x, "gamma", method = "mme")     # moments

# --- Draw a one-parameter log-likelihood
draw_loglik <- function(loglik, a, b, mark = NULL) {
  curve(Vectorize(loglik)(x), a, b, xlab = "parameter", ylab = "log-likelihood")
  if (!is.null(mark)) abline(v = mark, lty = 2)
}

# --- Invariance: plug the MLE into the formula, for example
# P(X > c) in the Rayleigh: exp(-c^2 / (2 * mle_rayleigh(x)))
# P(X < c) in the normal:   pnorm(c, mean(x), sqrt(s2p(x)))

What comes next?

With this topic you can get an estimator in two ways. But exercise 2 left a question hanging: the maximum likelihood one fell short on average (8.71 against 9) and the moments one was sometimes impossible. Which is better? The next ME1 topic, properties of estimators, gives the criteria: unbiasedness (right on average), efficiency (the smallest possible variance, with the Cramér-Rao bound) and consistency (getting closer to the parameter as n grows). What you computed here, the distribution of the Rayleigh MLE with its mean and variance, is exactly what you will need.

If you want to review first, you have the theory, the proofs and the practice of this topic, and sampling distributions, where Xˉ \bar X and S2 S^2 come from.

Keep goingAll Statistics articles

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

Ready for the exam?ME1 EC2 mock exam — 4 hours with R beside you, from the normal to confidence intervals, four versions and a mark out of 50 (and why exercise 1 is worth double)