interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tools / estimation / est3c_forensics.c

327 lines · 16208 bytes Raw Blame History
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/* est3c_forensics.c: read-only forensics of the EST-3 v1 and v2 FAILs
18 * (ESTIMATION-2, protocol v3 preparation). Deterministic; writes nothing.
19 *
20 * Inputs (paths relative to the omega repo root, the working directory):
21 * A = fit run of v1, B = held-out run of v1 (now a development artifact),
22 * C1 = v2 idle fit run, the v1 M0/M1 step streams and the v1 receipt.json.
23 * Nothing under evidence/EST3C/ is read.
24 *
25 * Definitions (printed again in the output):
26 * line index L counts from the first line of the file (L = 0).
27 * one-step change d_L = z_L - z_{L-1}, z = first field of thermal_mc (mC),
28 * kept when L >= 30 and the rounded wall gap is exactly 1 s (the v2
29 * pre-check rule). Gap rounding: (gap_ns + 5e8) / 1e9, t parsed as
30 * integer seconds plus nine fractional digits.
31 *
32 * Build: cc -std=c11 -O2 -Wall -Wextra -Werror -pedantic \
33 * -o /tmp/est3c_forensics tools/estimation/est3c_forensics.c -lm
34 */
35#include <math.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41#define MAXN 8192
42#define MAXM 1024
43#define BURN 30
44#define NS 1000000000LL
45#define CELL 50.0 /* half width of a 100 mC reading cell */
46
47static const char *PATH_A = "evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/machine-state.ndjson";
48static const char *PATH_B = "evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/machine-state.ndjson";
49static const char *MARK_A = "evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/machine-state-marks.txt";
50static const char *MARK_B = "evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/machine-state-marks.txt";
51static const char *PATH_C1 = "evidence/EST3B/raw/20261001T020412Z-est3b-fit-silicon/machine-state.ndjson";
52static const char *STREAM[2] = { "docs/estimation/receipts/est23-v1/receipt.json.M0.stream",
53 "docs/estimation/receipts/est23-v1/receipt.json.M1.stream" };
54static const char *RECEIPT = "docs/estimation/receipts/est23-v1/receipt.json";
55
56static void die(const char *m, const char *p) { fprintf(stderr, "est3c_forensics: %s: %s\n", m, p); exit(2); }
57
58/* "1790647557.392107561" -> ns; returns 0 on failure */
59static int parse_t(const char *s, int64_t *out) {
60 char *e; long long sec = strtoll(s, &e, 10);
61 if (e == s || *e != '.') return 0;
62 int64_t frac = 0; int k = 0;
63 for (e++; k < 9; k++, e++) { if (*e < '0' || *e > '9') return 0; frac = frac * 10 + (*e - '0'); }
64 *out = (int64_t)sec * NS + frac; return 1;
65}
66
67typedef struct { size_t n, bad; int64_t t[MAXN]; double z[MAXN]; } series;
68
69static void read_series(const char *path, series *s) {
70 FILE *f = fopen(path, "r"); if (!f) die("cannot open", path);
71 char buf[8192]; s->n = 0; s->bad = 0;
72 while (fgets(buf, sizeof buf, f)) {
73 if (!strchr(buf, '\n')) die("line too long or missing newline", path);
74 const char *pt = strstr(buf, "\"t\":"), *pz = strstr(buf, "\"thermal_mc\":\"");
75 int64_t t; char *e;
76 if (!pt || !pz || !parse_t(pt + 4, &t)) { s->bad++; continue; }
77 double z = strtod(pz + 14, &e);
78 if (e == pz + 14 || !isfinite(z)) { s->bad++; continue; }
79 if (s->n >= MAXN) die("too many lines", path);
80 s->t[s->n] = t; s->z[s->n] = z; s->n++;
81 }
82 fclose(f);
83}
84
85typedef struct { size_t n; int64_t t[MAXM]; int begin[MAXM]; } marks;
86
87static void read_marks(const char *path, marks *m) {
88 FILE *f = fopen(path, "r"); if (!f) die("cannot open", path);
89 char buf[1024]; m->n = 0;
90 while (fgets(buf, sizeof buf, f)) {
91 int64_t t; if (!parse_t(buf, &t)) die("bad mark time", path);
92 if (m->n >= MAXM) die("too many marks", path);
93 m->t[m->n] = t; m->begin[m->n] = strstr(buf, " begin ") != NULL; m->n++;
94 }
95 fclose(f);
96}
97
98typedef struct { size_t n, multi; double d[MAXN]; size_t L[MAXN]; } changes;
99
100static void make_changes(const series *s, changes *c) {
101 c->n = 0; c->multi = 0;
102 for (size_t L = 1; L < s->n; L++) {
103 int64_t h = (s->t[L] - s->t[L - 1] + NS / 2) / NS;
104 if (h < 1) h = 1;
105 if (L < BURN) continue;
106 if (h != 1) { c->multi++; continue; }
107 c->d[c->n] = s->z[L] - s->z[L - 1]; c->L[c->n] = L; c->n++;
108 }
109}
110
111typedef struct { size_t n, zero, big; double mean, sd, exkurt, maxabs; } dstat;
112
113static dstat describe(const double *x, size_t n) {
114 dstat r; memset(&r, 0, sizeof r); r.n = n; if (n < 4) return r;
115 double m = 0; for (size_t i = 0; i < n; i++) m += x[i]; m /= (double)n;
116 double m2 = 0, m4 = 0;
117 for (size_t i = 0; i < n; i++) {
118 double e = x[i] - m; m2 += e * e; m4 += e * e * e * e;
119 if (x[i] == 0.0) r.zero++;
120 if (fabs(x[i]) >= 1000.0) r.big++;
121 if (fabs(x[i]) > r.maxabs) r.maxabs = fabs(x[i]);
122 }
123 m2 /= (double)n; m4 /= (double)n;
124 r.mean = m; r.sd = sqrt(m2); r.exkurt = m2 > 0 ? m4 / (m2 * m2) - 3.0 : 0.0;
125 return r;
126}
127
128/* autocorrelation r_1..r_10 and Ljung-Box Q10 of the mean-subtracted series */
129static double acf10(const double *x, size_t n, double r[11]) {
130 double m = 0; for (size_t i = 0; i < n; i++) m += x[i]; m /= (double)n;
131 double den = 0; for (size_t i = 0; i < n; i++) den += (x[i] - m) * (x[i] - m);
132 double lb = 0, dn = (double)n;
133 for (size_t k = 1; k <= 10; k++) {
134 double num = 0; for (size_t i = 0; i + k < n; i++) num += (x[i] - m) * (x[i + k] - m);
135 r[k] = den > 0 ? num / den : 0; lb += r[k] * r[k] / (dn - (double)k);
136 }
137 return dn * (dn + 2.0) * lb;
138}
139
140static double Phi(double x) { return 0.5 * erfc(-x / sqrt(2.0)); }
141
142static void print_dstat(const char *tag, const dstat *r) {
143 double pz = r->sd > 0 ? erf(CELL / (r->sd * sqrt(2.0))) : 1.0;
144 printf(" %-22s n=%5zu sd=%7.1f exkurt=%7.2f max|d|=%6.0f zero=%zu (%.4f; N(0,sd) P|d|<=50: %.4f) |d|>=1000: %.4f\n",
145 tag, r->n, r->sd, r->exkurt, r->maxabs, r->zero, r->n ? (double)r->zero / (double)r->n : 0.0, pz,
146 r->n ? (double)r->big / (double)r->n : 0.0);
147}
148
149/* ---------- v1 stream reproduction ---------- */
150typedef struct { size_t L; int prior, coast; long h; double z, y, S, nu, nis; } step;
151static step ST[MAXN];
152
153static double kv(const char *line, const char *key, int *ok) {
154 char pat[32]; snprintf(pat, sizeof pat, " %s=", key);
155 const char *p = strstr(line, pat); if (!p) { *ok = 0; return 0; }
156 char *e; double v = strtod(p + strlen(pat), &e); if (e == p + strlen(pat)) *ok = 0;
157 return v;
158}
159
160static size_t read_stream(const char *path) {
161 FILE *f = fopen(path, "r"); if (!f) die("cannot open", path);
162 char buf[2048]; size_t n = 0;
163 while (fgets(buf, sizeof buf, f)) {
164 if (!strchr(buf, '\n')) die("stream line too long", path);
165 if (n >= MAXN) die("stream too long", path);
166 int ok = 1; step *s = &ST[n];
167 s->L = (size_t)kv(buf, "L", &ok); s->h = (long)kv(buf, "h", &ok);
168 s->z = kv(buf, "z", &ok); s->y = kv(buf, "y", &ok); s->S = kv(buf, "S", &ok);
169 s->nu = kv(buf, "nu", &ok); s->nis = kv(buf, "nis", &ok);
170 s->prior = strstr(buf, " prior ") != NULL; s->coast = strstr(buf, " coast ") != NULL;
171 if (!ok) die("bad stream line", path);
172 n++;
173 }
174 fclose(f); return n;
175}
176
177/* first number after "key": {"value": or "key": inside the model block */
178static double rec(const char *blk, const char *key) {
179 char pat[64]; snprintf(pat, sizeof pat, "\"%s\":", key);
180 const char *p = strstr(blk, pat); if (!p) die("receipt key missing", key);
181 p += strlen(pat); while (*p == ' ') p++;
182 if (*p == '{') { p = strstr(p, "\"value\":"); if (!p) die("receipt value missing", key); p += 8; }
183 return strtod(p, NULL);
184}
185
186static char RC[65536];
187
188static void cmp(const char *name, double recd, double mine) {
189 printf(" %-14s recorded=%.17g recomputed=%.17g |diff|=%.3g%s\n", name, recd, mine, fabs(recd - mine),
190 recd == mine ? " EXACT" : "");
191}
192
193static void v1_model(int m) {
194 size_t n_all = read_stream(STREAM[m]);
195 static double w[MAXN];
196 size_t n = 0, hit[3] = { 0, 0, 0 }, zero = 0;
197 double snis = 0, sse = 0, ssp = 0, prev_z = 0, numu = 0, nus[MAXN];
198 const double zc[3] = { 0.674490, 1.281552, 1.959964 }, lv[3] = { 0.50, 0.80, 0.95 };
199 double frac[3] = { 0, 0, 0 }, pit[10] = { 0 };
200 for (size_t i = 0; i < n_all; i++) {
201 const step *s = &ST[i];
202 if (s->prior) { prev_z = s->z; continue; }
203 if (s->coast) continue;
204 if (s->L >= BURN) {
205 double sd = sqrt(s->S), a = fabs(s->nu);
206 for (int k = 0; k < 3; k++) if (a <= zc[k] * sd) hit[k]++;
207 snis += s->nis; sse += s->nu * s->nu; numu += s->nu; nus[n] = s->nu;
208 if (s->nu == 0.0) zero++;
209 double e = s->z - prev_z; ssp += e * e;
210 w[n] = s->nu / sd;
211 /* fractional (randomized-PIT) coverage: outcome cell (z-50, z+50] */
212 double Flo = Phi((s->nu - CELL) / sd), Fhi = Phi((s->nu + CELL) / sd), wd = Fhi - Flo;
213 for (int k = 0; k < 3; k++) {
214 double lo = (1.0 - lv[k]) / 2.0, hi = 1.0 - lo;
215 if (wd > 0) { double ov = fmin(hi, Fhi) - fmax(lo, Flo); frac[k] += ov > 0 ? ov / wd : 0; }
216 else frac[k] += (Flo >= lo && Flo <= hi) ? 1.0 : 0.0;
217 }
218 for (int b = 0; b < 10; b++) {
219 double lo = b / 10.0, hi = (b + 1) / 10.0;
220 if (wd > 0) { double ov = fmin(hi, Fhi) - fmax(lo, Flo); pit[b] += ov > 0 ? ov / wd : 0; }
221 else if (Flo >= lo && (Flo < hi || b == 9)) pit[b] += 1.0;
222 }
223 n++;
224 }
225 prev_z = s->z;
226 }
227 double dn = (double)n, r[11];
228 double q10 = acf10(w, n, r);
229 double mn = numu / dn, v = 0; for (size_t i = 0; i < n; i++) v += (nus[i] - mn) * (nus[i] - mn);
230 const char *mod = strstr(RC, "\"models\""); if (!mod) die("models block", "receipt");
231 const char *blk = strstr(mod, m == 0 ? "\"M0\": {" : "\"M1\": {"); if (!blk) die("model block", "receipt");
232 printf("v1 %s on B (stream, L>=%d): n=%zu (recorded %.0f)\n", m ? "M1" : "M0", BURN, n, rec(blk, "samples"));
233 cmp("coverage_50", rec(blk, "coverage_50"), hit[0] / dn);
234 cmp("coverage_80", rec(blk, "coverage_80"), hit[1] / dn);
235 cmp("coverage_95", rec(blk, "coverage_95"), hit[2] / dn);
236 cmp("mean_nis", rec(blk, "mean_nis"), snis / dn);
237 cmp("std_bias", rec(blk, "standardized_bias"), mn / sqrt(v / (dn - 1.0)));
238 cmp("lag1", rec(blk, "lag1_autocorr"), r[1]);
239 cmp("LB_Q10", rec(blk, "ljung_box_q10_reported_only"), q10);
240 cmp("rmse", rec(blk, "rmse_one_step"), sqrt(sse / dn));
241 cmp("persist_rmse", rec(blk, "persistence_rmse"), sqrt(ssp / dn));
242 cmp("zero_innov", rec(blk, "zero_innovation_fraction_extra"), (double)zero / dn);
243 printf(" fractional coverage (cell z+-50): 50%%=%.4f 80%%=%.4f 95%%=%.4f (point: %.4f %.4f %.4f)\n",
244 frac[0] / dn, frac[1] / dn, frac[2] / dn, hit[0] / dn, hit[1] / dn, hit[2] / dn);
245 printf(" randomized PIT deciles (expected mass, ideal 0.100):");
246 for (int b = 0; b < 10; b++) printf(" %.3f", pit[b] / dn);
247 printf("\n");
248}
249
250/* ---------- per-run dependence, tails, regime split, EWMA ---------- */
251static void run_report(const char *name, const char *path, const char *mpath, int ewma) {
252 static series s; static changes c; static marks mk;
253 read_series(path, &s); make_changes(&s, &c);
254 printf("\n%s: lines=%zu unparsed=%zu kept changes (L>=30, gap=1s)=%zu excluded multi-horizon=%zu\n",
255 name, s.n + s.bad, s.bad, c.n, c.multi);
256 dstat all = describe(c.d, c.n);
257 print_dstat("all changes", &all);
258 if (mpath) {
259 read_marks(mpath, &mk);
260 static double din[MAXN], dsp[MAXN]; size_t nin = 0, nsp = 0, outside = 0, begins = 0;
261 for (size_t j = 0; j < mk.n; j++) begins += (size_t)mk.begin[j];
262 for (size_t L = 0; L < s.n; L++) { /* sample membership in any begin..end pair */
263 int in = 0;
264 for (size_t j = 0; j + 1 < mk.n; j++)
265 if (mk.begin[j] && !mk.begin[j + 1] && s.t[L] >= mk.t[j] && s.t[L] <= mk.t[j + 1]) { in = 1; break; }
266 if (!in) outside++;
267 }
268 for (size_t i = 0; i < c.n; i++) {
269 int64_t t0 = s.t[c.L[i] - 1], t1 = s.t[c.L[i]]; int span = 0;
270 for (size_t j = 0; j < mk.n; j++) if (mk.t[j] > t0 && mk.t[j] <= t1) { span = 1; break; }
271 if (span) dsp[nsp++] = c.d[i]; else din[nin++] = c.d[i];
272 }
273 printf(" marks: %zu lines, %zu begins; samples outside every begin..end pair: %zu of %zu\n",
274 mk.n, begins, outside, s.n);
275 dstat a = describe(din, nin), b = describe(dsp, nsp);
276 print_dstat("within one trial", &a);
277 print_dstat("spans a trial mark", &b);
278 }
279 double r[11], ra[11]; static double ad[MAXN];
280 for (size_t i = 0; i < c.n; i++) ad[i] = fabs(c.d[i]);
281 double q = acf10(c.d, c.n, r), qa = acf10(ad, c.n, ra);
282 printf(" acf d lags1-10:"); for (int k = 1; k <= 10; k++) printf(" %+.3f", r[k]); printf(" LB Q10=%.1f\n", q);
283 printf(" acf |d| lags1-10:"); for (int k = 1; k <= 10; k++) printf(" %+.3f", ra[k]); printf(" LB Q10=%.1f\n", qa);
284 if (!ewma) return;
285 /* EWMA scale, strictly causal: sigma2 at L uses changes up to L-1.
286 Init = mean d^2 over gap-1 changes with 1 <= L < 30. Scale floor 100^2/12
287 (uniform quantization variance) or 100^2 (one reading step). */
288 const double lam[2] = { 0.9, 0.97 }, fl = 10000.0 / 12.0;
289 double init = 0; size_t ni = 0;
290 for (size_t L = 1; L < BURN && L < s.n; L++) {
291 int64_t h = (s.t[L] - s.t[L - 1] + NS / 2) / NS;
292 if (h == 1) { double d = s.z[L] - s.z[L - 1]; init += d * d; ni++; }
293 }
294 init = ni ? init / (double)ni : fl; if (init < fl) init = fl;
295 {
296 size_t h50 = 0, h95 = 0; for (size_t i = 0; i < c.n; i++) { double a = fabs(c.d[i]) / all.sd; h50 += a <= 0.674490; h95 += a <= 1.959964; }
297 printf(" fixed scale (in-sample sd): exkurt=%7.2f cov50=%.4f cov95=%.4f\n", all.exkurt, h50 / (double)c.n, h95 / (double)c.n);
298 }
299 for (int k = 0; k < 4; k++) { const double flk = (k < 2) ? fl : 10000.0; const double lk = lam[k % 2];
300 static double zz[MAXN]; size_t m = 0, h50 = 0, h95 = 0; double s2 = init;
301 for (size_t L = 1; L < s.n; L++) {
302 int64_t h = (s.t[L] - s.t[L - 1] + NS / 2) / NS; double d = s.z[L] - s.z[L - 1];
303 if (L >= BURN && h == 1) {
304 double sc = sqrt(s2 < flk ? flk : s2); zz[m] = d / sc;
305 h50 += fabs(zz[m]) <= 0.674490; h95 += fabs(zz[m]) <= 1.959964; m++;
306 }
307 if (h == 1) s2 = lk * s2 + (1.0 - lk) * d * d;
308 }
309 dstat z = describe(zz, m);
310 printf(" EWMA lambda=%.2f floor=%5.0f mC^2 (init %.0f): exkurt=%7.2f cov50=%.4f cov95=%.4f sd(z)=%.3f max|z|=%.1f n=%zu\n",
311 lk, flk, init, z.exkurt, h50 / (double)m, h95 / (double)m, z.sd, z.maxabs, m);
312 }
313}
314
315int main(void) {
316 FILE *f = fopen(RECEIPT, "r"); if (!f) die("cannot open", RECEIPT);
317 size_t got = fread(RC, 1, sizeof RC - 1, f); RC[got] = 0; fclose(f);
318 if (got == sizeof RC - 1) die("receipt too large", RECEIPT);
319 printf("est3c_forensics v1. change d_L = z_L - z_{L-1}, kept for L>=30 and rounded gap = 1 s.\n");
320 printf("exkurt = m4/m2^2 - 3 (population moments). LB Q10 = n(n+2) sum_{k=1..10} r_k^2/(n-k).\n\n");
321 v1_model(0);
322 v1_model(1);
323 run_report("A (v1 fit, loaded)", PATH_A, MARK_A, 1);
324 run_report("B (v1 held-out, loaded)", PATH_B, MARK_B, 1);
325 run_report("C1 (v2 fit, idle; no marks)", PATH_C1, NULL, 0);
326 return 0;
327}