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_mix.c

201 lines · 6975 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#include "est_mix.h"
18#include "omega_native.h"
19
20#include <math.h>
21#include <string.h>
22
23#define LOG_2PI 1.8378770664093453
24
25est_status est_mix_check(const est_mix *mx)
26{
27 double tol = 1e-12;
28 uint64_t tol_b = 0;
29 int64_t rc = 0;
30 if (!mx) return EST_ERR_NULL;
31 if (mx->k < 1 || mx->k > EST_MIX_MAX) return EST_ERR_DIM;
32 memcpy(&tol_b, &tol, 8);
33 if (omega2_est_om_mix_check(&rc, (const uint64_t *)mx->w, EST_MIX_MAX,
34 (const uint64_t *)mx->v, EST_MIX_MAX, mx->k, tol_b) != 0)
35 return EST_ERR_ENCODING;
36 return (est_status)rc;
37}
38
39double est_mix_total_var(const est_mix *mx)
40{
41 uint64_t bits = 0;
42 double d = 0.0;
43 (void)omega2_est_om_mix_total_var(&bits, (const uint64_t *)mx->w, EST_MIX_MAX,
44 (const uint64_t *)mx->v, EST_MIX_MAX, mx->k);
45 memcpy(&d, &bits, 8);
46 return d;
47}
48
49/* Visit every count vector a[0..k-1] with sum h and positive probability:
50 * log weight = log multinomial + sum a_j log w_j, variance = sum a_j v_j. */
51typedef struct { double lw, var; } term;
52typedef void (*term_fn)(const term *t, void *ctx);
53
54static void each_term(const est_mix *mx, uint32_t h, term_fn fn, void *ctx)
55{
56 uint32_t a[EST_MIX_MAX] = { 0 };
57 uint32_t k = mx->k;
58 double lh = lgamma((double)h + 1.0);
59 /* a[0..k-2] free, a[k-1] = remainder */
60 for (;;) {
61 uint32_t used = 0;
62 for (uint32_t j = 0; j + 1 < k; j++) used += a[j];
63 if (used <= h) {
64 a[k - 1] = h - used;
65 term t = { lh, 0.0 };
66 int ok = 1;
67 for (uint32_t j = 0; j < k; j++) {
68 if (a[j] == 0) continue;
69 if (mx->w[j] <= 0.0) { ok = 0; break; }
70 t.lw += -lgamma((double)a[j] + 1.0) + (double)a[j] * log(mx->w[j]);
71 t.var += (double)a[j] * mx->v[j];
72 }
73 if (ok) fn(&t, ctx);
74 }
75 /* next count vector over the k-1 free entries */
76 if (k == 1) return;
77 uint32_t j = 0;
78 while (j + 1 < k) {
79 a[j]++;
80 uint32_t u = 0;
81 for (uint32_t i = 0; i + 1 < k; i++) u += a[i];
82 if (u <= h) break;
83 a[j] = 0; j++;
84 }
85 if (j + 1 >= k) return;
86 }
87}
88
89typedef struct { double t, acc; } cdf_ctx;
90static void cdf_term(const term *t, void *c)
91{
92 cdf_ctx *x = c;
93 x->acc += exp(t->lw) * erf(x->t / sqrt(2.0 * t->var));
94}
95
96est_status est_mix_cdf_abs(const est_mix *mx, uint32_t h, double t, double *out)
97{
98 est_status st = est_mix_check(mx);
99 if (st != EST_OK) return st;
100 if (!out) return EST_ERR_NULL;
101 if (h < 1 || h > EST_MIX_MAX_H) return EST_ERR_TIME;
102 if (!isfinite(t)) return EST_ERR_NONFINITE;
103 if (t <= 0.0) { *out = 0.0; return EST_OK; }
104 cdf_ctx c = { t, 0.0 };
105 each_term(mx, h, cdf_term, &c);
106 *out = c.acc > 1.0 ? 1.0 : c.acc;
107 return EST_OK;
108}
109
110est_status est_mix_quantile_abs(const est_mix *mx, uint32_t h, double p, double *out)
111{
112 est_status st = est_mix_check(mx);
113 if (st != EST_OK) return st;
114 if (!out) return EST_ERR_NULL;
115 if (h < 1 || h > EST_MIX_MAX_H) return EST_ERR_TIME;
116 if (!(p > 0.0 && p < 1.0)) return EST_ERR_DIM;
117 double vmax = 0;
118 for (uint32_t j = 0; j < mx->k; j++) if (mx->v[j] > vmax) vmax = mx->v[j];
119 double lo = 0.0, hi = 64.0 * sqrt((double)h * vmax);
120 for (int i = 0; i < 200; i++) {
121 double mid = 0.5 * (lo + hi), c = 0.0;
122 est_mix_cdf_abs(mx, h, mid, &c);
123 if (c < p) lo = mid; else hi = mid;
124 }
125 *out = hi;
126 return EST_OK;
127}
128
129typedef struct { double e, mx, acc; int pass; } lp_ctx;
130static void lp_term(const term *t, void *c)
131{
132 lp_ctx *x = c;
133 double l = t->lw - 0.5 * (LOG_2PI + log(t->var) + x->e * x->e / t->var);
134 if (x->pass == 0) { if (l > x->mx) x->mx = l; }
135 else x->acc += exp(l - x->mx);
136}
137
138est_status est_mix_logpdf(const est_mix *mx, uint32_t h, double e, double *out)
139{
140 est_status st = est_mix_check(mx);
141 if (st != EST_OK) return st;
142 if (!out) return EST_ERR_NULL;
143 if (h < 1 || h > EST_MIX_MAX_H) return EST_ERR_TIME;
144 if (!isfinite(e)) return EST_ERR_NONFINITE;
145 lp_ctx c = { e, -INFINITY, 0.0, 0 };
146 each_term(mx, h, lp_term, &c);
147 c.pass = 1;
148 each_term(mx, h, lp_term, &c);
149 *out = c.mx + log(c.acc);
150 return EST_OK;
151}
152
153est_status est_mix_fit_em(const double *e, size_t n, uint32_t k, double floor_var,
154 uint32_t iters, est_mix *out, double *loglik)
155{
156 static const double start[EST_MIX_MAX] = { 0.01, 0.3, 3.0 };
157 if (!e || !out) return EST_ERR_NULL;
158 if (k < 1 || k > EST_MIX_MAX || n < 2) return EST_ERR_DIM;
159 if (!isfinite(floor_var) || !(floor_var > 0.0)) return EST_ERR_NOT_PD;
160 double v0 = 0;
161 for (size_t i = 0; i < n; i++) {
162 if (!isfinite(e[i])) return EST_ERR_NONFINITE;
163 v0 += e[i] * e[i];
164 }
165 v0 /= (double)n;
166 est_mix m = { k, { 0 }, { 0 } };
167 for (uint32_t j = 0; j < k; j++) {
168 m.w[j] = 1.0 / (double)k;
169 m.v[j] = v0 * start[j];
170 if (m.v[j] < floor_var) m.v[j] = floor_var;
171 }
172 for (uint32_t it = 0; it < iters; it++) {
173 double sw[EST_MIX_MAX] = { 0 }, sv[EST_MIX_MAX] = { 0 };
174 for (size_t i = 0; i < n; i++) {
175 double l[EST_MIX_MAX], mxl = -INFINITY, tot = 0;
176 for (uint32_t j = 0; j < k; j++) {
177 l[j] = m.w[j] > 0 ? log(m.w[j]) - 0.5 * (log(m.v[j]) + e[i] * e[i] / m.v[j]) : -INFINITY;
178 if (l[j] > mxl) mxl = l[j];
179 }
180 for (uint32_t j = 0; j < k; j++) { l[j] = exp(l[j] - mxl); tot += l[j]; }
181 for (uint32_t j = 0; j < k; j++) { double g = l[j] / tot; sw[j] += g; sv[j] += g * e[i] * e[i]; }
182 }
183 double ws = 0;
184 for (uint32_t j = 0; j < k; j++) {
185 m.w[j] = sw[j] / (double)n;
186 if (sw[j] > 1e-300) m.v[j] = sv[j] / sw[j];
187 if (m.v[j] < floor_var) m.v[j] = floor_var;
188 ws += m.w[j];
189 }
190 for (uint32_t j = 0; j < k; j++) m.w[j] /= ws;
191 }
192 est_status st = est_mix_check(&m);
193 if (st != EST_OK) return st;
194 if (loglik) {
195 double ll = 0;
196 for (size_t i = 0; i < n; i++) { double l; est_mix_logpdf(&m, 1, e[i], &l); ll += l; }
197 *loglik = ll;
198 }
199 *out = m;
200 return EST_OK;
201}