Skip to content

Lab B: check size before power

A p-value belongs to a reference procedure. Simulate data where the null is true to see whether the procedure rejects at its nominal rate, report the Monte Carlo error, and read power only where the size holds.

Advanced ~15 min8 steps

You will be able to

  • Estimate a test's size and power by simulation, with a Monte Carlo standard error
  • Report a Wilson interval for a simulated rejection rate
  • Explain why power is meaningless when size fails
  • Tell an outer simulation study from a permutation test inside one dataset

Read first

01

A p-value is part of a procedure

Read

A test statistic and its p-value assume a reference: a distribution the statistic would follow if the null were true. That reference rests on conditions such as equal variances, independence or normal tails. In an unfamiliar setting, check that the procedure behaves before trusting it.

The check is a simulation. Generate many datasets where the null is true, with the features your data really has (unequal group sizes, heavy tails, ties, serial dependence, missing values), run the full analysis on each, and count how often it rejects. If the real analysis estimates nuisance parameters or selects variables, the simulated one must too.

02

Rates with error bars

Read

With BB simulated datasets and rejection indicators IbI_b,

r^=1B∑b=1BIb,MCSE⁡(r^)≈r^(1−r^)B.\hat r = \frac{1}{B}\sum_{b=1}^{B} I_b, \qquad \operatorname{MCSE}(\hat r) \approx \sqrt{\frac{\hat r(1-\hat r)}{B}} .

Under the null, r^\hat r estimates the size, to compare with α\alpha. Under an alternative, it estimates power for that scenario. The Monte Carlo standard errorMonte Carlo standard errorHow much a Monte Carlo estimate would move with a new set of draws or replicates; it shrinks as the number of draws (or ESS) grows. says how much r^\hat r could move from the number of replicates alone.

For rare rates or few replicates, use a Wilson intervalWilson intervalA confidence interval for a proportion from inverting the score test. It stays between 0 and 1, even in small samples. rather than the normal approximation. A power figure without the effect size, noise, design and sample size is incomplete.

03

Size first

Explore

Give the small group the larger spread. Student's pooled t test rejects a true null far more often than 5%, while Welch's stays near it.

BExplorable · Student's t against Welch's t

When the group variances differ, which t test keeps its promised 5% error rate?

When the smaller group has the larger SD, Student's t (pooled variance) rejects a true null…

How sure?
15
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Now imagine reading the two tests' power at the same settings. Student's would look higher, because a test that rejects too often under the null also rejects more under the alternative. That isn't power. Size before powerSize before powerRead a test's power only after its size holds α at the same settings; a test that rejects too often looks powerful.: compare power only among procedures whose size holds at those settings.

04

Laboratory B

Read

The reference's second laboratory runs Student's and Welch's t tests across six scenarios: four where the null is true (equal spreads, unequal spreads with unequal sizes, heavy tails, serially dependent rows) and two with a real difference. For each scenario it reports the rejection rate with a Wilson interval.

The serially dependent scenario is the instructive failure: both tests assume independent rows, and both reject a true null too often. Choosing between them can't fix a broken design assumption. Python and R draw different random numbers, so the two engines are compared by their Monte Carlo intervals, never by identical draws.

Notebook exercise · ~15 min

Does the test keep its 5%?

Simulate thousands of datasets where the null is true, count how often the pooled and Welch t tests reject, and report the Monte Carlo interval.

  1. How often does the pooled test reject?

    Store the share of the 2,000 null datasets in which the pooled test rejects at the 5% level (p < 0.05) in rate_pooled.

    Show a solution
    Python
    rate_pooled = np.mean(p_pooled < 0.05)
    R
    rate_pooled <- mean(p_pooled < 0.05)
  2. And the Welch test?

    Store the share of null datasets in which the Welch test rejects at the 5% level in rate_welch.

    Show a solution
    Python
    rate_welch = np.mean(p_welch < 0.05)
    R
    rate_welch <- mean(p_welch < 0.05)
  3. The Monte Carlo interval

    A rate from 2,000 simulations is an estimate. Store the 95% Wilson interval for the Welch rate, lower bound first, in wilson: with p̂ the rate and n = 2,000, the center is (p̂ + z²/2n) / (1 + z²/n) and the half-width is z √(p̂(1 − p̂)/n + z²/4n²) / (1 + z²/n). Does it contain 0.05?

    Show a solution
    Python
    n = 2000
    p = rate_welch
    center = (p + z ** 2 / (2 * n)) / (1 + z ** 2 / n)
    half = z * math.sqrt(p * (1 - p) / n + z ** 2 / (4 * n ** 2)) / (1 + z ** 2 / n)
    wilson = [center - half, center + half]
    R
    n <- 2000
    p <- rate_welch
    center <- (p + z^2 / (2 * n)) / (1 + z^2 / n)
    half <- z * sqrt(p * (1 - p) / n + z^2 / (4 * n^2)) / (1 + z^2 / n)
    wilson <- c(center - half, center + half)

In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.

Notebook exercise · ~15 min

Power only counts for a calibrated test

Simulate the power of the pooled and Welch t tests for a real shift, then compare them fairly by calibrating the pooled test on null simulations first.

  1. Welch's power

    Store the share of the 2,000 datasets with a real difference in which the Welch test rejects at the 5% level in power_welch.

    Show a solution
    Python
    power_welch = np.mean(p_welch_alt < 0.05)
    R
    power_welch <- mean(p_welch_alt < 0.05)
  2. The pooled test's apparent power

    Store the share of those datasets in which the pooled test rejects at the 5% level in power_pooled. It looks better than Welch's. Is it?

    Show a solution
    Python
    power_pooled = np.mean(p_pooled_alt < 0.05)
    R
    power_pooled <- mean(p_pooled_alt < 0.05)
  3. A fair comparison

    Calibrate the pooled test first: take the 100th smallest of its null p-values p_pooled_null (the cutoff that rejects 5% of the 2,000 null datasets), then store the share of p_pooled_alt at or below that cutoff in power_pooled_fair.

    Show a solution
    Python
    cutoff = np.sort(p_pooled_null)[99]
    power_pooled_fair = np.mean(p_pooled_alt <= cutoff)
    R
    cutoff <- sort(p_pooled_null)[100]
    power_pooled_fair <- mean(p_pooled_alt <= cutoff)

In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.

05

Two different simulations

Read

The outer simulation above asks how a procedure behaves across many datasets. A permutation test works inside one dataset: it reshuffles labels many times and counts how often the reshuffled statistic is at least as extreme as the observed one.

With bb such draws among BB, the Monte Carlo p-value (b+1)/(B+1)(b+1)/(B+1) never reports an impossible zero. It doesn't make an invalid reshuffling valid: the labels must be exchangeableExchangeabilityUnder the null, any relabeling of the data is as likely as the one observed. Permutation tests rest on this. under the null, and any nuisance fitting has to be repeated inside the reshuffles.

06

Lab B in the Simulation Lab

In the app

Open the Academy's Simulation Lab and choose the Size before power template. It runs Student's and Welch's t tests in Lab B's six scenarios. Each cell shows the rejection rate with its 95% Wilson interval. Where a scenario's null twin (the same settings with no effect) has an interval that excludes α, the power cell is grayed out with the reason.

Change the replicates and watch the intervals narrow. The engine-mode templates, such as Behrens–Fisher in Python and R, run CodeSplash's own analyses in both engines and report how often the two reached different decisions on the same data.

07

Check: size and power

Checkpoint

Check yourself

A simulation of 1,000 null datasets rejects in 5% of them. What is the Monte Carlo standard error of that rate? Four decimals.

Show the answer

Answer: 0.0069

0.05×0.95/1000≈0.0069\sqrt{0.05 \times 0.95 / 1000} \approx 0.0069. A rate of 5% ± about 1.4 points (two MCSEs) is consistent with a 5% test.

Check yourself

Test A rejects 9% of null datasets and 70% of datasets with the effect. Test B rejects 5% and 60%. Which is more powerful?

Show the answer

Answer: b

A test that rejects too often under the null also rejects more under the alternative. Compare power only among tests that hold α.

Check yourself

Which power statement is complete?

Show the answer

Answer: b

Power depends on the scenario: effect size, noise, design, sample size and the number of replicates behind the estimate.

Check yourself

In Lab B's serially dependent scenario, both Student's and Welch's tests reject a true null too often. What is the right response?

Show the answer

Answer: b

Both tests assume independent rows. More replicates only measure the failure more precisely.

Check yourself

A permutation test with 999 reshuffles finds 5 statistics at least as extreme as the observed one. What is the Monte Carlo p-value? Three decimals.

Show the answer

Answer: 0.006

(5+1)/(999+1)=0.006(5 + 1)/(999 + 1) = 0.006. Counting the observed arrangement keeps the p-value above zero.

Check yourself

Python and R run the same Lab B simulation with their own random number generators. How should their results be compared?

Show the answer

Answer: b

Different generators produce different datasets. The comparison is statistical: both estimate the same rate, each with its own Monte Carlo error.

08

Recap

Recap
  • Simulate the full analysis under a true null to estimate size; under alternatives, power.
  • Report Monte Carlo standard errors or Wilson intervals with every simulated rate.
  • Read power only where size holds; a test that rejects too often looks powerful.
  • A permutation p-value (b+1)/(B+1)(b+1)/(B+1) is valid only when the labels are exchangeable under the null.
  • The Simulation Lab's "Size before power" template runs Lab B and grays out power whose size fails.

Concepts in this lesson