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
omega-mv / tools / estimation / est6dev.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* ESTIMATION v6 pre-freeze development analysis (docs/estimation/protocols/est-v6.md section 9 |
| 18 | * items 1 and 4; appendix est-v6-appendix-prefreeze.md). NOT the v6 tool, not a result about any |
| 19 | * held-out data, and not a protocol tool: it changes no v5 threshold, parameter or receipt. |
| 20 | * |
| 21 | * est6dev g1pit --raw R --marks M --dyn D --q Q --lam L --nu N --c C [--boot B] [--seed S] [--blocks] |
| 22 | * One-step G1 (est_v4.c, the v5 code, unchanged) with the GIVEN parameters on one run folder. |
| 23 | * Prints the v5 statistics it must reproduce (n, PIT bin shares, coverage 50/80/95, mean z, |
| 24 | * lag-1 z), the lower and upper 2.5 % miss rates separately, and a block bootstrap over load |
| 25 | * segments (a block is one marked load segment, or one idle stretch between marked segments) |
| 26 | * giving standard errors, percentile intervals and the design effect against independent |
| 27 | * steps for every bin share and miss rate. |
| 28 | * est6dev sched <seed> <seconds> |
| 29 | * The est_load schedule for a seed (same splitmix64 draw order as est_load.c) with per-level |
| 30 | * totals and the section 3 balance check (each of idle, L6, L12, L18 within 400..900 s). |
| 31 | * |
| 32 | * Refuses, before opening anything, any path that names a held-out run (v3, v4, v5 or v6 held-out |
| 33 | * tag) or the v5 D2 folder; after loading, refuses an input whose SHA-256 is the v5 D2 raw or marks SHA (committed d2.sha256). */ |
| 34 | #include <errno.h> |
| 35 | #include <limits.h> |
| 36 | #include <math.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #include "est3c_common.h" |
| 42 | #include "est_v4.h" |
| 43 | |
| 44 | /* Allocation failure stops the tool with a message (no partial result is written). */ |
| 45 | static void *xalloc(void *p, const char *what) |
| 46 | { |
| 47 | if (!p) { fprintf(stderr, "est6dev: out of memory (%s)\n", what); exit(2); } |
| 48 | return p; |
| 49 | } |
| 50 | #define XMALLOC(n) xalloc(malloc(n), __func__) |
| 51 | #define XCALLOC(n, s) xalloc(calloc((n), (s)), __func__) |
| 52 | |
| 53 | #define D2_DATE_TAG "20261005T112838Z" |
| 54 | #define D2_RAW_SHA "7dda21f664587c4401326c3082f28039e133ba5627c8564678f259e2d7036ff0" |
| 55 | #define D2_MARKS_SHA "95879ed909b8fec1207695dc1093b24be56f00563cd364c62bd30007b0dc26bb" |
| 56 | |
| 57 | static uint64_t splitmix64(uint64_t *s) |
| 58 | { |
| 59 | uint64_t z = (*s += 0x9e3779b97f4a7c15ull); |
| 60 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; |
| 61 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; |
| 62 | return z ^ (z >> 31); |
| 63 | } |
| 64 | |
| 65 | static int forbidden_path(const char *path) |
| 66 | { |
| 67 | static const char *tags[] = { "-heldout-", D2_DATE_TAG, "-est5-heldout", "-est4-heldout", "-est3c-heldout", "-est6-heldout", NULL }; |
| 68 | char rp[PATH_MAX]; |
| 69 | for (int i = 0; tags[i]; i++) { |
| 70 | if (strstr(path, tags[i])) return 1; |
| 71 | if (realpath(path, rp) && strstr(rp, tags[i])) return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* ------------------------------------------------------------ schedule (item 4) */ |
| 77 | static int cmd_sched(int argc, char **argv) |
| 78 | { |
| 79 | if (argc != 2) { fprintf(stderr, "usage: est6dev sched <seed> <seconds>\n"); return 2; } |
| 80 | uint64_t seed = strtoull(argv[0], NULL, 0); |
| 81 | long total = strtol(argv[1], NULL, 10); |
| 82 | if (total <= 0) { fprintf(stderr, "seconds must be > 0\n"); return 2; } |
| 83 | static const int levels[4] = { 0, 6, 12, 18 }; |
| 84 | long secs_by_level[4] = { 0, 0, 0, 0 }; |
| 85 | long acc = 0; int n = 0; |
| 86 | uint64_t s = seed; |
| 87 | printf("seed %llu seconds %ld\n", (unsigned long long)seed, total); |
| 88 | while (acc < total && n < 4096) { |
| 89 | int li = (int)(splitmix64(&s) % 4u); |
| 90 | long d = 20 + (long)(splitmix64(&s) % 101u); |
| 91 | if (acc + d > total) d = total - acc; |
| 92 | printf("schedule %d %d %ld\n", n, levels[li], d); |
| 93 | secs_by_level[li] += d; acc += d; n++; |
| 94 | } |
| 95 | int ok = 1; |
| 96 | for (int i = 0; i < 4; i++) if (secs_by_level[i] < 400 || secs_by_level[i] > 900) ok = 0; |
| 97 | printf("segments %d idle %ld L6 %ld L12 %ld L18 %ld\n", n, secs_by_level[0], secs_by_level[1], secs_by_level[2], secs_by_level[3]); |
| 98 | printf("balance %s\n", ok ? "PASS" : "FAIL"); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* first seed at or after <seed> (step +1) whose schedule passes the section 3 balance rule */ |
| 103 | static int balance_ok(uint64_t seed, long total, long lv[4], int *nseg) |
| 104 | { |
| 105 | static const int levels[4] = { 0, 6, 12, 18 }; |
| 106 | uint64_t s = seed; long acc = 0; int n = 0, ok = 1; |
| 107 | for (int i = 0; i < 4; i++) lv[i] = 0; |
| 108 | while (acc < total && n < 4096) { |
| 109 | int li = (int)(splitmix64(&s) % 4u); |
| 110 | long d = 20 + (long)(splitmix64(&s) % 101u); |
| 111 | if (acc + d > total) d = total - acc; |
| 112 | lv[li] += d; acc += d; n++; |
| 113 | } |
| 114 | (void)levels; |
| 115 | for (int i = 0; i < 4; i++) if (lv[i] < 400 || lv[i] > 900) ok = 0; |
| 116 | *nseg = n; |
| 117 | return ok; |
| 118 | } |
| 119 | |
| 120 | static int cmd_pick(int argc, char **argv) |
| 121 | { |
| 122 | if (argc != 2) { fprintf(stderr, "usage: est6dev pick <seed> <seconds>\n"); return 2; } |
| 123 | uint64_t seed = strtoull(argv[0], NULL, 0); |
| 124 | long total = strtol(argv[1], NULL, 10), lv[4]; int ns; |
| 125 | for (int k = 0; k < 100000; k++) { |
| 126 | int ok = balance_ok(seed + (uint64_t)k, total, lv, &ns); |
| 127 | printf("seed 0x%llx segments %d idle %ld L6 %ld L12 %ld L18 %ld balance %s\n", (unsigned long long)(seed + (uint64_t)k), ns, lv[0], lv[1], lv[2], lv[3], ok ? "PASS" : "FAIL"); |
| 128 | if (ok) { printf("picked 0x%llx after %d failures\n", (unsigned long long)(seed + (uint64_t)k), k); return 0; } |
| 129 | } |
| 130 | printf("none found\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* ------------------------------------------------------------ schedule rule SR-1 (domain separated) |
| 136 | * est-v6 section 3 as proposed chains "seed + 1" from each declared seed, which sends H1 (0xD6E6C7) and |
| 137 | * H2 (0xD6E6C8) to the same seed 0xD6E6CB (appendix section 3). Rule SR-1 removes that: |
| 138 | * base(label, declared) = splitmix64 output of the state declared XOR fnv1a64("est6-sched-v1/" label) |
| 139 | * candidate k = base + k (k = 0, 1, 2, ...) |
| 140 | * effective seed = first candidate whose est_load schedule passes the balance rule, k <= SR1_MAXK |
| 141 | * The label is part of the derivation input, so two labels never share a chain. The balance rule is a |
| 142 | * property of the schedule alone (no data); the search is bounded and fails closed. A plan with two |
| 143 | * identical schedules (or seeds) is refused, never repaired by further search. */ |
| 144 | #define SR1_MAXK 255 |
| 145 | #define SEGMAX 4096 |
| 146 | typedef struct { int level[SEGMAX]; long dur[SEGMAX]; int n; long lv[4]; long total; } sched_t; |
| 147 | |
| 148 | static uint64_t fnv1a64(const char *s) |
| 149 | { |
| 150 | uint64_t h = 0xcbf29ce484222325ull; |
| 151 | for (; *s; s++) { h ^= (unsigned char)*s; h *= 0x100000001b3ull; } |
| 152 | return h; |
| 153 | } |
| 154 | |
| 155 | static void gen_sched(uint64_t seed, long total, sched_t *o) |
| 156 | { |
| 157 | static const int levels[4] = { 0, 6, 12, 18 }; |
| 158 | uint64_t s = seed; long acc = 0; |
| 159 | memset(o, 0, sizeof *o); |
| 160 | while (acc < total && o->n < SEGMAX) { |
| 161 | int li = (int)(splitmix64(&s) % 4u); |
| 162 | long d = 20 + (long)(splitmix64(&s) % 101u); |
| 163 | if (acc + d > total) d = total - acc; |
| 164 | o->level[o->n] = levels[li]; o->dur[o->n] = d; o->lv[li] += d; acc += d; o->n++; |
| 165 | } |
| 166 | o->total = acc; |
| 167 | } |
| 168 | |
| 169 | /* required properties: segments sum to the run length; every segment is 20..120 s except a clipped last |
| 170 | * one (1..120); each of idle, L6, L12, L18 totals 400..900 s */ |
| 171 | static int sched_props(const sched_t *o, long total, int *balanced) |
| 172 | { |
| 173 | long sum = 0; int ok = 1; |
| 174 | for (int i = 0; i < o->n; i++) { |
| 175 | sum += o->dur[i]; |
| 176 | if (o->dur[i] > 120 || o->dur[i] < 1) ok = 0; |
| 177 | if (o->dur[i] < 20 && i != o->n - 1) ok = 0; |
| 178 | } |
| 179 | if (sum != total || o->n >= SEGMAX) ok = 0; |
| 180 | *balanced = 1; |
| 181 | for (int i = 0; i < 4; i++) if (o->lv[i] < 400 || o->lv[i] > 900) *balanced = 0; |
| 182 | return ok; |
| 183 | } |
| 184 | |
| 185 | static uint64_t sr1_base(const char *label, uint64_t declared) |
| 186 | { |
| 187 | char buf[128 + 32]; |
| 188 | snprintf(buf, sizeof buf, "est6-sched-v1/%.100s", label); |
| 189 | uint64_t st = declared ^ fnv1a64(buf); |
| 190 | return splitmix64(&st); |
| 191 | } |
| 192 | |
| 193 | /* rule 0 = old (v6 proposal: declared + k), rule 1 = SR-1. Returns k or -1. */ |
| 194 | static int sr_pick(int rule, const char *label, uint64_t declared, long total, uint64_t *eff, sched_t *o) |
| 195 | { |
| 196 | uint64_t base = rule ? sr1_base(label, declared) : declared; |
| 197 | for (int k = 0; k <= (rule ? SR1_MAXK : 100000); k++) { |
| 198 | int bal; |
| 199 | gen_sched(base + (uint64_t)k, total, o); |
| 200 | if (sched_props(o, total, &bal) && bal) { *eff = base + (uint64_t)k; return k; } |
| 201 | } |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | static int same_sched(const sched_t *a, const sched_t *b) |
| 206 | { |
| 207 | if (a->n != b->n) return 0; |
| 208 | for (int i = 0; i < a->n; i++) if (a->level[i] != b->level[i] || a->dur[i] != b->dur[i]) return 0; |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | /* est6dev plan6 [--rule old|sr1] <seconds> <label>=<declared seed> ... */ |
| 213 | static int cmd_plan6(int argc, char **argv) |
| 214 | { |
| 215 | int rule = 1; |
| 216 | if (argc >= 2 && !strcmp(argv[0], "--rule")) { |
| 217 | if (!strcmp(argv[1], "old")) rule = 0; else if (!strcmp(argv[1], "sr1")) rule = 1; else { fprintf(stderr, "rule must be old or sr1\n"); return 2; } |
| 218 | argc -= 2; argv += 2; |
| 219 | } |
| 220 | if (argc < 2) { fprintf(stderr, "usage: est6dev plan6 [--rule old|sr1] <seconds> <label>=<seed> ...\n"); return 2; } |
| 221 | long total = strtol(argv[0], NULL, 10); |
| 222 | if (total <= 0) { fprintf(stderr, "seconds must be > 0\n"); return 2; } |
| 223 | int np = argc - 1; |
| 224 | static sched_t sc[16]; uint64_t eff[16]; char lab[16][64]; |
| 225 | if (np > 16) return 2; |
| 226 | printf("rule %s seconds %ld\n", rule ? "SR-1" : "OLD(seed+1)", total); |
| 227 | for (int i = 0; i < np; i++) { |
| 228 | char *eq = strchr(argv[1 + i], '='); |
| 229 | if (!eq || eq - argv[1 + i] >= 63) { fprintf(stderr, "bad plan item %s\n", argv[1 + i]); return 2; } |
| 230 | memcpy(lab[i], argv[1 + i], (size_t)(eq - argv[1 + i])); lab[i][eq - argv[1 + i]] = 0; |
| 231 | uint64_t decl = strtoull(eq + 1, NULL, 0); |
| 232 | int k = sr_pick(rule, lab[i], decl, total, &eff[i], &sc[i]); |
| 233 | if (k < 0) { printf("label %s declared 0x%llx NO_PASSING_SEED\n", lab[i], (unsigned long long)decl); return 1; } |
| 234 | printf("label %s declared 0x%llx effective 0x%llx k %d segments %d idle %ld L6 %ld L12 %ld L18 %ld balance PASS\n", |
| 235 | lab[i], (unsigned long long)decl, (unsigned long long)eff[i], k, sc[i].n, sc[i].lv[0], sc[i].lv[1], sc[i].lv[2], sc[i].lv[3]); |
| 236 | for (int j = 0; j < sc[i].n; j++) printf("schedule %s %d %d %ld\n", lab[i], j, sc[i].level[j], sc[i].dur[j]); |
| 237 | } |
| 238 | int bad = 0; |
| 239 | for (int i = 0; i < np; i++) for (int j = i + 1; j < np; j++) |
| 240 | if (eff[i] == eff[j] || same_sched(&sc[i], &sc[j])) { printf("COLLISION %s %s effective 0x%llx\n", lab[i], lab[j], (unsigned long long)eff[i]); bad = 1; } |
| 241 | if (bad) return 1; |
| 242 | printf("distinct PASS\n"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* ------------------------------------------------------------ one-step G1 PIT trace */ |
| 247 | typedef struct { int64_t t; double flo, fhi, z; } step_t; |
| 248 | |
| 249 | static est_obs_class classify(const c3_tick *t, est_pobs *ob) |
| 250 | { |
| 251 | est_assumption a; c3_assumption_base(&a, EST_FAM_STUDENT_T); |
| 252 | a.nparam = 2; a.param[0] = 5; a.param[1] = 100; |
| 253 | if (est_pobs_classify(&a, t->value, t->present, &t->ev, ob) != EST_OK) return EST_OBS_CLASSES_; |
| 254 | return ob->cls; |
| 255 | } |
| 256 | |
| 257 | /* mirrors est5.c e4_score, one-step part only (same classification, burn-in, unscorable rule) */ |
| 258 | static int run_g1(const c3_ticks *tk, const est4_params *p, step_t **out, size_t *nout, size_t *unscorable) |
| 259 | { |
| 260 | est4_state s; |
| 261 | est_dpred *dp = XMALLOC(sizeof *dp); |
| 262 | step_t *st = XMALLOC((tk->n ? tk->n : 1) * sizeof *st); |
| 263 | size_t n = 0; *unscorable = 0; |
| 264 | if (!dp || !st) return 1; |
| 265 | if (est4_init(p, &s) != EST_OK) return 1; |
| 266 | long first = -1; |
| 267 | for (size_t t = 0; t < tk->n; t++) { |
| 268 | const c3_tick *tc = &tk->t[t]; |
| 269 | est_pobs ob; est_pinnov iv; |
| 270 | est_obs_class cls = classify(tc, &ob); |
| 271 | if (cls == EST_OBS_CLASSES_) return 1; |
| 272 | int ok = cls == EST_OBS_OK; |
| 273 | if (ok && first < 0) first = (long)t; |
| 274 | int scored = first >= 0 && (long)t - first >= (long)C3_BURN_IN; |
| 275 | if (ok && scored) { |
| 276 | if ((uint64_t)s.gap + 1u > EST_PRED_MAX_H) (*unscorable)++; |
| 277 | else { |
| 278 | est4_pred pr; |
| 279 | if (est4_predict(&s, 1, &pr) != EST_OK || est4_dpred(&s, &pr, dp) != EST_OK) return 1; |
| 280 | if (est_dpred_score(dp, &ob, &iv) != EST_OK) return 1; |
| 281 | if (iv.valid) { st[n].t = tc->wall_ns; st[n].flo = iv.F_lo; st[n].fhi = iv.F_hi; st[n].z = iv.z; n++; } |
| 282 | } |
| 283 | } |
| 284 | if (est4_update(&s, ok, ok ? ob.value : NAN) != EST_OK) return 1; |
| 285 | } |
| 286 | free(dp); |
| 287 | *out = st; *nout = n; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | #define NSTAT 13 /* bins 0..9, lower miss, upper miss, cov95 */ |
| 292 | static const char *STAT_NAME[NSTAT] = { "pit_bin0", "pit_bin1", "pit_bin2", "pit_bin3", "pit_bin4", "pit_bin5", "pit_bin6", |
| 293 | "pit_bin7", "pit_bin8", "pit_bin9", "lower_miss_2.5", "upper_miss_2.5", "coverage95" }; |
| 294 | |
| 295 | static double overlap(double a, double b, double c, double d) |
| 296 | { |
| 297 | double lo = a > c ? a : c, hi = b < d ? b : d; |
| 298 | return hi > lo ? hi - lo : 0.0; |
| 299 | } |
| 300 | |
| 301 | /* per-step contribution to each statistic: expected value under u ~ U(F_lo, F_hi) */ |
| 302 | static void contrib(const step_t *s, double v[NSTAT]) |
| 303 | { |
| 304 | double span = s->fhi - s->flo; |
| 305 | for (int j = 0; j < 10; j++) { |
| 306 | if (span > 0.0) v[j] = overlap(s->flo, s->fhi, j / 10.0, (j + 1) / 10.0) / span; |
| 307 | else { int k = (int)floor(s->flo * 10.0); if (k < 0) k = 0; if (k > 9) k = 9; v[j] = (k == j) ? 1.0 : 0.0; } |
| 308 | } |
| 309 | if (span > 0.0) { |
| 310 | double l = (0.025 - s->flo) / span, u = (s->fhi - 0.975) / span; |
| 311 | v[10] = l < 0 ? 0 : l > 1 ? 1 : l; |
| 312 | v[11] = u < 0 ? 0 : u > 1 ? 1 : u; |
| 313 | } else { v[10] = s->flo < 0.025; v[11] = s->flo > 0.975; } |
| 314 | v[12] = 1.0 - v[10] - v[11]; |
| 315 | } |
| 316 | |
| 317 | static int cmpd(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } |
| 318 | |
| 319 | static double quant(const double *sorted, int n, double q) |
| 320 | { |
| 321 | double pos = q * (double)(n - 1); |
| 322 | int i = (int)floor(pos); |
| 323 | if (i >= n - 1) return sorted[n - 1]; |
| 324 | return sorted[i] + (pos - i) * (sorted[i + 1] - sorted[i]); |
| 325 | } |
| 326 | |
| 327 | /* block id of a time: odd 2i+1 inside marked pair i, even 2m between (m = pairs ended before t) */ |
| 328 | static size_t block_of(const c3_marks *mk, int64_t t) |
| 329 | { |
| 330 | size_t ended = 0; |
| 331 | for (size_t i = 0; i < mk->n; i++) { |
| 332 | if (t >= mk->b[i] && t <= mk->e[i]) return 2 * i + 1; |
| 333 | if (mk->e[i] < t) ended++; |
| 334 | } |
| 335 | return 2 * ended; |
| 336 | } |
| 337 | |
| 338 | static int cmd_g1pit(int argc, char **argv) |
| 339 | { |
| 340 | const char *raw = NULL, *mkp = NULL; |
| 341 | double dyn = NAN, q = NAN, lam = NAN, nu = NAN, c = NAN; |
| 342 | int boot = 2000, show_blocks = 0; |
| 343 | uint64_t seed = 0xE6B007ull; |
| 344 | for (int i = 0; i < argc; i++) { |
| 345 | if (!strcmp(argv[i], "--raw") && i + 1 < argc) raw = argv[++i]; |
| 346 | else if (!strcmp(argv[i], "--marks") && i + 1 < argc) mkp = argv[++i]; |
| 347 | else if (!strcmp(argv[i], "--dyn") && i + 1 < argc) dyn = atof(argv[++i]); |
| 348 | else if (!strcmp(argv[i], "--q") && i + 1 < argc) q = atof(argv[++i]); |
| 349 | else if (!strcmp(argv[i], "--lam") && i + 1 < argc) lam = atof(argv[++i]); |
| 350 | else if (!strcmp(argv[i], "--nu") && i + 1 < argc) nu = atof(argv[++i]); |
| 351 | else if (!strcmp(argv[i], "--c") && i + 1 < argc) c = atof(argv[++i]); |
| 352 | else if (!strcmp(argv[i], "--boot") && i + 1 < argc) boot = atoi(argv[++i]); |
| 353 | else if (!strcmp(argv[i], "--seed") && i + 1 < argc) seed = strtoull(argv[++i], NULL, 0); |
| 354 | else if (!strcmp(argv[i], "--blocks")) show_blocks = 1; |
| 355 | else { fprintf(stderr, "usage: est6dev g1pit --raw R --marks M --dyn D --q Q --lam L --nu N --c C [--boot B] [--seed S] [--blocks]\n"); return 2; } |
| 356 | } |
| 357 | if (!raw || !mkp || isnan(dyn) || isnan(q) || isnan(lam) || isnan(nu) || isnan(c) || boot < 10) { fprintf(stderr, "est6dev g1pit: missing argument\n"); return 2; } |
| 358 | if (forbidden_path(raw) || forbidden_path(mkp)) { fprintf(stderr, "refuse: a held-out path (v5 D2 and every held-out tag are forbidden in v6 development)\n"); return 2; } |
| 359 | char err[512]; |
| 360 | est_file f; |
| 361 | if (est_file_load(raw, &f, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 1; } |
| 362 | if (!strcmp(f.sha_hex, D2_RAW_SHA)) { fprintf(stderr, "refuse: input is the v5 D2 raw data (SHA-256)\n"); est_file_free(&f); return 2; } |
| 363 | static c3_marks mk; |
| 364 | if (c3_marks_load(mkp, 1, &mk, err, sizeof err)) { fprintf(stderr, "refuse: marks: %s\n", err); est_file_free(&f); return 1; } |
| 365 | if (!strcmp(mk.sha, D2_MARKS_SHA)) { fprintf(stderr, "refuse: marks are the v5 D2 marks (SHA-256)\n"); est_file_free(&f); return 2; } |
| 366 | c3_ticks tk; |
| 367 | if (c3_ticks_build(&f, &tk, err, sizeof err)) { fprintf(stderr, "ticks: %s\n", err); est_file_free(&f); return 1; } |
| 368 | printf("input raw_sha256 %s marks_sha256 %s lines %zu marks_pairs %zu\n", f.sha_hex, mk.sha, tk.lines, mk.n); |
| 369 | |
| 370 | est4_params p = { EST4_G1_LAG, dyn, q, lam, nu, c, 0.95, 0.8, 0.2, C3_QUANTUM }; |
| 371 | step_t *st = NULL; size_t n = 0, unsc = 0; |
| 372 | if (run_g1(&tk, &p, &st, &n, &unsc) || n == 0) { fprintf(stderr, "g1 run failed\n"); return 1; } |
| 373 | |
| 374 | /* per-step contributions, per-block sums */ |
| 375 | size_t nb_max = 2 * mk.n + 2; |
| 376 | double (*bs)[NSTAT] = XCALLOC(nb_max, sizeof *bs); |
| 377 | double *bn = XCALLOC(nb_max, sizeof *bn); |
| 378 | double tot[NSTAT] = { 0 }; |
| 379 | double zsum = 0; |
| 380 | if (!bs || !bn) return 1; |
| 381 | for (size_t i = 0; i < n; i++) { |
| 382 | double v[NSTAT]; |
| 383 | contrib(&st[i], v); |
| 384 | size_t b = block_of(&mk, st[i].t); |
| 385 | for (int k = 0; k < NSTAT; k++) { bs[b][k] += v[k]; tot[k] += v[k]; } |
| 386 | bn[b] += 1.0; |
| 387 | zsum += st[i].z; |
| 388 | } |
| 389 | double zm = zsum / (double)n; |
| 390 | printf("params dyn %.17g q %.17g lam %.17g nu %.17g c %.17g\n", dyn, q, lam, nu, c); |
| 391 | printf("n_scored %zu unscorable %zu\n", n, unsc); |
| 392 | printf("mean_z_midpit %.6f\n", zm); |
| 393 | |
| 394 | /* blocks that have steps */ |
| 395 | size_t nb = 0, nload = 0, *idx = XMALLOC(nb_max * sizeof *idx); |
| 396 | for (size_t b = 0; b < nb_max; b++) if (bn[b] > 0) { idx[nb++] = b; nload += (b & 1u); } |
| 397 | printf("blocks %zu (loaded %zu, idle %zu) mean_steps_per_block %.1f\n", nb, nload, nb - nload, (double)n / (double)nb); |
| 398 | if (show_blocks) |
| 399 | for (size_t i = 0; i < nb; i++) { |
| 400 | size_t b = idx[i]; |
| 401 | printf("block %zu %s level %d steps %.0f bin0_mass %.3f lower_miss_mass %.3f upper_miss_mass %.3f\n", b, (b & 1u) ? "load" : "idle", |
| 402 | (b & 1u) ? mk.level[b / 2] : 0, bn[b], bs[b][0], bs[b][10], bs[b][11]); |
| 403 | } |
| 404 | |
| 405 | { /* descriptive: shares by declared load level (idle = 0) */ |
| 406 | double ls[4][NSTAT] = { { 0 } }, ln[4] = { 0 }; |
| 407 | for (size_t i = 0; i < nb; i++) { |
| 408 | size_t b = idx[i]; |
| 409 | int lv = (b & 1u) ? mk.level[b / 2] : 0, li = lv == 6 ? 1 : lv == 12 ? 2 : lv == 18 ? 3 : 0; |
| 410 | ln[li] += bn[b]; |
| 411 | for (int k = 0; k < NSTAT; k++) ls[li][k] += bs[b][k]; |
| 412 | } |
| 413 | static const int lvn[4] = { 0, 6, 12, 18 }; |
| 414 | for (int li = 0; li < 4; li++) |
| 415 | if (ln[li] > 0) printf("by_level L%d steps %.0f pit_bin0 %.4f pit_bin9 %.4f lower_miss %.4f upper_miss %.4f\n", lvn[li], ln[li], ls[li][0] / ln[li], ls[li][9] / ln[li], ls[li][10] / ln[li], ls[li][11] / ln[li]); |
| 416 | } |
| 417 | /* bootstrap: resample whole blocks with replacement; ratio estimator */ |
| 418 | double *draw = XMALLOC((size_t)boot * NSTAT * sizeof(double)); |
| 419 | if (!draw) return 1; |
| 420 | uint64_t rng = seed; |
| 421 | for (int r = 0; r < boot; r++) { |
| 422 | double sum[NSTAT] = { 0 }, cnt = 0; |
| 423 | for (size_t i = 0; i < nb; i++) { |
| 424 | size_t b = idx[splitmix64(&rng) % nb]; |
| 425 | for (int k = 0; k < NSTAT; k++) sum[k] += bs[b][k]; |
| 426 | cnt += bn[b]; |
| 427 | } |
| 428 | for (int k = 0; k < NSTAT; k++) draw[(size_t)r * NSTAT + k] = sum[k] / cnt; |
| 429 | } |
| 430 | printf("bootstrap draws %d seed 0x%llx unit whole_blocks\n", boot, (unsigned long long)seed); |
| 431 | printf("# stat value iid_se boot_se design_effect ci95_lo ci95_hi ci995_lo ci995_hi\n"); |
| 432 | double *col = XMALLOC((size_t)boot * sizeof *col); |
| 433 | for (int k = 0; k < NSTAT; k++) { |
| 434 | double val = tot[k] / (double)n, mean = 0, var = 0; |
| 435 | for (int r = 0; r < boot; r++) { col[r] = draw[(size_t)r * NSTAT + k]; mean += col[r]; } |
| 436 | mean /= boot; |
| 437 | for (int r = 0; r < boot; r++) var += (col[r] - mean) * (col[r] - mean); |
| 438 | var /= (boot - 1); |
| 439 | double iid = sqrt(val * (1.0 - val) / (double)n); |
| 440 | qsort(col, (size_t)boot, sizeof *col, cmpd); |
| 441 | double bse = sqrt(var); |
| 442 | printf("stat %s %.6f %.6f %.6f %.3f %.6f %.6f %.6f %.6f\n", STAT_NAME[k], val, iid, bse, iid > 0 ? (bse * bse) / (iid * iid) : NAN, |
| 443 | quant(col, boot, 0.025), quant(col, boot, 0.975), quant(col, boot, 0.0025), quant(col, boot, 0.9975)); |
| 444 | } |
| 445 | free(st); free(bs); free(bn); free(idx); free(draw); free(col); |
| 446 | c3_ticks_free(&tk); est_file_free(&f); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /* ------------------------------------------------------------ G1S exploratory (item 2, reduced) */ |
| 451 | /* Reduced item 2: the filter parameters (dyn q lam nu) stay at the v5 D1 values; only the skew kappa |
| 452 | * and the scale c are searched (kappa x c grid), by 5-fold block cross-validation over load segments. |
| 453 | * The two-piece t is built from the symmetric est4_fast_logp kernel (est_v4.c unchanged): scale c b on |
| 454 | * the upper side and c kappa b on the lower side, side masses 1/(1+kappa) and kappa/(1+kappa). */ |
| 455 | typedef struct { int64_t t; double y, m, b, anchor; } trace_t; |
| 456 | |
| 457 | static int run_g1_trace(const c3_ticks *tk, const est4_params *p, trace_t **out, size_t *nout) |
| 458 | { |
| 459 | est4_state s; |
| 460 | trace_t *tr = XMALLOC((tk->n ? tk->n : 1) * sizeof *tr); |
| 461 | size_t n = 0; |
| 462 | if (!tr || est4_init(p, &s) != EST_OK) return 1; |
| 463 | long first = -1; |
| 464 | for (size_t t = 0; t < tk->n; t++) { |
| 465 | est_pobs ob; |
| 466 | est_obs_class cls = classify(&tk->t[t], &ob); |
| 467 | if (cls == EST_OBS_CLASSES_) return 1; |
| 468 | int ok = cls == EST_OBS_OK; |
| 469 | if (ok && first < 0) first = (long)t; |
| 470 | int scored = first >= 0 && (long)t - first >= (long)C3_BURN_IN; |
| 471 | if (ok && scored && (uint64_t)s.gap + 1u <= EST_PRED_MAX_H) { |
| 472 | double m, v; |
| 473 | if (est4_moments(&s, 1, &m, &v) != EST_OK) return 1; |
| 474 | tr[n].t = tk->t[t].wall_ns; tr[n].y = ob.value; tr[n].m = m; tr[n].b = sqrt(s.g * v); tr[n].anchor = s.anchor; n++; |
| 475 | } |
| 476 | if (est4_update(&s, ok, ok ? ob.value : NAN) != EST_OK) return 1; |
| 477 | } |
| 478 | *out = tr; *nout = n; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /* un-floored cdf at the bin edges from the symmetric kernel */ |
| 483 | static void sym_edges(const trace_t *r, double scale, double nu, double *lo, double *hi) |
| 484 | { |
| 485 | est4_pred pr = { 1u, r->m, scale, nu }; |
| 486 | double Fl, Fh; |
| 487 | est4_fast_logp(&pr, C3_QUANTUM, r->anchor, r->y, &Fl, &Fh); |
| 488 | double kd = nearbyint((r->y - r->anchor) / C3_QUANTUM); |
| 489 | if (kd < -(double)EST_PRED_K) kd = -(double)EST_PRED_K; |
| 490 | if (kd > (double)EST_PRED_K) kd = (double)EST_PRED_K; |
| 491 | double f = EST_PRED_FLOOR, u = EST_PRED_FLOOR / (double)EST_PRED_N, idx = kd + (double)EST_PRED_K; |
| 492 | *lo = (Fl - u * idx) / (1.0 - f); |
| 493 | *hi = (Fh - u * (idx + 1.0)) / (1.0 - f); |
| 494 | } |
| 495 | |
| 496 | static double two_piece(const trace_t *r, double c, double kappa, double nu, double *Flo, double *Fhi) |
| 497 | { |
| 498 | double a = 1.0 / (1.0 + kappa), b = kappa / (1.0 + kappa); |
| 499 | double lo_u, hi_u, lo_l, hi_l; |
| 500 | sym_edges(r, c * r->b, nu, &lo_u, &hi_u); |
| 501 | sym_edges(r, c * kappa * r->b, nu, &lo_l, &hi_l); |
| 502 | double kd = nearbyint((r->y - r->anchor) / C3_QUANTUM); |
| 503 | if (kd < -(double)EST_PRED_K) kd = -(double)EST_PRED_K; |
| 504 | if (kd > (double)EST_PRED_K) kd = (double)EST_PRED_K; |
| 505 | double mu = r->m - r->anchor, xa = (kd - 0.5) * C3_QUANTUM, xb = (kd + 0.5) * C3_QUANTUM; |
| 506 | double Fa = xa < mu ? 2.0 * b * lo_l : b + 2.0 * a * (lo_u - 0.5); |
| 507 | double Fb = xb < mu ? 2.0 * b * hi_l : b + 2.0 * a * (hi_u - 0.5); |
| 508 | if (kd == -(double)EST_PRED_K) Fa = 0.0; |
| 509 | if (kd == (double)EST_PRED_K) Fb = 1.0; |
| 510 | double p = Fb - Fa; if (p < 0) p = 0; |
| 511 | double f = EST_PRED_FLOOR, u = EST_PRED_FLOOR / (double)EST_PRED_N, idx = kd + (double)EST_PRED_K; |
| 512 | *Flo = (1.0 - f) * Fa + u * idx; *Fhi = (1.0 - f) * Fb + u * (idx + 1.0); |
| 513 | p = (1.0 - f) * p + u; |
| 514 | return log(p > 1e-300 ? p : 1e-300); |
| 515 | } |
| 516 | |
| 517 | #define NK 5 |
| 518 | #define NCG 21 |
| 519 | static const double KAPPA[NK] = { 0.6, 0.7, 0.8, 0.9, 1.0 }; |
| 520 | |
| 521 | static int cmd_g1s(int argc, char **argv) |
| 522 | { |
| 523 | const char *raw = NULL, *mkp = NULL; |
| 524 | double dyn = NAN, q = NAN, lam = NAN, nu = NAN; |
| 525 | int nfold = 5; |
| 526 | for (int i = 0; i < argc; i++) { |
| 527 | if (!strcmp(argv[i], "--raw") && i + 1 < argc) raw = argv[++i]; |
| 528 | else if (!strcmp(argv[i], "--marks") && i + 1 < argc) mkp = argv[++i]; |
| 529 | else if (!strcmp(argv[i], "--dyn") && i + 1 < argc) dyn = atof(argv[++i]); |
| 530 | else if (!strcmp(argv[i], "--q") && i + 1 < argc) q = atof(argv[++i]); |
| 531 | else if (!strcmp(argv[i], "--lam") && i + 1 < argc) lam = atof(argv[++i]); |
| 532 | else if (!strcmp(argv[i], "--nu") && i + 1 < argc) nu = atof(argv[++i]); |
| 533 | else if (!strcmp(argv[i], "--folds") && i + 1 < argc) nfold = atoi(argv[++i]); |
| 534 | else { fprintf(stderr, "usage: est6dev g1s --raw R --marks M --dyn D --q Q --lam L --nu N [--folds K]\n"); return 2; } |
| 535 | } |
| 536 | if (!raw || !mkp || isnan(dyn) || isnan(q) || isnan(lam) || isnan(nu) || nfold < 2 || nfold > 20) { fprintf(stderr, "est6dev g1s: missing argument\n"); return 2; } |
| 537 | if (forbidden_path(raw) || forbidden_path(mkp)) { fprintf(stderr, "refuse: a held-out path (v5 D2 and every held-out tag are forbidden in v6 development)\n"); return 2; } |
| 538 | char err[512]; |
| 539 | est_file f; |
| 540 | if (est_file_load(raw, &f, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 1; } |
| 541 | if (!strcmp(f.sha_hex, D2_RAW_SHA)) { fprintf(stderr, "refuse: input is the v5 D2 raw data (SHA-256)\n"); est_file_free(&f); return 2; } |
| 542 | static c3_marks mk; |
| 543 | if (c3_marks_load(mkp, 1, &mk, err, sizeof err)) { fprintf(stderr, "refuse: marks: %s\n", err); est_file_free(&f); return 1; } |
| 544 | if (!strcmp(mk.sha, D2_MARKS_SHA)) { fprintf(stderr, "refuse: marks are the v5 D2 marks (SHA-256)\n"); est_file_free(&f); return 2; } |
| 545 | c3_ticks tk; |
| 546 | if (c3_ticks_build(&f, &tk, err, sizeof err)) { fprintf(stderr, "ticks: %s\n", err); est_file_free(&f); return 1; } |
| 547 | printf("input raw_sha256 %s marks_sha256 %s\n", f.sha_hex, mk.sha); |
| 548 | est4_params p = { EST4_G1_LAG, dyn, q, lam, nu, 1.0, 0.95, 0.8, 0.2, C3_QUANTUM }; |
| 549 | trace_t *tr = NULL; size_t n = 0; |
| 550 | if (run_g1_trace(&tk, &p, &tr, &n) || n == 0) { fprintf(stderr, "g1 trace failed\n"); return 1; } |
| 551 | /* fold of a step = rank of its block among blocks that have steps, modulo nfold */ |
| 552 | size_t nb_max = 2 * mk.n + 2; |
| 553 | int *rank = XMALLOC(nb_max * sizeof *rank), *seen = XCALLOC(nb_max, sizeof *seen); |
| 554 | size_t *blk = XMALLOC(n * sizeof *blk); |
| 555 | for (size_t i = 0; i < n; i++) { blk[i] = block_of(&mk, tr[i].t); seen[blk[i]] = 1; } |
| 556 | int nr = 0; |
| 557 | for (size_t b = 0; b < nb_max; b++) rank[b] = seen[b] ? nr++ : -1; |
| 558 | int *fold = XMALLOC(n * sizeof *fold); |
| 559 | for (size_t i = 0; i < n; i++) fold[i] = rank[blk[i]] % nfold; |
| 560 | /* per grid point, per step: logp, F_lo, F_hi */ |
| 561 | size_t G = (size_t)NK * NCG; |
| 562 | double *lp = XMALLOC(G * n * sizeof *lp), *Flo = XMALLOC(G * n * sizeof *Flo), *Fhi = XMALLOC(G * n * sizeof *Fhi); |
| 563 | if (!lp || !Flo || !Fhi) return 1; |
| 564 | for (int ki = 0; ki < NK; ki++) |
| 565 | for (int ci = 0; ci < NCG; ci++) { |
| 566 | size_t g = (size_t)ki * NCG + (size_t)ci; |
| 567 | double c = (double)(20 + 5 * ci) / 100.0; |
| 568 | for (size_t i = 0; i < n; i++) lp[g * n + i] = two_piece(&tr[i], c, KAPPA[ki], nu, &Flo[g * n + i], &Fhi[g * n + i]); |
| 569 | } |
| 570 | /* bit-for-bit style check: kappa = 1.0 against est4_fast_logp (maximum absolute difference) */ |
| 571 | double maxd = 0; |
| 572 | for (int ci = 0; ci < NCG; ci++) |
| 573 | for (size_t i = 0; i < n; i++) { |
| 574 | est4_pred pr = { 1u, tr[i].m, ((double)(20 + 5 * ci) / 100.0) * tr[i].b, nu }; |
| 575 | double d = fabs(est4_fast_logp(&pr, C3_QUANTUM, tr[i].anchor, tr[i].y, NULL, NULL) - lp[(size_t)(NK - 1) * NCG * n + (size_t)ci * n + i]); |
| 576 | if (d > maxd) maxd = d; |
| 577 | } |
| 578 | printf("check kappa1_vs_est4_fast_logp max_abs_diff %.3g\n", maxd); |
| 579 | /* in-sample best over the whole run, per kappa and overall */ |
| 580 | for (int ki = 0; ki < NK; ki++) { |
| 581 | double best = -INFINITY; int bc = 0; |
| 582 | for (int ci = 0; ci < NCG; ci++) { |
| 583 | double s = 0; size_t g = (size_t)ki * NCG + (size_t)ci; |
| 584 | for (size_t i = 0; i < n; i++) s += lp[g * n + i]; |
| 585 | if (s / (double)n > best) { best = s / (double)n; bc = ci; } |
| 586 | } |
| 587 | { |
| 588 | size_t g = (size_t)ki * NCG + (size_t)bc; double b0 = 0, b9 = 0, l = 0, u = 0; |
| 589 | for (size_t i = 0; i < n; i++) { step_t st = { 0, Flo[g * n + i], Fhi[g * n + i], 0 }; double v[NSTAT]; contrib(&st, v); b0 += v[0]; b9 += v[9]; l += v[10]; u += v[11]; } |
| 590 | printf("insample kappa %.1f best_c %.2f mean_logscore %.6f pit_bin0 %.4f pit_bin9 %.4f lower_miss %.4f upper_miss %.4f\n", KAPPA[ki], (double)(20 + 5 * bc) / 100.0, best, b0 / (double)n, b9 / (double)n, l / (double)n, u / (double)n); |
| 591 | } |
| 592 | } |
| 593 | /* out-of-fold: for each fold choose (kappa, c) on the other folds, score the held-out fold. restrict = 0: kappa free; 1: kappa fixed at 1.0 */ |
| 594 | for (int restrict_k = 0; restrict_k < 2; restrict_k++) { |
| 595 | double bins[10] = { 0 }, lm = 0, um = 0, ls = 0; |
| 596 | printf("oof %s\n", restrict_k ? "kappa=1.0 only (c refit by fold, control)" : "kappa free in {0.6..1.0}, c refit by fold"); |
| 597 | for (int fo = 0; fo < nfold; fo++) { |
| 598 | double best = -INFINITY; size_t bg = 0; |
| 599 | for (int ki = restrict_k ? NK - 1 : 0; ki < NK; ki++) |
| 600 | for (int ci = 0; ci < NCG; ci++) { |
| 601 | size_t g = (size_t)ki * NCG + (size_t)ci; double s = 0; |
| 602 | for (size_t i = 0; i < n; i++) if (fold[i] != fo) s += lp[g * n + i]; |
| 603 | if (s > best) { best = s; bg = g; } |
| 604 | } |
| 605 | size_t nf = 0; double fl = 0; |
| 606 | for (size_t i = 0; i < n; i++) if (fold[i] == fo) { |
| 607 | step_t st = { 0, Flo[bg * n + i], Fhi[bg * n + i], 0 }; |
| 608 | double v[NSTAT]; contrib(&st, v); |
| 609 | for (int j = 0; j < 10; j++) bins[j] += v[j]; |
| 610 | lm += v[10]; um += v[11]; ls += lp[bg * n + i]; fl += lp[bg * n + i]; nf++; |
| 611 | } |
| 612 | printf("fold %d steps %zu chosen_kappa %.1f chosen_c %.2f oof_mean_logscore %.6f\n", fo, nf, KAPPA[bg / NCG], (double)(20 + 5 * (int)(bg % NCG)) / 100.0, nf ? fl / (double)nf : 0.0); |
| 613 | } |
| 614 | printf("oof_pooled n %zu mean_logscore %.6f pit_bin0 %.4f pit_bin9 %.4f lower_miss %.4f upper_miss %.4f bins", n, ls / (double)n, bins[0] / (double)n, bins[9] / (double)n, lm / (double)n, um / (double)n); |
| 615 | for (int j = 0; j < 10; j++) printf(" %.4f", bins[j] / (double)n); |
| 616 | printf("\n"); |
| 617 | } |
| 618 | /* in-sample overall best (kappa, c) PIT shares, for reference */ |
| 619 | { |
| 620 | double best = -INFINITY; size_t bg = 0; |
| 621 | for (size_t g = 0; g < G; g++) { double s = 0; for (size_t i = 0; i < n; i++) s += lp[g * n + i]; if (s > best) { best = s; bg = g; } } |
| 622 | double bins[10] = { 0 }, lm = 0, um = 0; |
| 623 | for (size_t i = 0; i < n; i++) { step_t st = { 0, Flo[bg * n + i], Fhi[bg * n + i], 0 }; double v[NSTAT]; contrib(&st, v); for (int j = 0; j < 10; j++) bins[j] += v[j]; lm += v[10]; um += v[11]; } |
| 624 | printf("insample_best kappa %.1f c %.2f mean_logscore %.6f pit_bin0 %.4f pit_bin9 %.4f lower_miss %.4f upper_miss %.4f\n", KAPPA[bg / NCG], (double)(20 + 5 * (int)(bg % NCG)) / 100.0, best / (double)n, bins[0] / (double)n, bins[9] / (double)n, lm / (double)n, um / (double)n); |
| 625 | } |
| 626 | free(tr); free(rank); free(seen); free(blk); free(fold); free(lp); free(Flo); free(Fhi); |
| 627 | c3_ticks_free(&tk); est_file_free(&f); |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | /* ------------------------------------------------------------ G1S full fit (item 2, DEVELOPMENT only) */ |
| 633 | /* est-v6.md section 5: G1 grid (dyn q lam nu c; lam grid extended downwards by 0.05 as section 5.5 says) times |
| 634 | * kappa in {0.6..1.0}, 5-fold block cross-validation (fold = rank of the block among load segments and idle |
| 635 | * stretches, modulo 5), selection by the pooled out-of-fold one-step mean log score; stage 2 (phi nu_h c_h |
| 636 | * kappa_h) per fold on the training folds with stage 1 frozen; a control with kappa = 1.0 (G1 exactly: that |
| 637 | * code path calls the unchanged est4_fast_logp) is fitted under the same scheme and folds. Reads D1 only. */ |
| 638 | #define NKS 5 |
| 639 | #define NLAMX 7 |
| 640 | #define NDYN 6 |
| 641 | #define NQX 9 |
| 642 | #define NNU 7 |
| 643 | #define NCX 21 |
| 644 | static const double LAMX[NLAMX] = { 0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.9 }; |
| 645 | static const double DYNX[NDYN] = { 0.5, 1, 2, 4, 10, 30 }; |
| 646 | static const double NUX[NNU] = { 0.8, 1, 1.25, 1.5, 2, 3, 5 }; |
| 647 | static const double PHIX[5] = { 1, 0.95, 0.9, 0.8, 0.6 }; |
| 648 | static const double NUHX[10] = { 0.5, 0.6, 0.7, 0.8, 1, 1.25, 1.5, 2, 3, 5 }; |
| 649 | #define NCHX 31 |
| 650 | |
| 651 | typedef struct { |
| 652 | size_t n1, n10; |
| 653 | int64_t *t1, *t10; |
| 654 | double *y1, *m1, *b1, *a1; |
| 655 | double *y10, *m10, *v10, *g10, *gl10, *a10; |
| 656 | } tr2_t; |
| 657 | |
| 658 | static int tr2_alloc(tr2_t *r, size_t cap) |
| 659 | { |
| 660 | memset(r, 0, sizeof *r); |
| 661 | cap = cap ? cap : 1; |
| 662 | double **f[10] = { &r->y1, &r->m1, &r->b1, &r->a1, &r->y10, &r->m10, &r->v10, &r->g10, &r->gl10, &r->a10 }; |
| 663 | for (int i = 0; i < 10; i++) if (!(*f[i] = malloc(cap * sizeof(double)))) return 1; |
| 664 | r->t1 = XMALLOC(cap * sizeof(int64_t)); r->t10 = XMALLOC(cap * sizeof(int64_t)); |
| 665 | return !(r->t1 && r->t10); |
| 666 | } |
| 667 | static void tr2_free(tr2_t *r) |
| 668 | { |
| 669 | free(r->y1); free(r->m1); free(r->b1); free(r->a1); free(r->y10); free(r->m10); free(r->v10); free(r->g10); free(r->gl10); free(r->a10); |
| 670 | free(r->t1); free(r->t10); |
| 671 | memset(r, 0, sizeof *r); |
| 672 | } |
| 673 | |
| 674 | typedef struct { int ok; double m, v, g, gl, a; } origin_t; |
| 675 | |
| 676 | /* same tick walk as est5.c trace_run (one-step and ten-step origins), plus the target tick time */ |
| 677 | static int tr2_run(const c3_ticks *tk, const est4_params *p, tr2_t *tr) |
| 678 | { |
| 679 | est4_state s; |
| 680 | if (est4_init(p, &s) != EST_OK) return 1; |
| 681 | origin_t ring[C3_TEN]; |
| 682 | memset(ring, 0, sizeof ring); |
| 683 | tr->n1 = tr->n10 = 0; |
| 684 | long first = -1; |
| 685 | for (size_t t = 0; t < tk->n; t++) { |
| 686 | est_pobs ob; |
| 687 | est_obs_class cls = classify(&tk->t[t], &ob); |
| 688 | if (cls == EST_OBS_CLASSES_) return 1; |
| 689 | int ok = cls == EST_OBS_OK; |
| 690 | if (ok && first < 0) first = (long)t; |
| 691 | int scored = first >= 0 && (long)t - first >= (long)C3_BURN_IN; |
| 692 | origin_t *o = &ring[t % C3_TEN]; |
| 693 | if (o->ok) { |
| 694 | o->ok = 0; |
| 695 | if (ok) { |
| 696 | size_t k = tr->n10++; |
| 697 | tr->y10[k] = ob.value; tr->m10[k] = o->m; tr->v10[k] = o->v; tr->g10[k] = o->g; tr->gl10[k] = o->gl; tr->a10[k] = o->a; |
| 698 | tr->t10[k] = tk->t[t].wall_ns; |
| 699 | } |
| 700 | } |
| 701 | if (ok && scored && (uint64_t)s.gap + 1u <= EST_PRED_MAX_H) { |
| 702 | double m, v; |
| 703 | if (est4_moments(&s, 1, &m, &v) != EST_OK) return 1; |
| 704 | size_t k = tr->n1++; |
| 705 | tr->y1[k] = ob.value; tr->m1[k] = m; tr->b1[k] = sqrt(s.g * v); tr->a1[k] = s.anchor; tr->t1[k] = tk->t[t].wall_ns; |
| 706 | } |
| 707 | if (est4_update(&s, ok, ok ? ob.value : NAN) != EST_OK) return 1; |
| 708 | if (ok && scored) { |
| 709 | double m, v; |
| 710 | if (est4_moments(&s, C3_TEN, &m, &v) != EST_OK) return 1; |
| 711 | o->ok = 1; o->m = m; o->v = v; o->g = s.g; o->gl = s.gl; o->a = s.anchor; |
| 712 | } |
| 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* one grid point = (scale multiplier c on base b, kappa, nu). edges of the symmetric kernel with the upper |
| 718 | * scale (kappa independent) are computed once per (nu, c) and reused for every kappa. */ |
| 719 | static double tp_point(double kappa, double m, double anchor, double y, double lo_u, double hi_u, double lo_l, double hi_l, |
| 720 | double *Flo, double *Fhi) |
| 721 | { |
| 722 | double a = 1.0 / (1.0 + kappa), b = kappa / (1.0 + kappa); |
| 723 | double kd = nearbyint((y - anchor) / C3_QUANTUM); |
| 724 | if (kd < -(double)EST_PRED_K) kd = -(double)EST_PRED_K; |
| 725 | if (kd > (double)EST_PRED_K) kd = (double)EST_PRED_K; |
| 726 | double mu = m - anchor, xa = (kd - 0.5) * C3_QUANTUM, xb = (kd + 0.5) * C3_QUANTUM; |
| 727 | double Fa = xa < mu ? 2.0 * b * lo_l : b + 2.0 * a * (lo_u - 0.5); |
| 728 | double Fb = xb < mu ? 2.0 * b * hi_l : b + 2.0 * a * (hi_u - 0.5); |
| 729 | if (kd == -(double)EST_PRED_K) Fa = 0.0; |
| 730 | if (kd == (double)EST_PRED_K) Fb = 1.0; |
| 731 | double p = Fb - Fa; if (p < 0) p = 0; |
| 732 | double f = EST_PRED_FLOOR, u = EST_PRED_FLOOR / (double)EST_PRED_N, idx = kd + (double)EST_PRED_K; |
| 733 | if (Flo) *Flo = (1.0 - f) * Fa + u * idx; |
| 734 | if (Fhi) *Fhi = (1.0 - f) * Fb + u * (idx + 1.0); |
| 735 | p = (1.0 - f) * p + u; |
| 736 | return log(p > 1e-300 ? p : 1e-300); |
| 737 | } |
| 738 | |
| 739 | /* symmetric kernel at scale `scale`: returns est4_fast_logp (the exact G1 score) and the un-floored edges */ |
| 740 | static double sym_edges2(double m, double anchor, double y, double scale, double nu, double *lo, double *hi) |
| 741 | { |
| 742 | est4_pred pr = { 1u, m, scale, nu }; |
| 743 | double Fl, Fh; |
| 744 | double lp = est4_fast_logp(&pr, C3_QUANTUM, anchor, y, &Fl, &Fh); |
| 745 | double kd = nearbyint((y - anchor) / C3_QUANTUM); |
| 746 | if (kd < -(double)EST_PRED_K) kd = -(double)EST_PRED_K; |
| 747 | if (kd > (double)EST_PRED_K) kd = (double)EST_PRED_K; |
| 748 | double f = EST_PRED_FLOOR, u = EST_PRED_FLOOR / (double)EST_PRED_N, idx = kd + (double)EST_PRED_K; |
| 749 | *lo = (Fl - u * idx) / (1.0 - f); |
| 750 | *hi = (Fh - u * (idx + 1.0)) / (1.0 - f); |
| 751 | return lp; |
| 752 | } |
| 753 | |
| 754 | /* log score and floored F interval of one step under (kappa, scale c*b, nu); kappa 1.0 is est4_fast_logp itself */ |
| 755 | static double one_step(double kappa, double m, double anchor, double y, double scale, double nu, double *Flo, double *Fhi) |
| 756 | { |
| 757 | if (kappa == 1.0) { |
| 758 | est4_pred pr = { 1u, m, scale, nu }; |
| 759 | double fl, fh, lp = est4_fast_logp(&pr, C3_QUANTUM, anchor, y, &fl, &fh); |
| 760 | if (Flo) *Flo = fl; |
| 761 | if (Fhi) *Fhi = fh; |
| 762 | return lp; |
| 763 | } |
| 764 | double lo_u, hi_u, lo_l, hi_l; |
| 765 | sym_edges2(m, anchor, y, scale, nu, &lo_u, &hi_u); |
| 766 | sym_edges2(m, anchor, y, scale * kappa, nu, &lo_l, &hi_l); |
| 767 | return tp_point(kappa, m, anchor, y, lo_u, hi_u, lo_l, hi_l, Flo, Fhi); |
| 768 | } |
| 769 | |
| 770 | typedef struct { double dyn, q, lam, nu, c, kappa; int have; double train; double sum_all; } best1_t; |
| 771 | typedef struct { double phi, nuh, ch, kh; int have; double train; } best2_t; |
| 772 | |
| 773 | /* stage 2 on the steps selected by mask (keep[j] != 0): ten-step mean log score over phi nu_h c_h kappa_h */ |
| 774 | static void stage2(const tr2_t *tr, const int *keep, int kappa_free, best2_t *b) |
| 775 | { |
| 776 | memset(b, 0, sizeof *b); |
| 777 | size_t m = 0; |
| 778 | for (size_t j = 0; j < tr->n10; j++) m += keep[j] ? 1u : 0u; |
| 779 | if (!m) return; |
| 780 | double best = -INFINITY; |
| 781 | for (int ph = 0; ph < 5; ph++) |
| 782 | for (int u = 0; u < 10; u++) |
| 783 | for (int ci = 0; ci < NCHX; ci++) { |
| 784 | double ch = (double)(10 + 5 * ci) / 100.0, sum[NKS] = { 0 }; |
| 785 | for (size_t j = 0; j < tr->n10; j++) { |
| 786 | if (!keep[j]) continue; |
| 787 | double gb = est4_gbar(tr->g10[j], tr->gl10[j], PHIX[ph], C3_TEN), sc = ch * sqrt(gb * tr->v10[j]); |
| 788 | double lo_u, hi_u; |
| 789 | sum[NKS - 1] += sym_edges2(tr->m10[j], tr->a10[j], tr->y10[j], sc, NUHX[u], &lo_u, &hi_u); |
| 790 | if (kappa_free) |
| 791 | for (int ki = 0; ki < NKS - 1; ki++) { |
| 792 | double lo_l, hi_l; |
| 793 | sym_edges2(tr->m10[j], tr->a10[j], tr->y10[j], sc * KAPPA[ki], NUHX[u], &lo_l, &hi_l); |
| 794 | sum[ki] += tp_point(KAPPA[ki], tr->m10[j], tr->a10[j], tr->y10[j], lo_u, hi_u, lo_l, hi_l, NULL, NULL); |
| 795 | } |
| 796 | } |
| 797 | for (int ki = kappa_free ? 0 : NKS - 1; ki < NKS; ki++) { |
| 798 | double ls = sum[ki] / (double)m; |
| 799 | if (isfinite(ls) && ls > best) { best = ls; b->have = 1; b->phi = PHIX[ph]; b->nuh = NUHX[u]; b->ch = ch; b->kh = KAPPA[ki]; b->train = ls; } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | static double ten_step(const tr2_t *tr, size_t j, const best2_t *b, double *Flo, double *Fhi) |
| 805 | { |
| 806 | double gb = est4_gbar(tr->g10[j], tr->gl10[j], b->phi, C3_TEN), sc = b->ch * sqrt(gb * tr->v10[j]); |
| 807 | return one_step(b->kh, tr->m10[j], tr->a10[j], tr->y10[j], sc, b->nuh, Flo, Fhi); |
| 808 | } |
| 809 | |
| 810 | static double probit(double u) |
| 811 | { |
| 812 | if (u < 1e-12) u = 1e-12; |
| 813 | if (u > 1.0 - 1e-12) u = 1.0 - 1e-12; |
| 814 | double lo = -9, hi = 9; |
| 815 | for (int i = 0; i < 80; i++) { double mid = 0.5 * (lo + hi); if (0.5 * erfc(-mid / sqrt(2.0)) < u) lo = mid; else hi = mid; } |
| 816 | return 0.5 * (lo + hi); |
| 817 | } |
| 818 | |
| 819 | /* per-step statistic vector for the screen and the load-stratum analysis */ |
| 820 | #define NV 9 |
| 821 | static const char *VNAME[NV] = { "pit_bin0", "pit_bin9", "lower_miss", "upper_miss", "cov50", "cov80", "cov95", "mean_u_minus_half", "mean_z_mid" }; |
| 822 | static void stepvec(double flo, double fhi, double v[NV]) |
| 823 | { |
| 824 | step_t st = { 0, flo, fhi, 0 }; |
| 825 | double c[NSTAT]; |
| 826 | contrib(&st, c); |
| 827 | v[0] = c[0]; v[1] = c[9]; v[2] = c[10]; v[3] = c[11]; |
| 828 | v[4] = est_frac_cover(flo, fhi, 0.50); v[5] = est_frac_cover(flo, fhi, 0.80); v[6] = est_frac_cover(flo, fhi, 0.95); |
| 829 | double um = 0.5 * (flo + fhi); |
| 830 | v[7] = um - 0.5; v[8] = probit(um); |
| 831 | } |
| 832 | |
| 833 | typedef struct { size_t n; double *flo, *fhi; int *fold; int64_t *t; } oofset_t; |
| 834 | |
| 835 | typedef struct { double dyn, q, lam, nu, c, kappa; } cfg_t; |
| 836 | |
| 837 | static est4_params cfg_params(const cfg_t *c) |
| 838 | { |
| 839 | est4_params p = { EST4_G1_LAG, c->dyn, c->q, c->lam, c->nu, c->c, 0.95, 0.8, 0.2, C3_QUANTUM }; |
| 840 | return p; |
| 841 | } |
| 842 | |
| 843 | /* statistics over a set of one-step (or ten-step) F intervals, optionally restricted to a stratum */ |
| 844 | static void accum(double *sum, double *cnt, double flo, double fhi) |
| 845 | { |
| 846 | double v[NV]; |
| 847 | stepvec(flo, fhi, v); |
| 848 | for (int k = 0; k < NV; k++) sum[k] += v[k]; |
| 849 | *cnt += 1.0; |
| 850 | } |
| 851 | |
| 852 | static void print_stats(const char *tag, const double *sum, double cnt) |
| 853 | { |
| 854 | printf("%s n %.0f", tag, cnt); |
| 855 | for (int k = 0; k < NV; k++) printf(" %s %.4f", VNAME[k], cnt > 0 ? sum[k] / cnt : NAN); |
| 856 | printf("\n"); |
| 857 | } |
| 858 | |
| 859 | /* gates of v6 section 6 on pooled OOF statistics: outer band and the section 5 inner band (shrunk) */ |
| 860 | static void screen_line(const char *name, double v, double lo, double hi, double shrink) |
| 861 | { |
| 862 | int outer = v >= lo && v <= hi, inner = v >= lo + shrink && v <= hi - shrink; |
| 863 | printf("gate %s value %.4f outer [%.3f,%.3f] %s inner [%.3f,%.3f] %s\n", name, v, lo, hi, outer ? "PASS" : "FAIL", lo + shrink, hi - shrink, inner ? "PASS" : "FAIL"); |
| 864 | } |
| 865 | |
| 866 | static int cmd_g1sfull(int argc, char **argv) |
| 867 | { |
| 868 | const char *raw = NULL, *mkp = NULL; |
| 869 | int nfold = 5, stride = 1, boot = 2000; |
| 870 | for (int i = 0; i < argc; i++) { |
| 871 | if (!strcmp(argv[i], "--raw") && i + 1 < argc) raw = argv[++i]; |
| 872 | else if (!strcmp(argv[i], "--marks") && i + 1 < argc) mkp = argv[++i]; |
| 873 | else if (!strcmp(argv[i], "--folds") && i + 1 < argc) nfold = atoi(argv[++i]); |
| 874 | else if (!strcmp(argv[i], "--grid-stride") && i + 1 < argc) stride = atoi(argv[++i]); |
| 875 | else if (!strcmp(argv[i], "--boot") && i + 1 < argc) boot = atoi(argv[++i]); |
| 876 | else { fprintf(stderr, "usage: est6dev g1sfull --raw R --marks M [--folds K] [--grid-stride S] [--boot B]\n"); return 2; } |
| 877 | } |
| 878 | if (!raw || !mkp || nfold < 2 || nfold > 10 || stride < 1 || boot < 100) { fprintf(stderr, "est6dev g1sfull: missing or bad argument\n"); return 2; } |
| 879 | if (forbidden_path(raw) || forbidden_path(mkp)) { fprintf(stderr, "refuse: a held-out path (v5 D2 and every held-out tag are forbidden in v6 development)\n"); return 2; } |
| 880 | char err[512]; |
| 881 | est_file f; |
| 882 | if (est_file_load(raw, &f, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 1; } |
| 883 | if (!strcmp(f.sha_hex, D2_RAW_SHA)) { fprintf(stderr, "refuse: input is the v5 D2 raw data (SHA-256)\n"); est_file_free(&f); return 2; } |
| 884 | static c3_marks mk; |
| 885 | if (c3_marks_load(mkp, 1, &mk, err, sizeof err)) { fprintf(stderr, "refuse: marks: %s\n", err); est_file_free(&f); return 1; } |
| 886 | if (!strcmp(mk.sha, D2_MARKS_SHA)) { fprintf(stderr, "refuse: marks are the v5 D2 marks (SHA-256)\n"); est_file_free(&f); return 2; } |
| 887 | c3_ticks tk; |
| 888 | if (c3_ticks_build(&f, &tk, err, sizeof err)) { fprintf(stderr, "ticks: %s\n", err); est_file_free(&f); return 1; } |
| 889 | printf("input raw_sha256 %s marks_sha256 %s\n", f.sha_hex, mk.sha); |
| 890 | printf("label DEVELOPMENT RESULT (v5 D1 only; not a held-out result; v6 NOT FROZEN)\n"); |
| 891 | |
| 892 | tr2_t tr; |
| 893 | if (tr2_alloc(&tr, tk.n)) return 1; |
| 894 | size_t nb_max = 2 * mk.n + 2; |
| 895 | int *rank = XMALLOC(nb_max * sizeof *rank), *seen = XCALLOC(nb_max, sizeof *seen); |
| 896 | int *fold1 = NULL; |
| 897 | size_t n1 = 0; |
| 898 | double bestf[10][2]; /* unused placeholder keeps layout simple */ |
| 899 | (void)bestf; |
| 900 | best1_t bfree[10], bctl[10], ball_free, ball_ctl; /* per fold, then whole pool */ |
| 901 | memset(bfree, 0, sizeof bfree); memset(bctl, 0, sizeof bctl); memset(&ball_free, 0, sizeof ball_free); memset(&ball_ctl, 0, sizeof ball_ctl); |
| 902 | for (int i = 0; i < 10; i++) bfree[i].train = bctl[i].train = -INFINITY; |
| 903 | ball_free.train = ball_ctl.train = -INFINITY; |
| 904 | size_t points = 0, skipped = 0; |
| 905 | double *edlo = NULL, *edhi = NULL; |
| 906 | |
| 907 | for (int a = 0; a < NDYN; a += stride) |
| 908 | for (int bq = 0; bq < NQX; bq += stride) |
| 909 | for (int l = 0; l < NLAMX; l += stride) { |
| 910 | est4_params p = { EST4_G1_LAG, DYNX[a], pow(10.0, 2.0 + 0.5 * (double)bq), LAMX[l], 1.0, 1.0, 0.95, 0.8, 0.2, C3_QUANTUM }; |
| 911 | if (tr2_run(&tk, &p, &tr)) { skipped++; continue; } |
| 912 | if (!fold1) { |
| 913 | n1 = tr.n1; |
| 914 | fold1 = XMALLOC(n1 * sizeof *fold1); |
| 915 | edlo = XMALLOC(n1 * sizeof *edlo); edhi = XMALLOC(n1 * sizeof *edhi); |
| 916 | size_t *blk = XMALLOC(n1 * sizeof *blk); |
| 917 | for (size_t i = 0; i < n1; i++) { blk[i] = block_of(&mk, tr.t1[i]); seen[blk[i]] = 1; } |
| 918 | int nr = 0; |
| 919 | for (size_t bb = 0; bb < nb_max; bb++) rank[bb] = seen[bb] ? nr++ : -1; |
| 920 | for (size_t i = 0; i < n1; i++) fold1[i] = rank[blk[i]] % nfold; |
| 921 | free(blk); |
| 922 | } else if (tr.n1 != n1) { fprintf(stderr, "step set changed between grid points\n"); return 1; } |
| 923 | for (int u = 0; u < NNU; u += stride) |
| 924 | for (int ci = 0; ci < NCX; ci += stride) { |
| 925 | double c = (double)(20 + 5 * ci) / 100.0; |
| 926 | double sf[NKS][10], tot[NKS]; |
| 927 | memset(sf, 0, sizeof sf); |
| 928 | for (size_t i = 0; i < n1; i++) { |
| 929 | double lo_u, hi_u, lo_l, hi_l; |
| 930 | sf[NKS - 1][fold1[i]] += sym_edges2(tr.m1[i], tr.a1[i], tr.y1[i], c * tr.b1[i], NUX[u], &lo_u, &hi_u); |
| 931 | for (int ki = 0; ki < NKS - 1; ki++) { |
| 932 | sym_edges2(tr.m1[i], tr.a1[i], tr.y1[i], c * KAPPA[ki] * tr.b1[i], NUX[u], &lo_l, &hi_l); |
| 933 | sf[ki][fold1[i]] += tp_point(KAPPA[ki], tr.m1[i], tr.a1[i], tr.y1[i], lo_u, hi_u, lo_l, hi_l, NULL, NULL); |
| 934 | } |
| 935 | } |
| 936 | points++; |
| 937 | for (int ki = 0; ki < NKS; ki++) { |
| 938 | tot[ki] = 0; |
| 939 | for (int fo = 0; fo < nfold; fo++) tot[ki] += sf[ki][fo]; |
| 940 | cfg_t cf = { DYNX[a], p.q, LAMX[l], NUX[u], c, KAPPA[ki] }; |
| 941 | for (int fo = 0; fo < nfold; fo++) { |
| 942 | double tr_ls = (tot[ki] - sf[ki][fo]); |
| 943 | best1_t *bb = ki == NKS - 1 ? &bctl[fo] : NULL; |
| 944 | if (bb && isfinite(tr_ls) && tr_ls > bb->train) { *bb = (best1_t){ cf.dyn, cf.q, cf.lam, cf.nu, cf.c, cf.kappa, 1, tr_ls, sf[ki][fo] }; } |
| 945 | if (isfinite(tr_ls) && tr_ls > bfree[fo].train) bfree[fo] = (best1_t){ cf.dyn, cf.q, cf.lam, cf.nu, cf.c, cf.kappa, 1, tr_ls, sf[ki][fo] }; |
| 946 | } |
| 947 | if (isfinite(tot[ki]) && tot[ki] > ball_free.train) ball_free = (best1_t){ cf.dyn, cf.q, cf.lam, cf.nu, cf.c, cf.kappa, 1, tot[ki], tot[ki] }; |
| 948 | if (ki == NKS - 1 && isfinite(tot[ki]) && tot[ki] > ball_ctl.train) ball_ctl = (best1_t){ cf.dyn, cf.q, cf.lam, cf.nu, cf.c, cf.kappa, 1, tot[ki], tot[ki] }; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | if (!fold1) { fprintf(stderr, "no grid point ran\n"); return 1; } |
| 953 | printf("grid dyn %d q %d lam %d nu %d c %d kappa %d stride %d points %zu filter_failed %zu steps %zu folds %d\n", NDYN, NQX, NLAMX, NNU, NCX, NKS, stride, points, skipped, n1, nfold); |
| 954 | (void)edlo; (void)edhi; |
| 955 | |
| 956 | /* out-of-fold predictions for the chosen configuration of each fold: one-step and ten-step */ |
| 957 | oofset_t oo[2]; /* 0 = G1S free kappa, 1 = control kappa 1.0 */ |
| 958 | size_t n10 = 0; |
| 959 | double *flo10[2] = { NULL, NULL }, *fhi10[2] = { NULL, NULL }, *lp10[2] = { NULL, NULL }; |
| 960 | int64_t *t10 = NULL; int *fold10v = NULL; |
| 961 | for (int w = 0; w < 2; w++) { |
| 962 | oo[w].n = n1; oo[w].flo = XCALLOC(n1, sizeof(double)); oo[w].fhi = XCALLOC(n1, sizeof(double)); oo[w].fold = fold1; oo[w].t = NULL; |
| 963 | } |
| 964 | double *lp1o[2] = { XCALLOC(n1, sizeof(double)), XCALLOC(n1, sizeof(double)) }; |
| 965 | int *step_ok1 = XCALLOC(n1, sizeof(int)); |
| 966 | (void)step_ok1; |
| 967 | char tags[2][8] = { "G1S", "G1ctl" }; |
| 968 | for (int w = 0; w < 2; w++) { |
| 969 | printf("== %s selection by %d-fold block CV, then stage 2 (ten-step) per fold\n", w ? "control kappa=1.0 (G1 exactly)" : "G1S kappa free in {0.6..1.0}", nfold); |
| 970 | for (int fo = 0; fo < nfold; fo++) { |
| 971 | best1_t *b = w ? &bctl[fo] : &bfree[fo]; |
| 972 | cfg_t cf = { b->dyn, b->q, b->lam, b->nu, b->c, b->kappa }; |
| 973 | est4_params p = cfg_params(&cf); |
| 974 | if (tr2_run(&tk, &p, &tr) || tr.n1 != n1) { fprintf(stderr, "refit trace failed\n"); return 1; } |
| 975 | size_t nf = 0; double s = 0; |
| 976 | for (size_t i = 0; i < n1; i++) if (fold1[i] == fo) { |
| 977 | double fl, fh, lp = one_step(cf.kappa, tr.m1[i], tr.a1[i], tr.y1[i], cf.c * tr.b1[i], cf.nu, &fl, &fh); |
| 978 | oo[w].flo[i] = fl; oo[w].fhi[i] = fh; lp1o[w][i] = lp; s += lp; nf++; |
| 979 | } |
| 980 | if (!fold10v) { |
| 981 | n10 = tr.n10; fold10v = XMALLOC(n10 * sizeof *fold10v); t10 = XMALLOC(n10 * sizeof *t10); |
| 982 | for (size_t j = 0; j < n10; j++) { fold10v[j] = rank[block_of(&mk, tr.t10[j])] % nfold; t10[j] = tr.t10[j]; } |
| 983 | for (int ww = 0; ww < 2; ww++) { flo10[ww] = XCALLOC(n10, sizeof(double)); fhi10[ww] = XCALLOC(n10, sizeof(double)); lp10[ww] = XCALLOC(n10, sizeof(double)); } |
| 984 | } |
| 985 | int *keep = XMALLOC(tr.n10 * sizeof *keep); |
| 986 | for (size_t j = 0; j < tr.n10; j++) keep[j] = fold10v[j] != fo; |
| 987 | best2_t b2; |
| 988 | stage2(&tr, keep, !w, &b2); |
| 989 | if (!b2.have) { fprintf(stderr, "est6dev: stage 2 found no finite score (fold %d)\n", fo); return 1; } |
| 990 | for (size_t j = 0; j < tr.n10; j++) if (fold10v[j] == fo) lp10[w][j] = ten_step(&tr, j, &b2, &flo10[w][j], &fhi10[w][j]); |
| 991 | free(keep); |
| 992 | printf("fold %d steps %zu stage1 dyn %g q %g lam %g nu %g c %.2f kappa %.1f train_mean_logscore %.6f oof_mean_logscore %.6f | stage2 phi %g nu_h %g c_h %.2f kappa_h %.1f\n", |
| 993 | fo, nf, cf.dyn, cf.q, cf.lam, cf.nu, cf.c, cf.kappa, b->train / (double)(n1 - nf), nf ? s / (double)nf : 0.0, b2.phi, b2.nuh, b2.ch, b2.kh); |
| 994 | } |
| 995 | /* pooled OOF statistics and the screen */ |
| 996 | double sum[NV] = { 0 }, cnt = 0, lsum = 0; |
| 997 | for (size_t i = 0; i < n1; i++) { accum(sum, &cnt, oo[w].flo[i], oo[w].fhi[i]); lsum += lp1o[w][i]; } |
| 998 | printf("oof_pooled %s mean_logscore %.6f\n", tags[w], lsum / (double)n1); |
| 999 | print_stats("oof_pooled", sum, cnt); |
| 1000 | double ten_cov = 0, ten_ls = 0; |
| 1001 | for (size_t j = 0; j < n10; j++) { ten_cov += est_frac_cover(flo10[w][j], fhi10[w][j], 0.95); ten_ls += lp10[w][j]; } |
| 1002 | printf("oof_pooled_ten_step %s n %zu coverage95 %.4f mean_logscore %.6f\n", tags[w], n10, ten_cov / (double)n10, ten_ls / (double)n10); |
| 1003 | { /* bins */ |
| 1004 | double bins[10] = { 0 }; |
| 1005 | for (size_t i = 0; i < n1; i++) { step_t st = { 0, oo[w].flo[i], oo[w].fhi[i], 0 }; double c[NSTAT]; contrib(&st, c); for (int j = 0; j < 10; j++) bins[j] += c[j]; } |
| 1006 | printf("oof_bins %s", tags[w]); |
| 1007 | for (int j = 0; j < 10; j++) printf(" %.4f", bins[j] / (double)n1); |
| 1008 | printf("\n"); |
| 1009 | double lag = 0, zz = 0, zm = 0; size_t ln = 0; |
| 1010 | double zprev = NAN; |
| 1011 | for (size_t i = 0; i < n1; i++) { |
| 1012 | double z = probit(0.5 * (oo[w].flo[i] + oo[w].fhi[i])); |
| 1013 | zz += z * z; zm += z; |
| 1014 | if (i > 0 && tr.t1[i] - tr.t1[i - 1] > 500000000LL && tr.t1[i] - tr.t1[i - 1] < 1500000000LL) { lag += z * zprev; ln++; } |
| 1015 | zprev = z; |
| 1016 | } |
| 1017 | double zbar = zm / (double)n1, var = zz / (double)n1 - zbar * zbar; |
| 1018 | (void)var; |
| 1019 | printf("oof_lag1_z_uncentred %s %.4f over %zu consecutive pairs\n", tags[w], ln ? lag / (double)ln : NAN, ln); |
| 1020 | screen_line("P1 cov50", sum[4] / cnt, 0.46, 0.54, 0.01); |
| 1021 | screen_line("P2 cov80", sum[5] / cnt, 0.76, 0.84, 0.01); |
| 1022 | screen_line("P3 cov95", sum[6] / cnt, 0.93, 0.97, 0.02); |
| 1023 | screen_line("P4 lower_miss", sum[2] / cnt, 0.010, 0.040, 0.01); |
| 1024 | screen_line("P5 upper_miss", sum[3] / cnt, 0.010, 0.040, 0.01); |
| 1025 | for (int j = 0; j < 10; j++) { char nm[16]; snprintf(nm, sizeof nm, "P6 bin%d", j); screen_line(nm, bins[j] / (double)n1, 0.07, 0.13, 0.01); } |
| 1026 | screen_line("P7 ten_cov95", ten_cov / (double)n10, 0.90, 0.99, 0.03); |
| 1027 | } |
| 1028 | /* load-stratum analysis of the OOF PIT with block bootstrap within stratum */ |
| 1029 | { |
| 1030 | size_t nb = nb_max; |
| 1031 | double (*bs)[NV] = XCALLOC(nb, sizeof *bs); double *bn = XCALLOC(nb, sizeof *bn); |
| 1032 | for (size_t i = 0; i < n1; i++) { size_t b = block_of(&mk, tr.t1[i]); accum(bs[b], &bn[b], oo[w].flo[i], oo[w].fhi[i]); } |
| 1033 | const int lvv[4] = { 0, 6, 12, 18 }; |
| 1034 | for (int li = 0; li < 4; li++) { |
| 1035 | size_t *ids = XMALLOC(nb * sizeof *ids); size_t ni = 0; |
| 1036 | double tot[NV] = { 0 }, tn = 0; |
| 1037 | for (size_t b = 0; b < nb; b++) { |
| 1038 | if (bn[b] <= 0) continue; |
| 1039 | int lv = (b & 1u) ? mk.level[b / 2] : 0; |
| 1040 | if (lv != lvv[li]) continue; |
| 1041 | ids[ni++] = b; tn += bn[b]; |
| 1042 | for (int k = 0; k < NV; k++) tot[k] += bs[b][k]; |
| 1043 | } |
| 1044 | if (ni == 0) { free(ids); continue; } |
| 1045 | uint64_t rng = 0xE6B007ull + (uint64_t)li; |
| 1046 | double *dr = XMALLOC((size_t)boot * NV * sizeof *dr); |
| 1047 | for (int r = 0; r < boot; r++) { |
| 1048 | double ss[NV] = { 0 }, nn = 0; |
| 1049 | for (size_t k = 0; k < ni; k++) { size_t b = ids[splitmix64(&rng) % ni]; nn += bn[b]; for (int q = 0; q < NV; q++) ss[q] += bs[b][q]; } |
| 1050 | for (int q = 0; q < NV; q++) dr[(size_t)r * NV + q] = ss[q] / nn; |
| 1051 | } |
| 1052 | printf("stratum %s L%d blocks %zu steps %.0f", tags[w], lvv[li], ni, tn); |
| 1053 | for (int k = 0; k < NV; k++) { |
| 1054 | if (k == 4 || k == 5) continue; |
| 1055 | double mean = 0, var = 0; |
| 1056 | for (int r = 0; r < boot; r++) mean += dr[(size_t)r * NV + k]; |
| 1057 | mean /= boot; |
| 1058 | for (int r = 0; r < boot; r++) { double d = dr[(size_t)r * NV + k] - mean; var += d * d; } |
| 1059 | var /= (boot - 1); |
| 1060 | printf(" %s %.4f(se %.4f)", VNAME[k], tot[k] / tn, sqrt(var)); |
| 1061 | } |
| 1062 | printf("\n"); |
| 1063 | free(dr); free(ids); |
| 1064 | } |
| 1065 | free(bs); free(bn); |
| 1066 | } |
| 1067 | } |
| 1068 | /* final refit on the whole pool (committed params would come from this) */ |
| 1069 | printf("== final whole-pool refit (stage 1; same grid)\n"); |
| 1070 | printf("final G1S dyn %g q %g lam %g nu %g c %.2f kappa %.1f in_sample_mean_logscore %.6f\n", ball_free.dyn, ball_free.q, ball_free.lam, ball_free.nu, ball_free.c, ball_free.kappa, ball_free.train / (double)n1); |
| 1071 | printf("final control dyn %g q %g lam %g nu %g c %.2f kappa %.1f in_sample_mean_logscore %.6f\n", ball_ctl.dyn, ball_ctl.q, ball_ctl.lam, ball_ctl.nu, ball_ctl.c, ball_ctl.kappa, ball_ctl.train / (double)n1); |
| 1072 | printf("tie_rule G1S preferred over G1 only if out-of-fold log score exceeds the control by more than 0.01 nats\n"); |
| 1073 | c3_ticks_free(&tk); est_file_free(&f); tr2_free(&tr); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | /* two-piece predictive self check on synthetic inputs (no data file): for each kappa, nu, scale and location |
| 1079 | * the bin probabilities over k = -K..K sum to 1, the floored CDF is continuous and non-decreasing across bins, |
| 1080 | * and kappa = 1.0 is exactly est4_fast_logp. The two-piece formula at kappa = 1.0 agrees with it to rounding. */ |
| 1081 | static int cmd_twopiece_selfcheck(void) |
| 1082 | { |
| 1083 | static const double nus[3] = { 0.8, 1.25, 5.0 }, scs[3] = { 0.3, 1.0, 4.0 }, locs[3] = { 0.0, 0.37, -2.6 }; |
| 1084 | double maxsum = 0, maxjump = 0, maxk1 = 0; int bad = 0; |
| 1085 | for (int ki = 0; ki < NKS; ki++) |
| 1086 | for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) for (int c = 0; c < 3; c++) { |
| 1087 | double anchor = 1234.0 * C3_QUANTUM, m = anchor + locs[c] * C3_QUANTUM, sum = 0, prev_hi = 0; |
| 1088 | for (int k = -EST_PRED_K; k <= EST_PRED_K; k++) { |
| 1089 | double y = anchor + (double)k * C3_QUANTUM, fl, fh; |
| 1090 | double lp = one_step(KAPPA[ki], m, anchor, y, scs[b] * C3_QUANTUM, nus[a], &fl, &fh); |
| 1091 | sum += exp(lp); |
| 1092 | if (k > -EST_PRED_K) { double j = fabs(fl - prev_hi); if (j > maxjump) maxjump = j; } |
| 1093 | if (fh < fl - 1e-15) bad = 1; |
| 1094 | prev_hi = fh; |
| 1095 | if (KAPPA[ki] == 1.0 && k >= -3 && k <= 3) { |
| 1096 | double lo_u, hi_u, lp2, f2l, f2h; |
| 1097 | sym_edges2(m, anchor, y, scs[b] * C3_QUANTUM, nus[a], &lo_u, &hi_u); |
| 1098 | lp2 = tp_point(1.0, m, anchor, y, lo_u, hi_u, lo_u, hi_u, &f2l, &f2h); |
| 1099 | double d = fabs(lp2 - lp); if (d > maxk1) maxk1 = d; |
| 1100 | } |
| 1101 | } |
| 1102 | if (fabs(sum - 1.0) > maxsum) maxsum = fabs(sum - 1.0); |
| 1103 | } |
| 1104 | printf("twopiece max_abs(sum p - 1) %.3g max_cdf_jump_between_bins %.3g max_abs(two_piece_formula_at_kappa1 - est4_fast_logp) %.3g monotone %s\n", maxsum, maxjump, maxk1, bad ? "NO" : "yes"); |
| 1105 | printf("twopiece-selfcheck: %s\n", (maxsum < 1e-9 && maxjump < 1e-9 && maxk1 < 1e-9 && !bad) ? "PASS" : "FAIL"); |
| 1106 | return (maxsum < 1e-9 && maxjump < 1e-9 && maxk1 < 1e-9 && !bad) ? 0 : 1; |
| 1107 | } |
| 1108 | |
| 1109 | int main(int argc, char **argv) |
| 1110 | { |
| 1111 | if (argc < 2) { fprintf(stderr, "usage: est6dev g1pit|g1s|g1sfull|twopiece-selfcheck|sched|pick|plan6 ...\n"); return 2; } |
| 1112 | if (!strcmp(argv[1], "g1pit")) return cmd_g1pit(argc - 2, argv + 2); |
| 1113 | if (!strcmp(argv[1], "sched")) return cmd_sched(argc - 2, argv + 2); |
| 1114 | if (!strcmp(argv[1], "pick")) return cmd_pick(argc - 2, argv + 2); |
| 1115 | if (!strcmp(argv[1], "plan6")) return cmd_plan6(argc - 2, argv + 2); |
| 1116 | if (!strcmp(argv[1], "g1s")) return cmd_g1s(argc - 2, argv + 2); |
| 1117 | if (!strcmp(argv[1], "g1sfull")) return cmd_g1sfull(argc - 2, argv + 2); |
| 1118 | if (!strcmp(argv[1], "twopiece-selfcheck")) return cmd_twopiece_selfcheck(); |
| 1119 | fprintf(stderr, "usage: est6dev g1pit|g1s|g1sfull|twopiece-selfcheck|sched|pick|plan6 ...\n"); |
| 1120 | return 2; |
| 1121 | } |