Day 37. Fisher's Exact Test & Randomisation Inference
Exact p-values from the randomisation itself, under the sharp null of no effect for anyone, with no distributional assumptions.
- ●State the sharp null hypothesis and impute the full science table under it.
- ●Compute an exact randomisation p-value by enumerating or simulating assignments.
- ●Prove the validity of the randomisation test and invert tests to get confidence intervals.
- ●Apply randomisation inference to small trials, binary outcomes, clustered designs and single treated units.
Theory
The sharp null
Fisher (1935) tested the sharp null
Under , each unit's outcome would have been the same whatever its assignment, so the observed fills in both potential outcomes. The whole science table is known.
The randomisation distribution
Pick a test statistic , e.g. the difference in means. Because the science table is known under , we can compute for every assignment the design could have produced. The exact p-value is
No normality, no large sample, no model: the only randomness is the one the researcher created. When there are too many assignments to list, draw at random (Monte Carlo); the p-value then has standard error .
Choices and extensions
- Statistic: any function of the data. Rank statistics (Wilcoxon) resist outliers.
- Other sharp nulls: for all . Subtract from treated outcomes and test again. Collecting all not rejected gives a confidence interval (test inversion).
- Design: permute exactly as the design randomised: within blocks for blocked designs, whole clusters for cluster designs.
- Sharp versus weak null: the sharp null (no effect for anyone) is stronger than Neyman's weak null (zero average effect). Randomisation tests are exact for the former and only approximately valid for the latter.
Part 1 · Warm-up
Exercise 1 · How many assignments?
A pilot randomises 4 of 8 villages to a microfinance programme. How many assignments are possible, and what is the smallest achievable two-sided p-value?
Show solution
Step 1. assignments.
Step 2. The observed assignment always counts itself. With a symmetric two-sided statistic, its mirror image (treated and control swapped) gives the same , so at least 2 of 70 qualify.
Answer: 70 assignments; smallest two-sided p-value .
Exercise 2 · Imputing under the sharp null
A treated village has profit 12 and a control village 9. Under , what are their missing potential outcomes?
Show solution
Step 1. No effect for anyone: for each village.
Step 2. Treated village: . Control village: .
Exercise 3 · The statistic under another assignment
Profits are 12, 15, 11, 14 (treated villages A–D) and 9, 10, 13, 8 (controls E–H). Under , what difference in means would the assignment {A, B, E, F} have produced?
Show solution
Step 1. Under outcomes do not move: "treated" A, B, E, F have .
Step 2. "Controls" C, D, G, H have .
Answer: 0.
Exercise 4 · From counts to a p-value
Of the 70 assignments, 8 give (the observed value). What is the p-value?
Show solution
Step 1. .
Answer: 0.11: not significant at 5% or 10%.
Exercise 5 · Monte Carlo precision
With 70 million possible assignments you sample . If the true p-value is 0.05, what is the Monte Carlo SE? How many draws give an SE of 0.002?
Show solution
Step 1. SE .
Step 2. Solve : .
Answer: SE ≈ 0.007 with 1,000 draws; about 12,000 draws for SE 0.002.
Part 2 · Proofs
Exercise 6 · The test is exact
Under the sharp null and the actual design, prove that .
Show solution
Step 1. Under the outcome vector is fixed whatever the assignment, so the statistic is a fixed number for each of the possible assignments.
Step 2. The observed assignment is one of them, chosen uniformly at random by the design. So is a uniformly random draw from the values .
Step 3. The p-value is the share of values at least as large as the draw. A uniformly random draw lands in the top with probability at most (ties only make the p-value larger).
Answer: : exact Type I error control in any sample size.
Exercise 7 · Confidence intervals by inversion
For , show how to impute the science table, and explain why is a confidence set.
Show solution
Step 1. Treated units: . Controls: .
Step 2. Equivalently, subtract from treated outcomes; under the adjusted outcomes satisfy the sharp null of no effect, so Exercise 6 applies.
Step 3. If the true effect is , the test of rejects with probability at most , so is in the set with probability at least .
Exercise 8 · The null distribution is centred at zero
Under the sharp null and complete randomisation, show the randomisation distribution of the difference in means has mean zero.
Show solution
Step 1. Under , , so the finite-population ATE is 0.
Step 2. By Day 31, Exercise 8, the difference in means averages to the ATE over all assignments.
Answer: mean 0.
Exercise 9 · Sharp versus weak null
Construct a two-unit example where the weak null (ATE = 0) holds but the sharp null fails.
Show solution
Step 1. Unit 1: , (effect +5). Unit 2: , (effect −5).
Step 2. ATE : the weak null holds.
Step 3. Individual effects are not zero: the sharp null fails.
Answer: a randomisation test of the sharp null can reject here even though the average effect is zero; it tests "no effect for anyone".
Exercise 10 · Rank statistics
Show that, under the sharp null, the randomisation distribution of the Wilcoxon rank-sum statistic (the sum of treated units' ranks) depends only on and (with no ties), not on the outcome values.
Show solution
Step 1. Under the outcomes, hence their ranks , are fixed.
Step 2. Each assignment picks of these ranks; the statistic is their sum.
Step 3. The set of possible sums and their frequencies depend only on which subsets of of size exist.
Answer: the null distribution can be tabulated once for each ; an extreme outlier changes one rank by at most a few places, so the test is robust.
Part 3 · Applied
Exercise 11 · A microfinance pilot
Using the eight villages of Exercise 3 (treated 12, 15, 11, 14; control 9, 10, 13, 8), compute the observed difference in means, the exact one-sided and two-sided p-values (see the code), and compare with a t-test.
Show solution
Step 1. Treated mean ; control mean ; observed difference 3.
Step 2. Enumerate all 70 assignments under : 4 give a difference and 8 give .
Step 3. One-sided ; two-sided .
Step 4. A Welch t-test gives , somewhat smaller, but it relies on approximate normality with only 8 villages.
Answer: no significant evidence at 5%. The randomisation p-value is exact whatever the distribution of profits.
Exercise 12 · A small hospital trial
10 patients are randomised, 5 per arm; all 5 treated recover versus 1 of 5 controls. Compute Fisher's exact p-values.
Show solution
Step 1. Under , the 6 recoverers would recover whatever the arm. The number of recoverers landing in the treated arm is hypergeometric: choose 5 treated from 10 patients, 6 of whom recover.
Step 2. All 5 treated recover: . That is the one-sided p-value (no table is more extreme).
Step 3. The opposite extreme (only 1 treated recoverer) also has probability .
Step 4. Two-sided .
Answer: one-sided 0.024, two-sided 0.048: significant at 5% with just 10 patients, and no large-sample approximation needed.
Exercise 13 · Testing a specific effect
For the microfinance pilot, test the sharp null that the programme raises every village's profit by exactly 2.
Show solution
Step 1. Subtract 2 from treated profits: 10, 13, 9, 12; controls stay 9, 10, 13, 8.
Step 2. The adjusted difference is ; the randomisation test of no effect on the adjusted data gives (code).
Step 3. Repeating for every and keeping those with gives a 90% interval; here it spans roughly 0 to 6.
Answer: an effect of 2 is entirely consistent with the data; the interval is wide because there are only 8 villages.
Exercise 14 · Clustered randomisation inference
A study randomises 10 of 20 schools; outcomes are measured for 3,000 pupils. How should the randomisation test permute the data, and why?
Show solution
Step 1. The design assigned schools, so the null distribution must come from re-assigning whole schools: possibilities.
Step 2. Permuting individual pupils would generate assignments the design could never produce (mixed schools) and understate variability, because pupils in a school share shocks.
Answer: permute at the school level, using a school-level or pupil-level statistic computed for each re-assignment.
Exercise 15 · One treated state
A tax reform is adopted by 1 of 20 states. You compute a statistic for each state as if it had been the one treated. What is the smallest possible p-value, and what does that imply?
Show solution
Step 1. If the reform were assigned at random to one state, there are 20 equally likely possibilities.
Step 2. The smallest one-sided p-value is (the treated state is the most extreme of all 20).
Answer: 0.05. With one treated unit, randomisation inference can never be very decisive; this placebo logic underlies inference for synthetic control (Day 86). (Here the "as if random" assumption must be argued, since states choose reforms.)
Verify in Python
import itertools
import numpy as np
from scipy import stats
y = np.array([12, 15, 11, 14, 9, 10, 13, 8.]); treated = np.array([1, 1, 1, 1, 0, 0, 0, 0], bool)
def rand_test(y, treated, tau0=0.0):
z = y - tau0 * treated # impute under H0(tau0)
obs = z[treated].mean() - z[~treated].mean()
dist = np.array([z[list(c)].mean() - np.delete(z, list(c)).mean()
for c in itertools.combinations(range(len(z)), treated.sum())])
return obs, (dist >= obs - 1e-9).mean(), (np.abs(dist) >= abs(obs) - 1e-9).mean()
obs, p1, p2 = rand_test(y, treated)
print(f"observed {obs}, one-sided p {p1:.3f}, two-sided p {p2:.3f}")
print("Welch t-test p:", round(stats.ttest_ind(y[treated], y[~treated], equal_var=False).pvalue, 3))
print("test of tau0 = 2: p =", round(rand_test(y, treated, 2)[2], 3))
ci = [t for t in np.arange(-4, 10, 0.1) if rand_test(y, treated, t)[2] > 0.10]
print(f"90% interval by inversion: [{min(ci):.1f}, {max(ci):.1f}]")
# Fisher's exact test for the hospital trial
print("Fisher exact (two-sided):", round(stats.fisher_exact([[5, 0], [1, 4]])[1], 4),
" one-sided:", round(stats.fisher_exact([[5, 0], [1, 4]], alternative="greater")[1], 4))
Key takeaways
- Under the sharp null, every potential outcome is known, so the randomisation itself gives an exact null distribution.
- Permute exactly as the design randomised (within blocks, by cluster).
- Inverting sharp-null tests yields confidence intervals; randomisation tests concern "no effect for anyone".
Simulation Lab: Fisher's Exact Randomisation Test
Open full screen →Enumerate every possible assignment under the sharp null and read off an exact p-value — for means or ranks.
Sharp null: the programme changed nobody's outcome. Then every outcome is fixed, and we can recompute the statistic under all possible assignments. The p-value is the share of assignments at least as extreme as the one that happened — exact, with no distributional assumptions.