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 / est3c_fit.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 | /* EST-3c protocol v3, section 5: the D1 fit (docs/estimation/EST3C_PROTOCOL_V3.md). |
| 18 | * |
| 19 | * est3c_fit --raw <dir>/machine-state.ndjson --marks <dir>/machine-state-marks.txt |
| 20 | * --out <params.txt> [--synthetic-test] [--grid-stride N] |
| 21 | * |
| 22 | * Grids exactly as section 5 (ties: first grid point in listed order, i.e. the |
| 23 | * first-listed parameter is the outer loop; strict > keeps the earlier point). |
| 24 | * Objective: mean discrete one-step log score after burn-in (est3c_common.h). |
| 25 | * F4 is the EM fit (k = 3, floor 100^2/12, 1000 iterations) on the horizon-1 |
| 26 | * changes. Every family is then screened in sample with every section 6 rule, |
| 27 | * and the selection rule picks the family (or PHASE_A_FAIL). |
| 28 | * |
| 29 | * Binding mode (no --synthetic-test) refuses: a dirty tree at build time, a |
| 30 | * compiled protocol SHA that is "unknown" or differs from the doc on disk, an |
| 31 | * input outside the D1 run folder, any held-out (D2) path, an existing output, |
| 32 | * --grid-stride. --synthetic-test lifts the dirty-tree and D1 guards (never the |
| 33 | * D2 guard) and marks the parameter file synthetic_test 1; --grid-stride N |
| 34 | * (synthetic only) keeps every Nth grid point per axis, for tests. */ |
| 35 | #include <errno.h> |
| 36 | #include <limits.h> |
| 37 | #include <math.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include "est3c_common.h" |
| 43 | #include "sha256.h" |
| 44 | |
| 45 | #ifndef TOOL_COMMIT |
| 46 | #define TOOL_COMMIT "unknown" |
| 47 | #endif |
| 48 | #ifndef TOOL_DIRTY |
| 49 | #define TOOL_DIRTY 1 |
| 50 | #endif |
| 51 | #ifndef C3_PROTOCOL_SHA |
| 52 | #define C3_PROTOCOL_SHA "unknown" |
| 53 | #endif |
| 54 | |
| 55 | static int tool_dirty(void) |
| 56 | { |
| 57 | #ifdef C3_TEST_BUILD |
| 58 | const char *e = getenv("C3_TEST_DIRTY"); if (e && *e) return atoi(e); |
| 59 | return TOOL_DIRTY; |
| 60 | #else |
| 61 | /* measured once, at the first call (before any output file exists) */ |
| 62 | static int cached = -1; |
| 63 | if (cached < 0) cached = TOOL_DIRTY ? TOOL_DIRTY : c3_tree_dirty_now(); |
| 64 | return cached; |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | /* compiled protocol SHA (test build: C3_TEST_PROTOCOL_SHA overrides it) */ |
| 69 | static const char *protocol_sha(void) |
| 70 | { |
| 71 | #ifdef C3_TEST_BUILD |
| 72 | const char *e = getenv("C3_TEST_PROTOCOL_SHA"); if (e) return e; |
| 73 | #endif |
| 74 | return C3_PROTOCOL_SHA; |
| 75 | } |
| 76 | |
| 77 | static int path_in_d1(const char *path) |
| 78 | { |
| 79 | char rp[PATH_MAX]; |
| 80 | return strstr(path, C3_D1_ID) != NULL || (realpath(path, rp) && strstr(rp, C3_D1_ID) != NULL); |
| 81 | } |
| 82 | |
| 83 | /* ---- grids (protocol section 5) ---- */ |
| 84 | #define NQ 25 /* q_proc = 10^(1.0 + 0.25 i) */ |
| 85 | #define NR 17 /* r = 10^(2.0 + 0.25 j) */ |
| 86 | #define NC2 4 |
| 87 | #define NNU3 10 |
| 88 | #define NS3 51 /* s = 10^(1.0 + 0.05 j) */ |
| 89 | #define NL5 7 |
| 90 | #define NNU5 7 |
| 91 | #define NC5 21 /* c = 0.50 + 0.05 j */ |
| 92 | #define NF5 3 |
| 93 | static const double C2[NC2] = { 1.0, 1.345, 2.0, 3.0 }; |
| 94 | static const double NU3[NNU3] = { 1.5, 2, 2.5, 3, 4, 5, 7, 10, 15, 30 }; |
| 95 | static const double L5[NL5] = { 0.5, 0.7, 0.8, 0.9, 0.95, 0.97, 0.99 }; |
| 96 | static const double NU5[NNU5] = { 2, 3, 4, 5, 7, 10, 30 }; |
| 97 | static const double F5FL[NF5] = { 100.0 * 100.0 / 12.0, 50.0 * 50.0, 100.0 * 100.0 }; |
| 98 | static double gq(int i) { return pow(10.0, 1.0 + 0.25 * (double)i); } |
| 99 | static double gr(int j) { return pow(10.0, 2.0 + 0.25 * (double)j); } |
| 100 | static double gs(int j) { return pow(10.0, 1.0 + 0.05 * (double)j); } |
| 101 | static double gc5(int j) { return (double)(50 + 5 * j) / 100.0; } |
| 102 | |
| 103 | typedef struct { |
| 104 | int have; c3_model m; double ls; |
| 105 | size_t points, finite, errors; |
| 106 | } fam_fit; |
| 107 | |
| 108 | static void consider(fam_fit *ff, const c3_ticks *tk, const c3_model *m) |
| 109 | { |
| 110 | c3_stats st; |
| 111 | ff->points++; |
| 112 | if (c3_score(tk, NULL, m, 0, &st)) { ff->errors++; return; } |
| 113 | if (!isfinite(st.logscore)) return; |
| 114 | ff->finite++; |
| 115 | if (c3_grid_take(ff->have, st.logscore, ff->ls)) { ff->have = 1; ff->m = *m; ff->ls = st.logscore; } |
| 116 | } |
| 117 | |
| 118 | static void put_family(FILE *fp, int id, const fam_fit *ff) |
| 119 | { |
| 120 | fprintf(fp, "family %s available %d nparam %u param", c3_name(id), ff->have, ff->have ? ff->m.a.nparam : 0u); |
| 121 | for (int j = 0; j < 8; j++) fprintf(fp, " %.17g", ff->have ? ff->m.a.param[j] : 0.0); |
| 122 | fprintf(fp, " logscore %.17g grid_points %zu grid_finite %zu grid_errors %zu\n", |
| 123 | ff->have ? ff->ls : -INFINITY, ff->points, ff->finite, ff->errors); |
| 124 | } |
| 125 | |
| 126 | static void put_screen(FILE *fp, int id, const c3_stats *st, int have) |
| 127 | { |
| 128 | if (!have) { fprintf(fp, "screen_result %s pass 0 first_fail unavailable n 0\n", c3_name(id)); return; } |
| 129 | for (int i = 0; i < C3_ST_COUNT; i++) |
| 130 | fprintf(fp, "screen %s %s value %.17g band %.17g %.17g n %zu gated %d pass %d\n", c3_name(id), c3_stat_name(i), |
| 131 | st->value[i], st->band_lo[i], st->band_hi[i], |
| 132 | (i >= C3_ST_Q0 && i <= C3_ST_TEN) ? st->sub_n[i] : st->n, st->gated[i], st->pass[i]); |
| 133 | fprintf(fp, "screen_sharpness %s width80_mean_mc %.17g width80_median_mc %.17g unscorable %zu\n", |
| 134 | c3_name(id), st->width_mean, st->width_median, st->unscorable); |
| 135 | fprintf(fp, "screen_result %s pass %d first_fail %s n %zu logscore %.17g\n", c3_name(id), st->calibrated, |
| 136 | st->calibrated ? "none" : c3_stat_name(st->first_fail), st->n, st->logscore); |
| 137 | } |
| 138 | |
| 139 | #ifdef C3_AS_LIB |
| 140 | #define C3_MAIN c3_fit_main |
| 141 | int c3_fit_main(int argc, char **argv); |
| 142 | #else |
| 143 | #define C3_MAIN main |
| 144 | #endif |
| 145 | |
| 146 | int C3_MAIN(int argc, char **argv) |
| 147 | { |
| 148 | const char *raw = NULL, *mkp = NULL, *out = NULL; |
| 149 | int synth = 0, stride = 1; |
| 150 | for (int i = 1; i < argc; i++) { |
| 151 | if (!strcmp(argv[i], "--raw") && i + 1 < argc) raw = argv[++i]; |
| 152 | else if (!strcmp(argv[i], "--marks") && i + 1 < argc) mkp = argv[++i]; |
| 153 | else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i]; |
| 154 | else if (!strcmp(argv[i], "--synthetic-test")) synth = 1; |
| 155 | else if (!strcmp(argv[i], "--grid-stride") && i + 1 < argc) stride = atoi(argv[++i]); |
| 156 | else { fprintf(stderr, "usage: est3c_fit --raw <ndjson> --marks <marks> --out <params.txt> [--synthetic-test] [--grid-stride N]\n"); return 2; } |
| 157 | } |
| 158 | if (!raw || !mkp || !out) { fprintf(stderr, "est3c_fit: --raw, --marks and --out are required\n"); return 2; } |
| 159 | if (stride < 1) { fprintf(stderr, "refuse: --grid-stride must be >= 1\n"); return 2; } |
| 160 | if (c3_path_is_heldout(raw) || c3_path_is_heldout(mkp)) { fprintf(stderr, "refuse: input is a held-out (D2) run; fitting on it is not allowed\n"); return 2; } |
| 161 | if (!synth) { |
| 162 | if (tool_dirty()) { fprintf(stderr, "refuse: the binding fit needs a clean working tree at build time (TOOL_DIRTY=%d); use --synthetic-test for tests\n", tool_dirty()); return 2; } |
| 163 | if (!path_in_d1(raw) || !path_in_d1(mkp)) { fprintf(stderr, "refuse: the binding fit opens only D1 (%s)\n", C3_D1_ID); return 2; } |
| 164 | char doc[65] = ""; |
| 165 | if (!strcmp(protocol_sha(), "unknown") || est_sha_file_hex(C3_PROTOCOL_DOC, doc) || strcmp(doc, protocol_sha())) { |
| 166 | fprintf(stderr, "refuse: the binding fit needs the compiled protocol SHA (%s) to equal the SHA-256 of %s on disk (%s)\n", protocol_sha(), C3_PROTOCOL_DOC, doc[0] ? doc : "unreadable"); return 2; } |
| 167 | #ifndef C3_TEST_BUILD |
| 168 | if (stride != 1) { fprintf(stderr, "refuse: --grid-stride only with --synthetic-test\n"); return 2; } |
| 169 | #endif |
| 170 | } |
| 171 | if (strpbrk(raw, " \t\n") || strpbrk(mkp, " \t\n")) { fprintf(stderr, "refuse: paths with whitespace are not supported\n"); return 2; } |
| 172 | |
| 173 | char err[512]; |
| 174 | est_file f; |
| 175 | if (est_file_load(raw, &f, err, sizeof err)) { fprintf(stderr, "refuse: %s\n", err); return 1; } |
| 176 | static c3_marks mk; |
| 177 | if (c3_marks_load(mkp, 1, &mk, err, sizeof err)) { fprintf(stderr, "refuse: marks: %s\n", err); est_file_free(&f); return 1; } |
| 178 | FILE *fp = fopen(out, "wbx"); |
| 179 | if (!fp) { fprintf(stderr, "refuse: cannot create %s (%s); outputs are never overwritten\n", out, strerror(errno)); est_file_free(&f); return 1; } |
| 180 | c3_ticks tk; |
| 181 | if (c3_ticks_build(&f, &tk, err, sizeof err)) { fprintf(stderr, "ticks: %s\n", err); fclose(fp); est_file_free(&f); return 1; } |
| 182 | |
| 183 | fam_fit ff[6]; |
| 184 | memset(ff, 0, sizeof ff); |
| 185 | c3_model m; |
| 186 | for (int i = 0; i < NQ; i += stride) |
| 187 | for (int j = 0; j < NR; j += stride) { c3_make_f1(&m, gq(i), gr(j)); consider(&ff[1], &tk, &m); } |
| 188 | fprintf(stderr, "est3c_fit: F1 done (%zu points)\n", ff[1].points); |
| 189 | for (int i = 0; i < NQ; i += stride) |
| 190 | for (int j = 0; j < NR; j += stride) |
| 191 | for (int k = 0; k < NC2; k += (stride > 1 ? 2 : 1)) { c3_make_f2(&m, gq(i), gr(j), C2[k]); consider(&ff[2], &tk, &m); } |
| 192 | fprintf(stderr, "est3c_fit: F2 done (%zu points)\n", ff[2].points); |
| 193 | for (int i = 0; i < NNU3; i += (stride > 1 ? 2 : 1)) |
| 194 | for (int j = 0; j < NS3; j += stride) { c3_make_f3(&m, NU3[i], gs(j)); consider(&ff[3], &tk, &m); } |
| 195 | fprintf(stderr, "est3c_fit: F3 done (%zu points)\n", ff[3].points); |
| 196 | |
| 197 | /* F4: EM on the horizon-1 changes (also the E0 histogram) */ |
| 198 | double *e = malloc((tk.n + 1) * sizeof *e); |
| 199 | if (!e) { fprintf(stderr, "out of memory\n"); fclose(fp); return 1; } |
| 200 | size_t ne = c3_h1_changes(&tk, e, tk.n + 1); |
| 201 | est_mix mx; double em_ll = NAN; |
| 202 | memset(&mx, 0, sizeof mx); |
| 203 | est_status ems = est_mix_fit_em(e, ne, 3u, 100.0 * 100.0 / 12.0, 1000u, &mx, &em_ll); |
| 204 | if (ems == EST_OK && c3_make_f4(&m, &mx) == 0 && est_assumption_check(&m.a) == EST_OK) consider(&ff[4], &tk, &m); |
| 205 | else ff[4].points = 1; |
| 206 | uint32_t *e0c = calloc(EST_PRED_N, sizeof *e0c); |
| 207 | if (!e0c) { fprintf(stderr, "out of memory\n"); fclose(fp); return 1; } |
| 208 | c3_e0_counts(e, ne, e0c); |
| 209 | fprintf(stderr, "est3c_fit: F4 EM status %d on %zu changes\n", (int)ems, ne); |
| 210 | |
| 211 | for (int a = 0; a < NL5; a += (stride > 1 ? 3 : 1)) |
| 212 | for (int b = 0; b < NNU5; b += (stride > 1 ? 3 : 1)) |
| 213 | for (int c = 0; c < NC5; c += stride) |
| 214 | for (int d = 0; d < NF5; d += (stride > 1 ? 2 : 1)) { c3_make_f5(&m, L5[a], NU5[b], gc5(c), F5FL[d]); consider(&ff[5], &tk, &m); } |
| 215 | fprintf(stderr, "est3c_fit: F5 done (%zu points)\n", ff[5].points); |
| 216 | |
| 217 | /* in-sample screen and selection */ |
| 218 | static c3_stats st[6]; |
| 219 | for (int id = 1; id <= 5; id++) { |
| 220 | memset(&st[id], 0, sizeof st[id]); |
| 221 | if (!ff[id].have) continue; |
| 222 | if (c3_score(&tk, &mk, &ff[id].m, 1, &st[id])) { fprintf(stderr, "screen %s: %s\n", c3_name(id), st[id].errmsg); ff[id].have = 0; } |
| 223 | } |
| 224 | int av[6], ps[6]; double lsv[6]; |
| 225 | for (int id = 0; id <= 5; id++) { av[id] = ff[id].have; ps[id] = id ? st[id].calibrated : 0; lsv[id] = ff[id].ls; } |
| 226 | int sel = c3_select(av, ps, lsv); |
| 227 | |
| 228 | char marks_sha[65]; memcpy(marks_sha, mk.sha, 65); |
| 229 | char doc_hex[65] = "unavailable"; |
| 230 | est_sha_file_hex(C3_PROTOCOL_DOC, doc_hex); |
| 231 | fprintf(fp, "est3c_params v3\n"); |
| 232 | fprintf(fp, "protocol %s\nprotocol_doc_sha256 %s\nprotocol_doc_sha256_on_disk %s\n", C3_PROTOCOL_DOC, protocol_sha(), doc_hex); |
| 233 | fprintf(fp, "tool_commit %s\ntool_dirty %d\nsynthetic_test %d\n", TOOL_COMMIT, tool_dirty(), synth); |
| 234 | fprintf(fp, "fit_raw_path %s\nfit_raw_sha256 %s\nfit_marks_path %s\nfit_marks_sha256 %s\nfit_lines %zu\n", raw, f.sha_hex, mkp, marks_sha, f.nlines); |
| 235 | fprintf(fp, "assumption quantum %.17g lo %.17g hi %.17g unit milli_celsius\n", C3_QUANTUM, C3_LO, C3_HI); |
| 236 | fprintf(fp, "rule burn_in %u ticks from the first valid observation\n", C3_BURN_IN); |
| 237 | fprintf(fp, "rule f1_f2_p0 p0 = r\nrule f5_s0 s0 = sqrt(floor)\n"); |
| 238 | fprintf(fp, "rule tick_mapping one tick per line; round(gap)-1 inserted MISSING ticks before a line whose gap rounds above 1 s\n"); |
| 239 | fprintf(fp, "rule objective mean discrete one-step log score (nats) over scored ticks\n"); |
| 240 | fprintf(fp, "rule grid_tie first grid point in listed order (strict >)\n"); |
| 241 | fprintf(fp, "grid_stride %d\n", stride); |
| 242 | fprintf(fp, "ticks total %zu inserted %zu gaps_capped %zu lines_multi %zu bad_t %zu absent_value %zu unparsable_value %zu gap_zero %zu gap_backward %zu\n", |
| 243 | tk.n, tk.inserted, tk.gaps_capped, tk.lines_multi, tk.bad_t, tk.absent_value, tk.unparsable_value, tk.gap_zero, tk.gap_backward); |
| 244 | fprintf(fp, "marks pairs %zu begins %zu ends %zu unclosed %zu nested %zu stray_ends %zu bad_lines %zu\n", |
| 245 | mk.n, mk.begins, mk.ends, mk.unclosed, mk.nested_begins, mk.stray_ends, mk.bad_lines); |
| 246 | for (int id = 1; id <= 5; id++) put_family(fp, id, &ff[id]); |
| 247 | fprintf(fp, "f4_em status %d changes %zu loglik %.17g\n", (int)ems, ne, em_ll); |
| 248 | for (int id = 1; id <= 5; id++) put_screen(fp, id, &st[id], ff[id].have); |
| 249 | fprintf(fp, "e0_changes %zu\n", ne); |
| 250 | for (int i = 0; i < EST_PRED_N; i++) if (e0c[i]) fprintf(fp, "e0_count %d %u\n", i - EST_PRED_K, e0c[i]); |
| 251 | fprintf(fp, "selection_rule best D1 log score among screen-passers; earliest family within %.2f nats preferred\n", C3_TIE_NATS); |
| 252 | if (sel) fprintf(fp, "selected %s\nphase_a PASS\n", c3_name(sel)); |
| 253 | else { |
| 254 | fprintf(fp, "selected none\nphase_a PHASE_A_FAIL\n"); |
| 255 | for (int id = 1; id <= 5; id++) { |
| 256 | fprintf(fp, "phase_a_fail %s", c3_name(id)); |
| 257 | if (!ff[id].have) fprintf(fp, " unavailable"); |
| 258 | else for (int i = 0; i < C3_ST_COUNT; i++) |
| 259 | if (st[id].gated[i] && !st[id].pass[i]) fprintf(fp, " %s=%.6g", c3_stat_name(i), st[id].value[i]); |
| 260 | fprintf(fp, "\n"); |
| 261 | } |
| 262 | } |
| 263 | int bad = fclose(fp); |
| 264 | char ph[65] = "unavailable"; |
| 265 | est_sha_file_hex(out, ph); |
| 266 | printf("EST3C_FIT selected=%s phase_a=%s params_sha256=%s synthetic_test=%d\n", sel ? c3_name(sel) : "none", |
| 267 | sel ? "PASS" : "PHASE_A_FAIL", ph, synth); |
| 268 | for (int id = 1; id <= 5; id++) c3_stats_free(&st[id]); |
| 269 | free(e); free(e0c); c3_ticks_free(&tk); est_file_free(&f); |
| 270 | return bad ? 1 : 0; |
| 271 | } |