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.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note EST-3b Gaussian scale mixture in pure seeded OM-2 (est_mix.h / est_mix.c). |
| 3 | |
| 4 | const EST_LOG_2PI f64 1.8378770664093453 |
| 5 | |
| 6 | fn est_mix_check mx view EstMix to long |
| 7 | k = get mx k |
| 8 | if or lt k 1 gt k EST_MIX_MAX |
| 9 | ret EST_ERR_DIM |
| 10 | end |
| 11 | s = f64 0.0f64 |
| 12 | for j ulong k |
| 13 | wf = get mx w j |
| 14 | vf = get mx v j |
| 15 | if or not est_isfinite wf not est_isfinite vf |
| 16 | ret EST_ERR_NONFINITE |
| 17 | end |
| 18 | if lt wf 0.0f64 |
| 19 | ret EST_ERR_NOT_PSD |
| 20 | end |
| 21 | if not gt vf 0.0f64 |
| 22 | ret EST_ERR_NOT_PD |
| 23 | end |
| 24 | s = add s wf |
| 25 | end |
| 26 | if le fabs sub s 1.0f64 1.0e-12f64 |
| 27 | ret EST_OK |
| 28 | end |
| 29 | ret EST_ERR_ENCODING |
| 30 | end |
| 31 | |
| 32 | fn est_mix_total_var mx view EstMix to f64 |
| 33 | k = get mx k |
| 34 | s = f64 0.0f64 |
| 35 | for j ulong k |
| 36 | s = add s mul get mx w j get mx v j |
| 37 | end |
| 38 | ret s |
| 39 | end |
| 40 | |
| 41 | fn est_mix_term_eval mx view EstMix a view ulong 3 lh f64 res edit f64 2 to bool |
| 42 | k = get mx k |
| 43 | lw = lh |
| 44 | var = f64 0.0f64 |
| 45 | for j ulong k |
| 46 | aj = get a j |
| 47 | if gt aj 0 |
| 48 | wj = get mx w j |
| 49 | if le wj 0.0f64 |
| 50 | ret false |
| 51 | end |
| 52 | ajf = f64 aj |
| 53 | lg = est_lgamma add ajf 1.0f64 |
| 54 | lw = add lw sub mul ajf est_log wj lg |
| 55 | var = add var mul ajf get mx v j |
| 56 | end |
| 57 | end |
| 58 | put res 0 lw |
| 59 | put res 1 var |
| 60 | ret true |
| 61 | end |
| 62 | |
| 63 | fn est_mix_cdf_contrib tv view f64 2 t f64 to f64 |
| 64 | w = est_exp get tv 0 |
| 65 | sd = fsqrt mul 2.0f64 get tv 1 |
| 66 | ret mul w est_erf div t sd |
| 67 | end |
| 68 | |
| 69 | fn est_mix_lp_contrib tv view f64 2 e f64 to f64 |
| 70 | v = get tv 1 |
| 71 | q = add add EST_LOG_2PI est_log v div mul e e v |
| 72 | ret sub get tv 0 mul 0.5f64 q |
| 73 | end |
| 74 | |
| 75 | fn est_mix_cdf_k3 mx view EstMix h ulong t f64 lh f64 to f64 |
| 76 | acc = f64 0.0 |
| 77 | a = new ulong 3 0 |
| 78 | tv = new f64 2 0.0 |
| 79 | for a0 ulong add h 1 bound 66 |
| 80 | rem1 = sub h a0 |
| 81 | for a1 ulong add rem1 1 bound 66 |
| 82 | put a 0 a0 |
| 83 | put a 1 a1 |
| 84 | put a 2 sub rem1 a1 |
| 85 | if est_mix_term_eval mx view a lh tv |
| 86 | acc = add acc est_mix_cdf_contrib view tv t |
| 87 | end |
| 88 | end |
| 89 | end |
| 90 | ret acc |
| 91 | end |
| 92 | |
| 93 | fn est_mix_cdf_abs mx view EstMix h ulong t f64 res edit f64 1 to long |
| 94 | st = est_mix_check mx |
| 95 | if ne st EST_OK |
| 96 | ret st |
| 97 | end |
| 98 | if or lt h 1 gt h EST_MIX_MAX_H |
| 99 | ret EST_ERR_TIME |
| 100 | end |
| 101 | if not est_isfinite t |
| 102 | ret EST_ERR_NONFINITE |
| 103 | end |
| 104 | if le t 0.0f64 |
| 105 | put res 0 0.0f64 |
| 106 | ret EST_OK |
| 107 | end |
| 108 | k = get mx k |
| 109 | lh = est_lgamma add f64 h 1.0f64 |
| 110 | acc = f64 0.0 |
| 111 | a = new ulong 3 0 |
| 112 | tv = new f64 2 0.0 |
| 113 | if eq k 1 |
| 114 | put a 0 h |
| 115 | if est_mix_term_eval mx view a lh tv |
| 116 | acc = est_mix_cdf_contrib view tv t |
| 117 | end |
| 118 | else |
| 119 | if eq k 2 |
| 120 | for a0 ulong add h 1 bound 66 |
| 121 | put a 0 a0 |
| 122 | put a 1 sub h a0 |
| 123 | if est_mix_term_eval mx view a lh tv |
| 124 | acc = add acc est_mix_cdf_contrib view tv t |
| 125 | end |
| 126 | end |
| 127 | else |
| 128 | acc = est_mix_cdf_k3 mx h t lh |
| 129 | end |
| 130 | end |
| 131 | if gt acc 1.0f64 |
| 132 | acc = 1.0f64 |
| 133 | end |
| 134 | put res 0 acc |
| 135 | ret EST_OK |
| 136 | end |
| 137 | |
| 138 | fn est_mix_quantile_abs mx view EstMix h ulong p f64 res edit f64 1 to long |
| 139 | st = est_mix_check mx |
| 140 | if ne st EST_OK |
| 141 | ret st |
| 142 | end |
| 143 | if or lt h 1 gt h EST_MIX_MAX_H |
| 144 | ret EST_ERR_TIME |
| 145 | end |
| 146 | if not and gt p 0.0f64 lt p 1.0f64 |
| 147 | ret EST_ERR_DIM |
| 148 | end |
| 149 | vmax = f64 0.0f64 |
| 150 | for j ulong get mx k |
| 151 | if gt get mx v j vmax |
| 152 | vmax = get mx v j |
| 153 | end |
| 154 | end |
| 155 | lo = f64 0.0f64 |
| 156 | hi = mul 64.0f64 fsqrt mul f64 h vmax |
| 157 | c = new f64 1 0.0f64 |
| 158 | for i ulong 200 |
| 159 | mid = mul 0.5f64 add lo hi |
| 160 | st = est_mix_cdf_abs mx h mid c |
| 161 | if lt get c 0 p |
| 162 | lo = mid |
| 163 | else |
| 164 | hi = mid |
| 165 | end |
| 166 | end |
| 167 | put res 0 hi |
| 168 | ret EST_OK |
| 169 | end |
| 170 | |
| 171 | fn est_mix_lp_k3_max mx view EstMix h ulong a0 ulong e f64 to f64 |
| 172 | lh = est_lgamma add f64 h 1.0f64 |
| 173 | rem1 = sub h a0 |
| 174 | outv = est_ninf |
| 175 | a = new ulong 3 0 |
| 176 | tv = new f64 2 0.0 |
| 177 | put a 0 a0 |
| 178 | for a1 ulong add rem1 1 bound 66 |
| 179 | put a 1 a1 |
| 180 | put a 2 sub rem1 a1 |
| 181 | if est_mix_term_eval mx view a lh tv |
| 182 | l = est_mix_lp_contrib view tv e |
| 183 | if gt l outv |
| 184 | outv = l |
| 185 | end |
| 186 | end |
| 187 | end |
| 188 | ret outv |
| 189 | end |
| 190 | |
| 191 | fn est_mix_lp_k3_sum mx view EstMix h ulong a0 ulong e f64 mxl f64 to f64 |
| 192 | lh = est_lgamma add f64 h 1.0f64 |
| 193 | rem1 = sub h a0 |
| 194 | outv = f64 0.0 |
| 195 | a = new ulong 3 0 |
| 196 | tv = new f64 2 0.0 |
| 197 | put a 0 a0 |
| 198 | for a1 ulong add rem1 1 bound 66 |
| 199 | put a 1 a1 |
| 200 | put a 2 sub rem1 a1 |
| 201 | if est_mix_term_eval mx view a lh tv |
| 202 | l = est_mix_lp_contrib view tv e |
| 203 | outv = add outv est_exp sub l mxl |
| 204 | end |
| 205 | end |
| 206 | ret outv |
| 207 | end |
| 208 | |
| 209 | fn est_mix_lp_k3 mx view EstMix h ulong e f64 pass ulong mxl f64 to f64 |
| 210 | outv = mxl |
| 211 | if eq pass 1 |
| 212 | outv = 0.0f64 |
| 213 | end |
| 214 | for a0 ulong add h 1 bound 66 |
| 215 | if eq pass 0 |
| 216 | r = est_mix_lp_k3_max mx h a0 e |
| 217 | if gt r outv |
| 218 | outv = r |
| 219 | end |
| 220 | else |
| 221 | outv = add outv est_mix_lp_k3_sum mx h a0 e mxl |
| 222 | end |
| 223 | end |
| 224 | ret outv |
| 225 | end |
| 226 | |
| 227 | fn est_mix_lp_pass mx view EstMix h ulong e f64 pass ulong mxl f64 to f64 |
| 228 | k = get mx k |
| 229 | if eq k 3 |
| 230 | ret est_mix_lp_k3 mx h e pass mxl |
| 231 | end |
| 232 | lh = est_lgamma add f64 h 1.0f64 |
| 233 | outv = mxl |
| 234 | if eq pass 1 |
| 235 | outv = 0.0f64 |
| 236 | end |
| 237 | a = new ulong 3 0 |
| 238 | tv = new f64 2 0.0 |
| 239 | if eq k 1 |
| 240 | put a 0 h |
| 241 | if est_mix_term_eval mx view a lh tv |
| 242 | l = est_mix_lp_contrib view tv e |
| 243 | if eq pass 0 |
| 244 | if gt l outv |
| 245 | outv = l |
| 246 | end |
| 247 | else |
| 248 | outv = add outv est_exp sub l mxl |
| 249 | end |
| 250 | end |
| 251 | else |
| 252 | for a0 ulong add h 1 bound 66 |
| 253 | put a 0 a0 |
| 254 | put a 1 sub h a0 |
| 255 | if est_mix_term_eval mx view a lh tv |
| 256 | l = est_mix_lp_contrib view tv e |
| 257 | if eq pass 0 |
| 258 | if gt l outv |
| 259 | outv = l |
| 260 | end |
| 261 | else |
| 262 | outv = add outv est_exp sub l mxl |
| 263 | end |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | ret outv |
| 268 | end |
| 269 | |
| 270 | fn est_mix_logpdf mx view EstMix h ulong e f64 res edit f64 1 to long |
| 271 | st = est_mix_check mx |
| 272 | if ne st EST_OK |
| 273 | ret st |
| 274 | end |
| 275 | if or lt h 1 gt h EST_MIX_MAX_H |
| 276 | ret EST_ERR_TIME |
| 277 | end |
| 278 | if not est_isfinite e |
| 279 | ret EST_ERR_NONFINITE |
| 280 | end |
| 281 | mxl = est_mix_lp_pass mx h e 0 est_ninf |
| 282 | acc = est_mix_lp_pass mx h e 1 mxl |
| 283 | put res 0 add mxl est_log acc |
| 284 | ret EST_OK |
| 285 | end |