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 i.i.d. from the exponential distribution with rate , where the maximum likelihood estimator is 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 , will the estimates form a symmetric bell around the true value ? Will the peak sit above, below or at ? How will the picture change for ?
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 and standard deviation . 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)
Figure 12.1 shows that for the estimates are not normally distributed: the histogram has a long right tail, the peak is to the left of and the quantile plot shows quantiles diverging from the line. Since is not an average, the central limit theorem (theorem A.15) does not apply directly. For 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 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 and lecture 17 will show that the spread of the estimator is approximately . Normally, the value of is unknown and we have to evaluate the information at the estimate instead. The standard error of the estimate is given by
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 covers the true value with probability approximately . 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 ?
The interval does not contain the true value . This is not a programming error: the sample happened to contain unusually large observations, and a 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 is the expected value of the indicator of , 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 , since mean interprets TRUE as and FALSE as . 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 samples of size , the probability that the Wald interval contains .
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 , and . Before running it, predict the coverage at : given the skewness in figure 12.1, above or below ?
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 <- 1ns <- 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)The coverage is close to the nominal for all three sample sizes, even for where the sampling distribution was far from normal. The total hides an imbalance: the standard error is proportional to the estimate, so small estimates get short intervals and at 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 is uniformly distributed on the interval and is a continuous, strictly increasing distribution function, then has distribution function . For the Pareto distribution with , this gives . For the Rayleigh distribution with , the square is exponentially distributed with mean , i.e. where 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 ? Which distribution has the longest tail? Finally, once you have done this, look up the means and variances in the table.
The empirical means and variances match the values from the table, except for the Pareto distribution where the variance is much smaller than the table value : This discrepancy is caused by the fact that the 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 sample means of 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?

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 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 , let , set the random number generator seed to your student ID, and simulate one sample from an exponential distribution. Determine the estimate , the standard error, and the Wald interval for this sample. Write down your results in one sentence each. Finally, using samples, estimate the coverage of the interval at . 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.
-
•
The written reference for the commands used in this session is appendix B.
-
•
The normal approximation to the sampling distribution of is poor for and good for . The standard error and the Wald interval 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.