Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tools/gen/protocol/genstats.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tools / gen / protocol / genstats.c` 450 lines ยท 24848 bytes Raw Blame History 1 /* 2 * genstats.c -- reference statistics of the GEN decision rule (aien-spark/omega#476, 3 * docs/one_layer/GEN_PROTOCOL_V1.md section 9). Writes tests/one_layer/generalization/stats_vectors.tsv, 4 * the test vectors the GEN reducer (tools/gen/reducer, omega#477) must reproduce. 5 * 6 * genstats wilson <95|90> "p lo hi" 7 * genstats mcnemar1 "p_num p_den p": exact one-sided McNemar, 8 * P(Bin(b+c, 1/2) >= b) = p_num / p_den, p_den = 2^(b+c) 9 * genstats newcombe <95|90> [8|10] 10 * "theta lo hi": Newcombe (1998, Stat Med 17:2635) paired 11 * interval for p1 - p2, method 10 (default) or 8; a = both, 12 * b = first only, c = second only, d = neither 13 * genstats verdict the full decision from four 0/1 strings (one char per task, n = 60 only); 14 * order INVALID > floor/ceiling INCONCLUSIVE > PASS > FAIL > INCONCLUSIVE 15 * genstats lenmatch :... length-matched ablation classes (section 10): per task gap, 16 * tolerance, matched|unmatched; then matched count and status 17 * genstats vectors print the whole stats_vectors.tsv 18 * genstats selftest check Newcombe methods 8 and 10 against every row of the 19 * paper's Table III (4 decimals), and internal identities 20 * Exact integer arithmetic for McNemar (unsigned __int128, b + c <= 120); IEEE double elsewhere. 21 * Deterministic: no clock, no randomness, no environment. 22 */ 23 #include 24 #include 25 #include 26 #include 27 28 typedef unsigned __int128 u128; 29 30 /* standard normal quantiles, pinned (GEN_PROTOCOL_V1 section 9) */ 31 #define Z95 1.`959963984540054` /* z(0.975): two-sided 95% */ 32 #define Z90 1.`644853626951472` /* z(0.95): two-sided 90% */ 33 34 static double zof(int conf) { return conf == 95 ? Z95 : Z90; } 35 36 /* Wilson score interval without continuity correction; exact 0 / 1 at k = 0 / k = n */ 37 static void wilson(long k, long n, double z, double *p, double *lo, double *hi) 38 { 39 double pp = (double)k / (double)n, z2 = z * z, nn = (double)n, den = 1 + z2 / nn; 40 double c = (pp + z2 / (2 * nn)) / den, h = z * sqrt(pp * (1 - pp) / nn + z2 / (4 * nn * nn)) / den; 41 *p = pp; 42 *lo = k == 0 ? 0.0 : c - h; 43 *hi = k == n ? 1.0 : c + h; 44 } 45 46 static void u128str(u128 v, char *out) 47 { 48 char t[64]; 49 int n = 0; 50 do { t[n++] = (char)('0' + (int)(v % 10)); v /= 10; } while (v); 51 for (int i = 0; i < n; i++) out[i] = t[n - 1 - i]; 52 out[n] = 0; 53 } 54 55 /* P(Y >= b), Y ~ Bin(b + c, 1/2), as num / 2^(b+c) */ 56 static void mcnemar1(int b, int c, u128 *num, u128 *den) 57 { 58 int n = b + c; 59 u128 s = 0, binom = 1; /* binom = C(n, i) */ 60 for (int i = 0; i <= n; i++) { 61 if (i >= b) s += binom; 62 binom = binom * (u128)(n - i) / (u128)(i + 1); 63 } 64 *num = s; 65 *den = (u128)1 << n; 66 } 67 68 /* Newcombe paired interval for theta = (b - c) / n. method 10: phi numerator a*d - b*c replaced by 69 * max(a*d - b*c - n/2, 0) when a*d > b*c; method 8: plain phi. phi = 0 when any marginal is 0. */ 70 static void newcombe(long a, long b, long c, long d, double z, int method, double *theta, double *lo, double *hi) 71 { 72 long n = a + b + c + d; 73 double p1, l1, u1, p2, l2, u2; 74 wilson(a + b, n, z, &p1, &l1, &u1); 75 wilson(a + c, n, z, &p2, &l2, &u2); 76 double num = (double)a * (double)d - (double)b * (double)c; 77 if (method == 10 && num > 0) { num -= (double)n / 2.0; if (num < 0) num = 0; } 78 double dprod = (double)(a + b) * (double)(c + d) * (double)(a + c) * (double)(b + d); 79 double phi = dprod == 0 ? 0.0 : num / sqrt(dprod); 80 double dl1 = p1 - l1, du1 = u1 - p1, dl2 = p2 - l2, du2 = u2 - p2; 81 double s1 = dl1 * dl1 - 2 * phi * dl1 * du2 + du2 * du2; 82 double s2 = du1 * du1 - 2 * phi * du1 * dl2 + dl2 * dl2; 83 double delta = sqrt(s1 > 0 ? s1 : 0), eps = sqrt(s2 > 0 ? s2 : 0); 84 *theta = (double)(b - c) / (double)n; 85 *lo = *theta - delta; 86 *hi = *theta + eps; 87 if (*lo < -1) *lo = -1; 88 if (*hi > 1) *hi = 1; 89 } 90 91 /* ---- the decision ---------------------------------------------------------------------------- */ 92 93 typedef struct { 94 int n, s[4]; /* successes of A B C D */ 95 int both[3], donly[3], xonly[3], neither[3]; 96 char pnum[3][48], pden[3][48]; 97 double p[3]; 98 int lift_ok[3], p_ok[3]; 99 double lo95, hi95, lo90, hi90; /* Newcombe method 10, D - C */ 100 int a_minus_b; 101 int floor, ceiling; 102 const char *verdict; 103 char reasons[256]; 104 } Decision; 105 106 static int decide(int valid, char *const v[4], Decision *r, char *err, size_t errn) 107 { 108 memset(r, 0, sizeof *r); 109 size_t n = strlen(v[0]); 110 if (n != 60) { snprintf(err, errn, "vector length %zu, the verdict is defined for n = 60 only (GEN_PROTOCOL_V1 section 9)", n); return -1; } 111 for (int x = 0; x < 4; x++) { 112 if (strlen(v[x]) != n) { snprintf(err, errn, "vectors differ in length (incomplete paired matrix)"); return -1; } 113 for (size_t i = 0; i < n; i++) if (v[x][i] != '0' && v[x][i] != '1') { snprintf(err, errn, "vector %c holds a byte other than 0/1", "ABCD"[x]); return -1; } 114 } 115 r->n = (int)n; 116 for (int x = 0; x < 4; x++) for (size_t i = 0; i < n; i++) r->s[x] += v[x][i] == '1'; 117 for (int x = 0; x < 3; x++) { 118 for (size_t i = 0; i < n; i++) { 119 int d = v[3][i] == '1', o = v[x][i] == '1'; 120 if (d && o) r->both[x]++; else if (d) r->donly[x]++; else if (o) r->xonly[x]++; else r->neither[x]++; 121 } 122 u128 num, den; 123 mcnemar1(r->donly[x], r->xonly[x], &num, &den); 124 u128str(num, r->pnum[x]); 125 u128str(den, r->pden[x]); 126 r->p[x] = (double)num / (double)den; 127 r->p_ok[x] = (u128)20 * num < den; /* p < 0.05, exactly */ 128 r->lift_ok[x] = 10 * (r->donly[x] - r->xonly[x]) >= r->n; /* (d_only - x_only) / n >= 0.10, exactly */ 129 } 130 double th; 131 newcombe(r->both[2], r->donly[2], r->xonly[2], r->neither[2], Z95, 10, &th, &r->lo95, &r->hi95); 132 newcombe(r->both[2], r->donly[2], r->xonly[2], r->neither[2], Z90, 10, &th, &r->lo90, &r->hi90); 133 r->a_minus_b = r->s[0] - r->s[1]; 134 int mx = r->s[0], mn = r->s[0]; 135 for (int x = 1; x < 4; x++) { if (r->s[x] > mx) mx = r->s[x]; if (r->s[x] < mn) mn = r->s[x]; } 136 r->floor = mx <= 3; /* max successes over A..D <= 3 */ 137 r->ceiling = mn >= r->n - 3; /* min successes over A..D >= n - 3 (57 at n = 60) */ 138 int pass = 1; 139 for (int x = 0; x < 3; x++) pass &= r->lift_ok[x] && r->p_ok[x]; 140 if (!valid || r->a_minus_b > 2) { 141 r->verdict = "INVALID"; 142 snprintf(r->reasons, sizeof r->reasons, "%s%s%s", !valid ? "validity_failure" : "", !valid && r->a_minus_b > 2 ? "," : "", 143 r->a_minus_b > 2 ? "sanity_a_minus_b_gt_2" : ""); 144 } else if (r->floor || r->ceiling) { 145 r->verdict = "INCONCLUSIVE"; /* the tasks were too hard or too easy to compare conditions */ 146 snprintf(r->reasons, sizeof r->reasons, "%s%s%s", r->floor ? "floor" : "", r->floor && r->ceiling ? "," : "", r->ceiling ? "ceiling" : ""); 147 } else if (pass) { 148 r->verdict = "PASS"; 149 snprintf(r->reasons, sizeof r->reasons, "all_controls_lift_and_p"); 150 } else if (r->hi95 < 0.10) { /* the full double, never the 6-decimal receipt value */ 151 r->verdict = "FAIL"; 152 snprintf(r->reasons, sizeof r->reasons, "newcombe95_hi_D-C_below_delta"); 153 } else { 154 r->verdict = "INCONCLUSIVE"; 155 int k = 0; 156 for (int x = 0; x < 3; x++) { 157 int w = 0; 158 if (!r->lift_ok[x]) w = snprintf(r->reasons + k, sizeof r->reasons - (size_t)k, "%slift_%c", k ? "," : "", "ABC"[x]); 159 if (w < 0 || (size_t)w >= sizeof r->reasons - (size_t)k) break; 160 k += w; 161 w = 0; 162 if (!r->p_ok[x]) w = snprintf(r->reasons + k, sizeof r->reasons - (size_t)k, "%sp_%c", k ? "," : "", "ABC"[x]); 163 if (w < 0 || (size_t)w >= sizeof r->reasons - (size_t)k) break; 164 k += w; 165 } 166 } 167 return 0; 168 } 169 170 /* ---- vectors --------------------------------------------------------------------------------- */ 171 172 /* a verdict case: groups of (ABCD pattern, count), concatenated in order */ 173 typedef struct { const char *id; int valid; const char *groups; const char *note; } VCase; 174 175 static const VCase vcases[] = { 176 {"v01_pass", 1, "1111:18 0001:16 0011:4 0111:2 0000:20", "D far ahead of every control"}, 177 {"v02_fail_equal", 1, "1111:20 0010:3 0001:3 0000:34", "D = C exactly; upper bound below 10pp"}, 178 {"v03_inconclusive_small_lift", 1, "1111:20 0011:5 0001:4 0010:1 0000:30", "D - C = 3 tasks: neither PASS nor FAIL"}, 179 {"v04_inconclusive_c_p", 1, "1111:15 0011:10 0001:8 0010:2 0000:25", "lift >= 10pp vs C (6 tasks) but p vs C = 0.0547"}, 180 {"v05_invalid_validity", 0, "1111:18 0001:16 0011:4 0111:2 0000:20", "v01 with a validity failure"}, 181 {"v06_invalid_sanity", 1, "1111:15 1001:3 0011:1 0001:20 0000:21", "A - B = 3 tasks"}, 182 {"v07_sanity_boundary_pass", 1, "1111:15 1011:2 0001:20 0000:23", "A - B = 2 tasks: allowed"}, 183 {"v08_boundary_lift6_p", 1, "1111:20 0001:6 0000:34", "d_only = 6, x_only = 0 vs every control: lift exactly 10pp, p = 1/64"}, 184 {"v09_boundary_lift5", 1, "1111:20 0001:5 0000:35", "lift 5/60 < 10pp vs every control"}, 185 {"v10_boundary_8_2", 1, "1111:20 0001:8 1110:2 0000:30", "d_only 8, x_only 2: lift 10pp, p = 56/1024 = 0.0547 (not < 0.05)"}, 186 {"v11_boundary_7_1", 1, "1111:20 0001:7 1110:1 0000:32", "d_only 7, x_only 1: lift 10pp, p = 9/256"}, 187 {"v12_all_fail", 1, "0000:60", "nothing succeeds: INCONCLUSIVE floor (was FAIL before the floor rule)"}, 188 {"v13_all_pass", 1, "1111:60", "everything succeeds: INCONCLUSIVE ceiling"}, 189 {"v14_d_worse", 1, "1111:20 1110:8 0001:1 0000:31", "D behind every control"}, 190 /* v15 (n = 10, dev size) removed 2026-10-16: the verdict is defined for n = 60 only; other n stay in the CI rows */ 191 {"v16_pass_vs_ab_not_c", 1, "1111:10 0011:12 0001:4 0000:34", "D ahead of A and B, tied-ish with C"}, 192 {"v17_floor_boundary", 1, "0001:3 0000:57", "max successes = 3 (D): INCONCLUSIVE floor"}, 193 {"v18_floor_above", 1, "0001:4 0000:56", "max successes = 4: not floor (INCONCLUSIVE on lift and p)"}, 194 {"v19_ceiling_boundary", 1, "1111:57 1110:3", "min successes = 57 (D): INCONCLUSIVE ceiling"}, 195 {"v20_ceiling_below", 1, "1111:56 1110:4", "min successes = 56: not ceiling"}, 196 {"v21_floor_overrides_fail", 1, "1111:2 0000:58", "everything at 2 successes: floor, not FAIL"}, 197 }; 198 199 static int build(const char *groups, char *v[4]) 200 { 201 static char buf[4][128]; 202 int n = 0; 203 const char *s = groups; 204 while (*s) { 205 char pat[5]; 206 int cnt, used; 207 if (sscanf(s, "%4[01]:%d%n", pat, &cnt, &used) != 2 || strlen(pat) != 4) return -1; 208 for (int i = 0; i < cnt; i++) { if (n >= 120) return -1; for (int x = 0; x < 4; x++) buf[x][n] = pat[x]; n++; } 209 s += used; 210 while (*s == ' ') s++; 211 } 212 for (int x = 0; x < 4; x++) { buf[x][n] = 0; v[x] = buf[x]; } 213 return n; 214 } 215 216 static void print_decision_cols(const Decision *r) 217 { 218 printf("%d\t%d\t%d\t%d", r->s[0], r->s[1], r->s[2], r->s[3]); 219 for (int x = 0; x < 3; x++) 220 printf("\t%d\t%d\t%d\t%d\t%s\t%s\t%.10f\t%s\t%s", r->both[x], r->donly[x], r->xonly[x], r->neither[x], r->pnum[x], r->pden[x], r->p[x], 221 r->lift_ok[x] ? "yes" : "no", r->p_ok[x] ? "yes" : "no"); 222 printf("\t%.10f\t%.10f\t%.10f\t%.10f\t%d\t%s\t%s", r->lo95, r->hi95, r->lo90, r->hi90, r->a_minus_b, r->verdict, r->reasons); 223 } 224 225 /* Table III of Newcombe (1998): e f g h, method 8 lo hi, method 10 lo hi (95%, 4 decimals as printed) */ 226 static const double table3[][8] = { 227 {36, 12, 2, 0, 0.0569, 0.3404, 0.0569, 0.3404}, 228 {20, 12, 2, 16, 0.0618, 0.3242, 0.0562, 0.3292}, 229 {18, 12, 2, 18, 0.0618, 0.3239, 0.0562, 0.3290}, 230 {36, 14, 0, 0, 0.1528, 0.4167, 0.1528, 0.4167}, 231 {35, 14, 0, 1, 0.1573, 0.4149, 0.1461, 0.4175}, 232 {18, 14, 0, 18, 0.1504, 0.3910, 0.1441, 0.3963}, 233 {2, 97, 1, 0, 0.8721, 0.9854, 0.8721, 0.9854}, 234 {1, 97, 1, 1, 0.8737, 0.9850, 0.8736, 0.9850}, 235 {0, 29, 1, 0, 0.6666, 0.9882, 0.6666, 0.9882}, 236 {2, 98, 0, 0, 0.9178, 0.9945, 0.9178, 0.9945}, 237 {1, 98, 0, 1, 0.9174, 0.9916, 0.9171, 0.9916}, 238 {0, 30, 0, 0, 0.8395, 1.0, 0.8395, 1.0}, 239 {54, 0, 0, 0, -0.0664, 0.0664, -0.0664, 0.0664}, 240 {53, 0, 0, 1, -0.0640, 0.0640, -0.0729, 0.0729}, 241 {30, 0, 0, 24, -0.0074, 0.0074, -0.0358, 0.0358}, 242 {29, 0, 0, 25, -0.0049, 0.0049, -0.0354, 0.0354}, 243 {28, 0, 0, 26, -0.0025, 0.0025, -0.0352, 0.0352}, 244 {27, 0, 0, 27, 0.0, 0.0, -0.0351, 0.0351}, 245 }; 246 #define NT3 (sizeof table3 / sizeof table3[0]) 247 248 /* ---- length-matched ablation (GEN_PROTOCOL_V1 section 10; dev-only diagnostic, never in the verdict) ---- 249 * gap = padded C suffix_bytes - target_bytes (D's suffix_bytes), signed (genret length_gap). 250 * A task is matched iff |gap| <= max(256 bytes, 15% of target), exactly: 100 |gap| <= max(25600, 15 target). 251 * A set is AVAILABLE iff at least half its tasks are matched (2 matched >= tasks), else UNAVAILABLE. */ 252 static int lm_matched(long target, long suffix, long *gap) 253 { 254 *gap = suffix - target; 255 long ag = *gap < 0 ? -*gap : *gap, lim = 15 * target > 25600 ? 15 * target : 25600; 256 return 100 * ag <= lim; 257 } 258 static const char *lm_status(int matched, int tasks) { return 2 * matched >= tasks ? "AVAILABLE" : "UNAVAILABLE"; } 259 /* tolerance in hundredths of a byte: max(25600, 15 target) */ 260 static void lm_tol(long target, char *out, size_t n) 261 { 262 long t = 15 * target > 25600 ? 15 * target : 25600; 263 snprintf(out, n, "%ld.%02ld", t / 100, t % 100); 264 } 265 266 static int vectors(void) 267 { 268 printf("# GEN decision statistics test vectors v2 (aien-spark/omega#476). Generated by tools/gen/protocol/genstats.c\n"); 269 printf("# (`genstats vectors`); never edit by hand. Rules: docs/one_layer/GEN_PROTOCOL_V1.md sections 9 and 10.\n"); 270 printf("# z95 = %.15f (z 0.975), z90 = %.15f (z 0.95). Reals printed %%.10f; a reader must agree within 1e-9,\n", Z95, Z90); 271 printf("# and its 6-decimal receipt values must equal these rounded to 6 decimals. Counts, p_num, p_den, yes/no and verdicts exactly.\n"); 272 printf("# One record per line, TAB-separated; field 1 names the record kind; each kind has a #cols line.\n"); 273 printf("# Verdict order: INVALID (validity, A - B > 2) > INCONCLUSIVE floor (max succ <= 3) / ceiling (min succ >= n - 3) > PASS > FAIL\n"); 274 printf("# (D-C newcombe95_hi < 0.10, compared on the full double) > INCONCLUSIVE. v2 = v1 + the floor/ceiling rule and the length-match records (review of PR #486).\n"); 275 printf("#cols\twilson\tid\tk\tn\tconf\tp\tlo\thi\n"); 276 static const int wk[][2] = {{0, 60}, {1, 60}, {6, 60}, {20, 60}, {30, 60}, {48, 60}, {59, 60}, {60, 60}, {3, 10}, {0, 10}, {10, 10}}; 277 for (size_t i = 0; i < sizeof wk / sizeof wk[0]; i++) 278 for (int conf = 95; conf >= 90; conf -= 5) { 279 double p, lo, hi; 280 wilson(wk[i][0], wk[i][1], zof(conf), &p, &lo, &hi); 281 printf("wilson\tw%02zu_%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\n", i + 1, conf, wk[i][0], wk[i][1], conf, p, lo, hi); 282 } 283 printf("#cols\tmcnemar1\tid\tb\tc\tp_num\tp_den\tp\tp_lt_0.05\n"); 284 static const int mc[][2] = {{0, 0}, {1, 0}, {0, 1}, {4, 0}, {5, 0}, {6, 0}, {7, 1}, {8, 2}, {9, 3}, {10, 4}, {12, 6}, {3, 9}, {15, 15}, {30, 30}, {60, 0}, {0, 60}, {16, 2}, {45, 15}}; 285 for (size_t i = 0; i < sizeof mc / sizeof mc[0]; i++) { 286 u128 num, den; 287 char ns[48], ds[48]; 288 mcnemar1(mc[i][0], mc[i][1], &num, &den); 289 u128str(num, ns); 290 u128str(den, ds); 291 printf("mcnemar1\tm%02zu\t%d\t%d\t%s\t%s\t%.10f\t%s\n", i + 1, mc[i][0], mc[i][1], ns, ds, (double)num / (double)den, (u128)20 * num < den ? "yes" : "no"); 292 } 293 printf("#cols\tnewcombe\tid\ta_both\tb_first_only\tc_second_only\td_neither\tconf\tmethod\ttheta\tlo\thi\tsource\n"); 294 int id = 0; 295 for (size_t i = 0; i < NT3; i++) { 296 double th, lo, hi; 297 newcombe((long)table3[i][0], (long)table3[i][1], (long)table3[i][2], (long)table3[i][3], Z95, 10, &th, &lo, &hi); 298 printf("newcombe\tn%02d\t%.0f\t%.0f\t%.0f\t%.0f\t95\t10\t%.10f\t%.10f\t%.10f\tNewcombe1998_TableIII_published_%.4f_%.4f\n", ++id, 299 table3[i][0], table3[i][1], table3[i][2], table3[i][3], th, lo, hi, table3[i][6], table3[i][7]); 300 } 301 static const int gc[][4] = {{20, 6, 0, 34}, {20, 8, 2, 30}, {20, 3, 3, 34}, {0, 0, 0, 60}, {60, 0, 0, 0}, {25, 12, 6, 17}, {20, 4, 1, 35}, {3, 4, 0, 3}}; 302 for (size_t i = 0; i < sizeof gc / sizeof gc[0]; i++) 303 for (int conf = 95; conf >= 90; conf -= 5) { 304 double th, lo, hi; 305 newcombe(gc[i][0], gc[i][1], gc[i][2], gc[i][3], zof(conf), 10, &th, &lo, &hi); 306 printf("newcombe\tn%02d\t%d\t%d\t%d\t%d\t%d\t10\t%.10f\t%.10f\t%.10f\tgen\n", ++id, gc[i][0], gc[i][1], gc[i][2], gc[i][3], conf, th, lo, hi); 307 } 308 printf("#cols\tlenmatch\tid\ttarget_bytes\tsuffix_bytes\tlength_gap\ttolerance_bytes\tclass\n"); 309 static const long lmv[][2] = {{295, 945}, {300, 738}, {300, 556}, {300, 557}, {300, 44}, {300, 43}, 310 {2000, 2300}, {2000, 2301}, {2000, 1700}, {2000, 1699}, {1000, 1000}, {1706, 1962}}; 311 for (size_t i = 0; i < sizeof lmv / sizeof lmv[0]; i++) { 312 long gap; 313 char tol[32]; 314 int m = lm_matched(lmv[i][0], lmv[i][1], &gap); 315 lm_tol(lmv[i][0], tol, sizeof tol); 316 printf("lenmatch\tl%02zu\t%ld\t%ld\t%ld\t%s\t%s\n", i + 1, lmv[i][0], lmv[i][1], gap, tol, m ? "matched" : "unmatched"); 317 } 318 printf("#cols\tlenset\tid\ttasks\tmatched\tunmatched\tstatus\n"); 319 static const int lsv[][2] = {{10, 10}, {10, 5}, {10, 4}, {10, 0}, {9, 5}, {9, 4}, {1, 1}}; 320 for (size_t i = 0; i < sizeof lsv / sizeof lsv[0]; i++) 321 printf("lenset\ts%02zu\t%d\t%d\t%d\t%s\n", i + 1, lsv[i][0], lsv[i][1], lsv[i][0] - lsv[i][1], lm_status(lsv[i][1], lsv[i][0])); 322 printf("#cols\tverdict\tid\tvalid\tvec_A\tvec_B\tvec_C\tvec_D\tsucc_A\tsucc_B\tsucc_C\tsucc_D"); 323 for (int x = 0; x < 3; x++) 324 printf("\tD-%c.both\tD-%c.d_only\tD-%c.x_only\tD-%c.neither\tD-%c.p_num\tD-%c.p_den\tD-%c.p\tD-%c.lift_ok\tD-%c.p_ok", "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x], "ABC"[x]); 325 printf("\tD-C.newcombe95_lo\tD-C.newcombe95_hi\tD-C.newcombe90_lo\tD-C.newcombe90_hi\ta_minus_b\tverdict\treasons\n"); 326 for (size_t i = 0; i < sizeof vcases / sizeof vcases[0]; i++) { 327 char *v[4]; 328 if (build(vcases[i].groups, v) < 0) { fprintf(stderr, "genstats: bad case %s\n", vcases[i].id); return 1; } 329 Decision r; 330 char err[160]; 331 if (decide(vcases[i].valid, v, &r, err, sizeof err)) { fprintf(stderr, "genstats: %s: %s\n", vcases[i].id, err); return 1; } 332 printf("verdict\t%s\t%d\t%s\t%s\t%s\t%s\t", vcases[i].id, vcases[i].valid, v[0], v[1], v[2], v[3]); 333 print_decision_cols(&r); 334 putchar('\n'); 335 } 336 return 0; 337 } 338 339 static int selftest(void) 340 { 341 int bad = 0; 342 for (size_t i = 0; i < NT3; i++) 343 for (int m = 0; m < 2; m++) { 344 double th, lo, hi; 345 newcombe((long)table3[i][0], (long)table3[i][1], (long)table3[i][2], (long)table3[i][3], Z95, m ? 10 : 8, &th, &lo, &hi); 346 double plo = table3[i][4 + 2 * m], phi = table3[i][5 + 2 * m]; 347 /* printed to 4 decimals: the exact value must round to the printed one. One known exception: 348 * row 1 97 1 1 has a*d < b*c, so methods 8 and 10 are the same formula, yet the paper prints 349 * lower limits 0.8737 (method 8) and 0.8736 (method 10); the computed 0.873672 matches the method 8 350 * cell, so that single method-10 cell is allowed one unit in the 4th decimal. */ 351 double tol = (m && table3[i][0] == 1 && table3[i][1] == 97 && table3[i][3] == 1) ? 0.0001 + 1e-12 : 0.00005 + 1e-12; 352 if (fabs(lo - plo) > tol || fabs(hi - phi) > 0.00005 + 1e-12) { 353 printf("FAIL newcombe method %d %.0f %.0f %.0f %.0f: got %.6f %.6f, paper %.4f %.4f\n", m ? 10 : 8, table3[i][0], table3[i][1], table3[i][2], table3[i][3], lo, hi, plo, phi); 354 bad++; 355 } 356 } 357 /* McNemar: the one-sided tails of b and of c sum to 1 + P(Y = b) */ 358 for (int b = 0; b <= 30; b++) 359 for (int c = 0; c <= 30; c++) { 360 u128 n1, d1, n2, d2, pb = 1; 361 mcnemar1(b, c, &n1, &d1); 362 mcnemar1(c, b, &n2, &d2); 363 for (int i = 0; i < b; i++) pb = pb * (u128)(b + c - i) / (u128)(i + 1); 364 if (d1 != d2 || n1 + n2 != d1 + pb) { printf("FAIL mcnemar identity b=%d c=%d\n", b, c); bad++; } 365 } 366 /* Wilson: the 95% interval contains the 90% one and the point */ 367 for (int k = 0; k <= 60; k++) { 368 double p, l95, h95, l90, h90; 369 wilson(k, 60, Z95, &p, &l95, &h95); 370 wilson(k, 60, Z90, &p, &l90, &h90); 371 if (!(l95 <= l90 && l90 <= p && p <= h90 && h90 <= h95)) { printf("FAIL wilson nesting k=%d\n", k); bad++; } 372 } 373 printf(bad ? "SELFTEST FAIL (%d)\n" : "SELFTEST PASS (Newcombe Table III: %d rows x methods 8 and 10; McNemar tail identity 31x31; Wilson nesting 61)\n", bad ? bad : (int)NT3); 374 return bad ? 1 : 0; 375 } 376 377 static long num(const char *s, long lo, long hi) 378 { 379 char *e; 380 long v = strtol(s, &e, 10); 381 if (!*s || *e || v < lo || v > hi) { fprintf(stderr, "genstats: '%s' is not an integer in %ld..%ld\n", s, lo, hi); exit(2); } 382 return v; 383 } 384 385 static int conf_arg(const char *s) 386 { 387 if (!strcmp(s, "95")) return 95; 388 if (!strcmp(s, "90")) return 90; 389 fprintf(stderr, "genstats: confidence must be 95 or 90\n"); 390 exit(2); 391 } 392 393 int main(int argc, char **argv) 394 { 395 if (argc == 5 && !strcmp(argv[1], "wilson")) { 396 long n = num(argv[3], 1, `1000000`), k = num(argv[2], 0, n); 397 double p, lo, hi; 398 wilson(k, n, zof(conf_arg(argv[4])), &p, &lo, &hi); 399 printf("%.10f %.10f %.10f\n", p, lo, hi); 400 return 0; 401 } 402 if (argc == 4 && !strcmp(argv[1], "mcnemar1")) { 403 int b = (int)num(argv[2], 0, 120), c = (int)num(argv[3], 0, 120 - b); 404 u128 n, d; 405 char ns[48], ds[48]; 406 mcnemar1(b, c, &n, &d); 407 u128str(n, ns); 408 u128str(d, ds); 409 printf("%s %s %.10f\n", ns, ds, (double)n / (double)d); 410 return 0; 411 } 412 if ((argc == 7 || argc == 8) && !strcmp(argv[1], "newcombe")) { 413 long a = num(argv[2], 0, `1000000`), b = num(argv[3], 0, `1000000`), c = num(argv[4], 0, `1000000`), d = num(argv[5], 0, `1000000`); 414 if (a + b + c + d == 0) { fprintf(stderr, "genstats: n = 0\n"); return 2; } 415 int m = argc == 8 ? (int)num(argv[7], 8, 10) : 10; 416 if (m == 9) { fprintf(stderr, "genstats: method 8 or 10\n"); return 2; } 417 double th, lo, hi; 418 newcombe(a, b, c, d, zof(conf_arg(argv[6])), m, &th, &lo, &hi); 419 printf("%.10f %.10f %.10f\n", th, lo, hi); 420 return 0; 421 } 422 if (argc == 7 && !strcmp(argv[1], "verdict")) { 423 Decision r; 424 char err[160]; 425 if (decide((int)num(argv[2], 0, 1), argv + 3, &r, err, sizeof err)) { fprintf(stderr, "genstats: %s\n", err); return 1; } 426 print_decision_cols(&r); 427 putchar('\n'); 428 return 0; 429 } 430 if (argc >= 3 && !strcmp(argv[1], "lenmatch")) { 431 int matched = 0; 432 for (int i = 2; i < argc; i++) { 433 char *colon = strchr(argv[i], ':'); 434 if (!colon) { fprintf(stderr, "genstats: lenmatch takes : pairs\n"); return 2; } 435 *colon = 0; 436 long t = num(argv[i], 1, `1000000`), s = num(colon + 1, 0, `1000000`), gap; 437 char tol[32]; 438 int m = lm_matched(t, s, &gap); 439 lm_tol(t, tol, sizeof tol); 440 printf("task %d target_bytes=%ld suffix_bytes=%ld length_gap=%ld tolerance_bytes=%s class=%s\n", i - 1, t, s, gap, tol, m ? "matched" : "unmatched"); 441 matched += m; 442 } 443 printf("tasks=%d matched=%d unmatched=%d status=%s\n", argc - 2, matched, argc - 2 - matched, lm_status(matched, argc - 2)); 444 return 0; 445 } 446 if (argc == 2 && !strcmp(argv[1], "vectors")) return vectors(); 447 if (argc == 2 && !strcmp(argv[1], "selftest")) return selftest(); 448 fprintf(stderr, "usage: genstats wilson <95|90> | mcnemar1 | newcombe <95|90> [8|10] | verdict | lenmatch :... | vectors | selftest\n"); 449 return 2; 450 }