Lecture 12 Sampling Distributions and Standard Errors

This is the R session for week 4. We will watch the sampling distribution of a maximum likelihood estimator converge to a normal distribution, we will use the Fisher information from this week’s lectures to compute a standard error and a confidence interval, and we will learn how to estimate a probability using simulation. (The latter skill will be needed for the coverage checks in the practical report, and for the size and power calculations in lecture 24.)

12.1 The Sampling Distribution of an Estimator

The model of the day is X1,…,XnX_{1},\dots,X_{n} i.i.d. from the exponential distribution with rate θ\theta, where the maximum likelihood estimator is θ^=1/X¯\hat{\theta}=1/\bar{X} by example 5.2. The command rexp can be used to simulate such a sample, and as in lecture 6 we package one run as a function. Before you run the code below, predict the two histograms: for n=10n=10, will the estimates form a symmetric bell around the true value θ=1\theta=1? Will the peak sit above, below or at 11? How will the picture change for n=100n=100?

est_rate <- function(n, theta) {
sample <- rexp(n, rate = theta)
1 / mean(sample)
}
theta <- 1
set.seed(2026)
small <- replicate(10000, est_rate(10, theta))
large <- replicate(10000, est_rate(100, theta))

As in lecture 9, we require the histograms to be generated with freq = FALSE and we overlay the density by using curve. The density we use is the large-sample approximation from lecture 17, which is normal with mean θ\theta and standard deviation θ/n\theta/\sqrt{n}. The command qqnorm can be used to plot the sorted estimates against the quantiles of a standard normal distribution, and qqline can be used to add the line for the quantiles of a normal distribution.

par(mfrow = c(2, 2))
hist(small, breaks = 40, freq = FALSE, col = "grey85", main = "n = 10",
xlab = "estimate")
curve(dnorm(x, mean = theta, sd = theta / sqrt(10)),
add = TRUE, lwd = 2)
qqnorm(small, main = "n = 10")
qqline(small)
hist(large, breaks = 40, freq = FALSE, col = "grey85", main = "n = 100",
xlab = "estimate")
curve(dnorm(x, mean = theta, sd = theta / sqrt(100)),
add = TRUE, lwd = 2)
qqnorm(large, main = "n = 100")
qqline(large)
Four panels. Top: for n = 10 the histogram is
right-skewed, with a peak below 1, and the normal curve is a poor
fit. The quantile plot shows the quantiles bending upwards from
the reference line. Bottom: for n = 100 the histogram is
bell-shaped and the curve is a good fit. The quantile plot shows
that the quantiles are close to the line, except for the far
right.
Figure 12.1: The sampling distribution of θ^=1/X¯\hat{\theta}=1/\bar{X} for the exponential model with θ=1\theta=1: histograms with the normal approximation overlaid (left column) and normal quantile plots (right column), for 1000010000 samples of size n=10n=10 (top row) and n=100n=100 (bottom row).

Figure 12.1 shows that for n=10n=10 the estimates are not normally distributed: the histogram has a long right tail, the peak is to the left of 11 and the quantile plot shows quantiles diverging from the line. Since 1/X¯1/\bar{X} is not an average, the central limit theorem (theorem A.15) does not apply directly. For n=100n=100 the distortion is gone: this is asymptotic normality of the maximum likelihood estimator, proved in lecture 17.

12.2 Standard Errors and a Wald Interval

The standard deviation θ/n\theta/\sqrt{n} of the normal curves in figure 12.1 is given by the Fisher information from lecture 11: By example 11.8 and definition 11.2 the sample contains information ℐ︀n⁢(θ)=n/θ2\mathcal{I}_{n}(\theta)=n/\theta^{2} and lecture 17 will show that the spread of the estimator is approximately 1/ℐ︀n⁢(θ)=θ/n1/\sqrt{\mathcal{I}_{n}(\theta)}=\theta/\sqrt{n}. Normally, the value of θ\theta is unknown and we have to evaluate the information at the estimate instead. The standard error of the estimate is given by

se(θ^)=1ℐ︀n⁢(θ^)=θ^n.\mathop{\mathrm{se}}\nolimits(\hat{\theta})=\frac{1}{\sqrt{\mathcal{I}_{n}(% \hat{\theta})}}=\frac{\hat{\theta}}{\sqrt{n}}.

This is a measure of the likely size of the estimation error, computed from the data. Since the estimator is approximately normally distributed with this spread, the Wald interval θ^±1.96⁢se(θ^)\hat{\theta}\pm 1.96\,\mathop{\mathrm{se}}\nolimits(\hat{\theta}) covers the true value with probability approximately 95%95\%. This is a confidence interval of the kind we have seen in MATH2701, and is used informally here and more formally in lecture 25. The theorem which describes the resulting recipe is corollary 17.3. Before you try the code, make a prediction: will the interval cover the true value 11?

n <- 30
set.seed(2026)
x <- rexp(n, rate = theta)
theta_hat <- 1 / mean(x)
se <- theta_hat / sqrt(n)
theta_hat
se
theta_hat + c(-1, 1) * 1.96 * se
## [1] 0.6337667
## [1] 0.1157094
## [1] 0.4069762 0.8605572

The interval [0.407,0.861][0.407,0.861] does not contain the true value 11. This is not a programming error: the sample happened to contain unusually large observations, and a 95%95\% interval misses the truth in about one sample out of twenty. From a single sample it is not possible to know whether this is one of the misses; how often the recipe is wrong in the long run can be answered by simulation.

12.3 Estimating a Probability by Simulation

The probability of an event AA is the expected value of the indicator of AA, and by the law of large numbers (theorem A.14) this expectation can be approximated by the average of the indicator over many independent repetitions. In R, the mean of the logical vector abs(large - theta) <= 0.2 can be used to estimate ℙ⁢(|θ^−θ|≤0.2)\mathbb{P}(|\hat{\theta}-\theta|\leq 0.2), since mean interprets TRUE as 11 and FALSE as 0. The same principle applies to all probabilities we estimate by simulation: for estimating the coverage probability of an interval, we take the average of the indicator that the interval contains the truth. Similarly, the rejection rate of a test (see lecture 24) is the average of the indicator that the test rejects.

We apply this to the coverage of the Wald interval: the box below contains a complete script, with its lines shuffled. Before you continue, arrange the lines on paper into a working script which estimates, from 1000010000 samples of size n=10n=10, the probability that the Wald interval contains θ\theta.

mean(ci[1, ] <= theta & theta <= ci[2, ]) # empirical coverage
theta <- 1
wald <- function(n, theta) {
x <- rexp(n, rate = theta)
theta_hat <- 1 / mean(x)
se <- theta_hat / sqrt(n)
theta_hat + c(-1, 1) * 1.96 * se
}
ci <- replicate(10000, wald(10, theta))
set.seed(2026)

Names must be defined before they are used and the seed set before the first random draw (lecture 6): the function and theta come first, then the seed, then replicate, then the summary. One detail is new: the function returns the two endpoints, which replicate collects as the columns of a two-row matrix, so that ci[1, ] and ci[2, ] hold the lower and upper endpoints and & combines two logical vectors elementwise. Below, the reconstructed script is extended by a loop over n=10n=10, 3030 and 100100. Before running it, predict the coverage at n=10n=10: given the skewness in figure 12.1, above or below 0.950.95?

wald <- function(n, theta) {
x <- rexp(n, rate = theta)
theta_hat <- 1 / mean(x)
se <- theta_hat / sqrt(n)
theta_hat + c(-1, 1) * 1.96 * se
}
theta <- 1
ns <- c(10, 30, 100)
coverage <- numeric(3)
set.seed(2026)
for (i in seq_along(ns)) {
ci <- replicate(10000, wald(ns[i], theta))
coverage[i] <- mean(ci[1, ] <= theta & theta <= ci[2, ])
}
data.frame(n = ns, coverage = coverage)
## n coverage
## 1 10 0.9535
## 2 30 0.9539
## 3 100 0.9504

The coverage is close to the nominal 0.950.95 for all three sample sizes, even for n=10n=10 where the sampling distribution was far from normal. The total hides an imbalance: the standard error θ^/n\hat{\theta}/\sqrt{n} is proportional to the estimate, so small estimates get short intervals and at n=10n=10 most misses are below the truth (check this yourself!).

12.4 Skewed Data

Tables A.1 and A.2 list three distributions which the lectures do not otherwise use, the negative binomial, the Pareto and the Rayleigh distributions. The command rnbinom can be used to draw from the negative binomial distribution, counting failures before the size-th success as in the table. The other two distributions cannot be directly generated in base R. Instead, if UU is uniformly distributed on the interval (0,1)(0,1) and FF is a continuous, strictly increasing distribution function, then F−1⁢(U)F^{-1}(U) has distribution function FF. For the Pareto distribution with α=3\alpha=3, this gives X=(1−U)−1/3X=(1-U)^{-1/3}. For the Rayleigh distribution with θ=2\theta=2, the square X2X^{2} is exponentially distributed with mean θ\theta, i.e. X=2⁢EX=\sqrt{2E} where EE is standard exponentially distributed. Before you try the code, try to sketch the three histograms you would expect: which distribution is discrete? Which distribution starts at 11? Which distribution has the longest tail? Finally, once you have done this, look up the means and variances in the table.

N <- 10000
set.seed(2026)
nb <- rnbinom(N, size = 3, prob = 0.4)
pa <- (1 - runif(N))^(-1 / 3)
ra <- sqrt(2 * rexp(N))
c(mean(nb), var(nb))
c(mean(pa), var(pa))
c(mean(ra), var(ra))
## [1] 4.5308 11.5188
## [1] 1.4993207 0.6484568
## [1] 1.2450801 0.4328587

The empirical means and variances match the values from the table, except for the Pareto distribution where the variance 0.6480.648 is much smaller than the table value 0.750.75: This discrepancy is caused by the fact that the 1000010000 samples have not yet observed the large, rare events which contribute significantly to the variance of this heavy-tailed distribution. The script R/S12-sheet.R continues by computing 1000010000 sample means of n=30n=30 draws from each distribution and by superimposing the normal density from the central limit theorem over the histograms. Which of the three histograms of means do you expect will be furthest away from the normal curve?

Six panels in two rows. Top row, histograms of 10000
draws: a right-skewed bar chart of counts peaking at 2 and 3; a
histogram starting at 1 with a steep decay; a right-skewed hump
peaking near 1. Bottom row, histograms of sample means of 30 draws
with normal curves overlaid: the first and third are bell-shaped
histograms, well-fitted by the curves; the middle histogram peaks to
the left of its curve and has a long right tail.
Figure 12.2: Top: histograms of 1000010000 draws from the negative binomial distribution with r=3r=3 and p=0.4p=0.4, the Pareto distribution with α=3\alpha=3 and the Rayleigh distribution with θ=2\theta=2. Bottom: histograms of 1000010000 sample means of n=30n=30 draws, with the normal density from the central limit theorem overlaid; the two middle panels are cut off on the right.

In figure 12.2 thirty observations are enough for the central limit theorem to work well in the first and third columns, but not in the middle column where the heavy tail causes the histogram of means to be still visibly skewed. The heavier the tail, the more nn is required and the same care is needed when using the Wald intervals above.

As a task to try at home, in the style of a short practical report, choose a rate θ\theta, let n=50n=50, set the random number generator seed to your student ID, and simulate one sample from an exponential distribution. Determine the estimate θ^=1/x¯\hat{\theta}=1/\bar{x}, the standard error, and the Wald interval for this sample. Write down your results in one sentence each. Finally, using 1000010000 samples, estimate the coverage of the interval at n=50n=50. Also, in separate analyses, determine how often the interval is entirely below the truth and how often it is entirely above the truth. In two sentences, discuss whether you would trust the interval for this sample size.

Before the next session, type the commands used in this session into R, and check that your answers match the text.

Summary.
  • •
    ​

    The written reference for the commands used in this session is appendix B.

  • •
    ​

    The normal approximation to the sampling distribution of θ^=1/X¯\hat{\theta}=1/\bar{X} is poor for n=10n=10 and good for n=100n=100. The standard error se(θ^)=1/ℐ︀n⁢(θ^)\mathop{\mathrm{se}}\nolimits(\hat{\theta})=1/\sqrt{\mathcal{I}_{n}(\hat{% \theta})} and the Wald interval θ^±1.96⁢se(θ^)\hat{\theta}\pm 1.96\,\mathop{\mathrm{se}}\nolimits(\hat{\theta}) can be computed from data.

  • •
    ​

    A probability can be estimated by simulation as the mean of a logical vector, i.e. by taking the average of an indicator function over replicates. This method can be used to compute both coverage probabilities and rejection rates.

  • •
    ​

    The negative binomial, Pareto and Rayleigh distributions can be simulated using rnbinom, the inverse distribution function and the square root of an exponential variable, respectively. The heavier the tail of a distribution, the longer the central limit theorem takes to apply.