# How often does a per-cell test find a "significant" difference when there is none? 10,000 simulated studies with
# the cells.csv design: 3 experiments x 2 conditions x 25 cells, no treatment effect, experiment SD 0.08 and
# condition-within-experiment SD 0.08 on the Fisher z scale, cell SD 0.15. Python 3.13, NumPy, SciPy.
import numpy as np
from scipy import stats
rng = np.random.default_rng(20261111); R = 10_000
def study(n_exp, effect=0.0, n_cells=25):
    ue = rng.normal(0, 0.08, n_exp)[:, None, None]; uc = rng.normal(0, 0.08, (n_exp, 2, 1))
    z = np.arctanh(0.6) + ue + uc + np.array([0, effect])[None, :, None] + rng.normal(0, 0.15, (n_exp, 2, n_cells))
    p_cell = stats.ttest_ind(z[:, 1].ravel(), z[:, 0].ravel(), equal_var=False).pvalue
    m = z.mean(2); p_exp = stats.ttest_rel(m[:, 1], m[:, 0]).pvalue
    return p_cell < 0.05, p_exp < 0.05
for n_exp in (3, 6):
    for eff, lab in ((0.0, "no effect (false positives)"), (0.2, "effect 0.2 on the z scale (power)")):
        r = np.array([study(n_exp, eff) for _ in range(R)])
        print(f"{n_exp} experiments, {lab}: per-cell test {r[:, 0].mean():.1%}; paired test on experiment means {r[:, 1].mean():.1%}")
