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 / tests / estimation / test_est_pred.om

899 lines · 26633 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note ESTIMATION-2 protocol v3 hostile suite in pure seeded OM-2
3note (port of tests/estimation/test_est_pred.c).
4
5global g_checks ulong 1
6global g_fail ulong 1
7global g_y f64 10000
8global g_pres ulong 10000
9global g_lvl f64 10000
10global g_xh f64 10000
11global g_one f64 801
12global g_out f64 801
13global g_ref f64 801
14global g_tmp f64 801
15global g_ar f64 1
16global g_lohi ulong 2
17
18const QU f64 100.0
19
20text P_PREFIX "test_est_pred: "
21text P_CHECKS " checks, "
22text P_FAILED " failed\n"
23text P_PASS "test_est_pred: PASS\n"
24text P_FAIL "test_est_pred: FAIL\n"
25text P_FTAG "FAIL tag="
26text P_NL "\n"
27
28fn chk cond bool tag ulong to void
29 put g_checks 0 add get g_checks 0 1
30 if not cond
31 put g_fail 0 add get g_fail 0 1
32 est_eputs P_FTAG
33 est_putu_fd 2 tag
34 est_eputs P_NL
35 end
36end
37
38fn chk_st got long want long tag ulong to void
39 chk eq got want tag
40end
41
42fn chk_near x f64 y f64 tol f64 tag ulong to void
43 chk le fabs sub x y tol tag
44end
45
46note ---- splitmix64 PRNG ----
47
48fn rnext st edit ulong 1 to ulong
49 z = add get st 0 0x9e3779b97f4a7c15
50 put st 0 z
51 z = mul xor z shr z 30 0xbf58476d1ce4e5b9
52 z = mul xor z shr z 27 0x94d049bb133111eb
53 ret xor z shr z 31
54end
55
56fn runif st edit ulong 1 to f64
57 u = add f64 shr rnext st 11 0.5f64
58 ret div u 9007199254740992.0f64
59end
60
61fn rnorm st edit ulong 1 to f64
62 u1 = runif st
63 u2 = runif st
64 ret mul fsqrt mul -2.0f64 est_log u1 est_cos mul EST_TWO_PI u2
65end
66
67fn rt st edit ulong 1 nu ulong to f64
68 c = f64 0.0
69 for i ulong nu bound 16
70 g = rnorm st
71 c = add c mul g g
72 end
73 ret div rnorm st fsqrt div c f64 nu
74end
75
76fn on_grid v f64 to f64
77 ret mul QU est_nearbyint div v QU
78end
79
80note ---- Helpers ----
81
82fn mk_assump fam ulong np ulong a edit EstAssump to void
83 put a family fam
84 put a unit EST_UNIT_MILLI_CELSIUS
85 put a quantum QU
86 put a lo -1.0e8f64
87 put a hi 1.0e8f64
88 put a nparam np
89 for i ulong 8
90 put a param i 0.0f64
91 end
92end
93
94fn mk_f1 qp f64 r f64 p0 f64 a edit EstAssump to void
95 mk_assump EST_FAM_GAUSS_KF 3 a
96 put a param 0 qp
97 put a param 1 r
98 put a param 2 p0
99end
100
101fn mk_f2 qp f64 r f64 p0 f64 c f64 a edit EstAssump to void
102 mk_assump EST_FAM_HUBER_KF 4 a
103 put a param 0 qp
104 put a param 1 r
105 put a param 2 p0
106 put a param 3 c
107end
108
109fn mk_f3 nu f64 s f64 a edit EstAssump to void
110 mk_assump EST_FAM_STUDENT_T 2 a
111 put a param 0 nu
112 put a param 1 s
113end
114
115fn mk_f4 k f64 w0 f64 w1 f64 w2 f64 v0 f64 a edit EstAssump to void
116 mk_assump EST_FAM_SCALE_MIX 7 a
117 put a param 0 k
118 put a param 1 w0
119 put a param 2 w1
120 put a param 3 w2
121 put a param 4 v0
122end
123
124fn mk_f5 lam f64 nu f64 c f64 fl f64 s0 f64 a edit EstAssump to void
125 mk_assump EST_FAM_ADAPTIVE_T 5 a
126 put a param 0 lam
127 put a param 1 nu
128 put a param 2 c
129 put a param 3 fl
130 put a param 4 s0
131end
132
133fn mk_evd t ulong d edit uchar 32 to void
134 for i ulong 32
135 v = xor xor shr mul add t 1 0x9d rem i 8 i 0x5a
136 put d i uchar and v 0xff
137 end
138 put d 0 uchar 0xee
139end
140
141fn mk_pob a view EstAssump v f64 present ulong t ulong o edit EstPObs to void
142 d = new uchar 32 0
143 mk_evd t d
144 chk_st est_pobs_classify a v present view d o EST_OK 50
145end
146
147fn pmf_ok pmf view f64 801 to bool
148 s = f64 0.0
149 for i ulong EST_PRED_N
150 v = get pmf i
151 if or not gt v 0.0f64 not est_isfinite v
152 ret false
153 end
154 s = add s v
155 end
156 ret le fabs sub s 1.0f64 1.0e-12f64
157end
158
159note ---- 1. Numerics ----
160
161fn test_numerics_t to void
162 ts = new f64 13 0.0
163 put ts 0 -1.0e6f64
164 put ts 1 -300.0f64
165 put ts 2 -12.0f64
166 put ts 3 -3.0f64
167 put ts 4 -1.0f64
168 put ts 5 -0.25f64
169 put ts 6 0.0f64
170 put ts 7 0.25f64
171 put ts 8 1.0f64
172 put ts 9 3.0f64
173 put ts 10 12.0f64
174 put ts 11 300.0f64
175 put ts 12 1.0e6f64
176 for i ulong 13
177 t = get ts i
178 c1 = add 0.5f64 div est_atan t EST_PI
179 if lt t 0.0f64
180 c1 = div est_atan div -1.0f64 t EST_PI
181 chk le fabs sub est_t_cdf t 1.0f64 c1 mul 1.0e-12f64 c1 110
182 else
183 chk_near est_t_cdf t 1.0f64 c1 1.0e-14f64 111
184 end
185 r2 = fsqrt add 2.0f64 mul t t
186 sf2 = f64 0.0
187 if ge t 0.0f64
188 sf2 = div 1.0f64 mul r2 add r2 t
189 chk le fabs sub est_t_sf t 2.0f64 sf2 mul 1.0e-12f64 sf2 112
190 else
191 sf2 = sub 1.0f64 div 1.0f64 mul r2 sub r2 t
192 chk_near est_t_sf t 2.0f64 sf2 1.0e-14f64 113
193 end
194 chk_near add est_t_cdf t 3.7f64 est_t_sf t 3.7f64 1.0f64 1.0e-14f64 114
195 chk eq est_t_cdf t 3.7f64 est_t_sf neg t 3.7f64 115
196 end
197 chk le fabs sub est_t_sf 1.0e3f64 1.0f64 div est_atan 1.0e-3f64 EST_PI mul 1.0e-12f64 est_t_sf 1.0e3f64 1.0f64 116
198 chk est_isnan est_t_cdf 1.0f64 0.0f64 117
199 chk est_isnan est_t_cdf est_nan 3.0f64 117
200 chk est_isnan est_t_sf 1.0f64 -2.0f64 117
201 chk eq est_t_sf est_inf 3.0f64 0.0f64 118
202 chk eq est_t_cdf est_inf 3.0f64 1.0f64 118
203 for ix ulong 25
204 x = add -6.0f64 mul 0.5f64 f64 ix
205 chk_near est_t_cdf x 1.0e5f64 est_norm_cdf x 1.0e-5f64 119
206 end
207end
208
209fn test_numerics to void
210 for ix ulong 9
211 x = mul 0.125f64 f64 ix
212 chk_near est_betai 1.0f64 1.0f64 x x 1.0e-14f64 100
213 chk_near est_betai 3.5f64 1.0f64 x est_pow x 3.5f64 1.0e-13f64 101
214 chk_near est_betai 1.0f64 2.5f64 x sub 1.0f64 est_pow sub 1.0f64 x 2.5f64 1.0e-13f64 102
215 end
216 chk_near est_betai 2.0f64 3.0f64 0.4f64 0.5248f64 1.0e-14f64 103
217 chk_near est_betai 7.3f64 7.3f64 0.5f64 0.5f64 1.0e-13f64 104
218 chk_near est_betai 0.5f64 0.5f64 0.25f64 mul div 2.0f64 EST_PI est_asin 0.5f64 1.0e-13f64 105
219 chk_near add est_betai 4.0f64 6.0f64 0.3f64 est_betai 6.0f64 4.0f64 0.7f64 1.0f64 1.0e-13f64 106
220 chk est_isnan est_betai -1.0f64 2.0f64 0.5f64 107
221 chk est_isnan est_betai 1.0f64 0.0f64 0.5f64 108
222 chk est_isnan est_betai 1.0f64 1.0f64 1.5f64 109
223 chk est_isnan est_betai 1.0f64 1.0f64 est_nan 109
224 test_numerics_t
225 chk and eq est_norm_ppf 0.0f64 -8.0f64 eq est_norm_ppf 1.0f64 8.0f64 120
226 chk and eq est_norm_ppf -1.0f64 -8.0f64 eq est_norm_ppf 2.0f64 8.0f64 121
227 chk le fabs est_norm_ppf 0.5f64 1.0e-15f64 122
228 chk_near est_norm_ppf 0.975f64 1.959963984540054f64 1.0e-12f64 123
229 for ix ulong 43
230 x = add -7.5f64 mul 0.25f64 f64 ix
231 chk_near est_norm_ppf est_norm_cdf x x 1.0e-9f64 124
232 end
233 chk eq est_norm_ppf 1.0e-20f64 -8.0f64 125
234 chk_near est_frac_cover 0.0f64 1.0f64 0.5f64 0.5f64 1.0e-15f64 126
235 chk_near est_frac_cover 0.0f64 1.0f64 0.95f64 0.95f64 1.0e-15f64 127
236 chk eq est_frac_cover 0.3f64 0.6f64 0.5f64 1.0f64 128
237 chk eq est_frac_cover 0.0f64 0.2f64 0.5f64 0.0f64 129
238 chk eq est_frac_cover 0.8f64 1.0f64 0.5f64 0.0f64 130
239 chk_near est_frac_cover 0.2f64 0.3f64 0.5f64 0.5f64 1.0e-15f64 131
240 chk_near est_frac_cover 0.7f64 0.9f64 0.5f64 0.25f64 1.0e-15f64 132
241 chk eq est_frac_cover 0.5f64 0.5f64 0.5f64 1.0f64 133
242 chk eq est_frac_cover 0.25f64 0.25f64 0.5f64 1.0f64 134
243 chk eq est_frac_cover 0.1f64 0.1f64 0.5f64 0.0f64 135
244end
245
246note ---- 2. Assumption & Classify ----
247
248fn test_assumption to void
249 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
250 d0 = new uchar 32 0
251 d1 = new uchar 32 0
252 mk_f1 1.0e4f64 1.0e3f64 1.0e5f64 a
253 chk_st est_assumption_check view a EST_OK 200
254 mk_f2 1.0e4f64 1.0e3f64 1.0e5f64 1.345f64 a
255 chk_st est_assumption_check view a EST_OK 201
256 mk_f3 3.0f64 150.0f64 a
257 chk_st est_assumption_check view a EST_OK 202
258 mk_f4 3.0f64 0.5f64 0.3f64 0.2f64 1.0e3f64 a
259 put a param 5 1.0e4f64
260 put a param 6 1.0e5f64
261 chk_st est_assumption_check view a EST_OK 203
262 mk_f5 0.9f64 3.0f64 1.0f64 833.3f64 28.9f64 a
263 chk_st est_assumption_check view a EST_OK 204
264 mk_f3 3.0f64 150.0f64 a
265 put a nparam 3
266 chk_st est_assumption_check view a EST_ERR_DIM 205
267 put a nparam 2
268 put a param 2 1.0f64
269 chk_st est_assumption_check view a EST_ERR_DIM 206
270 put a param 2 0.0f64
271 put a quantum 0.0f64
272 chk_st est_assumption_check view a EST_ERR_NOT_PD 207
273 put a quantum est_nan
274 chk_st est_assumption_check view a EST_ERR_NONFINITE 208
275 put a quantum QU
276 put a lo 5.0f64
277 put a hi 4.0f64
278 chk_st est_assumption_check view a EST_ERR_ENCODING 209
279 put a lo -1.0e8f64
280 put a hi 1.0e8f64
281 put a family 0
282 chk_st est_assumption_check view a EST_ERR_KIND 210
283 put a family EST_FAM_STUDENT_T
284 put a unit EST_UNIT_NONE
285 chk_st est_assumption_check view a EST_ERR_UNIT 211
286 mk_f3 3.0f64 150.0f64 a
287 put a lo 0.0f64
288 chk_st est_assumption_digest view a d0 EST_OK 212
289 put a lo est_neg_zero
290 chk_st est_assumption_digest view a d1 EST_OK 213
291 chk est_digest_eq view d0 view d1 214
292 put a quantum 50.0f64
293 chk_st est_assumption_digest view a d1 EST_OK 215
294 chk not est_digest_eq view d0 view d1 216
295end
296
297fn test_classify to void
298 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
299 mk_f3 3.0f64 150.0f64 a
300 put a lo 0.0f64
301 put a hi 100000.0f64
302 ev = new uchar 32 0
303 zero = new uchar 32 0
304 mk_evd 1 ev
305 o = new EstPObs f64 0.0 ulong 0 uchar 0
306 chk_st est_pobs_classify view a 500.0f64 1 view zero o EST_ERR_KIND 220
307 chk_st est_pobs_classify view a est_nan 0 view ev o EST_OK 221
308 chk and eq get o cls EST_OBS_MISSING est_isnan get o val 222
309 chk_st est_pobs_classify view a est_nan 1 view ev o EST_OK 223
310 chk eq get o cls EST_OBS_NONFINITE 224
311 chk_st est_pobs_classify view a est_ninf 1 view ev o EST_OK 225
312 chk eq get o cls EST_OBS_NONFINITE 226
313 chk_st est_pobs_classify view a -100.0f64 1 view ev o EST_OK 227
314 chk eq get o cls EST_OBS_OUT_OF_RANGE 228
315 chk_st est_pobs_classify view a 537.0f64 1 view ev o EST_OK 229
316 chk eq get o cls EST_OBS_OFF_GRID 230
317 chk_st est_pobs_classify view a 500.001f64 1 view ev o EST_OK 231
318 chk eq get o cls EST_OBS_OFF_GRID 232
319 chk_st est_pobs_classify view a add 500.0f64 1.0e-8f64 1 view ev o EST_OK 233
320 chk eq get o cls EST_OBS_OK 234
321 chk_st est_pobs_classify view a 100000.0f64 1 view ev o EST_OK 235
322 chk eq get o cls EST_OBS_OK 236
323end
324
325note ---- 3. State machine ----
326
327fn test_state_machine to void
328 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
329 mk_f3 3.0f64 150.0f64 a
330 b = new EstBState ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0 ulong 0 ulong 0 ulong 0 ulong 0 f64 0.0 long 0 long 0
331 cache = new f64 801 0.0
332 p = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
333 p2 = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
334 pmf = new f64 801 0.0
335 pmf2 = new f64 801 0.0
336 iv = new EstPInnov ulong 0 ulong 0 long 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0
337 o = new EstPObs f64 0.0 ulong 0 uchar 0
338 chk_st est_pred_init view a b cache EST_OK 300
339 chk and eq get b generation 0 eq get b has_anchor 0 301
340 chk_st est_pred_predict view a b cache 1 p pmf EST_ERR_STALE 302
341 mk_pob view a est_nan 1 0 o
342 chk_st est_pred_update view a b view o EST_OK 303
343 mk_pob view a 37.0f64 1 1 o
344 chk_st est_pred_update view a b view o EST_OK 304
345 mk_pob view a 1.0e9f64 1 2 o
346 chk_st est_pred_update view a b view o EST_OK 305
347 mk_pob view a 0.0f64 0 3 o
348 chk_st est_pred_update view a b view o EST_OK 306
349 chk eq get b cnt EST_OBS_NONFINITE 1 307
350 chk eq get b cnt EST_OBS_OFF_GRID 1 307
351 chk eq get b cnt EST_OBS_OUT_OF_RANGE 1 307
352 chk eq get b cnt EST_OBS_MISSING 1 307
353 chk_st est_pred_predict view a b cache 1 p pmf EST_ERR_STALE 308
354 mk_pob view a 25000.0f64 1 4 o
355 chk_st est_pred_update view a b view o EST_OK 309
356 chk eq get b has_anchor 1 310
357 chk eq get b anchor 25000.0f64 310
358 chk eq get b gap 0 310
359 put o val est_nan
360 chk_st est_pred_update view a b view o EST_ERR_KIND 311
361 put o val 25037.0f64
362 chk_st est_pred_update view a b view o EST_ERR_KIND 312
363 put o val 25000.0f64
364 put o cls EST_OBS_OFF_GRID
365 chk_st est_pred_update view a b view o EST_ERR_KIND 313
366 put o cls EST_OBS_OK
367 chk_st est_pred_predict view a b cache 1 p pmf EST_OK 314
368 chk pmf_ok view pmf 315
369 mk_pob view a 25100.0f64 1 99 o
370 chk_st est_pred_innov view b view p view pmf view o iv EST_OK 316
371 chk eq get iv valid 1 317
372 chk eq get iv k 1 317
373 chk gt get iv F_lo 0.5f64 317
374 chk_st est_pred_predict view a b cache 1 p2 pmf2 EST_OK 318
375 chk_st est_pred_innov view b view p view pmf view o iv EST_ERR_STALE 319
376 chk_st est_pred_innov view b view p2 view pmf2 view o iv EST_OK 320
377 chk_st est_pred_predict view a b cache 2 p pmf EST_OK 321
378 chk_st est_pred_innov view b view p view pmf view o iv EST_ERR_STALE 322
379 chk_st est_dpred_score view p view pmf view o iv EST_OK 323
380 chk_st est_pred_update view a b view o EST_OK 324
381 chk_st est_pred_predict view a b cache 0 p pmf EST_ERR_TIME 325
382 chk_st est_pred_predict view a b cache 65 p pmf EST_ERR_TIME 326
383 chk_st est_pred_predict view a b cache 64 p pmf EST_OK 327
384 for i ulong 60
385 mk_pob view a 0.0f64 0 add 1000 i o
386 chk_st est_pred_update view a b view o EST_OK 328
387 end
388 chk eq get b gap 60 329
389 chk_st est_pred_predict view a b cache 5 p pmf EST_ERR_TIME 330
390 chk_st est_pred_predict view a b cache 4 p pmf EST_OK 331
391 chk eq get p horizon 64 332
392 for i ulong 10
393 mk_pob view a est_nan 1 add 2000 i o
394 chk_st est_pred_update view a b view o EST_OK 333
395 end
396 chk_st est_pred_predict view a b cache 1 p pmf EST_ERR_TIME 334
397 mk_pob view a 26000.0f64 1 5000 o
398 chk_st est_pred_update view a b view o EST_OK 335
399 chk and eq get b gap 0 eq get b anchor 26000.0f64 336
400 chk_st est_pred_predict view a b cache 1 p pmf EST_OK 337
401end
402
403note ---- 4. PMFs: conv_pow, floor, F1/F3/F4/F5 closed forms, width80 ----
404
405fn naive_conv a view f64 801 b view f64 801 dst edit f64 801 to void
406 for i ulong EST_PRED_N
407 put dst i 0.0f64
408 end
409 for i ulong EST_PRED_N
410 ai = get a i
411 if ne ai 0.0f64
412 for j ulong EST_PRED_N
413 bj = get b j
414 if ne bj 0.0f64
415 t = sub add long i long j EST_PRED_K
416 if lt t 0
417 t = 0
418 end
419 if gt t 800
420 t = 800
421 end
422 ut = ulong t
423 put dst ut add get dst ut mul ai bj
424 end
425 end
426 end
427 end
428end
429
430fn is_conv_h h ulong to bool
431 if eq h 1
432 ret true
433 end
434 if eq h 2
435 ret true
436 end
437 if eq h 3
438 ret true
439 end
440 if eq h 7
441 ret true
442 end
443 ret eq h 16
444end
445
446fn test_pmfs_conv to void
447 r = new ulong 1 0xc0ffee
448 one = new f64 801 0.0
449 outp = new f64 801 0.0
450 refp = new f64 801 0.0
451 tmp = new f64 801 0.0
452 s = f64 0.0
453 for k0 ulong 11
454 k = sub long k0 5
455 w = runif r
456 if eq k 0
457 w = mul w 4.0f64
458 end
459 put one ulong add k EST_PRED_K w
460 s = add s w
461 end
462 for i ulong EST_PRED_N
463 put one i div get one i s
464 put refp i get one i
465 end
466 for h0 ulong 16
467 h = add h0 1
468 if gt h 1
469 naive_conv view refp view one tmp
470 for i ulong EST_PRED_N
471 put refp i get tmp i
472 end
473 end
474 if is_conv_h h
475 chk_st est_pmf_conv_pow view one h outp EST_OK 400
476 md = f64 0.0
477 tot = f64 0.0
478 for i ulong EST_PRED_N
479 d = fabs sub get outp i get refp i
480 if gt d md
481 md = d
482 end
483 tot = add tot get outp i
484 end
485 chk le md 1.0e-14f64 401
486 chk_near tot 1.0f64 1.0e-12f64 402
487 end
488 end
489 for i ulong EST_PRED_N
490 put one i div 1.0f64 801.0f64
491 end
492 chk_st est_pmf_conv_pow view one 64 outp EST_OK 403
493 tot = f64 0.0
494 for i ulong EST_PRED_N
495 tot = add tot get outp i
496 end
497 chk_near tot 1.0f64 1.0e-12f64 404
498 chk gt get outp 0 0.1f64 405
499 chk lt fabs sub get outp 0 get outp 800 1.0e-12f64 405
500 chk_st est_pmf_conv_pow view one 0 outp EST_ERR_TIME 406
501 chk_st est_pmf_conv_pow view one 65 outp EST_ERR_TIME 407
502end
503
504fn pmf_max_diff a view f64 801 b view f64 801 to f64
505 md = f64 0.0
506 for i ulong EST_PRED_N
507 d = fabs sub get a i get b i
508 if gt d md
509 md = d
510 end
511 end
512 ret md
513end
514
515fn chk_t3_closed pmft view f64 801 to void
516 for k0 ulong 9
517 k = sub long k0 4
518 rf = sub est_t_cdf div mul add f64 k 0.5f64 QU 150.0f64 3.0f64 est_t_cdf div mul sub f64 k 0.5f64 QU 150.0f64 3.0f64
519 chk_near get pmft ulong add k EST_PRED_K rf 1.0e-12f64 417
520 end
521end
522
523fn test_pmfs_t_vs_f5 to void
524 t3 = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
525 t5 = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
526 mk_f3 3.0f64 150.0f64 t3
527 mk_f5 1.0f64 3.0f64 1.0f64 833.3f64 150.0f64 t5
528 bt = new EstBState ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0 ulong 0 ulong 0 ulong 0 ulong 0 f64 0.0 long 0 long 0
529 b5 = new EstBState ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0 ulong 0 ulong 0 ulong 0 ulong 0 f64 0.0 long 0 long 0
530 ct = new f64 801 0.0
531 c5 = new f64 801 0.0
532 pt = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
533 p5 = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
534 pmft = new f64 801 0.0
535 pmf5 = new f64 801 0.0
536 o = new EstPObs f64 0.0 ulong 0 uchar 0
537 chk_st est_pred_init view t3 bt ct EST_OK 410
538 chk_st est_pred_init view t5 b5 c5 EST_OK 411
539 mk_pob view t3 30000.0f64 1 1 o
540 chk_st est_pred_update view t3 bt view o EST_OK 412
541 chk_st est_pred_update view t5 b5 view o EST_OK 413
542 chk_st est_pred_predict view t3 bt ct 1 pt pmft EST_OK 414
543 chk_st est_pred_predict view t5 b5 c5 1 p5 pmf5 EST_OK 415
544 chk le pmf_max_diff view pmft view pmf5 1.0e-15f64 416
545 chk_t3_closed view pmft
546end
547
548fn test_pmfs_mix_and_gauss to void
549 bt = new EstBState ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0 ulong 0 ulong 0 ulong 0 ulong 0 f64 0.0 long 0 long 0
550 ct = new f64 801 0.0
551 pt = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
552 pmft = new f64 801 0.0
553 o = new EstPObs f64 0.0 ulong 0 uchar 0
554 m4 = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
555 mk_f4 2.0f64 0.7f64 0.3f64 0.0f64 2500.0f64 m4
556 put m4 param 5 90000.0f64
557 mx = new EstMix ulong 2 f64 0.0 f64 0.0
558 put mx w 0 0.7f64
559 put mx w 1 0.3f64
560 put mx v 0 2500.0f64
561 put mx v 1 90000.0f64
562 mk_pob view m4 30000.0f64 1 1 o
563 chk_st est_pred_init view m4 bt ct EST_OK 420
564 chk_st est_pred_update view m4 bt view o EST_OK 421
565 chk_st est_pred_predict view m4 bt ct 1 pt pmft EST_OK 422
566 chi = new f64 1 0.0
567 clo = new f64 1 0.0
568 for k ulong 7
569 chk_st est_mix_cdf_abs view mx 1 mul add f64 k 0.5f64 QU chi EST_OK 423
570 put clo 0 0.0f64
571 if gt k 0
572 chk_st est_mix_cdf_abs view mx 1 mul sub f64 k 0.5f64 QU clo EST_OK 424
573 end
574 rf = get chi 0
575 if gt k 0
576 rf = mul 0.5f64 sub get chi 0 get clo 0
577 end
578 chk_near get pmft add EST_PRED_KU k rf 1.0e-11f64 425
579 chk_near get pmft sub EST_PRED_KU k rf 1.0e-11f64 426
580 end
581 mk_f1 1.0e6f64 0.0f64 0.0f64 m4
582 chk_st est_pred_init view m4 bt ct EST_OK 430
583 chk_st est_pred_update view m4 bt view o EST_OK 431
584 chk_st est_pred_predict view m4 bt ct 1 pt pmft EST_OK 432
585 chk_st est_dpred_width80 view pt view pmft chi EST_OK 433
586 chk le fabs sub get chi 0 mul 2.0f64 mul 1.2816f64 1000.0f64 mul 2.0f64 QU 434
587 iv = new EstPInnov ulong 0 ulong 0 long 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0
588 mk_pob view m4 add 30000.0f64 1.0e6f64 1 3 o
589 chk_st est_dpred_score view pt view pmft view o iv EST_OK 435
590 chk eq get iv valid 1 436
591 chk eq get iv k EST_PRED_K 436
592 chk eq get iv F_hi 1.0f64 436
593 mk_pob view m4 sub 30000.0f64 1.0e6f64 1 4 o
594 chk_st est_dpred_score view pt view pmft view o iv EST_OK 437
595 chk eq get iv valid 1 438
596 chk eq get iv k neg EST_PRED_K 438
597 chk eq get iv F_lo 0.0f64 438
598end
599
600fn test_pmfs_families to void
601 test_pmfs_t_vs_f5
602 test_pmfs_mix_and_gauss
603end
604
605note ---- 5. Verdict order ----
606
607fn test_verdict_order to void
608 bd = new EstBands f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0
609 est_bands_default bd
610 chk and eq get bd cov50 0 0.46f64 eq get bd min_n 2000 500
611 c = new EstCalib 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
612 want_rc = new ulong 9 0
613 put want_rc 0 1
614 put want_rc 1 2
615 put want_rc 2 3
616 put want_rc 3 4
617 put want_rc 4 10
618 put want_rc 5 17
619 put want_rc 6 20
620 put want_rc 7 21
621 put want_rc 8 0
622 rc = new ulong 1 0
623 for stage ulong 9
624 est_calib_init c
625 nf = f64 3000.0
626 if eq stage 0
627 put c n 1999
628 else
629 put c n 3000
630 end
631 if eq stage 1
632 put c cov 0 mul 0.60f64 nf
633 else
634 put c cov 0 mul 0.50f64 nf
635 end
636 if le stage 2
637 put c cov 1 mul 0.70f64 nf
638 else
639 put c cov 1 mul 0.80f64 nf
640 end
641 if le stage 3
642 put c cov 2 mul 0.99f64 nf
643 else
644 put c cov 2 mul 0.95f64 nf
645 end
646 for j ulong 10
647 put c pit j mul 0.1f64 nf
648 end
649 if le stage 4
650 put c pit 0 mul 0.05f64 nf
651 put c pit 7 mul 0.15f64 nf
652 end
653 if eq stage 5
654 put c pit 7 mul 0.14f64 nf
655 put c pit 8 mul 0.06f64 nf
656 end
657 if le stage 6
658 put c z_sum mul 0.2f64 nf
659 end
660 put c z_sq nf
661 if le stage 7
662 put c z_lag mul 0.5f64 sub nf 1.0f64
663 end
664 v = est_calib_verdict view c view bd rc
665 chk eq get rc 0 get want_rc stage 501
666 if eq stage 8
667 chk eq v EST_CALIBRATED 502
668 else
669 chk eq v EST_NOT_CALIBRATED 503
670 end
671 end
672end
673
674note ---- 6. Calibration scenarios ----
675
676fn stream_step a view EstAssump b edit EstBState cache edit f64 801 c1 edit EstCalib t ulong to void
677 p1 = new EstDPred uchar 0 ulong 0 ulong 0 ulong 0 f64 0.0 f64 0.0
678 pmf1 = new f64 801 0.0
679 o = new EstPObs f64 0.0 ulong 0 uchar 0
680 iv = new EstPInnov ulong 0 ulong 0 long 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0
681 have1 = false
682 if eq get b has_anchor 1
683 st = est_pred_predict a b cache 1 p1 pmf1
684 if eq st EST_OK
685 have1 = true
686 end
687 end
688 mk_pob a get g_y t get g_pres t t o
689 if have1
690 st = est_pred_innov view b view p1 view pmf1 view o iv
691 if eq st EST_OK
692 if eq get iv valid 1
693 if ge t get g_lohi 0
694 if lt t get g_lohi 1
695 u = est_calib_add c1 view p1 view pmf1 view iv
696 end
697 end
698 end
699 end
700 end
701 u = est_pred_update a b view o
702 put g_xh t get b xhat
703end
704
705fn run_stream a view EstAssump n ulong lo ulong hi ulong c1 edit EstCalib to void
706 est_calib_init c1
707 put g_lohi 0 lo
708 put g_lohi 1 hi
709 b = new EstBState ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0 ulong 0 ulong 0 ulong 0 ulong 0 f64 0.0 long 0 long 0
710 cache = new f64 801 0.0
711 chk_st est_pred_init a b cache EST_OK 600
712 for t ulong n bound 10005
713 stream_step a b cache c1 t
714 end
715end
716
717fn chk_verdict c view EstCalib want ulong tag ulong to void
718 bd = new EstBands f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0
719 est_bands_default bd
720 rc = new ulong 1 0
721 v = est_calib_verdict c view bd rc
722 chk eq v want tag
723end
724
725fn scen_gauss_and_t3 to void
726 r = new ulong 1 101
727 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
728 c1 = new EstCalib 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
729 y = f64 30000.0
730 for t ulong 4000
731 if gt t 0
732 y = add y on_grid mul 300.0f64 rnorm r
733 end
734 put g_y t y
735 put g_pres t 1
736 end
737 mk_f1 9.0e4f64 0.0f64 0.0f64 a
738 run_stream view a 4000 0 4000 c1
739 chk_verdict view c1 EST_CALIBRATED 610
740 mk_f1 3.6e5f64 0.0f64 0.0f64 a
741 run_stream view a 4000 0 4000 c1
742 chk_verdict view c1 EST_NOT_CALIBRATED 611
743 mk_f1 2.25e4f64 0.0f64 0.0f64 a
744 run_stream view a 4000 0 4000 c1
745 chk_verdict view c1 EST_NOT_CALIBRATED 612
746
747 put r 0 103
748 y = 30000.0f64
749 for t ulong 4000
750 if gt t 0
751 y = add y on_grid mul 150.0f64 rt r 3
752 end
753 put g_y t y
754 put g_pres t 1
755 end
756 mk_f3 3.0f64 150.0f64 a
757 run_stream view a 4000 0 4000 c1
758 chk_verdict view c1 EST_CALIBRATED 620
759 lp_true = get c1 logp_sum
760 mk_f3 3.0f64 37.5f64 a
761 run_stream view a 4000 0 4000 c1
762 chk_verdict view c1 EST_NOT_CALIBRATED 621
763 chk lt get c1 logp_sum lp_true 622
764end
765
766fn max_err_50 to f64
767 em = f64 0.0
768 for t0 ulong 3950
769 t = add t0 50
770 d = fabs sub get g_xh t get g_lvl t
771 if gt d em
772 em = d
773 end
774 end
775 ret em
776end
777
778fn scen_spikes_drift_ar to void
779 r = new ulong 1 104
780 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
781 c1 = new EstCalib 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
782 y = f64 30000.0
783 spikes = ulong 0
784 for t ulong 4000
785 if gt t 0
786 y = add y on_grid mul 200.0f64 rnorm r
787 end
788 put g_lvl t y
789 put g_y t y
790 put g_pres t 1
791 if ge t 50
792 if lt runif r 0.005f64
793 put g_y t add y 20000.0f64
794 spikes = add spikes 1
795 end
796 end
797 end
798 chk gt spikes 5 630
799 mk_f1 4.0e4f64 2500.0f64 1.0e6f64 a
800 run_stream view a 4000 0 4000 c1
801 e1 = max_err_50
802 mk_f2 4.0e4f64 2500.0f64 1.0e6f64 2.0f64 a
803 run_stream view a 4000 0 4000 c1
804 e2 = max_err_50
805 chk gt e1 15000.0f64 631
806 chk lt e2 1500.0f64 631
807
808 put r 0 105
809 y = 30000.0f64
810 for t ulong 4000
811 if gt t 0
812 y = add y on_grid add mul 150.0f64 rnorm r 60.0f64
813 end
814 put g_y t y
815 put g_pres t 1
816 end
817 mk_f1 22500.0f64 0.0f64 0.0f64 a
818 run_stream view a 4000 0 4000 c1
819 chk_verdict view c1 EST_NOT_CALIBRATED 640
820
821 put r 0 109
822 put g_ar 0 0.0f64
823 y = 30000.0f64
824 for t ulong 6000
825 if gt t 0
826 ar = add mul 0.6f64 get g_ar 0 mul 400.0f64 rnorm r
827 put g_ar 0 ar
828 y = add y on_grid ar
829 end
830 put g_y t y
831 put g_pres t 1
832 end
833 mk_f1 250000.0f64 0.0f64 0.0f64 a
834 run_stream view a 6000 0 6000 c1
835 bd = new EstBands f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 f64 0.0 ulong 0
836 est_bands_default bd
837 rc = new ulong 1 0
838 v = est_calib_verdict view c1 view bd rc
839 chk and eq v EST_NOT_CALIBRATED eq get rc 0 21 650
840end
841
842fn scen_stable_and_jump to void
843 r = new ulong 1 110
844 a = new EstAssump ulong 0 ulong 0 f64 0.0 f64 0.0 f64 0.0 ulong 0 f64 0.0
845 c1 = new EstCalib 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
846 y = f64 30000.0
847 for t ulong 4000
848 if gt t 0
849 y = add y on_grid mul 20.0f64 rt r 3
850 end
851 put g_y t y
852 put g_pres t 1
853 end
854 mk_f3 3.0f64 20.0f64 a
855 run_stream view a 4000 0 4000 c1
856 chk_verdict view c1 EST_CALIBRATED 660
857 mk_f3 3.0f64 2.0f64 a
858 run_stream view a 4000 0 4000 c1
859 chk_verdict view c1 EST_NOT_CALIBRATED 661
860
861 put r 0 112
862 for t ulong 6000
863 lv = f64 25030.0
864 if ge t 3000
865 lv = add lv 5000.0f64
866 end
867 put g_y t on_grid add lv mul 300.0f64 rnorm r
868 put g_pres t 1
869 end
870 mk_f1 1.0f64 add 9.0e4f64 div mul QU QU 12.0f64 1.0e6f64 a
871 run_stream view a 6000 0 3000 c1
872 chk_verdict view c1 EST_CALIBRATED 670
873 run_stream view a 6000 0 6000 c1
874 chk_verdict view c1 EST_NOT_CALIBRATED 671
875end
876
877fn main to int
878 test_numerics
879 test_assumption
880 test_classify
881 test_state_machine
882 test_pmfs_conv
883 test_pmfs_families
884 test_verdict_order
885 scen_gauss_and_t3
886 scen_spikes_drift_ar
887 scen_stable_and_jump
888 est_puts P_PREFIX
889 est_putu get g_checks 0
890 est_puts P_CHECKS
891 est_putu get g_fail 0
892 est_puts P_FAILED
893 if gt get g_fail 0 0
894 est_puts P_FAIL
895 ret 1
896 end
897 est_puts P_PASS
898 ret 0
899end