Hypothesis testing is how we draw conclusions about a population from a sample. It's also the most-misunderstood part of statistics. By the end of this chapter you'll know how to set up a test, choose the right one, and read the result without making the classic mistakes.
The framework
- H₀ (null hypothesis): "nothing's going on" — no difference, no effect.
- H₁ (alternative): the effect you actually want to detect.
- α (significance level): the false-positive rate you're willing to accept. Conventionally 0.05.
- p-value: probability of seeing data at least this extreme, assuming H₀ is true.
- If
p < α, reject H₀. Ifp ≥ α, fail to reject — not the same as "accept H₀".
p-values demystified
If a coin returns 70 heads in 100 flips and the p-value is 0.0001, it means: "if the coin were fair, we'd see results this extreme only 0.01% of the time". It does not mean the probability of fairness is 0.01%.
Type I & Type II errors
| Reality \ Decision | Reject H₀ | Fail to reject H₀ |
|---|---|---|
| H₀ true | Type I error (α) | Correct |
| H₀ false | Correct | Type II error (β) |
Power = 1 − β — the probability of correctly detecting an effect when it exists. Aim for power ≥ 0.8 in experiment design.
Picking the right test
| Question | Test |
|---|---|
| Compare a sample mean to a known value | one-sample t-test |
| Compare two independent groups (means) | independent two-sample t-test |
| Compare paired observations (before/after) | paired t-test |
| Compare a proportion to a known value | one-sample z-test |
| Compare two proportions | two-proportion z-test |
| More than 2 groups (means) | ANOVA |
| Categorical vs categorical | Chi-squared test |
| Non-normal data, two groups | Mann-Whitney U |
t-test in Python
from scipy import stats# Did variant B (n=200, mean=12.4 min, std=3.1) reduce time-on-task vs A (n=200, mean=14.1, std=3.5)?import numpy as npnp.random.seed(42)a = np.random.normal(14.1, 3.5, 200)b = np.random.normal(12.4, 3.1, 200)t_stat, p_value = stats.ttest_ind(a, b, equal_var=False)print(f"t = {t_stat:.3f}, p = {p_value:.4f}")# t = 5.171, p = 0.0000# Paired t-test — same users, before & afterbefore = np.array([72, 65, 80, 90, 68])after = np.array([74, 70, 85, 88, 72])t, p = stats.ttest_rel(before, after)print(f"paired t = {t:.3f}, p = {p:.4f}")
Chi-squared in Python
from scipy.stats import chi2_contingency# Is button colour independent of click behaviour?# Rows = colour (red, green), Columns = clicked / didn'tobserved = [[120, 380], # red[180, 320] # green]chi2, p, dof, expected = chi2_contingency(observed)print(f"chi² = {chi2:.3f}, p = {p:.4f}")# chi² = 18.18, p = 0.0000 ← reject independence: colour matters
Reading an A/B test result
You ran an A/B test. The output says p = 0.03, lift = +4.2%. What's the right read?
- p < 0.05: statistically significant at the 95% level. You reject the null.
- +4.2% lift: the point estimate of effect size. The actual effect could be smaller or larger.
- Always look at confidence intervals. A "+4.2% lift, 95% CI [0.5%, 8.0%]" is honest — the true effect could realistically be a tiny 0.5%.
- Practical significance ≠ statistical significance. A 0.05% lift can be statistically significant if your sample is huge — but might not justify shipping.
- Stopping early invalidates the test. Pre-register your sample size; don't peek and stop.
Key takeaways
- 01p-value = P(data | H₀), not P(H₀ | data). The reverse is the most common stats mistake.
- 02Type I (α): false positive. Type II (β): false negative. Power = 1 − β; target 0.8.
- 03Pick the test by question type — t-test for means, chi-squared for categorical, ANOVA for >2 groups.
- 04In Python: scipy.stats covers t-test, chi-squared, ANOVA in one line each.
- 05Always report a confidence interval alongside the p-value. Practical significance > statistical significance.
