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_common.h
| 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 tools (docs/estimation/EST3C_PROTOCOL_V3.md): shared |
| 18 | * replay, statistics, pre-check and parameter-file code for est3c_fit and |
| 19 | * est3c_eval. Tools only: may open files and print. The estimators are |
| 20 | * src/estimation/est_pred.c (contract est_pred.h); this file only drives them. |
| 21 | * |
| 22 | * Documented choices (fixed before any D1 value was read): |
| 23 | * - Assumption shared by every family: unit mC, quantum 100 mC, valid range |
| 24 | * lo = -40000 mC, hi = 150000 mC (C3_LO, C3_HI: wider than any plausible |
| 25 | * die temperature, so the range class only catches garbage). |
| 26 | * - F1/F2 initial level variance: p0 = r (the fitted observation variance; |
| 27 | * the first valid reading is the prior mean). |
| 28 | * - F5 initial scale: s0 = sqrt(floor) (protocol: s0^2 = floor). |
| 29 | * - Tick mapping: one logical tick per ndjson line. Before a line whose wall |
| 30 | * gap to the previous parsed t rounds (half up) to h > 1 s, h - 1 MISSING |
| 31 | * ticks are inserted, so the effective horizon of that line's observation |
| 32 | * is round(gap) as in protocol v1 section 2. A line whose t does not parse |
| 33 | * is one nominal tick and a missing observation (v1 rule); a duplicate or |
| 34 | * backward t is one tick. Inserted ticks per gap are capped at |
| 35 | * C3_MAX_INSERT (counted). Evidence digest of a line tick = SHA-256 of the |
| 36 | * raw line bytes (no newline); of an inserted tick = SHA-256 of |
| 37 | * "est3c.inserted-missing" || 0x00 || that following line's bytes || u64le |
| 38 | * insertion index. |
| 39 | * - Burn-in: tick index L counted from the first valid observation (L = 0); |
| 40 | * ticks with L < 30 are not scored. Missing / bad ticks are never scored. |
| 41 | * A valid tick whose effective horizon exceeds EST_PRED_MAX_H (predict |
| 42 | * refuses) is counted as unscorable and becomes the new anchor. |
| 43 | * - Horizon-1 changes (F4 EM data, E0 histogram): y(t) - y(t-1) for scored |
| 44 | * ticks t (L >= 30) whose previous tick is also a valid observation. |
| 45 | * - E0: histogram of those changes in quanta smoothed by one pseudo-observation |
| 46 | * spread uniformly, clipped to [-400, 400] (outside values counted in the |
| 47 | * edge bin), p(k) = (c_k + 1/801) / (N + 1); persistence centre; |
| 48 | * for effective horizon h > 1 the pmf is |
| 49 | * est_pmf_conv_pow(h) then est_pmf_floor; h = 1 uses the smoothed pmf as is. |
| 50 | * The counts are stored in params.txt (e0_count lines) and est3c_eval |
| 51 | * rebuilds E0 from them only (it never reopens D1 for E0). |
| 52 | * - Regime of a tick: in-trial when the line's wall t lies inside a |
| 53 | * begin/end pair of the marks file (v1 section 1), else idle. |
| 54 | * - Quarters: four equal spans of the scored one-step steps by index |
| 55 | * (i * n / 4 boundaries). |
| 56 | * - Ten-step: origins are valid-observation ticks with L >= 30; the target is |
| 57 | * tick origin + 10 when that tick is a valid observation; the forecast is |
| 58 | * est_pred_predict(h = 10) right after the origin's update (E0: conv to the |
| 59 | * effective horizon). |
| 60 | */ |
| 61 | #ifndef OMEGA_EST3C_COMMON_H |
| 62 | #define OMEGA_EST3C_COMMON_H |
| 63 | |
| 64 | #include <stddef.h> |
| 65 | #include <stdint.h> |
| 66 | #include <stdio.h> |
| 67 | |
| 68 | #include "est_pred.h" |
| 69 | #include "est_replay.h" |
| 70 | |
| 71 | /* 1 when the working tree in the current directory is not clean (git status). */ |
| 72 | int c3_tree_dirty_now(void); |
| 73 | |
| 74 | #define C3_BURN_IN 30u |
| 75 | #define C3_QUANTUM 100.0 |
| 76 | #define C3_LO (-40000.0) |
| 77 | #define C3_HI 150000.0 |
| 78 | #define C3_MAX_INSERT 100000u |
| 79 | #define C3_TEN 10u |
| 80 | #define C3_MIN_LINES 2030u |
| 81 | #define C3_NCORES 20.0 |
| 82 | |
| 83 | #define C3_D1_ID "20261001T025159Z-est3c-fit-silicon" |
| 84 | #define C3_D1_DIR_DEFAULT "evidence/EST3C/raw/" C3_D1_ID |
| 85 | #define C3_HELDOUT_TAG "-est3c-heldout-" |
| 86 | #define C3_DEV_A "evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon" |
| 87 | #define C3_DEV_B "evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon" |
| 88 | #define C3_DEV_C1 "evidence/EST3B/raw/20261001T020412Z-est3b-fit-silicon" |
| 89 | #define C3_PROTOCOL_DOC "docs/estimation/EST3C_PROTOCOL_V3.md" |
| 90 | |
| 91 | /* ---- protocol section 6 bands (copied from the protocol text) ---- */ |
| 92 | #define C3_COV50_LO 0.46 |
| 93 | #define C3_COV50_HI 0.54 |
| 94 | #define C3_COV80_LO 0.76 |
| 95 | #define C3_COV80_HI 0.84 |
| 96 | #define C3_COV95_LO 0.93 |
| 97 | #define C3_COV95_HI 0.97 |
| 98 | #define C3_PIT_LO 0.07 |
| 99 | #define C3_PIT_HI 0.13 |
| 100 | #define C3_BIAS_MAX 0.10 |
| 101 | #define C3_LAG1_MAX 0.20 |
| 102 | #define C3_SUB95_LO 0.90 |
| 103 | #define C3_SUB95_HI 0.99 |
| 104 | #define C3_REGIME_MIN 100u |
| 105 | #define C3_MIN_N 2000u |
| 106 | #define C3_TIE_NATS 0.01 |
| 107 | |
| 108 | /* ---- tick stream ---- */ |
| 109 | typedef struct { |
| 110 | size_t line; /* source line index */ |
| 111 | int inserted; /* 1: MISSING tick inserted for a wall gap */ |
| 112 | int present; /* thermal zone 0 value present (parsed or not) */ |
| 113 | double value; /* raw value; NaN when present but unparsable */ |
| 114 | int t_ok; |
| 115 | int64_t wall_ns; /* line t (inserted ticks: 0) */ |
| 116 | est_digest ev; |
| 117 | } c3_tick; |
| 118 | |
| 119 | typedef struct { |
| 120 | c3_tick *t; |
| 121 | size_t n, cap; |
| 122 | size_t lines, bad_t, absent_value, unparsable_value; |
| 123 | size_t inserted, gaps_capped, gap_zero, gap_backward, lines_multi; |
| 124 | } c3_ticks; |
| 125 | |
| 126 | int c3_ticks_build(const est_file *f, c3_ticks *out, char *err, size_t cap); |
| 127 | void c3_ticks_free(c3_ticks *tk); |
| 128 | |
| 129 | /* ---- marks ---- */ |
| 130 | #define C3_MAX_MARKS 512 |
| 131 | #define C3_MAX_EV 1024 |
| 132 | typedef struct { |
| 133 | size_t n; /* closed or run-to-eof pairs */ |
| 134 | int64_t b[C3_MAX_MARKS], e[C3_MAX_MARKS]; |
| 135 | int level[C3_MAX_MARKS], index[C3_MAX_MARKS]; /* from "load-L<l>-<i>", -1 if not that shape */ |
| 136 | size_t begins, ends, unclosed, nested_begins, stray_ends, bad_lines; |
| 137 | /* every begin/end line in file order (for the section 7 pair rule) */ |
| 138 | size_t nev; int64_t ev_t[C3_MAX_EV]; int ev_end[C3_MAX_EV], ev_level[C3_MAX_EV], ev_index[C3_MAX_EV]; |
| 139 | char sha[65]; |
| 140 | } c3_marks; |
| 141 | /* verify = 1: est_file_load (SHA256SUMS); 0: raw read. */ |
| 142 | int c3_marks_load(const char *path, int verify, c3_marks *mk, char *err, size_t cap); |
| 143 | int c3_in_trial(const c3_marks *mk, int64_t t); |
| 144 | |
| 145 | /* ---- models ---- */ |
| 146 | typedef struct { |
| 147 | int id; /* 1..5 = F1..F5, 0 = E0 */ |
| 148 | est_assumption a; /* F1..F5 */ |
| 149 | double e0_one[EST_PRED_N]; /* E0 one-step pmf */ |
| 150 | } c3_model; |
| 151 | |
| 152 | void c3_assumption_base(est_assumption *a, est_family fam); |
| 153 | void c3_make_f1(c3_model *m, double q_proc, double r); |
| 154 | void c3_make_f2(c3_model *m, double q_proc, double r, double c); |
| 155 | void c3_make_f3(c3_model *m, double nu, double s); |
| 156 | int c3_make_f4(c3_model *m, const est_mix *mx); |
| 157 | void c3_make_f5(c3_model *m, double lambda, double nu, double c, double floor_var); |
| 158 | /* counts[EST_PRED_N] (offset index EST_PRED_K = change 0) -> E0 model */ |
| 159 | void c3_make_e0(c3_model *m, const uint32_t *counts, uint64_t n); |
| 160 | const char *c3_name(int id); /* "E0", "F1".."F5" */ |
| 161 | |
| 162 | /* horizon-1 changes (mC) of scored ticks; returns count (<= cap) */ |
| 163 | size_t c3_h1_changes(const c3_ticks *tk, double *e, size_t cap); |
| 164 | void c3_e0_counts(const double *e, size_t n, uint32_t *counts); |
| 165 | |
| 166 | /* ---- statistics ---- */ |
| 167 | enum { C3_ST_COV50, C3_ST_COV80, C3_ST_COV95, C3_ST_PIT0, C3_ST_BIAS = C3_ST_PIT0 + 10, C3_ST_LAG1, |
| 168 | C3_ST_Q0, C3_ST_REG_TRIAL = C3_ST_Q0 + 4, C3_ST_REG_IDLE, C3_ST_TEN, C3_ST_N, C3_ST_COUNT }; |
| 169 | /* order = protocol section 6 rule order (rule 5: in-trial before idle); c3_judge |
| 170 | * names the first failing statistic in this order */ |
| 171 | const char *c3_stat_name(int s); |
| 172 | |
| 173 | typedef struct { |
| 174 | int error; char errmsg[160]; |
| 175 | size_t n; /* scored one-step steps */ |
| 176 | size_t unscorable; /* valid, L >= burn-in, predict refused (gap) */ |
| 177 | double logsum, logscore; /* mean log score */ |
| 178 | est_calib c; |
| 179 | size_t cap; double *cov95_step; double *width_step; unsigned char *regime_step; |
| 180 | double ten_cov; size_t ten_n; |
| 181 | double value[C3_ST_COUNT]; |
| 182 | double band_lo[C3_ST_COUNT], band_hi[C3_ST_COUNT]; |
| 183 | int gated[C3_ST_COUNT], pass[C3_ST_COUNT]; |
| 184 | size_t sub_n[C3_ST_COUNT]; /* quarter / regime / ten-step counts */ |
| 185 | double width_mean, width_median; |
| 186 | int calibrated; int first_fail; /* -1 when calibrated */ |
| 187 | } c3_stats; |
| 188 | |
| 189 | /* full = 0: log score only (grid search). full = 1: every section 6 statistic. */ |
| 190 | int c3_score(const c3_ticks *tk, const c3_marks *mk, const c3_model *m, int full, c3_stats *st); |
| 191 | void c3_stats_free(c3_stats *st); |
| 192 | /* applies the section 6 rules to the accumulated values (called by c3_score) */ |
| 193 | void c3_judge(c3_stats *st); |
| 194 | |
| 195 | /* ---- precheck (section 7); reads only t, loadavg, cpu, marks, schedule ---- */ |
| 196 | typedef struct { |
| 197 | size_t lines, gaps, gaps_big, bad_t; double gap_big_frac; |
| 198 | int have_schedule; size_t sched_segments, sched_trials; |
| 199 | int marks_ok; char marks_why[160]; |
| 200 | size_t foreign_n, foreign_unmeasured, foreign_over3; |
| 201 | double foreign_mean, foreign_over3_frac, loadavg1_mean; size_t loadavg_n; |
| 202 | int ok_lines, ok_gaps, ok_marks, ok_foreign_mean, ok_foreign_tail, ok_foreign_measured; |
| 203 | int valid; char why[256]; |
| 204 | char raw_sha[65], marks_sha[65], sched_sha[65]; |
| 205 | } c3_pre; |
| 206 | int c3_precheck(const char *dir, c3_pre *p, char *err, size_t cap); |
| 207 | /* field extractor: finds top-level "key": value in one ndjson line without |
| 208 | * converting any other field's value. Returns 0 and the value span (quotes |
| 209 | * stripped) when found. */ |
| 210 | int c3_json_field(const char *line, size_t len, const char *key, const char **v, size_t *vl); |
| 211 | |
| 212 | /* ---- parameter file ---- */ |
| 213 | typedef struct { |
| 214 | int phase_a_pass; /* 1 PASS, 0 PHASE_A_FAIL */ |
| 215 | int selected; /* 1..5, 0 none */ |
| 216 | int have[6]; |
| 217 | est_assumption fam[6]; |
| 218 | double logscore[6]; |
| 219 | int screen_pass[6]; |
| 220 | char protocol_sha[65], tool_commit[80]; |
| 221 | int tool_dirty, synthetic; |
| 222 | char fit_raw_path[1024], fit_raw_sha[65], fit_marks_path[1024], fit_marks_sha[65]; |
| 223 | size_t fit_lines; |
| 224 | uint64_t e0_n; |
| 225 | uint32_t e0_count[EST_PRED_N]; |
| 226 | } c3_params; |
| 227 | int c3_params_read(const char *path, c3_params *p, char *err, size_t cap); |
| 228 | int c3_model_from_params(const c3_params *p, int id, c3_model *m); |
| 229 | |
| 230 | /* ---- helpers ---- */ |
| 231 | int c3_path_is_heldout(const char *path); |
| 232 | /* writes SHA256SUMS for the named files in dir (sha256sum text format) */ |
| 233 | int c3_write_sums(const char *dir, const char *const *names, size_t n); |
| 234 | /* synthetic run: iid grid changes, schedule + marks + cpu consistent with it */ |
| 235 | typedef struct { |
| 236 | uint64_t seed; |
| 237 | size_t lines; |
| 238 | double foreign; /* extra busy cores on every sample */ |
| 239 | size_t gap_every; /* every Nth line comes after a 3 s gap (0: none) */ |
| 240 | int drop_end_mark; /* drop the end mark of the first trial */ |
| 241 | int heavy_trial_noise; /* 1: trials have wider changes */ |
| 242 | size_t spike_every; /* every Nth sample gets 4 extra foreign busy cores (0: none) */ |
| 243 | } c3_synth; |
| 244 | int c3_synth_write(const char *dir, const c3_synth *s); |
| 245 | void c3_json_str(FILE *fp, const char *s); |
| 246 | /* Strict-JSON receipt rules (est-json-1, docs/estimation/RECEIPT_JSON.md): a receipt never |
| 247 | * contains inf or nan. c3_json_num writes a finite double with %.17g, anything else as null. |
| 248 | * c3_json_stats writes one score block: infinite thresholds as null + lo/hi_unbounded true, |
| 249 | * unmeasured values as null + a status, and every non-finite MEASURED value as null + listed |
| 250 | * in "invalid_fields". c3_stats_invalid counts those invalid fields; a caller must make its |
| 251 | * verdict fail when it is non-zero. ten_mode: ABSENT omits the ten-step score field, |
| 252 | * UNAVAILABLE writes null + status "unavailable" (a baseline that has none), MEASURED writes |
| 253 | * the number, or null + status "invalid" when it is not finite. */ |
| 254 | enum { C3_TEN_ABSENT = 0, C3_TEN_UNAVAILABLE = 1, C3_TEN_MEASURED = 2 }; |
| 255 | void c3_json_num(FILE *fp, double v); |
| 256 | size_t c3_stats_invalid(const c3_stats *st, int ten_mode, double ls10); |
| 257 | size_t c3_json_stats(FILE *o, const char *name, const c3_stats *st, int ten_mode, double ls10); |
| 258 | /* grid search: take a point when it is the first finite one or strictly better |
| 259 | * (ties keep the earlier point in listed order). */ |
| 260 | int c3_grid_take(int have, double ls, double best); |
| 261 | /* section 5 selection: pass[1..5], ls[1..5]; best log score among passers, an |
| 262 | * earlier family within C3_TIE_NATS preferred. 0 = none (PHASE_A_FAIL). */ |
| 263 | int c3_select(const int *avail, const int *pass, const double *ls); |
| 264 | |
| 265 | #endif |