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_mix.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-3b noise shape: zero-mean Gaussian scale mixture of a one-step change |
| 18 | * (protocol v2, docs/estimation/EST23_PROTOCOL_V2.md). |
| 19 | * |
| 20 | * Why it exists: under protocol v1 the one-step innovations of the CPU thermal |
| 21 | * signal were heavy-tailed (recorded failure class: model misspecification, |
| 22 | * docs/estimation/receipts/est23-v1/RESULT.md). One Gaussian width cannot be |
| 23 | * right both in the middle and in the tails. This module gives the predicted |
| 24 | * change a declared shape: k components with weights w[j] and variances v[j] |
| 25 | * per logical step. An h-step change is the sum of h independent draws, which |
| 26 | * is again a finite Gaussian mixture (enumerated over the multinomial counts), |
| 27 | * so coverage, quantiles and log density are exact for h <= EST_MIX_MAX_H. |
| 28 | * |
| 29 | * The shape is data about uncertainty, like a covariance: it grants nothing |
| 30 | * and reads nothing. Pure (no I/O, no allocation, no globals); libm only. */ |
| 31 | #ifndef OMEGA_EST_MIX_H |
| 32 | #define OMEGA_EST_MIX_H |
| 33 | |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | #include "est_types.h" |
| 38 | |
| 39 | #define EST_MIX_MAX 3u |
| 40 | #define EST_MIX_MAX_H 64u /* longest horizon with an exact h-fold sum */ |
| 41 | |
| 42 | typedef struct { |
| 43 | uint32_t k; /* components, 1..EST_MIX_MAX */ |
| 44 | double w[EST_MIX_MAX]; /* weights, each >= 0, sum 1 within 1e-12 */ |
| 45 | double v[EST_MIX_MAX]; /* variance per step, each finite and > 0 */ |
| 46 | } est_mix; |
| 47 | |
| 48 | est_status est_mix_check(const est_mix *mx); |
| 49 | /* sum_j w[j] v[j]: the variance of a one-step change */ |
| 50 | double est_mix_total_var(const est_mix *mx); |
| 51 | /* P(|e| <= t) for e the sum of h independent draws (1 <= h <= EST_MIX_MAX_H) */ |
| 52 | est_status est_mix_cdf_abs(const est_mix *mx, uint32_t h, double t, double *out); |
| 53 | /* smallest t with P(|e| <= t) >= p, p in (0,1): bisection, 200 halvings */ |
| 54 | est_status est_mix_quantile_abs(const est_mix *mx, uint32_t h, double p, double *out); |
| 55 | /* natural log of the density of the h-step sum at e */ |
| 56 | est_status est_mix_logpdf(const est_mix *mx, uint32_t h, double e, double *out); |
| 57 | /* Deterministic EM fit of a k-component shape to changes e[0..n-1] (one step |
| 58 | * each). Start: weights 1/k, variances V0 * {0.01, 0.3, 3} (first k), V0 the |
| 59 | * mean square of e. `iters` full EM rounds; every variance is held at or above |
| 60 | * floor_var after each M step. loglik = sum of log densities at the end. */ |
| 61 | est_status est_mix_fit_em(const double *e, size_t n, uint32_t k, double floor_var, |
| 62 | uint32_t iters, est_mix *out, double *loglik); |
| 63 | |
| 64 | #endif /* OMEGA_EST_MIX_H */ |