Lecture 24 Power and Size by Simulation

This is the R session for week 8. The lectures this week have been about the Neyman–Pearson lemma and the likelihood-ratio tests. In this session we will see how these tests perform in practice. The R technique we will use here is the indicator average from lecture 12, where the indicator now stands for the event of being rejected. The written reference for this is in appendix B.

24.1 The Size of a Test by Simulation

The test we consider today is the one-sided zz-test from example 20.6, in the numerical setting of the example: we have n=25n=25 observations from N⁢(θ,σ2)N(\theta,\sigma^{2}) with known σ=2\sigma=2 and we want to test H0:θ=10H_{0}\colon\theta=10 against H1:θ>10H_{1}\colon\theta>10 at level α=0.05\alpha=0.05, i.e. we reject if and only if X¯>10.658\bar{X}>10.658. By definition 20.4, the power function β⁢(θ)=ℙθ⁢(X¯>10.658)\beta(\theta)=\mathbb{P}_{\theta}(\bar{X}>10.658) is a probability, and by theorem A.14 we can approximate this probability by the proportion of samples where the test rejects. As in lecture 12, we write a function which, for one run of the test, returns TRUE if the test rejects and FALSE otherwise. We can then use mean to get the proportion of rejections. Before you try the code, predict the value of mean(rejected). Will it be exactly 0.050.05? If not, how far away from 0.050.05 would it have to be before you would suspect that there might be a mistake in the code?

n <- 25
sigma <- 2
theta0 <- 10
crit <- theta0 + qnorm(0.95) * sigma / sqrt(n)
crit
z_reject <- function(theta) {
x <- rnorm(n, mean = theta, sd = sigma)
mean(x) > crit
}
set.seed(2026)
rejected <- replicate(10000, z_reject(theta0))
head(rejected)
mean(rejected)
sqrt(0.05 * 0.95 / 10000)
## [1] 10.65794
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
## [1] 0.0523
## [1] 0.002179449

The rejection rate for H0H_{0} is 0.05230.0523. This is the size of the test in the sense of definition 20.5. The value is not exactly 0.050.05, and it should not be: as in lecture 12, the estimate has standard deviation 0.05⋅0.95/10000=0.0022\sqrt{0.05\cdot 0.95/10000}=0.0022. The last line of the output shows this value. A value between 0.0460.046 and 0.0540.054 is what we expect for a correct test. If we get 0.070.07, we should suspect that there is an error in the code. We can use the same function to get the rejection rate for any other θ\theta. Predict the three numbers which follow, for the first value of θ\theta which is less than θ0\theta_{0}, and the other two values are the alternatives considered in example 20.6.

mean(replicate(10000, z_reject(9.5)))
mean(replicate(10000, z_reject(10.5)))
mean(replicate(10000, z_reject(11)))
## [1] 0.0016
## [1] 0.344
## [1] 0.7953

At θ=9.5\theta=9.5 the test rejects 0.2%0.2\% of samples: for the composite null hypothesis H0:θ≤10H_{0}\colon\theta\leq 10 the size is attained at the boundary point θ0=10\theta_{0}=10, as remarked after example 20.6. The values 0.3440.344 and 0.7950.795 agree with the exact powers 0.3460.346 and 0.8040.804 from that example to within simulation error.

24.2 The Power Curve

A single number is a poor summary of a test, so we consider the power function of a grid of alternatives. We compare the one-sided zz-test to the two-sided zz-test from example 20.7, and the one-sided zz-test for the first 1212 observations. The final test is wasteful, but a legal level 0.050.05 test. What is the cost of the waste? The following box contains the full script, with lines in random order (except for the function body). Before you read the rest of this section, try to arrange these lines into a working script.

colnames(power) <- thetas
decide <- function(theta) {
x <- rnorm(n, mean = theta, sd = sigma)
z <- sqrt(n) * (mean(x) - theta0) / sigma
z_half <- sqrt(12) * (mean(x[1:12]) - theta0) / sigma
c(one = z > qnorm(0.95), two = abs(z) > qnorm(0.975),
half = z_half > qnorm(0.95))
}
set.seed(2026)
theta0 <- 10
round(power[, thetas %in% c(9.5, 10, 10.5, 11, 11.5)], 3)
power <- sapply(thetas, function(theta) rowMeans(replicate(5000, decide(theta))))
thetas <- seq(9, 12, by = 0.1)
n <- 25
sigma <- 2

The rule is as in lectures 9 and 12: all names must be defined before they are used, and the seed must be set before the first random draw. The new function returns a named logical vector of length three, giving the decisions for the three tests. The command replicate then returns a three-row matrix, and rowMeans returns the three rejection rates. The command sapply can then be used to apply this recipe to every value of the grid. Before you try the reconstructed script, predict the row for the two-sided test. Is the rejection rate of the two-sided test at θ=9.5\theta=9.5 above or below the rejection rate of the one-sided test? Is the rejection rate at θ=11\theta=11 above or below? Predict the position of the half-sample test at θ=11\theta=11, closer to 0.80.8 or to 0.50.5?

n <- 25
sigma <- 2
theta0 <- 10
decide <- function(theta) {
x <- rnorm(n, mean = theta, sd = sigma)
z <- sqrt(n) * (mean(x) - theta0) / sigma
z_half <- sqrt(12) * (mean(x[1:12]) - theta0) / sigma
c(one = z > qnorm(0.95), two = abs(z) > qnorm(0.975),
half = z_half > qnorm(0.95))
}
thetas <- seq(9, 12, by = 0.1)
set.seed(2026)
power <- sapply(thetas, function(theta) rowMeans(replicate(5000, decide(theta))))
colnames(power) <- thetas
round(power[, thetas %in% c(9.5, 10, 10.5, 11, 11.5)], 3)
## 9.5 10 10.5 11 11.5
## one 0.002 0.052 0.359 0.803 0.982
## two 0.227 0.051 0.248 0.701 0.963
## half 0.007 0.053 0.230 0.526 0.827
The rejection rate as a function of theta,
ranging from 9 to 12. The solid, S-shaped curve (filled circles)
starts at 0 for theta = 9, reaches 0.05 for theta = 10, and ends at
1 for theta = 12. The open circles form a U-shape with a minimum
of 0.05 for theta = 10 and stay below the filled circles for theta
above 10. The crosses follow the filled circles for theta below 10,
but increase more slowly above this value and only reach 0.83 for
theta = 11.5. The dashed horizontal line gives the value 0.05.
Figure 24.1: Estimated power functions for three level 0.050.05 tests for H0:θ=10H_{0}\colon\theta=10, using 50005000 samples of size n=25n=25 for each grid point: one-sided zz-test (filled circles), two-sided zz-test (open circles) and one-sided zz-test on the first 1212 observations only (crosses). For comparison, the line gives the exact power function of the one-sided test from lecture 20.

Figure 24.1, produced by the script R/S24-power.R, plots the data for the three rows and the grid. The exact power function (20.2) of the one-sided test is given by the line. All three curves start at 0.050.05 for θ0=10\theta_{0}=10 as required for level 0.050.05 tests, and the filled circles are on the exact curve at this point. The rest of the plot shows the power of the tests. The two-sided test is always smaller than the one-sided test for θ>10\theta>10 and is much larger than the one-sided test for θ<10\theta<10. Thus, neither curve is always larger than the other curve, and this illustrates proposition 22.5. From example 22.3 and proposition 22.4 we know that no level 0.050.05 test based on these 2525 observations can reject θ=11\theta=11 more than 80%80\% of the time, and the crosses indicate what power a poor test would have for this bound: the power at θ=11\theta=11 decreases from 0.800.80 to 0.530.53, and this is the cost of having discarded thirteen of the observations.

24.3 Wilks’ Theorem in Action

Assume now that the variance is not known. In this case we cannot perform the zz-test and we have two parameters, where H0:θ=10H_{0}\colon\theta=10 is fixed. From exercise 23.1 we know that the likelihood-ratio statistic from lecture 23 is given by

W=−2⁢log⁡Λ=n⁢log⁡(1+t2n−1),t=n⁢(X¯−θ0)S,W=-2\log\Lambda=n\log\Bigl{(}1+\frac{t^{2}}{n-1}\Bigr{)},\qquad t=\frac{\sqrt{% n}\,(\bar{X}-\theta_{0})}{S},

where SS is the sample standard deviation, and by Wilks’ theorem (theorem 23.3) the distribution of WW under H0H_{0} converges to a chi-squared distribution with one degree of freedom as n→∞n\to\infty. In contrast, in the case with known variance from lecture 23, the chi-squared distribution was exact. Here, the approximation will not be exact and we need to determine how well the approximation holds for the nn we have. We can simulate WW under H0H_{0} for n=10n=10 and n=100n=100, using σ=2\sigma=2 again, and we can reject the hypothesis if WW is greater than the critical value 3.8413.841 of the chi-squared distribution, as if the theorem were exact. Before you try the code, predict the two rejection rates. Will the rate for n=10n=10 be above or below 0.050.05? Note that the value SS in the denominator of tt is variable, since nn can be small.

theta0 <- 10
sigma <- 2
wilks <- function(n) {
x <- rnorm(n, mean = theta0, sd = sigma)
t <- sqrt(n) * (mean(x) - theta0) / sd(x)
n * log(1 + t^2 / (n - 1))
}
set.seed(2026)
w10 <- replicate(10000, wilks(10))
w100 <- replicate(10000, wilks(100))
crit <- qchisq(0.95, df = 1)
crit
c(n10 = mean(w10 > crit), n100 = mean(w100 > crit))
## [1] 3.841459
## 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, since SS is a noisy estimator for σ\sigma using only ten observations. The top row of figure 24.2 shows the two histograms, together with the density of the chi-squared distribution. The plot below shows the sorted values of WW together with the quantiles of the chi-squared distribution. This is similar to the plot we used in lecture 12 using qqnorm. The command qchisq with ppoints(10000) can be used to get the quantiles. Before you look at figure 24.2, predict the shape of the quantile plot for n=10n=10: is the quantile plot on the line, or is it curved above or below the line?

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 24.2: The likelihood-ratio statistic W=n⁢log⁡(1+t2/(n−1))W=n\log\bigl{(}1+t^{2}/(n-1)\bigr{)} 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 histograms do not do a good job of showing the difference in sample size, because the density of the chi-squared distribution is unbounded at zero and because most of the mass of the distribution is concentrated in the first few bars. In contrast, the quantile plots show better: for n=10n=10 the points start deviating from the line (above) at around 22, indicating that the upper tail of WW is heavier than expected from the limit. For n=100n=100 the points stay on the line throughout the plot. The solution to the problem of small nn is to use the exact distribution: under H0H_{0} the test statistic tt follows the tt-distribution with n−1n-1 degrees of freedom, and we can reject if |t||t| exceeds the 0.9750.975-quantile of this distribution. This is the one-sample tt-test from lecture 29. Since WW is increasing as a function of t2t^{2}, this is equivalent to rejecting if WW exceeds the value n⁢log⁡(1+t0.975,n−12/(n−1))n\log\bigl{(}1+t_{0.975,n-1}^{2}/(n-1)\bigr{)}.

ns <- c(10, 100)
exact <- ns * log(1 + qt(0.975, df = ns - 1)^2 / (ns - 1))
exact
c(n10 = mean(w10 > exact[1]), n100 = mean(w100 > exact[2]))
## [1] 4.501803 3.899844
## n10 n100
## 0.0532 0.0501

Using the exact threshold 4.504.50 instead of 3.843.84, the rejection rate for n=10n=10 is back to 0.050.05 and for n=100n=100 the two thresholds are close enough that it does not matter which one is used. An asymptotic level can only be guaranteed in the limit; otherwise, an exact test should be used if available, and if not, a simulation under H0H_{0} can be used to determine the actual size at a given nn.

24.4 A Task to Take Home

The following task is in the style of a practical report. Set the seed of the random number generator to your student ID and consider the setting from section 24.2, but with sample size n=10n=10 instead of n=25n=25. Estimate the size of the one-sided zz-test for 1000010000 samples and state whether the estimated size is consistent with 0.050.05, within simulation error. Next, estimate the power of the test on the grid seq(9, 13, by = 0.1) and create a plot of the estimated power curve, together with the exact power function from lecture 20. From the plot, determine the smallest θ\theta at which the power is greater than 0.90.9. Finally, consider the test which rejects for large values of the sample median. The critical value must be found first: simulate the median of 1010 observations under θ0=10\theta_{0}=10 and use the 0.950.95-quantile of the simulated values to get a test with size 0.050.05. (Using the critical value of the zz-test would lead to a much larger size and comparing the power of two tests with different size is not meaningful.) Estimate the power curve of the calibrated median test and add it to your plot. Write a short paragraph comparing the two power curves, and use example 22.3 to explain why the median test cannot win.

All of this session concerns a single test at a time. The question of what happens when many tests are performed on the same data set and when only the significant tests are reported will be discussed in lecture 30.

Before the next session, type the commands of this session into R, and check that what you get is what the text says you should get.

Summary.
  • •
    ​

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

  • •
    ​

    We can estimate the size and power of a test by simulation, by taking the average of the indicator function for the rejection. An estimated rate for 1000010000 samples has standard deviation of approximately 0.0020.002 near 0.050.05.

  • •
    ​

    A power curve can be generated by applying the rejection-rate recipe to a grid of alternative values, using sapply. For the one-sided zz-test, the estimated points lie on the exact curve from lecture 20.

  • •
    ​

    For every level 0.050.05 test, the point 0.050.05 for θ0\theta_{0} is passed through. The tests differ in power only. The two-sided test is weaker than the one-sided test on one side and stronger on the other, and a test which excludes observations is weaker for all alternatives, as shown in lecture 22.

  • •
    ​

    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 corresponds to size 0.0750.075 instead of 0.050.05, and for n=100n=100 the approximation is good. If an exact test is available, this should be used. If not, simulation under H0H_{0} should be used.