File size: 3,436 Bytes
51b4977 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #!/usr/bin/env python3
"""How small a difference could this study see? A simulation, run before any real session.
It makes up many imaginary studies of the planned size, each with a known true difference,
analyses each one the way analyze.py does (per-task differences, interval across tasks), and
reports how often the interval excludes zero. The "reach" is the smallest true difference
that is detected in about 80 of 100 imaginary studies.
The answer depends on assumptions that cannot be known in advance; they are stated here and
printed with the result:
- control rates differ by task (drawn evenly between LOW and HIGH);
- each subject leans its own way on each task (SD 0.30), the same in both arms;
- the paragraph's true effect differs from task to task (SD 10 points around the mean).
Usage: python3 sizing.py --tasks 19 --subjects 8 --repeats 2 --low 0.2 --high 0.8
"""
import argparse
import math
import random
import statistics
T_CRIT = {8: 2.365, 10: 2.262, 12: 2.201, 14: 2.160, 16: 2.131, 17: 2.120, 18: 2.110, 19: 2.101,
20: 2.093, 21: 2.086, 22: 2.080, 24: 2.069, 30: 2.045, 36: 2.030}
def clip(x):
return min(max(x, 0.0), 1.0)
def detected_share(true_diff, n_task, n_subj, rep, low, high, effect_sd, trials, rng):
hits, widths = 0, []
tc = T_CRIT.get(n_task, 2.1)
for _ in range(trials):
diffs = []
for _t in range(n_task):
base = rng.uniform(low, high)
effect = rng.gauss(true_diff, effect_sd)
total = 0
for _s in range(n_subj):
lean = rng.gauss(0, 0.30)
p_control, p_paragraph = clip(base + lean), clip(base + lean + effect)
for _r in range(rep):
total += (rng.random() < p_paragraph) - (rng.random() < p_control)
diffs.append(total / (n_subj * rep))
mean = statistics.mean(diffs)
half = tc * statistics.stdev(diffs) / math.sqrt(n_task)
widths.append(half)
hits += abs(mean) > half
return hits / trials, statistics.mean(widths)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--tasks", type=int, required=True)
ap.add_argument("--subjects", type=int, required=True)
ap.add_argument("--repeats", type=int, required=True)
ap.add_argument("--low", type=float, required=True)
ap.add_argument("--high", type=float, required=True)
ap.add_argument("--effect-sd", type=float, default=0.10)
ap.add_argument("--trials", type=int, default=2000)
a = ap.parse_args()
rng = random.Random(20261122)
print(f"{a.tasks} tasks x {a.subjects} subjects x {a.repeats} repeats per arm; "
f"control rate per task between {a.low:.0%} and {a.high:.0%}; "
f"effect varies by task with SD {a.effect_sd * 100:.0f} points")
reach = None
for points in (0, 5, 8, 10, 12, 15, 18, 20, 25):
share, half = detected_share(-points / 100, a.tasks, a.subjects, a.repeats, a.low, a.high,
a.effect_sd, a.trials, rng)
print(f" true difference {points:2d} points: detected in {share:.0%} of imaginary studies "
f"(typical interval: plus or minus {half * 100:.1f} points)")
if reach is None and points > 0 and share >= 0.80:
reach = points
print(f"REACH: about {reach} points" if reach else "REACH: more than 25 points")
if __name__ == "__main__":
main()
|