Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/estimation/est_v4.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / estimation / est_v4.c` 354 lines ยท 12895 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 /* ESTIMATION v4 predictive families. Model and formulas: est_v4.h. */ 18 #include "est_v4.h" 19 #include "sha256.h" 20 21 #include 22 #include 23 24 #define D EST4_MAX_D 25 26 static void mat_mul(uint32_t d, const double *a, const double *b, double *o) 27 { 28 double t[D * D]; 29 for (uint32_t i = 0; i < d; i++) 30 for (uint32_t j = 0; j < d; j++) { 31 double s = 0.0; 32 for (uint32_t k = 0; k < d; k++) s += a[i * d + k] * b[k * d + j]; 33 t[i * d + j] = s; 34 } 35 memcpy(o, t, sizeof(double) * d * d); 36 } 37 38 /* o = a b a' */ 39 static void sandwich(uint32_t d, const double *a, const double *b, double *o) 40 { 41 double t[D * D], at[D * D]; 42 for (uint32_t i = 0; i < d; i++) 43 for (uint32_t j = 0; j < d; j++) at[j * d + i] = a[i * d + j]; 44 mat_mul(d, a, b, t); 45 mat_mul(d, t, at, o); 46 } 47 48 static int fin(double v) { return isfinite(v); } 49 50 est_status est4_params_check(const est4_params *p) 51 { 52 if (!p) return EST_ERR_NULL; 53 if (p->family != EST4_G1_LAG && p->family != EST4_G2_AR && p->family != EST4_G3_TWO) return EST_ERR_KIND; 54 if (!fin(p->dyn) || !fin(p->q) || !fin(p->lam) || !fin(p->phi) || !fin(p->nu) || !fin(p->c) || 55 !fin(p->nu_h) || !fin(p->c_h) || !fin(p->quantum)) 56 return EST_ERR_NONFINITE; 57 if (p->family == EST4_G2_AR) { if (!(p->dyn >= 0.0 && p->dyn < 1.0)) return EST_ERR_DIM; } 58 else if (!(p->dyn > 0.0)) return EST_ERR_DIM; 59 if (!(p->q > 0.0) || !(p->lam >= 0.0 && p->lam < 1.0) || !(p->phi > 0.0 && p->phi <= 1.0) || 60 !(p->nu > 0.0) || !(p->c > 0.0) || !(p->nu_h > 0.0) || !(p->c_h > 0.0) || 61 !(p->quantum > 0.0)) 62 return EST_ERR_DIM; 63 return EST_OK; 64 } 65 66 static size_t put_u32(uint8_t *b, size_t o, uint32_t v) 67 { 68 for (int i = 0; i < 4; i++) b[o + (size_t)i] = (uint8_t)(v >> (8 * i)); 69 return o + 4u; 70 } 71 static size_t put_f64(uint8_t *b, size_t o, double d) 72 { 73 if (d == 0.0) d = 0.0; 74 uint64_t v; 75 memcpy(&v, &d, sizeof v); 76 for (int i = 0; i < 8; i++) b[o + (size_t)i] = (uint8_t)(v >> (8 * i)); 77 return o + 8u; 78 } 79 80 /* Encoding: u32 family, then f64 dyn q lam nu c phi nu_h c_h quantum, then f64 81 * EST4_GL_LAMBDA, EST4_TAU_RATIO, EST4_G3_W (the fixed constants are bound). */ 82 est_status est4_params_digest(const est4_params *p, est_digest *out) 83 { 84 if (!p || !out) return EST_ERR_NULL; 85 est_status st = est4_params_check(p); 86 if (st != EST_OK) return st; 87 uint8_t enc[4 + 12 * 8]; 88 size_t o = put_u32(enc, 0, (uint32_t)p->family); 89 const double v[12] = { p->dyn, p->q, p->lam, p->nu, p->c, p->phi, p->nu_h, p->c_h, p->quantum, 90 EST4_GL_LAMBDA, EST4_TAU_RATIO, EST4_G3_W }; 91 for (int i = 0; i < 12; i++) o = put_f64(enc, o, v[i]); 92 const uint8_t zero = 0; 93 sha256_ctx c; 94 sha256_init(&c); 95 sha256_update(&c, (const uint8_t *)EST_DOMAIN_V4, strlen(EST_DOMAIN_V4)); 96 sha256_update(&c, &zero, 1); 97 sha256_update(&c, enc, o); 98 sha256_final(&c, out->b); 99 return EST_OK; 100 } 101 102 est_status est4_init(const est4_params *p, est4_state *s) 103 { 104 if (!p || !s) return EST_ERR_NULL; 105 est_status st = est4_params_check(p); 106 if (st != EST_OK) return st; 107 memset(s, 0, sizeof *s); 108 s->p = *p; 109 s->r = p->quantum * p->quantum / 12.0; 110 s->g = 1.0; 111 s->gl = 1.0; 112 if (p->family == EST4_G1_LAG) { 113 double a = exp(-1.0 / p->dyn); 114 s->d = 2; 115 s->A[0] = a; s->A[1] = 1.0 - a; s->A[3] = 1.0; 116 s->Q[3] = p->q; 117 s->H[0] = 1.0; 118 } else if (p->family == EST4_G2_AR) { 119 double r = p->dyn; 120 s->d = 2; 121 s->A[0] = 1.0; s->A[1] = r; s->A[3] = r; 122 s->Q[0] = s->Q[1] = s->Q[2] = s->Q[3] = p->q; 123 s->H[0] = 1.0; 124 } else { 125 double af = exp(-1.0 / p->dyn), as = exp(-1.0 / (p->dyn * EST4_TAU_RATIO)), w = EST4_G3_W; 126 s->d = 3; 127 s->A[0] = af; s->A[2] = (1.0 - af) * w; 128 s->A[4] = as; s->A[5] = (1.0 - as) * (1.0 - w); 129 s->A[8] = 1.0; 130 s->Q[8] = p->q; 131 s->H[0] = 1.0; s->H[1] = 1.0; 132 } 133 return EST_OK; 134 } 135 136 /* First valid observation: equilibrium state at y0 with r on every observed 137 * part and r + q on the driving level. */ 138 static void first_obs(est4_state *s, double y0) 139 { 140 memset(s->x, 0, sizeof s->x); 141 memset(s->P, 0, sizeof s->P); 142 double q = s->p.q, r = s->r; 143 if (s->p.family == EST4_G1_LAG) { 144 s->x[0] = y0; s->x[1] = y0; s->P[0] = r; s->P[3] = r + q; 145 } else if (s->p.family == EST4_G2_AR) { 146 s->x[0] = y0; s->x[1] = 0.0; s->P[0] = r; s->P[3] = q; 147 } else { 148 s->x[0] = EST4_G3_W * y0; s->x[1] = (1.0 - EST4_G3_W) * y0; s->x[2] = y0; 149 s->P[0] = r; s->P[4] = r; s->P[8] = r + q; 150 } 151 } 152 153 /* mean and variance of H x after h steps from (x, P) */ 154 static void propagate(const est4_state *s, uint32_t h, double *m, double *v) 155 { 156 uint32_t d = s->d; 157 double x[D], P[D * D]; 158 memcpy(x, s->x, sizeof x); 159 memcpy(P, s->P, sizeof P); 160 for (uint32_t k = 0; k < h; k++) { 161 double xn[D] = { 0 }; 162 for (uint32_t i = 0; i < d; i++) { 163 xn[i] = 0.0; 164 for (uint32_t j = 0; j < d; j++) xn[i] += s->A[i * d + j] * x[j]; 165 } 166 memcpy(x, xn, sizeof x); 167 sandwich(d, s->A, P, P); 168 for (uint32_t i = 0; i < d * d; i++) P[i] += s->Q[i]; 169 } 170 double mm = 0.0, vv = 0.0; 171 for (uint32_t i = 0; i < d; i++) { 172 mm += s->H[i] * x[i]; 173 for (uint32_t j = 0; j < d; j++) vv += s->H[i] * P[i * d + j] * s->H[j]; 174 } 175 *m = mm; 176 *v = vv + s->r; 177 } 178 179 double est4_gbar(double g, double gl, double phi, uint32_t h) 180 { 181 double f = 1.0, gb = 0.0; 182 for (uint32_t j = 0; j < h; j++) { gb += gl + (g - gl) * f; f *= phi; } 183 return h ? gb / (double)h : NAN; 184 } 185 186 est_status est4_moments(const est4_state *s, uint32_t h, double *loc, double *V) 187 { 188 if (!s || !loc || !V) return EST_ERR_NULL; 189 if (h < 1u || h > EST_PRED_MAX_H) return EST_ERR_TIME; 190 if (!s->has_anchor) return EST_ERR_STALE; 191 propagate(s, h, loc, V); 192 return isfinite(*loc) && *V > 0.0 && isfinite(*V) ? EST_OK : EST_ERR_NONFINITE; 193 } 194 195 est_status est4_predict(const est4_state *s, uint32_t h, est4_pred *out) 196 { 197 if (!s || !out) return EST_ERR_NULL; 198 if (h < 1u || h > EST_PRED_MAX_H) return EST_ERR_TIME; 199 if (!s->has_anchor) return EST_ERR_STALE; 200 double mh, Vh; 201 propagate(s, h, &mh, &Vh); 202 if (!(Vh > 0.0) || !isfinite(Vh)) return EST_ERR_NONFINITE; 203 out->h = h; 204 out->loc = mh; 205 if (h == 1u) { 206 out->nu = s->p.nu; 207 out->scale = s->p.c * sqrt(s->g * Vh); 208 } else { 209 double gb = est4_gbar(s->g, s->gl, s->p.phi, h); 210 out->nu = s->p.nu_h; 211 out->scale = s->p.c_h * sqrt(gb * Vh); 212 } 213 if (!isfinite(out->loc) || !(out->scale > 0.0) || !isfinite(out->scale)) return EST_ERR_NONFINITE; 214 return EST_OK; 215 } 216 217 /* P(X > x) for the centred law, x >= 0 */ 218 static double sf_pos(double x, double scale, double nu) { return est_t_sf(x / scale, nu); } 219 220 est_status est4_dpred(const est4_state *s, const est4_pred *pr, est_dpred *dp) 221 { 222 if (!s || !pr || !dp) return EST_ERR_NULL; 223 if (!s->has_anchor) return EST_ERR_STALE; 224 uint64_t hz = (uint64_t)s->gap + pr->h; 225 if (pr->h < 1u || hz > EST_PRED_MAX_H) return EST_ERR_TIME; 226 if (!isfinite(pr->loc) || !(pr->scale > 0.0) || !isfinite(pr->scale) || !(pr->nu > 0.0)) 227 return EST_ERR_NONFINITE; 228 memset(dp, 0, sizeof *dp); 229 est_status st = est4_params_digest(&s->p, &dp->assumption); 230 if (st != EST_OK) return st; 231 dp->family = (est_family)0; 232 dp->horizon = (uint32_t)hz; 233 dp->generation = s->generation; 234 dp->anchor = s->anchor; 235 dp->quantum = s->p.quantum; 236 const double q = s->p.quantum, mu = pr->loc - s->anchor; 237 enum { NE = EST_PRED_N - 1 }; 238 double S[NE], x[NE]; 239 for (int e = 0; e < NE; e++) { 240 x[e] = ((double)(e - EST_PRED_K) + 0.5) * q - mu; 241 S[e] = sf_pos(fabs(x[e]), pr->scale, pr->nu); 242 if (!(S[e] >= 0.0 && S[e] <= 1.0)) return EST_ERR_NONFINITE; 243 } 244 double *out = dp->pmf; 245 out[0] = x[0] <= 0.0 ? S[0] : 1.0 - S[0]; 246 out[EST_PRED_N - 1] = x[NE - 1] >= 0.0 ? S[NE - 1] : 1.0 - S[NE - 1]; 247 for (int i = 1; i < EST_PRED_N - 1; i++) { 248 double lo = x[i - 1], hi = x[i], m; 249 if (lo >= 0.0) m = S[i - 1] - S[i]; 250 else if (hi <= 0.0) m = S[i] - S[i - 1]; 251 else m = 1.0 - S[i - 1] - S[i]; 252 out[i] = m > 0.0 ? m : 0.0; 253 } 254 double sum = 0.0; 255 for (int i = 0; i < EST_PRED_N; i++) sum += out[i]; 256 if (!(sum > 0.0) || !isfinite(sum)) return EST_ERR_NONFINITE; 257 for (int i = 0; i < EST_PRED_N; i++) out[i] /= sum; 258 est_pmf_floor(out); 259 return EST_OK; 260 } 261 262 /* survival of the centred standard law at x, x may be +-inf */ 263 static double sf_any(double x, double nu) 264 { 265 if (x == INFINITY) return 0.0; 266 if (x == -INFINITY) return 1.0; 267 return x >= 0.0 ? est_t_sf(x, nu) : 1.0 - est_t_sf(-x, nu); 268 } 269 270 double est4_fast_logp(const est4_pred *pr, double quantum, double anchor, double y, double *F_lo, double *F_hi) 271 { 272 double kd = nearbyint((y - anchor) / quantum); 273 if (kd < -(double)EST_PRED_K) kd = -(double)EST_PRED_K; 274 if (kd > (double)EST_PRED_K) kd = (double)EST_PRED_K; 275 int k = (int)kd; 276 const double mu = pr->loc - anchor; /* bin edges exactly as est4_dpred */ 277 double a = k == -EST_PRED_K ? -INFINITY : ((kd - 0.5) * quantum - mu) / pr->scale; 278 double b = k == EST_PRED_K ? INFINITY : ((kd + 0.5) * quantum - mu) / pr->scale; 279 double lo, hi, p; 280 /* difference formed on the small-tail side, as est4_dpred */ 281 if (a >= 0.0) { double sa = sf_any(a, pr->nu), sb = sf_any(b, pr->nu); p = sa - sb; lo = 1.0 - sa; hi = 1.0 - sb; } 282 else if (b <= 0.0) { double ca = sf_any(-a, pr->nu), cb = sf_any(-b, pr->nu); p = cb - ca; lo = ca; hi = cb; } 283 else { lo = sf_any(-a, pr->nu); double sb = sf_any(b, pr->nu); p = 1.0 - lo - sb; hi = 1.0 - sb; } 284 if (p < 0.0) p = 0.0; 285 /* the est_pmf_floor mix */ 286 const double f = EST_PRED_FLOOR, u = EST_PRED_FLOOR / (double)EST_PRED_N; 287 double idx = (double)(k + EST_PRED_K); 288 p = (1.0 - f) * p + u; 289 if (F_lo) *F_lo = (1.0 - f) * lo + u * idx; 290 if (F_hi) *F_hi = (1.0 - f) * hi + u * (idx + 1.0); 291 return log(p > 1e-300 ? p : 1e-300); 292 } 293 294 static void time_update(est4_state *s) 295 { 296 uint32_t d = s->d; 297 double xn[D] = { 0 }; 298 for (uint32_t i = 0; i < d; i++) { 299 xn[i] = 0.0; 300 for (uint32_t j = 0; j < d; j++) xn[i] += s->A[i * d + j] * s->x[j]; 301 } 302 memcpy(s->x, xn, sizeof xn); 303 sandwich(d, s->A, s->P, s->P); 304 for (uint32_t i = 0; i < d * d; i++) s->P[i] += s->Q[i]; 305 } 306 307 est_status est4_update(est4_state *s, int present, double y) 308 { 309 if (!s) return EST_ERR_NULL; 310 int ok = present && isfinite(y); 311 s->generation++; 312 if (!s->has_anchor) { 313 if (!ok) return EST_OK; 314 first_obs(s, y); 315 s->has_anchor = 1; 316 s->anchor = y; 317 s->gap = 0; 318 return EST_OK; 319 } 320 const double lam = s->p.lam; 321 if (!ok) { 322 time_update(s); 323 s->gl = EST4_GL_LAMBDA * s->gl + (1.0 - EST4_GL_LAMBDA) * s->g; 324 s->gap++; 325 return EST_OK; 326 } 327 uint32_t d = s->d; 328 time_update(s); 329 double m = 0.0, PH[D], S1 = s->r; 330 for (uint32_t i = 0; i < d; i++) { 331 m += s->H[i] * s->x[i]; 332 PH[i] = 0.0; 333 for (uint32_t j = 0; j < d; j++) PH[i] += s->P[i * d + j] * s->H[j]; 334 } 335 for (uint32_t i = 0; i < d; i++) S1 += s->H[i] * PH[i]; 336 if (!(S1 > 0.0) || !isfinite(S1)) return EST_ERR_NONFINITE; 337 double e = y - m; 338 for (uint32_t i = 0; i < d; i++) s->x[i] += PH[i] / S1 * e; 339 for (uint32_t i = 0; i < d; i++) 340 for (uint32_t j = 0; j < d; j++) s->P[i * d + j] -= PH[i] * PH[j] / S1; 341 /* keep P exactly symmetric */ 342 for (uint32_t i = 0; i < d; i++) 343 for (uint32_t j = i + 1; j < d; j++) { 344 double v = 0.5 * (s->P[i * d + j] + s->P[j * d + i]); 345 s->P[i * d + j] = v; s->P[j * d + i] = v; 346 } 347 double e2 = e * e > s->r ? e * e : s->r; 348 s->g = lam * s->g + (1.0 - lam) * e2 / S1; 349 s->gl = EST4_GL_LAMBDA * s->gl + (1.0 - EST4_GL_LAMBDA) * s->g; 350 s->anchor = y; 351 s->gap = 0; 352 for (uint32_t i = 0; i < d; i++) if (!isfinite(s->x[i])) return EST_ERR_NONFINITE; 353 return EST_OK; 354 }