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 extend the simulation idea of lecture 9 from estimating a probability to estimating the coverage of an interval. (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, the histograms are drawn with freq = FALSE, so that a density can be drawn on top of them. The command curve can be used to plot any function of x on top of the existing plot, if the argument add = TRUE is used; we use it to overlay the density. The density we overlay is a normal approximation which we can derive from appendix A: the observations have mean and variance , and thus the central limit theorem (theorem A.15) gives . Applying the delta method (proposition A.18) with and we find
so that for large the estimator is approximately 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 straight line which the points would follow if the estimates were exactly normally distributed. Points above the line at the right-hand end indicate a right tail which is heavier than that of the normal distribution, and points above the line at the left-hand end indicate a left tail which is lighter.
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. This is no contradiction: the central limit theorem and the delta method only promise the normal shape for large . For the distortion is gone.
12.2 Standard Errors and a Wald Interval
The standard deviation of the normal curves in figure 12.1 can also be read off the Fisher information from lecture 11: by example 11.8 and proposition 11.4 the sample contains information , and thus . This is not a coincidence. Lecture 17 will show that every maximum likelihood estimator is, under conditions, approximately normal with spread for large , and that may be replaced by the estimate inside the information. We use these two facts on trust in this session; the delta method above has verified the first of them for the exponential rate. Since the value of is normally unknown, 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. 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: this seed happens to give one of the misses, and in the long run 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
Recall from lecture 9 that a probability is the expected value of an indicator, and thus by the law of large numbers (proposition A.14) it can be estimated by the mean of a logical vector over many independent repetitions. The same principle applies to all probabilities we estimate by simulation: for the coverage probability of an interval, the indicator is that the interval contains the truth, and for the rejection rate of a test (see lecture 24) it is 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, except that the lines of the function are kept together. 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 . Several orders satisfy the rules below.
Names must be defined before they are used and the seed set before the first random draw (lecture 6): the function, theta and the seed come first, in any order, 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 (see appendix B). Below, the reconstructed script is extended by a loop over , and . Before running it, make a prediction about the misses at : the standard error is proportional to the estimate, so will an interval which misses the truth more often lie entirely below or entirely above ?
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. Since repetitions estimate a probability near with a standard error of about , the differences between the three values are within simulation error. The total hides an imbalance: small estimates get short intervals, and at most misses are intervals whose upper endpoint lies below the truth. Check this yourself, using the logical vectors ci[2, ] < theta and ci[1, ] > theta in place of the coverage indicator.
12.4 A Task to Take Home
The following task is 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. Then, using samples, estimate the coverage of the interval at . Also determine how often the upper endpoint of the interval lies below and how often the lower endpoint lies above , and report the two proportions separately. 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. For the Wald interval of the exponential rate, the coverage is close to already for , but most misses are intervals which lie below the truth, since small estimates come with short intervals.