Sub topic 15
How much data is enough: sample size and power
This chapter assumes you can state a null and alternative hypothesis, know what Type I and Type II errors are, and can read a p-value. It also assumes you know the two-sample t-test and the chi-square test from earlier chapters. Here you will plan a data collection before you run it, so the test has a fair chance of finding a difference that matters.
Why plan sample size first
A test that fails to reject the null does not show that the groups are the same. With too few observations, a real difference stays hidden in the noise. This is a Type II error, and its probability is called beta. Power is 1 − beta, the probability that the test detects a difference of a given size when that difference exists.
Too much data has costs as well. Measurements cost time and money, and a huge sample makes trivial differences "significant". Sample size planning finds the smallest sample that gives acceptable power for a difference worth detecting.
The four quantities that determine sample size
Four quantities are linked. Fix any three and the fourth follows.
| Quantity | Symbol | Typical value | Effect on required n |
|---|---|---|---|
| Significance level | alpha | 0.05 | Smaller alpha needs more data |
| Power | 1 − beta | 0.80 to 0.90 | Higher power needs more data |
| Smallest difference worth detecting | delta | Set by the business | Smaller delta needs much more data |
| Process variation | sigma | Estimated from past data | Larger sigma needs more data |
Delta and sigma combine into one number, the standardized effect size. For a two-sample t-test this is Cohen's d:
d = delta / sigma
If a shift of 2 units matters and the process standard deviation is 4 units, then d = 0.5. The same shift in a process with sigma = 2 gives d = 1.0, which needs far fewer observations.
Computing a sample size
The statsmodels library solves for whichever quantity you set to None. The next example finds the sample size per group for a two-sample t-test that detects a medium effect (d = 0.5) with alpha = 0.05 and 80% power.
from statsmodels.stats.power import TTestIndPower
# Sample size per group to detect a medium effect (Cohen's d = 0.5)
n_per_group = TTestIndPower().solve_power(
effect_size=0.5, alpha=0.05, power=0.8, alternative="two-sided"
)
print(n_per_group)The result is about 63.8. Always round up, so plan for 64 per group, or 128 observations in total.
Solving for the other quantities
The same function answers two other practical questions. Suppose you can only afford 30 observations per group.
from statsmodels.stats.power import TTestIndPower
analysis = TTestIndPower()
# Power achieved with 30 per group for d = 0.5
power = analysis.solve_power(effect_size=0.5, nobs1=30, alpha=0.05)
print("Power with n=30:", round(power, 2))
# Smallest effect size detectable with 80% power at n = 30
d_min = analysis.solve_power(nobs1=30, alpha=0.05, power=0.8)
print("Detectable d with n=30:", round(d_min, 2))With 30 per group, power for d = 0.5 is only about 0.48, so the test misses a real medium effect more often than it finds it. The smallest effect you can detect reliably is about d = 0.74. Reporting this figure is more honest than saying "no significant difference".
How n grows as the effect shrinks
Required n rises with the inverse square of the effect size. Halving the difference you want to detect roughly quadruples the sample.
| Cohen's d | Description | n per group (alpha 0.05, power 0.80) |
|---|---|---|
| 0.2 | Small | 394 |
| 0.5 | Medium | 64 |
| 0.8 | Large | 26 |
Sample size for proportions
For discrete data the process variation depends on the proportion itself, so you do not supply a sigma. Instead you give the two proportions you want to tell apart. Take the shift comparison from the chi-square chapter, where the defect rate was 9% on one shift and 17% on the other. How many units per shift would you need to detect a gap that large?
from statsmodels.stats.power import NormalIndPower
from statsmodels.stats.proportion import proportion_effectsize
h = proportion_effectsize(0.17, 0.09) # Cohen's h
n = NormalIndPower().solve_power(
effect_size=h, alpha=0.05, power=0.8, alternative="two-sided"
)
print("h =", round(h, 3), "n per group =", round(n))The answer is roughly 270 units per shift. That is more than the 200 per shift in the earlier example, so that study was underpowered for such a gap. Proportions near zero need especially large samples, because rare defects appear infrequently in any sample.
Estimating sigma
The sigma estimate is usually the weakest input. Sources, from best to worst:
- Historical data from the same process under stable conditions, such as a control chart's standard deviation.
- A small pilot study of 10 to 20 observations. Its standard deviation is itself uncertain, so add a safety margin.
- Expert judgment or a published value for similar processes. Use this only when nothing else exists.
If sigma is underestimated, the real d is smaller than planned and the study ends up underpowered. When unsure, use a larger sigma.
Design choices that change the required n
- Paired designs. For a paired t-test, sigma is the standard deviation of the within-pair differences. When the before and after measurements are strongly correlated, that value is much smaller than the standard deviation of the raw data, so fewer pairs are needed. Use
TTestPowerfor one-sample and paired designs. - Unequal group sizes. The
ratioargument ofTTestIndPowersets the size of group 2 relative to group 1. Balanced groups need the fewest total observations for a given power. - More than two groups. ANOVA planning uses
FTestAnovaPowerwith Cohen's f. Plan for the post hoc comparisons you will actually run, as these have less power than the overall F test. - Non-parametric tests. When data are normal, rank-based tests need about 5% more data than the t-test. When data are heavily skewed, they can need less. A small safety margin covers the difference.
- One-sided tests. These need less data, but only use one when a difference in the other direction would truly be irrelevant and you decided this before collecting data.
Common errors
- Post hoc power. Computing power from the effect size you observed in the finished study adds no information. It is a direct function of the p-value. Plan power before the study from the delta that matters, and report a confidence interval afterwards.
- Planning for the effect you hope to see. If you plan around an optimistic delta and the real effect is smaller, the study is underpowered. Plan around the smallest difference worth acting on.
- Ignoring losses. Missing values, unusable measurements, and gauge problems reduce the usable n. Inflate the target, for example divide by 0.9 to allow for 10% loss.
- Treating the number as exact. The formula rests on assumptions about normality and about sigma. Treat 64 as "around 60 to 70" and spend any spare budget on more data.
- Sampling that breaks independence. Ten readings from one part are not ten independent observations. Count the independent units.
Exercise
A team wants to detect a 3-second reduction in mean cycle time. The historical standard deviation is 6 seconds. They will use a two-sample t-test with alpha = 0.05 and 80% power. What effect size d should they enter, and about how many observations per group do they need? What happens to the required n if the true standard deviation turns out to be 9 seconds?
Show answer
d = 3 / 6 = 0.5, which needs about 64 per group (63.8 rounded up). If sigma is 9 seconds, the real effect is d = 3 / 9 ≈ 0.33. The required n rises to roughly 144 per group, because n scales with 1/d². A sample of 64 per group would give power of only about 0.5 for that smaller effect.