11/36
Part 2 of 6 · The formulaLearn
Put together, the formula is one short function
Here are the pieces in one function, the shape level 1 of the practice asks for.
from math import ceil
from statistics import NormalDist
def sample_size_per_arm(p_baseline, mde, alpha=0.05, power=0.8):
# absolute, not relative
p_treat = p_baseline + mde
# two-sided: 1.96
z_alpha = NormalDist().inv_cdf(1 - alpha / 2)
# 0.84 at 80% power
z_power = NormalDist().inv_cdf(power)
variance = p_baseline * (1 - p_baseline) + p_treat * (1 - p_treat)
return ceil((z_alpha + z_power) ** 2 * variance / mde ** 2)
print(sample_size_per_arm(0.10, 0.02))
print(sample_size_per_arm(0.10, 0.01))
print(sample_size_per_arm(0.10, 0.02, power=0.9))
10% to 12% needs 3,839 per arm. About how many does a 1-point lift, 10% to 11%, need?
Pick one to see what it prints.
Pick an answer above to see the rest