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 95%95\% confidence interval covers the true parameter value with probability 0.950.95, and the likelihood-ratio test with a critical value from the chi-squared distribution has size close to 0.050.05 for large nn. 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 zz-interval from example 26.3, with the numerical values n=16n=16 and σ=2\sigma=2 (known) from that example, so that the 95%95\% interval is X¯±1.96⋅0.5\bar{X}\pm 1.96\cdot 0.5. Since QQ in that example is a pivot, the random interval contains the fixed value θ\theta with probability exactly 0.950.95, whatever the value of θ\theta. To see what this means, we take θ=10\theta=10 and compute the interval for 100100 samples. Before you execute the code, answer the following questions: (1) How many of the 100100 intervals will not cover the value 1010? (2) Will the misses come in runs, or be scattered at random among the 100100 samples?

theta <- 10
sigma <- 2
n <- 16
z_interval <- function() {
x <- rnorm(n, mean = theta, sd = sigma)
mean(x) + c(-1, 1) * qnorm(0.975) * sigma / sqrt(n)
}
set.seed(2026)
ci <- replicate(100, z_interval())
covers <- ci[1, ] <= theta & theta <= ci[2, ]
sum(covers)
which(!covers)
## [1] 95
## [1] 1 29 34 63 83

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, 9595 out of 100100 intervals cover the truth, and the misses are for samples 11, 2929, 3434, 6363 and 8383. The exact number 9595 is a coincidence of the seed: the number of intervals which cover the truth is binomially distributed with standard deviation 2.22.2, so 9292 or 9898 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.

One hundred horizontal segments, each of equal
length, are shown, stacked on top of each other. The samples
are ordered so that sample 1 is at the bottom and sample 100 is
at the top. A dashed vertical line marks the location of
theta = 10. Ninety-five of the grey segments intersect the line,
while the five black segments, corresponding to samples 1,
29, 34, 63 and 83, are completely on one side of the line.
Figure 27.1: One hundred 95%95\% confidence intervals for the mean of a N⁢(θ,4)N(\theta,4) sample of size n=16n=16, with true value θ=10\theta=10 (dashed line). The grey intervals cover the truth, the black intervals do not.

The intervals in the figure all have the same width, since σ\sigma is known: the intervals move, while the dashed line stays fixed. The misses are scattered at random through the figure, since the samples are independent.

Sample 2929 gives the interval [10.43,12.39][10.43,12.39]. Before reading on, decide whether the following statement is correct: “θ\theta is in the interval [10.43,12.39][10.43,12.39] with probability 0.950.95”.

The statement is wrong, since the parameter θ\theta is a fixed number: the interval computed from sample 2929 either contains θ\theta or it does not, and here it does not. Nothing in sample 2929 shows this, since its interval looks like any other. The probability refers to the procedure, not to the computed interval. In the script R/S27-coverage.R, 1000010000 intervals give the coverage 0.9470.947, which is within simulation error of 0.950.95.

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 θ\theta and σ2\sigma^{2}, and consider the likelihood-ratio test of H0:θ=θ0H_{0}\colon\theta=\theta_{0}, where H0H_{0} leaves σ2\sigma^{2} free. By exercise 25.1, the likelihood-ratio statistic is

W=n⁢log⁡∑i=1n(Xi−θ0)2∑i=1n(Xi−X¯)2.W=n\log\frac{\sum_{i=1}^{n}(X_{i}-\theta_{0})^{2}}{\sum_{i=1}^{n}(X_{i}-\bar{X% })^{2}}.

Since H0H_{0} fixes θ\theta and leaves the nuisance parameter σ2\sigma^{2} free, we have r=1r=1, and by Wilks’ theorem (theorem 25.3) the distribution of WW under H0H_{0} converges to χ12\chi^{2}_{1} as n→∞n\to\infty. Under H0H_{0} we can write Xi−θ0=σ⁢ZiX_{i}-\theta_{0}=\sigma Z_{i} with i.i.d. standard normal ZiZ_{i}, and the factor σ2\sigma^{2} cancels in the ratio. Thus the distribution of WW under H0H_{0} is the same for every σ\sigma, and the rejection rate for one value of σ\sigma is the size of the test. We simulate WW under H0H_{0} for n=10n=10 and n=100n=100, using θ0=10\theta_{0}=10 and the value σ=2\sigma=2 from the first section, and we reject if WW is at least the critical value 3.8413.841 of χ12\chi^{2}_{1}, as if the theorem were exact. Before you run the code, predict the rejection rate for n=10n=10: will it be above or below 0.050.05, and by roughly how much?

theta0 <- 10
wilks <- 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))
## n10 n100
## 0.0745 0.0516

The rejection rate for n=100n=100 is 0.0520.052, which is within simulation error of the nominal level. The rejection rate for n=10n=10 is 0.0750.075, with a simulation standard error of about 0.0030.003, and exercise 25.1 leads to the exact size 0.0700.070: the test with nominal level 0.050.05 has type I errors about 40%40\% more often than it should. Figure 27.2 shows the same effect in the distribution of WW. In its quantile plots, made in the same way as the normal quantile plots of lecture 12, the points for n=10n=10 lie above the line throughout, with a gap which grows in the upper tail. Thus large values of WW are more likely than the limit predicts.

Two panels, for n=10 and n=100, each showing the
sorted simulated values of the test statistic W against quantiles
of the chi-squared distribution with one degree of freedom, with
the identity line. Dotted lines mark the critical value 3.84 on
both axes. For n=10 the points lie above the line throughout,
with a gap which grows in the upper tail; for n=100 the points
follow the line throughout.
Figure 27.2: The likelihood-ratio statistic WW for a normal mean with unknown variance, simulated 1000010000 times under H0H_{0} for n=10n=10 (left) and n=100n=100 (right): the sorted simulated values against the quantiles of the chi-squared distribution with one degree of freedom, with the identity line. The dotted lines mark the critical value 3.843.84.

The error carries over to intervals: by the proof of lemma 26.6, the confidence interval obtained by inverting the test with critical value 3.843.84 covers the truth with probability one minus the size of the test, which is about 93%93\% for samples of size 1010, whatever the values of θ\theta and σ\sigma. For this model an exact test exists, and lecture 31 builds it. In general, the size given by Wilks’ theorem is reached only in the limit. An exact test should be used if one is available, and if not, a simulation under H0H_{0} can be used to determine the actual size at a given nn.

27.3 A Task to Take Home

Repeat the picture in figure 27.1 for the tt-interval from example 26.4, with n=5n=5. In the last line of z_interval, replace sigma by sd(x) and qnorm(0.975) by qt(0.975, df = 4), and note that the intervals now differ in width. Estimate the long-run coverage from 1000010000 samples. Then repeat the experiment with qnorm(0.975) in place of the tt 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.

Summary.
  • •
    ​

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

  • •
    ​

    The confidence level is a property of the procedure: in repeated samples, about 95%95\% of the 95%95\% intervals contain the truth, and the misses look 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 n=10n=10 the chi-squared critical value 3.843.84 gives size about 0.070.07 instead of 0.050.05, and the inverted interval has coverage about 93%93\%. For n=100n=100 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 nn.