Lecture 27 Coverage and Level by Simulation
This is the R session for week 9 of the course. In lectures this week we have seen Wilks’ theorem and confidence intervals. Both come with a stated probability: a confidence interval covers the true parameter value with probability , and the likelihood-ratio test with a critical value from the chi-squared distribution has level , approximately. In this session we check both statements by simulation. The written reference for the R commands is in appendix B.
27.1 Coverage by Simulation
We first use the -interval from example 26.3, with the numerical values and (known) from that example, so that the interval is . By definition 26.1 of the level , the random interval contains the fixed value in of all samples. To verify this, we consider and compute the interval for samples. Before you execute the code, answer the following questions: (1) How many of the intervals will not cover the value ? (2) Will all intervals have the same width? (3) Will the misses be clustered or evenly distributed among the 100 samples?
As in lecture 12, covers is a vector of TRUE and FALSE, one for each interval, and the command sum can be used to count the TRUE values. In our example, out of intervals cover the truth, and the misses are for samples , , , and . The exact number is a coincidence of the seed: the number of intervals which cover the truth is binomially distributed with standard deviation , so or would have been just as unremarkable.
Figure 27.1 shows each interval as a horizontal line, with the intervals which cover the truth in grey and the misses in black. The script R/S27-coverage.R shows how the command segments can be used to create this plot.

The intervals in the figure all have the same width, since is known: the intervals move, while the dashed line stays fixed. The misses are scattered throughout the figure, since the samples are independent. If we had only observed sample , the interval would have looked like any other, and there would have been no indication in the data that this was a miss. For this reason, the statement “ is in the interval with probability ” is wrong: the value is either in the interval or it is not, and in this instance it is not. The probability refers to the procedure, not to the computed interval. In the script R/S27-coverage.R the experiment is repeated with intervals, giving the coverage , which is within simulation error of . The interval is exact, and the simulation confirms this.
27.2 Wilks’ Theorem in Action
We now turn from an exact statement to an approximate one. Assume that the variance of the normal sample is not known, so that the model has the two parameters and , and consider the likelihood-ratio test of , where leaves free. By example 5.4, the log-likelihood is maximised over all parameters at , and the same computation with in place of gives the maximum under . In both cases the maximised log-likelihood equals , with in place of under , and thus by (25.1) we find
By Wilks’ theorem (theorem 25.3), with as in lecture 25, the distribution of under converges to as . In the case of known variance from lecture 25 the chi-squared distribution was exact, but here it is only an approximation, and we need to check how good it is for the we have. We simulate under for and , using and the value from the first section, and we reject if exceeds the critical value of , as if the theorem were exact. Before you run the code, predict the two rejection rates. Will the rate for be above or below ?
theta0 <- 10wilks <- function(n) { x <- rnorm(n, mean = theta0, sd = sigma) n * log(sum((x - theta0)^2) / sum((x - mean(x))^2))}set.seed(2026)w10 <- replicate(10000, wilks(10))w100 <- replicate(10000, wilks(100))crit_w <- qchisq(0.95, df = 1)c(n10 = mean(w10 > crit_w), n100 = mean(w100 > crit_w))The rejection rate for is , which is within simulation error of the nominal level. The rejection rate for is : the test with nominal level has type I errors half again as often as it should. Figure 27.2 shows why. In the quantile plots of the bottom row, made in the same way as the normal quantile plots of lecture 12, the points for rise above the line from about onwards, and thus the upper tail of is heavier than that of the limit.

The script R/S27-wilks.R also computes the -quantile of the simulated values of , which is the threshold that does give size : for the threshold needs to be about instead of , while for the simulated quantile is close to the chi-squared value. The error carries over to intervals: by lemma 26.6, the confidence interval obtained by inverting the test with critical value covers the truth in only about of all samples of size . For this model the exact distribution is known: exercise 25.1 uses the distribution of appendix A to compare the two tests, and lecture 31 builds the exact test. In general, an asymptotic level is only guaranteed in the limit. An exact test should be used if one is available, and if not, a simulation under can be used to determine the actual size at a given .
27.3 A Task to Take Home
Repeat the picture in figure 27.1 for the -interval from example 26.4, with , sigma replaced by sd(x) and qnorm(0.975) replaced by qt(0.975, df = 4). Estimate the long-run coverage from samples. Then repeat the experiment with qnorm(0.975) in place of the quantile, and comment on your result.
Before the next session, you should type the commands used in this session, and should check that you get the same results as the text.
-
•
The written reference for the commands used in this session is appendix B.
-
•
The confidence level is a property of the procedure: of simulated intervals, contained the truth, and the misses looked like the other intervals. A computed confidence interval either contains the parameter or it does not.
-
•
For the normal mean with unknown variance, the likelihood-ratio statistic is only approximately chi-squared: for the chi-squared critical value gives size instead of , and the simulated -quantile is about . For the approximation is good.
-
•
If an exact test or interval is available, this should be used. If not, simulation can be used to determine the actual size or coverage at the given .