#!/usr/bin/env python3
"""Recipients needed per A/B arm to detect a lift between two rates.
Two-sided test of two proportions, alpha 0.05, power 0.80. Python 3 stdlib only.
Usage: python3 sample_size.py
"""
from math import ceil, sqrt
from statistics import NormalDist

Z_A = NormalDist().inv_cdf(1 - 0.05 / 2)  # 1.96
Z_B = NormalDist().inv_cdf(0.80)          # 0.84


def n_per_arm(p1, p2):
    pbar = (p1 + p2) / 2
    num = Z_A * sqrt(2 * pbar * (1 - pbar)) + Z_B * sqrt(p1 * (1 - p1) + p2 * (1 - p2))
    return ceil(num ** 2 / (p1 - p2) ** 2)


CASES = [
    ("Open rate 43% -> 48% (small subject-line lift)", 0.43, 0.48),
    ("Sample-link click 30% -> 40%", 0.30, 0.40),
    ("Pricing click 10% -> 20%", 0.10, 0.20),
    ("Paid order 5% -> 15% (big offer change)", 0.05, 0.15),
]

if __name__ == "__main__":
    print(f"{'Test':<48}{'n per arm':>10}")
    for label, a, b in CASES:
        print(f"{label:<48}{n_per_arm(a, b):>10}")
