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 -test from example 20.6, in the numerical setting of the example: we have observations from with known and we want to test against at level , i.e. we reject if and only if . By definition 20.4, the power function 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 ? If not, how far away from would it have to be before you would suspect that there might be a mistake in the code?
The rejection rate for is . This is the size of the test in the sense of definition 20.5. The value is not exactly , and it should not be: as in lecture 12, the estimate has standard deviation . The last line of the output shows this value. A value between and is what we expect for a correct test. If we get , 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 . Predict the three numbers which follow, for the first value of which is less than , and the other two values are the alternatives considered in example 20.6.
At the test rejects of samples: for the composite null hypothesis the size is attained at the boundary point , as remarked after example 20.6. The values and agree with the exact powers and 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 -test to the two-sided -test from example 20.7, and the one-sided -test for the first observations. The final test is wasteful, but a legal level 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) <- thetasdecide <- 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 <- 10round(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 <- 25sigma <- 2The 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 above or below the rejection rate of the one-sided test? Is the rejection rate at above or below? Predict the position of the half-sample test at , closer to or to ?
n <- 25sigma <- 2theta0 <- 10decide <- 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) <- thetasround(power[, thetas %in% c(9.5, 10, 10.5, 11, 11.5)], 3)
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 for as required for level 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 and is much larger than the one-sided test for . 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 test based on these observations can reject more than of the time, and the crosses indicate what power a poor test would have for this bound: the power at decreases from to , 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 -test and we have two parameters, where is fixed. From exercise 23.1 we know that the likelihood-ratio statistic from lecture 23 is given by
where is the sample standard deviation, and by Wilks’ theorem (theorem 23.3) the distribution of under converges to a chi-squared distribution with one degree of freedom as . 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 we have. We can simulate under for and , using again, and we can reject the hypothesis if is greater than the critical value 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 be above or below ? Note that the value in the denominator of is variable, since can be small.
theta0 <- 10sigma <- 2wilks <- 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)critc(n10 = mean(w10 > crit), n100 = mean(w100 > crit))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, since is a noisy estimator for 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 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 : is the quantile plot on the line, or is it curved above or below the 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 the points start deviating from the line (above) at around , indicating that the upper tail of is heavier than expected from the limit. For the points stay on the line throughout the plot. The solution to the problem of small is to use the exact distribution: under the test statistic follows the -distribution with degrees of freedom, and we can reject if exceeds the -quantile of this distribution. This is the one-sample -test from lecture 29. Since is increasing as a function of , this is equivalent to rejecting if exceeds the value .
Using the exact threshold instead of , the rejection rate for is back to and for 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 can be used to determine the actual size at a given .
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 instead of . Estimate the size of the one-sided -test for samples and state whether the estimated size is consistent with , 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 at which the power is greater than . Finally, consider the test which rejects for large values of the sample median. The critical value must be found first: simulate the median of observations under and use the -quantile of the simulated values to get a test with size . (Using the critical value of the -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.
-
•
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 samples has standard deviation of approximately near .
-
•
A power curve can be generated by applying the rejection-rate recipe to a grid of alternative values, using sapply. For the one-sided -test, the estimated points lie on the exact curve from lecture 20.
-
•
For every level test, the point for 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 the chi-squared critical value corresponds to size instead of , and for the approximation is good. If an exact test is available, this should be used. If not, simulation under should be used.