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 / src / estimation / est_types.h

233 lines · 11292 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/* EST-0 semantic contract: observation, belief (state estimate), prediction,
18 * innovation, and the linear-Gaussian model they are bound to (ARCH-0020).
19 *
20 * Standalone module. It is not linked into the resident runtime, the default
21 * build, or the default test suite (mk/estimation.mk only). It reads nothing
22 * from World, grants nothing, and its object files are checked with nm -u
23 * against every authority, World, generation and process entry point.
24 *
25 * The five categories never collapse:
26 * observation evidence: a measured value plus its declared noise. Immutable
27 * once recorded. The estimator never writes one.
28 * belief an estimate: state vector + covariance at a generation,
29 * bound to a model and to the evidence it was derived from.
30 * prediction a belief propagated forward, plus the predicted observation
31 * distribution. Never an observation, never overwrites one.
32 * innovation observation minus predicted observation, with its covariance
33 * and normalized squared surprise. A number, never a proof.
34 * model transition + observation model; identity is its digest.
35 * Confidence in any of these is data. Nothing here is an authority input.
36 *
37 * Records are distinct C types with distinct kind bytes and distinct digest
38 * domains. Digest = SHA-256(domain || 0x00 || encoding) using src/sha256.c,
39 * the same domain-separation rule as src/turing/ty_qrecord.h. Encoding is a
40 * fixed little-endian layout (est_encode_*), doubles as IEEE-754 binary64 bit
41 * patterns; the layout is specified in est_types.c above each encoder.
42 *
43 * Bounded: dimensions are at most EST_MAX_DIM; no allocation; no globals. */
44#ifndef OMEGA_EST_TYPES_H
45#define OMEGA_EST_TYPES_H
46
47#include <stddef.h>
48#include <stdint.h>
49
50#define EST_MAX_DIM 8u
51#define EST_DIGEST_SIZE 32u
52#define EST_FORMAT_VERSION 1u
53/* Longest predict horizon in steps; above this predict and every prediction
54 * check refuse with EST_ERR_TIME (bounded CPU per call). */
55#define EST_MAX_HORIZON 1000000u
56
57/* Digest domains (versioned; a layout change is a new domain). */
58#define EST_DOMAIN_MODEL "omega.est.model.v1"
59#define EST_DOMAIN_OBSERVATION "omega.est.observation.v1"
60#define EST_DOMAIN_BELIEF "omega.est.belief.v1"
61#define EST_DOMAIN_PREDICTION "omega.est.prediction.v1"
62#define EST_DOMAIN_INNOVATION "omega.est.innovation.v1"
63
64typedef struct { uint8_t b[EST_DIGEST_SIZE]; } est_digest; /* all-zero = absent */
65
66typedef enum {
67 EST_KIND_MODEL = 1,
68 EST_KIND_OBSERVATION = 2,
69 EST_KIND_BELIEF = 3,
70 EST_KIND_PREDICTION = 4,
71 EST_KIND_INNOVATION = 5
72} est_kind;
73
74/* Units of one state or observation component. Uncertainty (a covariance
75 * entry) has the product of the two components' units; a variance has the
76 * unit squared. EST_UNIT_NONE is refused: every component declares a unit. */
77typedef enum {
78 EST_UNIT_NONE = 0,
79 EST_UNIT_MILLI_CELSIUS = 1,
80 EST_UNIT_MILLI_CELSIUS_PER_S = 2,
81 EST_UNIT_WATT = 3,
82 EST_UNIT_WATT_PER_S = 4,
83 EST_UNIT_NANOSECOND = 5,
84 EST_UNIT_BYTE = 6,
85 EST_UNIT_DIMENSIONLESS = 7,
86 EST_UNIT_LOG2_PICOSECOND = 8,
87 EST_UNIT_MAX_ = 9
88} est_unit;
89
90/* What a covariance means. v1 accepts only the linear-Gaussian reading:
91 * the second central moment of a Gaussian error in the declared units. */
92typedef enum {
93 EST_UNCERTAINTY_GAUSSIAN_COVARIANCE = 1
94} est_uncertainty_meaning;
95
96typedef enum {
97 EST_ESTIMATOR_LINEAR_KALMAN = 1
98 /* reserved, not implemented: extended, unscented, particle, synthesized */
99} est_estimator_kind;
100
101typedef enum {
102 EST_OK = 0,
103 EST_ERR_NULL = -1,
104 EST_ERR_DIM = -2, /* dimension 0, above EST_MAX_DIM, or mismatch */
105 EST_ERR_NONFINITE = -3, /* NaN or Inf anywhere */
106 EST_ERR_ASYMMETRIC = -4, /* covariance not symmetric within tolerance */
107 EST_ERR_NOT_PSD = -5, /* covariance has a materially negative direction */
108 EST_ERR_NOT_PD = -6, /* matrix that must be invertible is not */
109 EST_ERR_UNIT = -7, /* missing or unknown unit / meaning */
110 EST_ERR_STALE = -8, /* generation or parent digest does not match */
111 EST_ERR_MODEL = -9, /* record bound to a different model */
112 EST_ERR_KIND = -10, /* wrong record kind: also an observation whose source digest is
113 zero, whose evidence digest is zero, or whose evidence digest
114 equals a digest of a prediction or belief passed to update
115 (a model output passed off as evidence) */
116 EST_ERR_ENCODING = -11, /* truncated or malformed bytes, bad version */
117 EST_ERR_TIME = -12 /* logical time does not advance as required */
118} est_status;
119
120/* Row-major n*n matrices; only the first n*n entries are meaningful. */
121typedef struct {
122 uint32_t n; /* state dimension */
123 uint32_t m; /* observation dimension */
124 est_estimator_kind estimator;
125 est_uncertainty_meaning meaning;
126 est_unit state_unit[EST_MAX_DIM];
127 est_unit obs_unit[EST_MAX_DIM];
128 double F[EST_MAX_DIM * EST_MAX_DIM]; /* n*n transition per step */
129 double Q[EST_MAX_DIM * EST_MAX_DIM]; /* n*n process noise per step */
130 double H[EST_MAX_DIM * EST_MAX_DIM]; /* m*n observation matrix */
131 double R[EST_MAX_DIM * EST_MAX_DIM]; /* m*m default observation noise */
132 int64_t step_ns; /* logical duration of one transition step */
133} est_model;
134
135/* What the estimator can NOT verify: that `evidence` is really a digest of raw
136 * bytes a sensor path produced. It only checks that source and evidence are
137 * non-zero and that the evidence digest is not the digest of any prediction or
138 * belief record handed to the same update call (est_kf_update). Binding
139 * evidence digests to raw bytes, and keeping model-derived digests out of that
140 * field, is the CALLER's job (ARCH-0020 section 2.1). */
141/* Evidence. value and noise are what the sensor path declared; source names
142 * the producer (e.g. digest of "R15.machine-state.thermal_mc[0]"); evidence
143 * binds the raw bytes (e.g. the raw SHA256SUMS line or record digest). */
144typedef struct {
145 uint32_t m;
146 est_unit unit[EST_MAX_DIM];
147 double z[EST_MAX_DIM];
148 double R[EST_MAX_DIM * EST_MAX_DIM];
149 int64_t t_ns; /* logical time of the measurement */
150 uint64_t seq; /* per-source sequence number */
151 est_digest source;
152 est_digest evidence; /* must be non-zero */
153} est_observation;
154
155/* Estimate. evidence_root chains every observation digest that shaped it:
156 * root' = SHA-256(EST_DOMAIN_BELIEF "-root" || 0x00 || root || obs digest). */
157typedef struct {
158 uint32_t n;
159 est_unit unit[EST_MAX_DIM];
160 double x[EST_MAX_DIM];
161 double P[EST_MAX_DIM * EST_MAX_DIM];
162 uint64_t generation; /* +1 per predict or update */
163 int64_t t_ns; /* logical time the estimate refers to */
164 est_digest model;
165 est_digest parent; /* digest of the record it was derived from */
166 est_digest evidence_root; /* zero only for a declared prior */
167} est_belief;
168
169/* Prediction: prior belief propagated `horizon` steps, plus the predicted
170 * observation distribution y ~ N(y_mean, S). */
171typedef struct {
172 est_digest prior; /* digest of the belief it came from */
173 est_digest model;
174 uint32_t horizon; /* steps, 1..EST_MAX_HORIZON */
175 uint64_t generation;
176 int64_t t_ns; /* logical time predicted for */
177 uint32_t n, m;
178 double x[EST_MAX_DIM];
179 double P[EST_MAX_DIM * EST_MAX_DIM];
180 double y_mean[EST_MAX_DIM];
181 double S[EST_MAX_DIM * EST_MAX_DIM]; /* H P H^T + R */
182 uint32_t has_control; /* 1 if a known input was applied each step, else 0 */
183 double Bu[EST_MAX_DIM]; /* the input (n entries, -0.0 canonicalized); zero if has_control == 0 */
184} est_prediction;
185
186/* Innovation: nu = z - y_mean, S its covariance, nis = nu^T S^-1 nu.
187 * Under a calibrated model nis ~ chi-square with m degrees of freedom. */
188typedef struct {
189 est_digest prediction;
190 est_digest observation;
191 est_digest model;
192 uint32_t m;
193 double nu[EST_MAX_DIM];
194 double S[EST_MAX_DIM * EST_MAX_DIM];
195 double nis;
196 int64_t t_ns;
197} est_innovation;
198
199/* ---- validation (every constructor and every encoder calls these) ---- */
200/* Symmetric within 1e-9 relative to the largest |entry|, every entry finite,
201 * and LDL^T has no pivot below -1e-12 * max diagonal (PSD). */
202est_status est_check_covariance(const double *A, uint32_t n);
203est_status est_check_model(const est_model *mdl);
204est_status est_check_observation(const est_observation *obs);
205est_status est_check_belief(const est_belief *b);
206est_status est_check_prediction(const est_prediction *p);
207est_status est_check_innovation(const est_innovation *iv);
208
209/* ---- identity ---- */
210est_status est_digest_model(const est_model *mdl, est_digest *out);
211est_status est_digest_observation(const est_observation *obs, est_digest *out);
212est_status est_digest_belief(const est_belief *b, est_digest *out);
213est_status est_digest_prediction(const est_prediction *p, est_digest *out);
214est_status est_digest_innovation(const est_innovation *iv, est_digest *out);
215est_status est_evidence_root_extend(const est_digest *root, const est_digest *obs,
216 est_digest *out);
217int est_digest_is_zero(const est_digest *d);
218
219/* ---- serialization: exact round trip; decode validates and refuses a kind
220 * byte, version or length that does not match the target type ---- */
221#define EST_ENCODED_MAX 2304u /* n = m = 8 with control: model 2160, prediction 1320 */
222est_status est_encode_model(const est_model *mdl, uint8_t *buf, size_t cap, size_t *len);
223est_status est_decode_model(const uint8_t *buf, size_t len, est_model *out);
224est_status est_encode_observation(const est_observation *o, uint8_t *buf, size_t cap, size_t *len);
225est_status est_decode_observation(const uint8_t *buf, size_t len, est_observation *out);
226est_status est_encode_belief(const est_belief *b, uint8_t *buf, size_t cap, size_t *len);
227est_status est_decode_belief(const uint8_t *buf, size_t len, est_belief *out);
228est_status est_encode_prediction(const est_prediction *p, uint8_t *buf, size_t cap, size_t *len);
229est_status est_decode_prediction(const uint8_t *buf, size_t len, est_prediction *out);
230est_status est_encode_innovation(const est_innovation *iv, uint8_t *buf, size_t cap, size_t *len);
231est_status est_decode_innovation(const uint8_t *buf, size_t len, est_innovation *out);
232
233#endif /* OMEGA_EST_TYPES_H */