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 / src / estimation / est_v4.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 | /* ESTIMATION v4 (protocol docs/estimation/protocols/est-v4.md): persistence- |
| 18 | * aware predictive families for a quantized, 1 Hz temperature series. |
| 19 | * |
| 20 | * v3 finding (receipts/est3c-v3/RESULT.md): F5 passed every one-step rule and |
| 21 | * failed only the ten-step rule (0.809), because it treated successive changes |
| 22 | * as independent and held its volatility constant over the horizon. v4 keeps |
| 23 | * the v3 discrete predictive (est_dpred, scored by est_dpred_score) and replaces |
| 24 | * the change model with a small linear state model whose multi-step forecast is |
| 25 | * propagated through the model, plus a Student-t scale with mean reversion. |
| 26 | * |
| 27 | * Families (state dimension d, observation y = H x + quantization noise): |
| 28 | * G1 thermal lag x = [T, U] T' = a T + (1-a) U, U' = U + w, |
| 29 | * a = exp(-1/tau), w ~ (0, q). |
| 30 | * G2 AR(1) change x = [Y, D] D' = rho D + w, Y' = Y + D' , |
| 31 | * so changes are AR(1) correlated. |
| 32 | * G3 two time consts x = [Tf, Ts, U] Tf' = af Tf + (1-af) w0 U, |
| 33 | * Ts' = as Ts + (1-as)(1-w0) U, U' = U + w, |
| 34 | * tau_s = 10 tau_f, w0 = 1/2, y = Tf + Ts. |
| 35 | * r = quantum^2 / 12 is the observation variance in every family. |
| 36 | * |
| 37 | * Predictive at horizon h (ticks after the current state): |
| 38 | * location m_h = H A^h x, V_h = H (A^h P A^h' + sum_{j<h} A^j Q A^j') H' + r. |
| 39 | * One-step law (h = 1): Y_1 ~ m_1 + c sqrt(g V_1) t_nu. |
| 40 | * Horizon law (h >= 2): Y_h ~ m_h + c_h sqrt(gbar_h V_h) t_nu_h, |
| 41 | * gbar_h = (1/h) sum_{j=1..h} (gl + (g - gl) phi^(j-1)), |
| 42 | * so location and the shape of the spread come from propagating the model, |
| 43 | * the scale forecast reverts toward the slow level gl at rate phi, and the |
| 44 | * horizon tail (nu_h) and multiplier (c_h) are separate parameters fitted on |
| 45 | * the ten-step score (protocol section 5, stage 2). Both laws are discretized |
| 46 | * onto the quantum grid around the anchor exactly as v3 (edge bins take the |
| 47 | * tails, EST_PRED_FLOOR mixed in). |
| 48 | * Update on a valid observation y (e = y - m_1 before the update): |
| 49 | * Kalman time + measurement update of (x, P) with the unscaled Q and r; |
| 50 | * g' = lam g + (1-lam) max(e^2, r) / V_1; |
| 51 | * gl' = EST4_GL_LAMBDA gl + (1 - EST4_GL_LAMBDA) g'. |
| 52 | * On a missing tick: time update of (x, P) only; g unchanged; gl as above. |
| 53 | * phi, nu_h and c_h never change the filter or the one-step law. |
| 54 | * |
| 55 | * Standalone, bounded, no allocation, no globals; like est_pred.c it is not |
| 56 | * linked into the runtime and references no authority entry point. */ |
| 57 | #ifndef OMEGA_EST_V4_H |
| 58 | #define OMEGA_EST_V4_H |
| 59 | |
| 60 | #include <stdint.h> |
| 61 | |
| 62 | #include "est_pred.h" |
| 63 | |
| 64 | #define EST4_MAX_D 3u |
| 65 | #define EST4_GL_LAMBDA 0.995 |
| 66 | #define EST4_TAU_RATIO 10.0 /* G3: tau_slow / tau_fast */ |
| 67 | #define EST4_G3_W 0.5 /* G3: share of U carried by the fast part */ |
| 68 | #define EST_DOMAIN_V4 "omega.est.v4.params.v1" |
| 69 | |
| 70 | typedef enum { EST4_G1_LAG = 1, EST4_G2_AR = 2, EST4_G3_TWO = 3 } est4_family; |
| 71 | |
| 72 | typedef struct { |
| 73 | est4_family family; |
| 74 | double dyn; /* G1, G3: tau (fast) in ticks, > 0; G2: rho in [0, 1) */ |
| 75 | double q; /* process variance per tick, mC^2, > 0 */ |
| 76 | double lam; /* fast scale memory, [0, 1) */ |
| 77 | double nu; /* one-step Student-t degrees of freedom, > 0 */ |
| 78 | double c; /* one-step scale multiplier, > 0 */ |
| 79 | double phi; /* horizon scale persistence, (0, 1] */ |
| 80 | double nu_h; /* horizon Student-t degrees of freedom, > 0 */ |
| 81 | double c_h; /* horizon scale multiplier, > 0 */ |
| 82 | double quantum; /* grid quantum, mC, > 0 */ |
| 83 | } est4_params; |
| 84 | |
| 85 | typedef struct { |
| 86 | est4_params p; |
| 87 | uint32_t d; |
| 88 | double A[EST4_MAX_D * EST4_MAX_D], Q[EST4_MAX_D * EST4_MAX_D], H[EST4_MAX_D]; |
| 89 | double r; |
| 90 | uint32_t has_anchor; |
| 91 | double anchor; /* last valid observation */ |
| 92 | uint32_t gap; /* ticks since the last valid observation */ |
| 93 | double x[EST4_MAX_D], P[EST4_MAX_D * EST4_MAX_D]; |
| 94 | double g, gl; /* scale state (dimensionless, relative to S_1) */ |
| 95 | uint64_t generation; /* +1 per update or missing tick */ |
| 96 | } est4_state; |
| 97 | |
| 98 | /* Continuous predictive: Y_h ~ loc + scale * t_nu. */ |
| 99 | typedef struct { uint32_t h; double loc, scale, nu; } est4_pred; |
| 100 | |
| 101 | est_status est4_params_check(const est4_params *p); |
| 102 | est_status est4_params_digest(const est4_params *p, est_digest *out); |
| 103 | est_status est4_init(const est4_params *p, est4_state *s); |
| 104 | /* Predict h ticks ahead of the current state (1 <= h <= EST_PRED_MAX_H). |
| 105 | * Refuses (EST_ERR_STALE) before the first valid observation. */ |
| 106 | est_status est4_predict(const est4_state *s, uint32_t h, est4_pred *out); |
| 107 | /* Discrete predictive pmf around s->anchor, floored as in v3. dp->horizon is |
| 108 | * gap + h (ticks after the anchor); dp->family is 0 (not an est_pred family). */ |
| 109 | est_status est4_dpred(const est4_state *s, const est4_pred *pr, est_dpred *dp); |
| 110 | /* Closed-form log score of an on-grid y under the est4_dpred pmf for anchor: |
| 111 | * outcome bin k = round((y - anchor)/q) clamped to +-EST_PRED_K, edge bins |
| 112 | * take the tails, EST_PRED_FLOOR mix applied. Equals the pmf score up to |
| 113 | * rounding (the pmf path renormalises sums that are 1 to rounding). |
| 114 | * F_lo/F_hi optional (floored CDF below / through the bin). */ |
| 115 | double est4_fast_logp(const est4_pred *pr, double quantum, double anchor, double y, double *F_lo, double *F_hi); |
| 116 | /* Model moments at horizon h: m_h and V_h (est_v4.h), no scale law. */ |
| 117 | est_status est4_moments(const est4_state *s, uint32_t h, double *loc, double *V); |
| 118 | /* gbar_h = (1/h) sum_{j=1..h} (gl + (g - gl) phi^(j-1)). */ |
| 119 | double est4_gbar(double g, double gl, double phi, uint32_t h); |
| 120 | /* Advance one tick. present = 0 (or a non-finite value) is a missing tick. */ |
| 121 | est_status est4_update(est4_state *s, int present, double y); |
| 122 | |
| 123 | #endif |