Hypothesis Testing
A hypothesis test is a formal way to decide whether data provide enough evidence against a default assumption. It answers questions like "Does the new fertiliser increase yield?" or "Is this coin fair?" while controlling how often we fool ourselves.
The two hypotheses
- The null hypothesis
H₀is the default, "nothing special" claim: no effect, no difference, the parameter equals a specific value. For exampleH₀: p = 0.5(the coin is fair). - The alternative hypothesis
H₁(orHₐ) is what we look for evidence of:H₁: p ≠ 0.5,p > 0.5orp < 0.5.
Hypotheses are always about population parameters (μ, p), never about sample statistics: we can see x̄ directly, so there is nothing to test about it.
The direction of H₁ determines the type of test:
| Alternative | Test | Lessons |
|---|---|---|
H₁: parameter > value |
right-tailed | proportion, mean |
H₁: parameter < value |
left-tailed | proportion, mean |
H₁: parameter ≠ value |
two-tailed | proportion, mean |
Choose the direction before looking at the data, based on the research question.
The logic: proof by "that would be surprising"
We temporarily assume H₀ is true and ask: how likely would data at least as extreme as ours be? That probability is the p-value. If it is very small, either something very unusual happened, or H₀ is wrong. We then reject H₀ in favour of H₁.
A friend claims her coin is fair, but in 100 flips you see 61 heads. Let's estimate the p-value by simulating 100,000 experiments with a truly fair coin:
import numpy as np
rng = np.random.default_rng(61)
heads = rng.binomial(n=100, p=0.5, size=100_000) # 100,000 runs of 100 fair flips
observed = 61
as_extreme = np.abs(heads - 50) >= abs(observed - 50) # two-tailed: 61+ or 39-
print(f"share of fair-coin experiments at least as extreme: {as_extreme.mean():.4f}")share of fair-coin experiments at least as extreme: 0.0358
The exact answer comes from the binomial distribution:
from scipy import stats
result = stats.binomtest(61, n=100, p=0.5, alternative="two-sided")
print(f"exact p-value: {result.pvalue:.4f}")exact p-value: 0.0352
Only about 3.5% of fair coins would produce a result this lopsided. That is fairly strong evidence against fairness.
Significance level and decision
Before the test you choose a significance level α, the threshold for "surprising enough". The most common choice is α = 0.05; 0.01 and 0.10 are also used.
- If
p-value ≤ α: rejectH₀. The result is statistically significant. - If
p-value > α: fail to rejectH₀. The data are compatible withH₀.
"Fail to reject" is not the same as "accept". Absence of evidence is not evidence of absence: maybe the effect exists but the sample was too small to detect it.
An equivalent approach compares the test statistic with a critical value (for example, reject if |z| > 1.96 at α = 0.05, two-tailed). Both approaches always lead to the same decision.
The six steps
- State
H₀andH₁. - Choose
α. - Check the conditions (random sample, sample size, normality…).
- Compute the test statistic (
z,t,χ², …). - Find the p-value (or critical value).
- Decide and state the conclusion in context.
Two kinds of error
H₀ actually true |
H₀ actually false |
|
|---|---|---|
Reject H₀ |
Type I error (false positive), probability α |
correct (probability = power, 1 − β) |
| Fail to reject | correct | Type II error (false negative), probability β |
The significance level α is the Type I error rate: if H₀ is true, a test at α = 0.05 wrongly rejects it 5% of the time. The simulation below runs 10,000 tests on fair coins:
import numpy as np
from scipy import stats
rng = np.random.default_rng(5)
false_alarms = 0
for heads in rng.binomial(100, 0.5, size=10_000):
if stats.binomtest(int(heads), 100, 0.5).pvalue <= 0.05:
false_alarms += 1
print(f"fair coins declared unfair: {false_alarms / 10_000:.2%}")fair coins declared unfair: 3.26%
The rate is below 5% (about 3.3%) because the binomial distribution is discrete, so the exact test can't hit 5% precisely and errs on the safe side.
Power
Power is the probability of correctly rejecting a false H₀. It grows with the true effect size, the sample size and α. Suppose the coin really lands heads 60% of the time:
import numpy as np
from scipy import stats
rng = np.random.default_rng(6)
for n in [50, 100, 200, 400]:
heads = rng.binomial(n, 0.60, size=2000)
rejected = [stats.binomtest(int(h), n, 0.5).pvalue <= 0.05 for h in heads]
print(f"n={n:3d} flips: power = {np.mean(rejected):.2f}")n= 50 flips: power = 0.23
n=100 flips: power = 0.47
n=200 flips: power = 0.80
n=400 flips: power = 0.97
With 50 flips we would miss this biased coin most of the time. With 400 flips we almost always catch it. Planning the sample size so that power is high (often 80% or more) is part of designing a good study.
Interpreting p-values correctly
- The p-value is not the probability that
H₀is true. - A small p-value does not mean the effect is large or important. With huge samples, trivial differences become significant.
p = 0.049andp = 0.051are practically the same evidence. Treatαas a guideline, and report the actual p-value and an effect size or confidence interval.
See Statistical Pitfalls for what happens when these rules are ignored.
Test yourself
What is the p-value?
- The probability that H₀ is true
- The probability, assuming H₀ is true, of data at least as extreme as observed
- The probability that H₁ is true
- The chance of a Type II error
A test at α = 0.05 gives p = 0.21. What is the correct conclusion?
- Accept H₀: it is proven true
- Reject H₀
- Fail to reject H₀: the data don't provide enough evidence against it
- The test is invalid
Convicting an innocent person corresponds to which error, if H₀ is 'the defendant is innocent'?
- Type I error
- Type II error
- Sampling error
- No error