Experimental design: power analysis

A/B Testing in Python

Moe Lotfy, PhD

Principal Data Science Manager

Effect size

  • Cohen's d for differences in means Cohen's d effect size formula
  • Cohen's h for differences in proportions

Cohen's h effect size formula

  • Rule of thumb
    • Small effect = 0.2
    • Medium effect = 0.5
    • Large effect = 0.8
# Calculate standardized effect size
from statsmodels.stats.proportion import proportion_effectsize
effect_size_std = proportion_effectsize(.33, .3)
print(effect_size_std)
0.0645
# Calculate standardized effect size
from statsmodels.stats.proportion import proportion_effectsize
effect_size_std = proportion_effectsize(p_B, p_A)
print(effect_size_std)
0.0716
A/B Testing in Python

Sample size estimation for proportions

# Import power module
from statsmodels.stats import power
# Calculate sample size
sample_size = power.TTestIndPower().solve_power(effect_size=effect_size_std,
                                                power=.80,
                                                alpha=.05,
                                                nobs1=None)
print(sample_size)
3057.547
A/B Testing in Python

Effect of sample size and MDE on power

# Import t-test power package
from statsmodels.stats.power import TTestIndPower
# Specify parameters for power analysis
sample_sizes = array(range(10, 120))
effect_sizes = array([0.2, 0.5, 0.8])
# Plot power curves
TTestIndPower().plot_power(nobs=sample_sizes, effect_size=effect_sizes)
plt.show()

Power curves plot showing how the sample size changes with power and effect size

A/B Testing in Python

Sample size estimation for means

# Calculate the baseline mean order value
mean_A = checkout[checkout['checkout_page']=='A']['order_value'].mean()
print(mean_A)
24.9564
std_A = checkout[checkout['checkout_page']=='A']['order_value'].std()
print(std_A)
2.418
# Specify the desired minimum average order value
mean_new = 26
# Calculate the standardized effect size
std_effect_size=(mean_new-mean_A)/std_A
A/B Testing in Python

Sample size estimation for means

sample_size = power.TTestIndPower().solve_power(effect_size=std_effect_size,
                                                power=.80,
                                                alpha=.05,
                                                nobs1=None)
print(sample_size)
85.306
A/B Testing in Python

Let's practice!

A/B Testing in Python

Preparing Video For Download...