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 level 0.050.05, 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 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. By definition 26.1 of the level 0.950.95, the random interval contains the fixed value θ\theta in 95%95\% of all samples. To verify this, we consider θ=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 all intervals have the same width? (3) Will the misses be clustered or evenly distributed among the 100 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 five of the 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 throughout the figure, since the samples are independent. If we had only observed sample 2929, the interval [10.43,12.39][10.43,12.39] 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 “θ\theta is in the interval [10.43,12.39][10.43,12.39] with probability 0.950.95” is wrong: the value 1010 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 1000010000 intervals, giving the coverage 0.9470.947, which is within simulation error of 0.950.95. 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 θ\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 example 5.4, the log-likelihood is maximised over all parameters at σ^2=1n⁢∑i(Xi−X¯)2\hat{\sigma}^{2}=\frac{1}{n}\sum_{i}(X_{i}-\bar{X})^{2}, and the same computation with θ0\theta_{0} in place of X¯\bar{X} gives the maximum σ^02=1n⁢∑i(Xi−θ0)2\hat{\sigma}_{0}^{2}=\frac{1}{n}\sum_{i}(X_{i}-\theta_{0})^{2} under H0H_{0}. In both cases the maximised log-likelihood equals −n2⁢log⁡(2⁢π⁢σ^2)−n2-\frac{n}{2}\log(2\pi\hat{\sigma}^{2})-\frac{n}{2}, with σ^02\hat{\sigma}_{0}^{2} in place of σ^2\hat{\sigma}^{2} under H0H_{0}, and thus by (25.1) we find

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}}.

By Wilks’ theorem (theorem 25.3), with r=1r=1 as in lecture 25, the distribution of WW under H0H_{0} converges to χ12\chi^{2}_{1} as n→∞n\to\infty. 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 nn we have. 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 exceeds the critical value 3.8413.841 of χ12\chi^{2}_{1}, as if the theorem were exact. Before you run the code, predict the two rejection rates. Will the rate for n=10n=10 be above or below 0.050.05?

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: the test with nominal level 0.050.05 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 n=10n=10 rise above the line from about 22 onwards, and thus the upper tail of WW is heavier than that of the limit.

Four panels. Top row: two histograms, tallest at
0 and decaying, with the density of the chi-squared distribution
with one degree of freedom shown over the histograms; for n=10
the bars between 1 and 4 are slightly above the curve, for n=100
the bars follow the curve. Bottom row: sorted values of the
simulated test statistic against quantiles of the chi-squared
distribution with one degree of freedom, with the identity line. For
n=10 the points start deviating above the line from 2 onwards,
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). Top: histograms with the density of the chi-squared distribution with one degree of freedom (truncated at 88). Bottom: the sorted values of the test statistic against the quantiles of the chi-squared distribution with one degree of freedom, with the identity line.

The script R/S27-wilks.R also computes the 0.950.95-quantile of the simulated values of WW, which is the threshold that does give size 0.050.05: for n=10n=10 the threshold needs to be about 4.654.65 instead of 3.843.84, while for n=100n=100 the simulated quantile 3.903.90 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 3.843.84 covers the truth in only about 93%93\% of all samples of size 1010. For this model the exact distribution is known: exercise 25.1 uses the tt 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 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, sigma replaced by sd(x) and qnorm(0.975) replaced by qt(0.975, df = 4). 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: of 100100 simulated 95%95\% intervals, 9595 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 n=10n=10 the chi-squared critical value 3.843.84 gives size 0.0750.075 instead of 0.050.05, and the simulated 0.950.95-quantile is about 4.654.65. 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.