How to Calculate Minimum Sample Size for a T-Test in Python

Compute the minimum sample size per group for a two-sample t-test using effect size, significance level, and statistical power.

Easy Python 3.9+ Aug 9, 2026 A/B testing & experimentation 15 views 0 copies

Requires third-party packages — install first
pip install scipy

Python code

28 lines
Python 3.9+
import math
from scipy.stats import norm


def min_sample_size(effect_size, alpha=0.05, power=0.8):
    """
    Calculate minimum sample size for a two-sample t-test (equal groups).

    Args:
        effect_size: Cohen's d (standardized mean difference)
        alpha: significance level (Type I error)
        power: desired statistical power (1 - Type II error)

    Returns:
        Minimum sample size per group (rounded up)
    """
    z_alpha = norm.ppf(1 - alpha / 2)
    z_beta = norm.ppf(power)

    n = (2 * (z_alpha + z_beta) ** 2) / (effect_size ** 2)
    return math.ceil(n)


if __name__ == "__main__":
    # Example: effect size 0.5, 95% confidence, 80% power
    n_per_group = min_sample_size(effect_size=0.5)
    print(f"Minimum sample size per group: {n_per_group}")
    print(f"Total sample size (two groups): {n_per_group * 2}")

Output

stdout
Minimum sample size per group: 64
Total sample size (two groups): 128

How it works

The function uses the normal approximation to estimate the required sample size per group for a two-sample t-test. It computes z-scores for the chosen alpha and power using scipy.stats.norm.ppf. The formula n = 2 * (z_alpha + z_beta)^2 / effect_size^2 balances Type I and Type II error rates. math.ceil rounds up to ensure you meet the minimum statistical requirement. This approach is standard for balanced A/B tests before data collection begins.

Common mistakes

  • Using `math.floor` instead of `math.ceil`, underestimating needed participants
  • Forgetting to account for unequal group sizes in real experiments
  • Using one-tailed z-values when a two-tailed test is required

Variations

  1. Use `statsmodels` `NormalIndPower` for more complex designs
  2. Add a correction factor for finite populations

Real-world use cases

  • Determining participant counts for an A/B test on a marketing landing page.
  • Sizing a clinical trial cohort before recruiting patients for a drug study.
  • Estimating required sample sizes for user research experiments in product analytics.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from A/B testing & experimentation

Related tutorials and quizzes for this topic.