Select and run the correct hypothesis test based on data properties. Covers parametric/non-parametric tests, effect sizes, and multiple comparison correction.
Select and run the correct hypothesis test based on data properties. Covers parametric/non-parametric tests, effect sizes, and multiple comparison correction.
Choose and execute the right statistical test for your data. This skill covers
normality checks, parametric and non-parametric tests, effect size computation,
and multiple comparison correction.
"""
Two-sample t-test power analysis.
Provide any three of (effect_size, alpha, power, n_per_group) to solve for the fourth.
"""
if
is
None
1.0
return
"required_n_per_group"
"effect_size"
"alpha"
"power"
else
1.0
return
"n_per_group"
"effect_size"
"alpha"
"achieved_power"
round
4
def
chi_square_test_with_effect
contingency: np.ndarray,
alpha: float = 0.05,
Dict
str
Any
"""Chi-square test with Cramér's V effect size."""
"test_name"
"Chi-square"
if
min
5
else
"Fisher exact (recommended)"
"chi2"
round
4
"p_value"
round
6
"dof"
"cramers_v"
round
4
"significant"
if
min
5
if
2
2
"fisher_p"
round
6
return
Example 1 — Comparing Three Treatment Groups
import numpy as np
from statistical_testing import run_comparison, correct_pvalues, power_analysis_ttest
rng = np.random.default_rng(42)
# Simulate three treatment groups (non-normal distributions)
control = rng.exponential(scale=5, size=40)
treatment1 = rng.exponential(scale=7, size=38)
treatment2 = rng.exponential(scale=9, size=42)
# Automatic test selection
result = run_comparison(control, treatment1, treatment2, alpha=0.05)
# → Kruskal-Wallis H (non-normal data detected automatically)# If significant, run post-hoc pairwise tests with FDR correctionfrom scipy.stats import mannwhitneyu
pairs = [
("control vs t1", mannwhitneyu(control, treatment1, alternative="two-sided").pvalue),
("control vs t2", mannwhitneyu(control, treatment2, alternative="two-sided").pvalue),
("t1 vs t2", mannwhitneyu(treatment1, treatment2, alternative="two-sided").pvalue),
]
labels, raw_p = zip(*pairs)
correction_df = correct_pvalues(list(raw_p), method="fdr_bh")
correction_df.index = labels
print(correction_df)
Example 2 — Two-Group Comparison with Power Report
import numpy as np
from statistical_testing import (
test_normality, run_comparison, cohens_d, power_analysis_ttest
)
rng = np.random.default_rng(0)
pre = rng.normal(loc=100, scale=15, size=30)
post = pre + rng.normal(loc=8, scale=10, size=30) # ~0.5 SD improvement# Step 1: Check normality of the difference
diff = post - pre
norm_result = test_normality(diff)
# Step 2: Run the appropriate test
result = run_comparison(pre, post, paired=True)
# Step 3: Retrospective power
d = result["effect_size"].get("cohens_d", cohens_d(pre, post))
power_report = power_analysis_ttest(effect_size=abs(d), alpha=0.05, n_per_group=30)
print(f"Achieved power: {power_report['achieved_power']:.2f}")
# Step 4: How many participants for 90% power?
needed = power_analysis_ttest(effect_size=abs(d), alpha=0.05, power=0.90)
print(f"N needed for 90% power: {int(needed['required_n_per_group'])}")
Categorical Data Example
import numpy as np
from statistical_testing import chi_square_test_with_effect, correct_pvalues
# 2x2 contingency: treatment vs outcome
table = np.array([[45, 15],
[30, 30]])
result = chi_square_test_with_effect(table)
print(result)
# {'test_name': 'Chi-square', 'chi2': ..., 'p_value': ..., 'cramers_v': ..., 'significant': True}# 3x3 contingency across three centres
multi_table = np.array([[50, 20, 10],
[40, 25, 15],
[35, 30, 20]])
result3 = chi_square_test_with_effect(multi_table)
print(result3)
Quick Reference: Effect Size Benchmarks
Measure
Small
Medium
Large
Cohen's d
0.2
0.5
0.8
η² (eta-squared)
0.01
0.06
0.14
Cramér's V (2×2)
0.1
0.3
0.5
Pearson r
0.1
0.3
0.5
Multiple Comparison Methods
Method
Controls
Best For
Bonferroni
Family-wise error rate (FWER)
Few comparisons, strict control
Holm
FWER (less conservative)
General use
Benjamini-Hochberg (FDR)
False discovery rate
Many comparisons (genomics, etc.)
Benjamini-Yekutieli
FDR under dependence
Correlated tests
Common Pitfalls
Assuming normality without testing: Always run normality checks, especially for n < 30.
Ignoring equal-variance assumption: Run Levene's test before independent t-test.
Multiple comparisons inflation: Any time you run ≥3 tests on the same dataset, apply correction.
Over-relying on p < 0.05: Always report effect sizes alongside p-values.
Wrong test for paired data: Pre/post measurements are paired; use paired tests.
Environment Variables
No API keys required. All computation is local.
# Optional: set random seed for reproducibility in scriptsexport STATS_RANDOM_SEED=42