Pulse Data Hub

Statistical Power in Python: Sample Size, Tested

Statistical power in Python comes down to one question: how many observations do you need before a real difference shows up as a significant result? This guide answers it with statsmodels, checks the formulas by simulation, and shows what goes wrong when a study is too small.

What statistical power means

Power is the probability that a test reports a significant result when the effect is real. It depends on four linked numbers: the effect size, the sample size, the significance level (alpha) and the power itself. Fix any three and you can solve for the fourth. The usual targets are alpha = 0.05 and power = 0.80.

For two group means the effect size is Cohen’s d: the difference in means divided by the pooled standard deviation. Conventional labels are 0.2 small, 0.5 medium and 0.8 large, but treat those as rough anchors, not facts about your data.

How many samples do you need?

from statsmodels.stats.power import TTestIndPower
pw = TTestIndPower()
for d in [0.2, 0.5, 0.8]:
    n = pw.solve_power(effect_size=d, power=0.8, alpha=0.05)
    print(d, int(np.ceil(n)))

For 80% power at alpha 0.05, the sample needed per group is 394 for d = 0.2, 64 for d = 0.5 and 26 for d = 0.8. Asking for 90% power at d = 0.2 raises it to 527 per group. Halving the effect size roughly quadruples the sample, which is why small effects are expensive to study.

Check the formula by simulation

A formula is only as good as its assumptions, so test it. Draw two normal samples with a true gap of 0.5 standard deviations, run a t-test, and count how often p is below 0.05.

rng = np.random.default_rng(1)
def sim(n, d, reps=5000):
    hits = 0
    for _ in range(reps):
        x = rng.normal(0, 1, n); y = rng.normal(d, 1, n)
        hits += stats.ttest_ind(x, y).pvalue < .05
    return hits / reps
print(sim(64, .5), sim(20, .5), sim(64, 0))

With 64 per group the simulation rejects in 79.6% of runs (formula: 80.1%). With 20 per group it rejects in 33.6% (formula: 33.8%). When the true difference is zero, it rejects 4.6% of the time, close to the 5% false-positive rate that alpha promises. The formula and the simulation agree.

Bar chart: t-test power for effect size 0.5 rises from 18.5% at 10 per group to 80.1% at 64 per group and 99.9% at 200
Power for a true effect of d = 0.5. Computed with statsmodels.

A real example: lunch vs dinner tips

The seaborn tips file records 244 restaurant bills. Compare the tip percentage at lunch (68 bills) with dinner (176 bills):

tips["pct"] = tips.tip / tips.total_bill * 100
a = tips[tips.time == "Dinner"].pct; b = tips[tips.time == "Lunch"].pct
print(stats.ttest_ind(b, a, equal_var=False))

Mean tip is 16.41% at lunch and 15.95% at dinner, a difference of 0.46 points. Welch’s t-test gives p = 0.514, so there is no evidence of a difference. Cohen’s d is 0.075. With these group sizes, the power to detect a true effect of that size is only 8.2%, and detecting a true d of 0.075 with 80% power would take about 2,792 bills per group.

That last number is a trap if misread. Power computed from the observed effect adds nothing to the p-value (a high p always means low “observed power”). Use power before collecting data, with an effect size you care about, not after the fact to explain a non-significant result.

The cost of an underpowered study

Small samples do not only miss real effects. When they do reach significance, they exaggerate. Simulating 5,000 studies with a true d of 0.5 and 20 per group, only 34.2% were significant, and in those the average estimated d was 0.862, about 1.7 times the truth. Publishing only the significant ones would overstate the effect (sometimes called the winner’s curse).

Power for conversion rates

from statsmodels.stats.proportion import proportion_effectsize
from statsmodels.stats.power import NormalIndPower
es = proportion_effectsize(.12, .14)
print(NormalIndPower().solve_power(es, power=.8, alpha=.05))

To detect a lift from a 12% conversion rate to 14%, you need about 4,433 visitors per group. For 12% to 13% it is 17,164 per group. The same arithmetic sits behind A/B test significance, where we also show how peeking at results inflates false positives.

Practical checklist

  • Decide the smallest effect worth detecting before you collect data, in real units, then convert it to d.
  • Solve for the sample size at 80% to 90% power, and add a margin for dropouts.
  • If the sample is fixed (as with the tips file), compute the smallest effect you could detect instead and report that.
  • Do not compute “observed power” after a non-significant result.

Reference: statsmodels TTestIndPower. Related reading: t-test vs Mann-Whitney in Python, correlation vs causation and exploratory data analysis.

FAQ

What is a good power level?

80% is the common convention and 90% is used when missing an effect is costly. There is nothing special about either number.

Does a larger sample always help?

It raises power, but with very large samples tiny, unimportant differences become significant. Report the effect size and a confidence interval, not only the p-value.

How do I get Cohen’s d for my own data?

Use a pilot study or published results, or choose the smallest difference that would matter in practice. Our tips example computed it from the data only to show the calculation.

Which versions were used?

Python with statsmodels 0.15.0, SciPy 1.18.1, NumPy 2.5.3 and pandas 3.0.5. Simulations used a fixed seed, so reruns give the same counts.

Newsletter Signup Form

Latest tutorials

Latest articles

On this page
Verified by MonsterInsights