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_v4.om

613 lines · 13568 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note ESTIMATION v4 persistence-aware predictive families in pure seeded OM-2 (est_v4.h / est_v4.c).
3
4text EST_DOM_V4 "omega.est.v4.params.v1"
5
6fn est4_mat_mul d ulong a view f64 9 b view f64 9 dst edit f64 9 to void
7 t = new f64 9 0.0f64
8 for i ulong d
9 for j ulong d
10 s = f64 0.0f64
11 for k ulong d
12 aik = get a add mul i d k
13 bkj = get b add mul k d j
14 s = add s mul aik bkj
15 end
16 put t add mul i d j s
17 end
18 end
19 for i ulong mul d d
20 put dst i get t i
21 end
22end
23
24fn est4_sandwich d ulong a view f64 9 b view f64 9 dst edit f64 9 to void
25 t = new f64 9 0.0
26 atr = new f64 9 0.0
27 for i ulong d
28 for j ulong d
29 put atr add mul j d i get a add mul i d j
30 end
31 end
32 est4_mat_mul d a b t
33 est4_mat_mul d view t view atr dst
34end
35
36fn est4_params_check p view Est4Params to long
37 fam = get p family
38 if and ne fam EST4_G1_LAG and ne fam EST4_G2_AR ne fam EST4_G3_TWO
39 ret EST_ERR_KIND
40 end
41 if not est_isfinite get p dyn
42 ret EST_ERR_NONFINITE
43 end
44 if not est_isfinite get p q
45 ret EST_ERR_NONFINITE
46 end
47 if not est_isfinite get p lam
48 ret EST_ERR_NONFINITE
49 end
50 if not est_isfinite get p phi
51 ret EST_ERR_NONFINITE
52 end
53 if not est_isfinite get p nu
54 ret EST_ERR_NONFINITE
55 end
56 if not est_isfinite get p c
57 ret EST_ERR_NONFINITE
58 end
59 if not est_isfinite get p nu_h
60 ret EST_ERR_NONFINITE
61 end
62 if not est_isfinite get p c_h
63 ret EST_ERR_NONFINITE
64 end
65 if not est_isfinite get p quantum
66 ret EST_ERR_NONFINITE
67 end
68 if eq fam EST4_G2_AR
69 if or lt get p dyn 0.0f64 ge get p dyn 1.0f64
70 ret EST_ERR_DIM
71 end
72 else
73 if not gt get p dyn 0.0f64
74 ret EST_ERR_DIM
75 end
76 end
77 if not gt get p q 0.0f64
78 ret EST_ERR_DIM
79 end
80 if or lt get p lam 0.0f64 ge get p lam 1.0f64
81 ret EST_ERR_DIM
82 end
83 if or le get p phi 0.0f64 gt get p phi 1.0f64
84 ret EST_ERR_DIM
85 end
86 if not gt get p nu 0.0f64
87 ret EST_ERR_DIM
88 end
89 if not gt get p c 0.0f64
90 ret EST_ERR_DIM
91 end
92 if not gt get p nu_h 0.0f64
93 ret EST_ERR_DIM
94 end
95 if not gt get p c_h 0.0f64
96 ret EST_ERR_DIM
97 end
98 if not gt get p quantum 0.0f64
99 ret EST_ERR_DIM
100 end
101 ret EST_OK
102end
103
104fn est4_params_digest p view Est4Params dst edit uchar 32 to long
105 st = est4_params_check p
106 if ne st EST_OK
107 ret st
108 end
109 enc = new uchar 100 0
110 o = est_put_u32 enc 0 get p family
111 o = est_put_f64_canon enc o get p dyn
112 o = est_put_f64_canon enc o get p q
113 o = est_put_f64_canon enc o get p lam
114 o = est_put_f64_canon enc o get p nu
115 o = est_put_f64_canon enc o get p c
116 o = est_put_f64_canon enc o get p phi
117 o = est_put_f64_canon enc o get p nu_h
118 o = est_put_f64_canon enc o get p c_h
119 o = est_put_f64_canon enc o get p quantum
120 o = est_put_f64_canon enc o EST4_GL_LAMBDA
121 o = est_put_f64_canon enc o EST4_TAU_RATIO
122 o = est_put_f64_canon enc o EST4_G3_W
123 est_sha256_domain EST_DOM_V4 view enc o dst
124 ret EST_OK
125end
126
127fn est4_state_to_params s view Est4State p edit Est4Params to void
128 put p family get s family
129 put p dyn get s pv 0
130 put p q get s pv 1
131 put p lam get s pv 2
132 put p nu get s pv 3
133 put p c get s pv 4
134 put p phi get s pv 5
135 put p nu_h get s pv 6
136 put p c_h get s pv 7
137 put p quantum get s pv 8
138end
139
140fn est4_init p view Est4Params s edit Est4State to long
141 st = est4_params_check p
142 if ne st EST_OK
143 ret st
144 end
145 fam = get p family
146 put s family fam
147 put s pv 0 get p dyn
148 put s pv 1 get p q
149 put s pv 2 get p lam
150 put s pv 3 get p nu
151 put s pv 4 get p c
152 put s pv 5 get p phi
153 put s pv 6 get p nu_h
154 put s pv 7 get p c_h
155 put s pv 8 get p quantum
156 for i ulong 9
157 put s A i 0.0f64
158 put s Q i 0.0f64
159 put s P i 0.0f64
160 end
161 for i ulong 3
162 put s H i 0.0f64
163 put s x i 0.0f64
164 end
165 qu = get p quantum
166 put s r div mul qu qu 12.0f64
167 put s has_anchor 0
168 put s anchor 0.0f64
169 put s gap 0
170 put s g 1.0f64
171 put s gl 1.0f64
172 put s generation 0
173 if eq fam EST4_G1_LAG
174 a = est_exp div -1.0f64 get p dyn
175 put s d 2
176 put s A 0 a
177 put s A 1 sub 1.0f64 a
178 put s A 3 1.0f64
179 put s Q 3 get p q
180 put s H 0 1.0f64
181 else
182 if eq fam EST4_G2_AR
183 rv = get p dyn
184 put s d 2
185 put s A 0 1.0f64
186 put s A 1 rv
187 put s A 3 rv
188 qv = get p q
189 put s Q 0 qv
190 put s Q 1 qv
191 put s Q 2 qv
192 put s Q 3 qv
193 put s H 0 1.0f64
194 else
195 af = est_exp div -1.0f64 get p dyn
196 asv = est_exp div -1.0f64 mul get p dyn EST4_TAU_RATIO
197 w = EST4_G3_W
198 put s d 3
199 put s A 0 af
200 put s A 2 mul sub 1.0f64 af w
201 put s A 4 asv
202 put s A 5 mul sub 1.0f64 asv sub 1.0f64 w
203 put s A 8 1.0f64
204 put s Q 8 get p q
205 put s H 0 1.0f64
206 put s H 1 1.0f64
207 end
208 end
209 ret EST_OK
210end
211
212fn est4_first_obs s edit Est4State y0 f64 to void
213 for i ulong 3
214 put s x i 0.0f64
215 end
216 for i ulong 9
217 put s P i 0.0f64
218 end
219 qv = get s pv 1
220 rv = get s r
221 fam = get s family
222 if eq fam EST4_G1_LAG
223 put s x 0 y0
224 put s x 1 y0
225 put s P 0 rv
226 put s P 3 add rv qv
227 else
228 if eq fam EST4_G2_AR
229 put s x 0 y0
230 put s x 1 0.0f64
231 put s P 0 rv
232 put s P 3 qv
233 else
234 put s x 0 mul EST4_G3_W y0
235 put s x 1 mul sub 1.0f64 EST4_G3_W y0
236 put s x 2 y0
237 put s P 0 rv
238 put s P 4 rv
239 put s P 8 add rv qv
240 end
241 end
242end
243
244fn est4_prop_step d ulong Am view f64 9 Qm view f64 9 x edit f64 3 P edit f64 9 to void
245 xn = new f64 3 0.0
246 for i ulong d
247 acc = f64 0.0
248 base = mul i d
249 for j ulong d
250 acc = add acc mul get Am add base j get x j
251 end
252 put xn i acc
253 end
254 for i ulong d
255 put x i get xn i
256 end
257 Pn = new f64 9 0.0
258 est4_sandwich d Am view P Pn
259 for i ulong mul d d
260 put P i add get Pn i get Qm i
261 end
262end
263
264fn est4_prop_vrow s view Est4State P view f64 9 i ulong d ulong to f64
265 acc = f64 0.0
266 base = mul i d
267 for j ulong d
268 acc = add acc mul get P add base j get s H j
269 end
270 ret mul get s H i acc
271end
272
273fn est4_prop_moments s view Est4State x view f64 3 P view f64 9 res edit f64 2 to void
274 d = get s d
275 mm = f64 0.0
276 vv = f64 0.0
277 for i ulong d
278 mm = add mm mul get s H i get x i
279 vv = add vv est4_prop_vrow s P i d
280 end
281 put res 0 mm
282 put res 1 add vv get s r
283end
284
285fn est4_propagate s view Est4State h ulong res edit f64 2 to void
286 d = get s d
287 x = new f64 3 0.0
288 P = new f64 9 0.0
289 Am = new f64 9 0.0
290 Qm = new f64 9 0.0
291 for i ulong d
292 put x i get s x i
293 end
294 for i ulong mul d d
295 put P i get s P i
296 put Am i get s A i
297 put Qm i get s Q i
298 end
299 for k ulong h bound 66
300 est4_prop_step d view Am view Qm x P
301 end
302 est4_prop_moments s view x view P res
303end
304
305fn est4_gbar g f64 gl f64 phi f64 h ulong to f64
306 if eq h 0
307 ret est_nan
308 end
309 f = f64 1.0f64
310 gb = f64 0.0f64
311 for j ulong h bound 66
312 gb = add gb add gl mul sub g gl f
313 f = mul f phi
314 end
315 ret div gb f64 h
316end
317
318fn est4_moments s view Est4State h ulong res edit f64 2 to long
319 if or lt h 1 gt h EST_PRED_MAX_H
320 ret EST_ERR_TIME
321 end
322 if eq get s has_anchor 0
323 ret EST_ERR_STALE
324 end
325 est4_propagate s h res
326 if and est_isfinite get res 0 and gt get res 1 0.0f64 est_isfinite get res 1
327 ret EST_OK
328 end
329 ret EST_ERR_NONFINITE
330end
331
332fn est4_predict s view Est4State h ulong dst edit Est4Pred to long
333 if or lt h 1 gt h EST_PRED_MAX_H
334 ret EST_ERR_TIME
335 end
336 if eq get s has_anchor 0
337 ret EST_ERR_STALE
338 end
339 mv = new f64 2 0.0f64
340 est4_propagate s h mv
341 mh = get mv 0
342 Vh = get mv 1
343 if or not gt Vh 0.0f64 not est_isfinite Vh
344 ret EST_ERR_NONFINITE
345 end
346 put dst h h
347 put dst loc mh
348 if eq h 1
349 put dst nu get s pv 3
350 put dst scale mul get s pv 4 fsqrt mul get s g Vh
351 else
352 gb = est4_gbar get s g get s gl get s pv 5 h
353 put dst nu get s pv 6
354 put dst scale mul get s pv 7 fsqrt mul gb Vh
355 end
356 if or not est_isfinite get dst loc or not gt get dst scale 0.0f64 not est_isfinite get dst scale
357 ret EST_ERR_NONFINITE
358 end
359 ret EST_OK
360end
361
362fn est4_state_digest s view Est4State dig edit uchar 32 to long
363 p = new Est4Params ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0
364 est4_state_to_params s p
365 ret est4_params_digest view p dig
366end
367
368fn est4_dpred s view Est4State pr view Est4Pred dp edit EstDPred pmf edit f64 801 to long
369 if eq get s has_anchor 0
370 ret EST_ERR_STALE
371 end
372 ph = get pr h
373 hz = add get s gap ph
374 if or lt ph 1 gt hz EST_PRED_MAX_H
375 ret EST_ERR_TIME
376 end
377 if not est_isfinite get pr loc
378 ret EST_ERR_NONFINITE
379 end
380 if or not gt get pr scale 0.0f64 not est_isfinite get pr scale
381 ret EST_ERR_NONFINITE
382 end
383 if not gt get pr nu 0.0f64
384 ret EST_ERR_NONFINITE
385 end
386 dig = new uchar 32 0
387 st = est4_state_digest s dig
388 if ne st EST_OK
389 ret st
390 end
391 for i ulong 32
392 put dp assumption i get dig i
393 end
394 put dp family 0
395 put dp horizon hz
396 put dp generation get s generation
397 put dp anchor get s anchor
398 put dp quantum get s pv 8
399 mx = new EstMix ulong 0 f64 0.0 f64 0.0
400 mq = new f64 2 0.0
401 put mq 0 sub get pr loc get s anchor
402 put mq 1 get s pv 8
403 st = est_pmf_from_dist 1 get pr scale get pr nu view mx view mq pmf
404 if ne st EST_OK
405 ret st
406 end
407 est_pmf_floor pmf
408 ret EST_OK
409end
410
411fn est4_sf_any x f64 nu f64 to f64
412 if est_isinf x
413 if gt x 0.0f64
414 ret 0.0f64
415 end
416 ret 1.0f64
417 end
418 if ge x 0.0f64
419 ret est_t_sf x nu
420 end
421 ret sub 1.0f64 est_t_sf neg x nu
422end
423
424fn est4_fast_prob a f64 b f64 nu f64 res edit f64 3 to void
425 lo = f64 0.0
426 hi = f64 0.0
427 p = f64 0.0
428 if ge a 0.0f64
429 sa = est4_sf_any a nu
430 sb = est4_sf_any b nu
431 p = sub sa sb
432 lo = sub 1.0f64 sa
433 hi = sub 1.0f64 sb
434 else
435 if le b 0.0f64
436 ca = est4_sf_any neg a nu
437 cb = est4_sf_any neg b nu
438 p = sub cb ca
439 lo = ca
440 hi = cb
441 else
442 lo = est4_sf_any neg a nu
443 sb = est4_sf_any b nu
444 p = sub sub 1.0f64 lo sb
445 hi = sub 1.0f64 sb
446 end
447 end
448 if lt p 0.0f64
449 p = 0.0f64
450 end
451 put res 0 p
452 put res 1 lo
453 put res 2 hi
454end
455
456fn est4_fast_finish plh view f64 3 k long fout edit f64 2 to f64
457 u = div EST_PRED_FLOOR 801.0f64
458 omf = sub 1.0f64 EST_PRED_FLOOR
459 idx = f64 add k EST_PRED_K
460 p = add mul omf get plh 0 u
461 put fout 0 add mul omf get plh 1 mul u idx
462 put fout 1 add mul omf get plh 2 mul u add idx 1.0f64
463 if lt p 1.0e-300f64
464 p = 1.0e-300f64
465 end
466 ret est_log p
467end
468
469fn est4_fast_logp pr view Est4Pred quantum f64 anchor f64 y f64 fout edit f64 2 to f64
470 kd = est_nearbyint div sub y anchor quantum
471 if lt kd -400.0f64
472 kd = -400.0f64
473 end
474 if gt kd 400.0f64
475 kd = 400.0f64
476 end
477 k = long kd
478 mu = sub get pr loc anchor
479 a = est_ninf
480 if ne k -400
481 a = div sub mul sub kd 0.5f64 quantum mu get pr scale
482 end
483 b = est_inf
484 if ne k 400
485 b = div sub mul add kd 0.5f64 quantum mu get pr scale
486 end
487 plh = new f64 3 0.0
488 est4_fast_prob a b get pr nu plh
489 ret est4_fast_finish view plh k fout
490end
491
492fn est4_time_update s edit Est4State to void
493 d = get s d
494 x = new f64 3 0.0
495 P = new f64 9 0.0
496 Am = new f64 9 0.0
497 Qm = new f64 9 0.0
498 for i ulong d
499 put x i get s x i
500 end
501 for i ulong mul d d
502 put P i get s P i
503 put Am i get s A i
504 put Qm i get s Q i
505 end
506 est4_prop_step d view Am view Qm x P
507 for i ulong d
508 put s x i get x i
509 end
510 for i ulong mul d d
511 put s P i get P i
512 end
513end
514
515fn est4_update_p s edit Est4State PH view f64 3 S1 f64 to void
516 d = get s d
517 for i ulong d
518 base = mul i d
519 phi = get PH i
520 for j ulong d
521 idx = add base j
522 dec = div mul phi get PH j S1
523 put s P idx sub get s P idx dec
524 end
525 end
526 for i ulong d
527 j = add i 1
528 while lt j d bound 8
529 p1 = add mul i d j
530 p2 = add mul j d i
531 v = mul 0.5f64 add get s P p1 get s P p2
532 put s P p1 v
533 put s P p2 v
534 j = add j 1
535 end
536 end
537end
538
539fn est4_compute_ph s view Est4State PH edit f64 3 to f64
540 d = get s d
541 m = f64 0.0
542 for i ulong d
543 m = add m mul get s H i get s x i
544 acc = f64 0.0
545 base = mul i d
546 for j ulong d
547 acc = add acc mul get s P add base j get s H j
548 end
549 put PH i acc
550 end
551 ret m
552end
553
554fn est4_update_scale s edit Est4State e f64 S1 f64 to void
555 e2 = mul e e
556 if lt e2 get s r
557 e2 = get s r
558 end
559 lam = get s pv 2
560 gn = add mul lam get s g mul sub 1.0f64 lam div e2 S1
561 put s g gn
562 put s gl add mul EST4_GL_LAMBDA get s gl mul sub 1.0f64 EST4_GL_LAMBDA gn
563end
564
565fn est4_update_meas s edit Est4State y f64 to long
566 d = get s d
567 est4_time_update s
568 PH = new f64 3 0.0
569 m = est4_compute_ph view s PH
570 S1 = get s r
571 for i ulong d
572 S1 = add S1 mul get s H i get PH i
573 end
574 if or not gt S1 0.0f64 not est_isfinite S1
575 ret EST_ERR_NONFINITE
576 end
577 e = sub y m
578 for i ulong d
579 put s x i add get s x i mul div get PH i S1 e
580 end
581 est4_update_p s view PH S1
582 est4_update_scale s e S1
583 put s anchor y
584 put s gap 0
585 for i ulong d
586 if not est_isfinite get s x i
587 ret EST_ERR_NONFINITE
588 end
589 end
590 ret EST_OK
591end
592
593fn est4_update s edit Est4State present ulong y f64 to long
594 ok = and ne present 0 est_isfinite y
595 put s generation add get s generation 1
596 if eq get s has_anchor 0
597 if not ok
598 ret EST_OK
599 end
600 est4_first_obs s y
601 put s has_anchor 1
602 put s anchor y
603 put s gap 0
604 ret EST_OK
605 end
606 if not ok
607 est4_time_update s
608 put s gl add mul EST4_GL_LAMBDA get s gl mul sub 1.0f64 EST4_GL_LAMBDA get s g
609 put s gap add get s gap 1
610 ret EST_OK
611 end
612 ret est4_update_meas s y
613end