File size: 8,271 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
"""Sizing simulation for the second Five Moves test. Built to PLAN.md section 6, nothing more.

It makes up many imaginary studies with a known true reduction of D points, analyses each one
the way section 7 analyses the real study, and reports how often the whole 95% interval lies
below zero ("power"). It is run and frozen before any spare session.

Every draw, in this order, from ONE random.Random(20261122 + R) stream per R:
  for D = 0, 1, ..., 30 (ascending)
    for each of the studies (10,000 unless --studies says otherwise)
      for each task M01..M24 (ascending)
        b = 0.20 + 0.60*random()                    filler probability
        e = -D/100 + 0.10*normalvariate(0,1)        effect on this task
        q = min(0.936, max(0.064, b + e))           Five Moves probability
        for each of the seven models, in roster order
          for arm in filler (p=b), then Five Moves (p=q)
            R=1: one random() < p gives the outcome
            R=2: one uniform u, with d = 0.128, gives the pair of repeats:
                 (1,1) if u < p-d/2, (1,0) if u < p, (0,1) if u < p+d/2, otherwise (0,0)

Analysis of each imaginary study: Five Moves minus filler; repeats averaged within model-task,
models equally within task, tasks equally; two-sided Student-t 95% interval across the 24 task
differences with critical value 2.06865761. Detection = the whole interval is below zero.

Usage:
  python3 sizing.py --repeats 1 > sizing_output_R1.txt
  python3 sizing.py --repeats 2 > sizing_output_R2.txt
  python3 sizing.py                 (prints R=1 then R=2)
  python3 sizing.py --studies 200   (a quick reduced run, for tests only)

Standard output is deterministic; the run time goes to standard error.
"""
import argparse
import math
import random
import sys
import time

SEED_BASE = 20261122
D_VALUES = range(0, 31)                      # reductions in points, integer steps
DEFAULT_STUDIES = 10000
TASKS = ["M%02d" % i for i in range(1, 25)]  # M01..M24, ascending
ROSTER = ["Claude Opus 5.5", "Claude Sonnet 5.5", "GPT-6.2 Sol", "GPT-6 Luna",
          "DeepSeek V4.1 Flash", "Gemini 4 Neon", "Gemini 3.8 Flash"]   # PLAN.md section 2 order
PAIR_D = 0.128                               # "d" in the paired two-repeat draw
T_CRIT = 2.06865761                          # two-sided 95%, 23 degrees of freedom
TARGET_POWER_PERCENT = 80


def interval_below_zero(k_sums, per_task_sessions):
    """Section 7's analysis for one complete imaginary study.

    k_sums: for each task, (number of Five Moves outcomes that are 1) minus (number of filler
    outcomes that are 1), summed over models and repeats. Because every model-task has the same
    number of repeats and every task the same number of models, "average repeats within
    model-task, models equally within task" is exactly k / per_task_sessions for each task.
    Returns (detected, identical): detected is True when the whole 95% interval is below zero;
    identical is True when all task differences are the same, in which case section 7 omits the
    interval and nothing is detected.
    """
    n = len(k_sums)
    s1 = sum(k_sums)
    s2 = sum(k * k for k in k_sums)
    if n * s2 == s1 * s1:                    # exact: true only when every k is the same
        return False, True
    mean = s1 / n / per_task_sessions
    variance = (s2 - s1 * s1 / n) / (n - 1) / (per_task_sessions * per_task_sessions)
    upper = mean + T_CRIT * math.sqrt(variance / n)
    return upper < 0, False


def run(repeats, studies):
    """Return (list of (D, detected count), count of studies with identical differences)."""
    rng = random.Random(SEED_BASE + repeats)
    rnd = rng.random
    normal = rng.normalvariate
    half_d = PAIR_D / 2
    per_task_sessions = len(ROSTER) * repeats
    results = []
    identical_total = 0
    for D in D_VALUES:
        detected = 0
        for _study in range(studies):
            k_sums = []
            for _task in TASKS:
                b = 0.20 + 0.60 * rnd()
                e = -D / 100 + 0.10 * normal(0, 1)
                q = min(0.936, max(0.064, b + e))
                k = 0
                if repeats == 1:
                    for _model in ROSTER:
                        filler = rnd() < b
                        five = rnd() < q
                        k += five - filler
                else:
                    for _model in ROSTER:
                        u = rnd()                      # filler, p = b
                        if u < b - half_d:
                            filler = 2                 # (1,1)
                        elif u < b:
                            filler = 1                 # (1,0)
                        elif u < b + half_d:
                            filler = 1                 # (0,1)
                        else:
                            filler = 0                 # (0,0)
                        u = rnd()                      # Five Moves, p = q
                        if u < q - half_d:
                            five = 2
                        elif u < q:
                            five = 1
                        elif u < q + half_d:
                            five = 1
                        else:
                            five = 0
                        k += five - filler
                k_sums.append(k)
            hit, identical = interval_below_zero(k_sums, per_task_sessions)
            detected += hit
            identical_total += identical
        results.append((D, detected))
    return results, identical_total


def report(repeats, studies):
    results, identical_total = run(repeats, studies)
    lines = []
    lines.append("SECOND FIVE MOVES TEST: SIZING SIMULATION (PLAN.md section 6)")
    lines.append("REPEATS: %d" % repeats)
    lines.append("SEED: %d" % (SEED_BASE + repeats))
    lines.append("STUDIES_PER_D: %d" % studies)
    lines.append("Each imaginary study: %d tasks x %d models x 2 arms (filler, Five Moves) x %d "
                 "repeat%s." % (len(TASKS), len(ROSTER), repeats, "" if repeats == 1 else "s"))
    lines.append("Assumptions: filler probability per task even between 0.20 and 0.80; effect per "
                 "task = -D points with SD 10 points;")
    lines.append("Five Moves probability kept between 0.064 and 0.936" +
                 ("." if repeats == 1 else "; the two repeats of a session are drawn as a pair "
                                           "with d = 0.128."))
    lines.append("Detected = the whole 95%% Student-t interval across the %d task differences "
                 "(critical value %.8f) is below zero." % (len(TASKS), T_CRIT))
    lines.append("")
    lines.append("POWER BY TRUE REDUCTION D (points)")
    smallest = None
    for D, detected in results:
        lines.append("D=%2d: detected in %5d of %d = %6.2f%%"
                     % (D, detected, studies, 100 * detected / studies))
        if smallest is None and 100 * detected >= TARGET_POWER_PERCENT * studies:
            smallest = D
    lines.append("")
    lines.append("Imaginary studies whose %d task differences were all identical "
                 "(interval omitted, counted as not detected): %d" % (len(TASKS), identical_total))
    if smallest is None:
        lines.append("SMALLEST_D_REACHING_80_PERCENT_POWER: NONE (below 80%% at every D through %d)"
                     % D_VALUES[-1])
    else:
        lines.append("SMALLEST_D_REACHING_80_PERCENT_POWER: %d" % smallest)
    return "\n".join(lines)


def main(argv=None):
    ap = argparse.ArgumentParser()
    ap.add_argument("--repeats", type=int, choices=(1, 2), default=None,
                    help="1 or 2; left out, both are printed (R=1 then R=2)")
    ap.add_argument("--studies", type=int, default=DEFAULT_STUDIES,
                    help="imaginary studies per D (the frozen outputs use the default, 10000)")
    a = ap.parse_args(argv)
    if a.studies < 1:
        ap.error("--studies must be at least 1")
    blocks = []
    for repeats in ((a.repeats,) if a.repeats else (1, 2)):
        start = time.time()
        blocks.append(report(repeats, a.studies))
        sys.stderr.write("R=%d run time: %.1f seconds\n" % (repeats, time.time() - start))
    print("\n\n".join(blocks))


if __name__ == "__main__":
    main()