Foundations of Inference in Python
Paul Savala
Assistant Professor of Mathematics
| Sample size | Effect size | Alpha | 
|---|---|---|
![]()  | 
![]()  | 
![]()  | 
from scipy.stats import norm# Control group: Mean 0 pounds, std 1 poundcontrol = norm.rvs(loc=0,scale=1,size=100)# Treatment group: Mean -2 pounds, std 1 poundtreatment = norm.rvs(loc=-2, scale=1, size=100)
$H_0$: No difference in weight loss (wrong)
$H_a$: Treatment group loses weight (correct)
Conclusion: Correctly reject $H_0$ in favor of $H_a$.
from scipy.stats import ttest_ind# Conduct a t-test alpha = 0.05 t_test = ttest_ind(treatment, control, alternative='less')# Check the significance print(t_test.pvalue < alpha)
TRUE
  control = norm.rvs(loc=0, scale=1, size=5) treatment = norm.rvs(loc=-2, scale=1, size=5)# Conduct a t-test tt = ttest_ind(treatment, control, alternative='less')print(tt.pvalue < 0.05)
FALSE
Conclusion: Fail to reject H0 (wrong)
# Weight loss = 0.2 pounds, sample size = 100treatment = norm.rvs(loc=-0.2, scale=1, size=100)# Conduct a t-test t_test = ttest_ind(treatment, control, alternative='less')print(t_test.pvalue < 0.05)
FALSE
  
If there is a significant effect, will our test detect it?

Power of a test: If the alternative hypothesis (Ha) is true, how likely is our test to reject the null (H0) given the data we've supplied it?
Calculate power before collecting a sample
from statsmodels.stats import power # Power function tt_power = power.TTestIndPower()# Calculate power pwr = tt_power.power(effect_size=0.2,nobs1=100,alpha=0.05)print(pwr)
0.291
Low chance of detection!
nobs1 = TTestIndPower().solve_power(effect_size=-0.2, nobs1=None, # Solve for alpha=0.05, power=0.8)print(nobs1)
13735.26
  Foundations of Inference in Python