# Power for the treatment x sex interaction in a balanced 2 x 2 design (two-sided alpha 0.05, t-test on the
# interaction contrast). The contrast (male effect - female effect) has standard error 2 * SD / sqrt(n), where n is
# the number of mice per sex per treatment, twice that of the treatment effect averaged over sexes.
# Example: SD 12 g; the drug adds 24 g in males and nothing in females (difference 24 g). Python 3.13, SciPy.
import numpy as np
from scipy import stats
sd, diff = 12, 24
for n in range(5, 13):
    df = 4 * n - 4; ncp = diff / (2 * sd / np.sqrt(n)); tc = stats.t.ppf(0.975, df)
    power = stats.nct.sf(tc, df, ncp) + stats.nct.cdf(-tc, df, ncp)
    print(f"n = {n} per sex per treatment ({4 * n} mice): power {power:.1%}")
