Power Analysis¶
- hypotestx.power.sample_size.n_ttest_two_sample(effect_size: float, alpha: float = 0.05, power: float = 0.8, alternative: str = 'two-sided') int[source]¶
Minimum n per group (balanced) for an independent-samples t-test.
- Parameters:
effect_size (Cohen's d)
alpha (Type I error rate)
power (desired power)
alternative ('two-sided', 'greater', or 'less')
- Returns:
int
- Return type:
minimum n per group; total N = 2 * returned value
- hypotestx.power.analysis.power_ttest_two_sample(effect_size: float, n1: int, n2: int | None = None, alpha: float = 0.05, alternative: str = 'two-sided') float[source]¶
Post-hoc power for an independent-samples t-test.
- Parameters:
effect_size (Cohen's d ((mean1 - mean2) / pooled_sigma))
n1 (first group size)
n2 (second group size (default = n1, i.e. balanced design))
alpha (significance level)
alternative ('two-sided', 'greater', or 'less')
- Returns:
float
- Return type:
estimated statistical power (0..1)
Notes
NCP = d * sqrt(n1*n2/(n1+n2)).
Functions Reference¶
n_ttest_two_sample¶
Calculate the required sample size per group to detect a given effect size at the desired power level for an independent two-sample t-test.
import hypotestx as hx
# Medium effect, standard alpha, 80% power
n = hx.n_ttest_two_sample(effect_size=0.5, alpha=0.05, power=0.8)
print(f"Required n per group: {n}") # 64
# Small effect, 90% power
n = hx.n_ttest_two_sample(effect_size=0.2, alpha=0.05, power=0.9)
power_ttest_two_sample¶
Compute the achieved power for a two-sample t-test given the observed effect size and sample sizes.
pow_result = hx.power_ttest_two_sample(
effect_size=0.4,
n1=30,
n2=30,
alpha=0.05,
)
print(f"Achieved power: {pow_result.power:.2f}")