-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
1713 lines (1274 loc) · 48.6 KB
/
utils.py
File metadata and controls
1713 lines (1274 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Common utils for probability density processings
@author: antonio vergari
"""
import numpy as np
import scipy.stats
from scipy.special import logsumexp
import numba
from numba import jit, uint8, int64, float64, optional, boolean
from numba.types import Tuple
def is_missing_value(x):
"""
TODO: use this function in the future to determine if a value has to be treated as missing
"""
return np.isnan(x)
@numba.jit(nopython=True)
def numba_gaussian_pdf(x, scale):
"""
Pdf of a gaussian with zero mean and std sigma to be wrapped in numba for efficiency
"""
u = x / np.abs(scale)
p = (1 / (np.sqrt(2 * np.pi) * np.abs(scale))) * np.exp(-u * u / 2)
return p
@numba.jit(nopython=True)
def fre_1(x, w, mu):
"""
Scaling and centering inverse function for real data pseudo transformations
WRITEME: args
TODO: refactor names
"""
return w * (x - mu)
@numba.jit(nopython=True)
def fre(y, w, mu):
"""
Scaling and centering function for real data pseudo transformations
WRITEME: args
TODO: refactor names
"""
return w * y + mu
@numba.jit(nopython=True)
def f_1(x, w):
"""
Inverse transformation for positive real data
WRITEME: args
TODO: refactor names
"""
return np.log(np.exp(w * x) - 1)
# return w * np.log(np.exp(x) - 1)
@numba.jit(nopython=True)
def f_pos(x, w):
"""
Transformation for positive real data
WRITEME: args
TODO: refactor names
"""
# return np.log(np.exp(w * x) + 1)
return w * np.log(np.exp(x) + 1)
@numba.jit(nopython=True)
def f_int(x, w, theta_L, theta_H):
"""
Transformation for interval real data
WRITEME: args
TODO: refactor names
"""
return (theta_H - theta_L) / (1 + np.exp(-w * x)) + theta_L
@numba.jit(nopython=True)
def fint_1(x, w, theta_L, theta_H):
"""
Inverse transformation for interval real data
WRITEME: args
TODO: refactor names
"""
return -1 / w * np.log((theta_H - x) / (x - theta_L))
@numba.jit()
def f_cat(x, Rd):
"""
Transformation for categorical data
x is assumed to have shape Rxn (also Rx1)
Rd: uint
cardinality of values for attribute
"""
# print('FCAT', x.shape)
# print(x[:Rd])
return np.argmax(x[:Rd, :], axis=0) + 1
@numba.jit(nopython=True)
def f_ord(x, thetas, Rd):
"""
Transformation for ordinal data
x is assumed to have shape n
thetas intervals (length maxRd)
Rd: uint
cardinality of values for attribute
"""
# print(thetas[:Rd - 1])
return np.digitize(x, thetas[:Rd - 1]) + 1
@numba.jit(nopython=True)
def f_count(x, w):
"""
Transformation for count data
x is assumed to have shape n
thetas intervals (length maxRd)
Rd: uint
cardinality of values for attribute
"""
return np.floor(f_pos(x, w)) + 1
@numba.jit(nopython=True)
def dfre_1(x, w):
"""
Derivative of the inverse transformations df^-1(x)/dx
WRITEME: args
TODO: refactor names
"""
return w
@numba.jit(nopython=True)
def df_1(x, w):
return w / (1 - np.exp(-w * x))
@numba.jit(nopython=True)
def dfint_1(x, w, theta_L, theta_H):
return 1 / w * (theta_H - theta_L) / ((theta_H - x) * (x - theta_L))
@numba.jit(nopython=True)
def inverse(A):
"""
Invert data matrix
Question:
"""
return np.linalg.inv(A)
@numba.jit(nopython=True)
def xpdf_re(x, w, muX, mu, s2Y, s2u):
# return np.log(stats.norm.pdf(x=fre_1(x, w, muX) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(np.abs(dfre_1(x, w)))
return np.log(numba_gaussian_pdf(x=fre_1(x, w, muX) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(np.abs(dfre_1(x, w)))
EPS_ZERO = 1e-7
@numba.jit(nopython=True)
# @numba.jit(float64(float64, float64, float64, float64, float64), nopython=False)
def xpdf_pos(x, w, mu, s2Y, s2u):
# # return np.log(stats.norm.pdf(x=f_1(x, w) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(abs(df_1(x, w)))
# gpd = numba_gaussian_pdf(x=f_1(x, w) - mu, scale=np.sqrt(s2Y + s2u))
# # if np.isclose(gpd, 0):
# gpd += EPS_ZERO
return np.log(numba_gaussian_pdf(x=f_1(x, w) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(abs(df_1(x, w)))
@numba.jit(nopython=True)
def xpdf_int(x, w, theta_L, theta_H, mu, s2Y, s2u):
# return np.log(stats.norm.pdf(x=fint_1(x, w, theta_L, theta_H) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(abs(dfint_1(x, w, theta_L, theta_H)))
return np.log(numba_gaussian_pdf(x=fint_1(x, w, theta_L, theta_H) - mu, scale=np.sqrt(s2Y + s2u))) + np.log(abs(dfint_1(x, w, theta_L, theta_H)))
@numba.jit(nopython=True)
def WNpdf(x, theta, mu, sigma, n):
kopt = int(mu / theta)
px = 0.0
for i in range(kopt - n, kopt + n + 1):
# px += stats.norm.pdf(x=x - mu + i * theta, scale=sigma)
px += numba_gaussian_pdf(x=x - mu + i * theta, scale=sigma)
return px
@numba.jit(nopython=True)
def xpdf_dir(x, theta, mu, s2Y, s2u, n=100):
return np.log(WNpdf(x, theta, mu, np.sqrt(s2Y + s2u), n))
@numba.jit
def mvnrnd(Mu, Sigma, rand_gen):
"""
Sample from a multivariate gaussian with mean Mu (size d) and covariance matrix Sigma (size dxd)
Using numpy's random utils
"""
return rand_gen.multivariate_normal(Mu, Sigma)
@numba.jit
def mnrnd(p, rand_gen):
"""
Sample from a categorical distribution
"""
n_vals = len(p)
return rand_gen.choice(n_vals, replace=False, p=p)
"""
Literal porting of gsl functions to operate on the the CDF of a gaussian for efficiently compiling them with numba
see https://github.com/ampl/gsl/blob/master/cdf/gauss.c
"""
@numba.jit
def truncnormrnd_scipy(mu, sigma, xlo, xhi, rand_gen, n_samples=1):
"""
Sample from a truncated normal with mean mu, variance sigma and truncated at [xlo, xhi]
"""
X = scipy.stats.truncnorm((xlo - mu) / sigma, (xhi - mu) / sigma, loc=mu, scale=sigma)
return X.rvs(size=n_samples)
@numba.jit(nopython=True)
def rat_eval(a, na, b, nb, x):
"""
"""
u = a[na - 1]
i = na - 1
while i > 0:
u = x * u + a[i - 1]
i -= 1
v = b[nb - 1]
j = nb - 1
while j > 0:
v = x * v + b[j - 1]
j -= 1
r = u / v
return r
A_SMALL = np.array([3.387132872796366608, 133.14166789178437745,
1971.5909503065514427, 13731.693765509461125,
45921.953931549871457, 67265.770927008700853,
33430.575583588128105, 2509.0809287301226727])
B_SMALL = np.array([1.0, 42.313330701600911252,
687.1870074920579083, 5394.1960214247511077,
21213.794301586595867, 39307.89580009271061,
28729.085735721942674, 5226.495278852854561])
@numba.jit(nopython=True)
def small(q):
"""
"""
r = 0.180625 - q * q
x = q * rat_eval(A_SMALL, 8, B_SMALL, 8, r)
return x
A_INTERMEDIATE = np.array([1.42343711074968357734, 4.6303378461565452959,
5.7694972214606914055, 3.64784832476320460504,
1.27045825245236838258, 0.24178072517745061177,
0.0227238449892691845833, 7.7454501427834140764e-4])
B_INTERMEDIATE = np.array([1.0, 2.05319162663775882187,
1.6763848301838038494, 0.68976733498510000455,
0.14810397642748007459, 0.0151986665636164571966,
5.475938084995344946e-4, 1.05075007164441684324e-9])
@numba.jit(nopython=True)
def intermediate(r):
"""
"""
x = rat_eval(A_INTERMEDIATE, 8, B_INTERMEDIATE, 8, (r - 1.6))
return x
A_TAIL = np.array([6.6579046435011037772, 5.4637849111641143699,
1.7848265399172913358, 0.29656057182850489123,
0.026532189526576123093, 0.0012426609473880784386,
2.71155556874348757815e-5, 2.01033439929228813265e-7])
B_TAIL = np.array([1.0, 0.59983220655588793769,
0.13692988092273580531, 0.0148753612908506148525,
7.868691311456132591e-4, 1.8463183175100546818e-5,
1.4215117583164458887e-7, 2.04426310338993978564e-15])
@numba.jit(nopython=True)
def tail(r):
x = rat_eval(A_TAIL, 8, B_TAIL, 8, (r - 5.0))
return x
@numba.jit(nopython=True)
def gsl_cdf_ugaussian_Pinv(P):
dP = P - 0.5
if P == 1.0:
return np.inf
elif P == 0.0:
return -np.inf
if np.abs(dP) <= 0.425:
x = small(dP)
return x
pp = P if P < 0.5 else 1.0 - P
r = np.sqrt(-np.log(pp))
if r <= 5.0:
x = intermediate(r)
else:
x = tail(r)
if (P < 0.5):
return -x
else:
return x
M_2_SQRTPI = 1.12837916709551257389615890312 # 2/sqrt(pi)
# M_SQRT1_2 = 1.77245385090551602729816748334 # sqrt(pi)
M_SQRT1_2 = 0.70710678118654752440084436210 # sqrt(1/2)
M_1_SQRT2PI = (M_2_SQRTPI * M_SQRT1_2 / 2.0)
M_SQRT2 = 1.41421356237309504880168872421 # sqrt(2)
SQRT32 = 4.0 * M_SQRT2
"""
* IEEE double precision dependent constants.
*
* GAUSS_EPSILON: Smallest positive value such that
* gsl_cdf_gaussian(x) > 0.5.
* GAUSS_XUPPER: Largest value x such that gsl_cdf_gaussian(x) < 1.0.
* GAUSS_XLOWER: Smallest value x such that gsl_cdf_gaussian(x) > 0.0.
*/
"""
GSL_DBL_EPSILON = 2.2204460492503131e-16
GAUSS_EPSILON = GSL_DBL_EPSILON / 2
GAUSS_XUPPER = 8.572
GAUSS_XLOWER = -37.519
GAUSS_SCALE = 16.0
@numba.jit(nopython=True)
def get_del(x, rational):
xsq = np.floor(x * GAUSS_SCALE) / GAUSS_SCALE
_del = (x - xsq) * (x + xsq)
_del *= 0.5
result = np.exp(-0.5 * xsq * xsq) * np.exp(-1.0 * _del) * rational
# if np.isinf(result) or np.isnan(result):
# print('geddel res {} x {} rat {} del {}', result, x, rational, _del)
return result
A_G_SMALL = np.array([2.2352520354606839287,
161.02823106855587881,
1067.6894854603709582,
18154.981253343561249,
0.065682337918207449113
])
B_G_SMALL = np.array([47.20258190468824187,
976.09855173777669322,
10260.932208618978205,
45507.789335026729956
])
@numba.jit(nopython=True)
def gauss_small(x):
"""
Normal cdf for fabs(x) < 0.66291
"""
xsq = x * x
xnum = A_G_SMALL[4] * xsq
xden = xsq
for i in range(3):
xnum = (xnum + A_G_SMALL[i]) * xsq
xden = (xden + B_G_SMALL[i]) * xsq
result = x * (xnum + A_G_SMALL[3]) / (xden + B_G_SMALL[3])
# if np.isinf(result) or np.isnan(result):
# print('gauss-small res {} x {}', result, x)
return result
C_G_MEDIUM = np.array([0.39894151208813466764,
8.8831497943883759412,
93.506656132177855979,
597.27027639480026226,
2494.5375852903726711,
6848.1904505362823326,
11602.651437647350124,
9842.7148383839780218,
1.0765576773720192317e-8
])
D_G_MEDIUM = np.array([22.266688044328115691,
235.38790178262499861,
1519.377599407554805,
6485.558298266760755,
18615.571640885098091,
34900.952721145977266,
38912.003286093271411,
19685.429676859990727
])
@numba.jit(nopython=True)
def gauss_medium(x):
"""
Normal cdf for 0.66291 < fabs(x) < sqrt(32).
"""
absx = np.abs(x)
xnum = C_G_MEDIUM[8] * absx
xden = absx
for i in range(7):
xnum = (xnum + C_G_MEDIUM[i]) * absx
xden = (xden + D_G_MEDIUM[i]) * absx
temp = (xnum + C_G_MEDIUM[7]) / (xden + D_G_MEDIUM[7])
result = get_del(x, temp)
# if np.isinf(result) or np.isnan(result):
# print('gauss-medium res {} x {}', result, x)
return result
P_G_LARGE = np.array([0.21589853405795699,
0.1274011611602473639,
0.022235277870649807,
0.001421619193227893466,
2.9112874951168792e-5,
0.02307344176494017303
])
Q_G_LARGE = np.array([1.28426009614491121,
0.468238212480865118,
0.0659881378689285515,
0.00378239633202758244,
7.29751555083966205e-5
])
@numba.jit(nopython=True)
def gauss_large(x):
"""
Normal cdf for
* {sqrt(32) < x < GAUSS_XUPPER} union {GAUSS_XLOWER < x < -sqrt(32)}.
"""
absx = np.abs(x)
xsq = 1.0 / (x * x)
xnum = P_G_LARGE[5] * xsq
xden = xsq
for i in range(4):
xnum = (xnum + P_G_LARGE[i]) * xsq
xden = (xden + Q_G_LARGE[i]) * xsq
temp = xsq * (xnum + P_G_LARGE[4]) / (xden + Q_G_LARGE[4])
temp = (M_1_SQRT2PI - temp) / absx
result = get_del(x, temp)
# if np.isinf(result) or np.isnan(result):
# print('gauss-large res x ', result, x)
return result
@numba.jit(float64(float64), nopython=True)
def phi(x):
absx = np.abs(x)
if (absx < GAUSS_EPSILON):
result = 0.5
return result
elif (absx < 0.66291):
result = 0.5 + gauss_small(x)
return result
elif (absx < SQRT32):
result = gauss_medium(x)
if (x > 0.0):
result = 1.0 - result
return result
elif (x > GAUSS_XUPPER):
result = 1.0
return result
elif (x < GAUSS_XLOWER):
result = 0.0
return result
else:
result = gauss_large(x)
if (x > 0.0):
result = 1.0 - result
return result
@numba.jit
# def truncnormrnd(mu, sigma, xlo, xhi, n_samples=1):
def truncnormrnd(mu, sigma, xlo, xhi, rand_gen, n_samples=1):
"""
Sample from a truncated normal
"""
_plo = phi((xlo - mu) / sigma)
_phi = phi((xhi - mu) / sigma)
r = rand_gen.rand()
# r = np.random.rand()
r = _plo + (_phi - _plo) * r
z = gsl_cdf_ugaussian_Pinv(r)
x = mu + z * sigma
return x
@numba.jit
def truncnormrnd_det(i, mu, sigma, xlo, xhi, rand_gen, n_samples=1):
"""
Deterministic dummy for testing purposes
"""
return mu + i
@numba.jit
def mvnrnd_det(i, Mu, Sigma, rand_gen):
"""
Deterministic dummy for testing purposes
"""
return Mu + i
@numba.jit
def mnrnd_det(i, p, rand_gen):
"""
Deterministic dummy for testing purposes
"""
return i % len(p)
@numba.jit
def random_dirichlet_det(i, p, size):
"""
Deterministic dummy for testing purposes
"""
_p = np.array([i + 1 for j in range(len(p))])
return _p / _p.sum()
@numba.jit
def random_normal_det(i, loc, scale, size=1):
"""
Deterministic dummy for testing purposes
"""
return np.array([loc + i for j in range(size)])
@numba.jit
def random_rand_det(i, _max=500):
"""
Deterministic dummy for testing purposes
"""
return i / _max
from numba import jit, uint8, int64, float64, optional, boolean
@numba.jit(float64[:](float64[:, :]), nopython=True)
def amax_2D_numba(a):
"""
Optimized maximum version of numba operating along the last axis of a 2D vector
"""
N, D = a.shape
# amax = np.zeros((N, D))
# for n in range(N):
# amax[n, :] = np.max(a[n, :])
amax = np.zeros(N)
for n in range(N):
amax[n] = np.max(a[n, :])
if not np.isfinite(amax[n]):
amax[n] = 0
return amax
@numba.jit(float64[:, :](float64[:, :, :]), nopython=True)
def amax_3D_numba(a):
"""
Optimized maximum version of numba operating along the last axis of a 2D vector
"""
N, D, L = a.shape
# amax = np.zeros((N, D))
# for n in range(N):
# amax[n, :] = np.max(a[n, :])
amax = np.zeros((N, D))
for n in range(N):
for d in range(D):
amax[n, d] = np.max(a[n, d, :])
if not np.isfinite(amax[n, d]):
amax[n, d] = 0
return amax
@numba.jit(float64[:](float64[:, :], float64[:, :]),
# locals={'a_max': float64[:, :]},
nopython=True)
def logsumexp_2D_numba(a, b):
"""
Optimized logsumexp op in numba, operating only on last axis of a 2D vector
"""
# a_max = np.max(a, axis=-1)
N, D = a.shape
a_max = amax_2D_numba(a)
tmp = np.zeros((N, D))
for d in range(D):
tmp[:, d] = b[:, d] * np.exp(a[:, d] - a_max)
s = np.sum(tmp, axis=-1)
out = np.log(s)
out += a_max
return out
@numba.jit(float64[:, :](float64[:, :, :], float64[:, :, :]),
# locals={'a_max': float64[:, :]},
nopython=True)
def logsumexp_3D_numba(a, b):
"""
Optimized logsumexp op in numba, operating only on last axis of a 3D vector
"""
# a_max = np.max(a, axis=-1)
N, D, L = a.shape
a_max = amax_3D_numba(a)
tmp = np.zeros((N, D, L))
for l in range(L):
tmp[:, :, l] = b[:, :, l] * np.exp(a[:, :, l] - a_max)
s = np.sum(tmp, axis=-1)
out = np.log(s)
out += a_max
return out
@numba.jit(float64(float64[:], float64[:]), nopython=True)
def logsumexp_1Dw_numba(a, b):
"""
Optimized logsumexp op in numba, operating only on last axis of a 1D vector
"""
# a_max = np.max(a, axis=-1)
a_max = np.max(a)
if not np.isfinite(a_max):
a_max = 0
tmp = b * np.exp(a - a_max)
s = np.sum(tmp, axis=-1)
out = np.log(s)
out += a_max
return out
@numba.jit(float64(float64[:]), nopython=True)
def logsumexp_1D_numba(a):
"""
Optimized logsumexp op in numba, operating only on last axis of a 1D vector
"""
# a_max = np.max(a, axis=-1)
a_max = np.max(a)
if not np.isfinite(a_max):
a_max = 0
tmp = np.exp(a - a_max)
s = np.sum(tmp, axis=-1)
out = np.log(s)
out += a_max
return out
# @numba.jit(nopython=True)
# @numba.jit(float64(int64, int64,
# float64[:, :, :],
# float64[:], float64[:], int64, int64), nopython=True)
# def cat_pdf_numba(d, r, Bcat, Zn, u, n_samples_cat, R_d):
# prodC = np.ones(n_samples_cat)
# # u = rand_gen.normal(loc=0, scale=sYd, size=n_samples_cat)
# # u = np.array([np.random.normal(loc=0, scale=sYd)
# # for j in range(n_samples_cat)])
# for r2 in range(R_d):
# if r2 != r:
# Baux = np.copy(Bcat[d, r, :])
# Baux -= Bcat[d, r2, :]
# aux = np.dot(Zn, Baux)
# for ii in range(n_samples_cat):
# # prodC[ii] = prodC[ii] * \
# # stats.norm.cdf(x=u[ii] + aux, scale=1)
# # print(ii, u[ii], aux, phi(x=u[ii] + aux))
# prodC[ii] = prodC[ii] * \
# phi(x=u[ii] + aux)
# sumC = prodC.sum()
# # print(sumC, Zn, R_d, Bcat[d, r, :], d, r)
# return np.log(sumC / n_samples_cat)
@numba.jit(float64(int64, int64,
optional(float64[:, :, :]),
float64[:], float64[:],
int64, uint8), nopython=True)
def cat_pdf_numba(d, r, Bcat, Zn, u, n_samples_cat, R_d):
# logsumC = float64(0)
logsumC = np.zeros(n_samples_cat, dtype=np.float64)
for r2 in range(R_d):
if r2 != r:
Baux = np.copy(Bcat[d, r, :])
Baux -= Bcat[d, r2, :]
aux = np.dot(Zn, Baux)
for ii in range(n_samples_cat):
# logsumC += np.log(phi(x=u[ii] + aux))
logsumC[ii] += np.log(phi(x=u[ii] + aux))
#
# this shall be a logsumexp
# return logsumC - np.log(n_samples_cat)
# return logsumexp(logsumC) - np.log(n_samples_cat)
return logsumexp_1D_numba(logsumC) - np.log(n_samples_cat)
from math import erf, sqrt
@numba.njit
def phi_erf_numba(x):
return (1.0 + erf(x / sqrt(2.0))) / 2.0
@numba.jit(float64(int64, int64,
optional(float64[:, :, :]),
float64[:], float64[:],
int64, uint8, float64[:]), nopython=True)
def cat_pdf_numba_prealloc(d, r, Bcat, Zn, u, n_samples_cat, R_d, logsumC):
# logsumC = float64(0)
# logsumC = np.zeros(n_samples_cat, dtype=np.float64)
logsumC.fill(0)
for r2 in range(R_d):
if r2 != r:
Baux = np.copy(Bcat[d, r, :])
Baux -= Bcat[d, r2, :]
aux = np.dot(Zn, Baux)
for ii in range(n_samples_cat):
# logsumC += np.log(phi(x=u[ii] + aux))
logsumC[ii] += np.log(phi_erf_numba(x=u[ii] + aux))
#
# this shall be a logsumexp
# return logsumC - np.log(n_samples_cat)
# return logsumexp(logsumC) - np.log(n_samples_cat)
return logsumexp_1D_numba(logsumC) - np.log(n_samples_cat)
@numba.jit(float64[:, :, :](float64[:, :], float64[:, :, :],
uint8[:], uint8[:], float64[:],
float64[:, :], float64[:, :], int64,
optional(float64[:, :]), optional(
float64[:, :]), optional(float64[:, :]),
optional(float64[:, :]), optional(float64[:, :, :]),
optional(float64[:, :]), optional(float64[:, :]),
float64, float64, float64, int64,
float64[:], float64[:], float64[:],
float64[:, :], float64[:], float64[:],
int64[:],
int64[:],
boolean[:],
boolean[:],
int64[:],
int64), nopython=True)
def compute_lvstd_leaf_ll_samp_Z(X, LL,
C, R, u,
Z, Z_sampled, Kest,
Breal, Bpos, Bint, Bbin, Bcat, Bord, Bcount,
s2Y, sYd, s2U, Wint,
maxX, meanX, minX,
theta, theta_L, theta_H,
instance_ids,
feature_ids,
# scope,
instance_scope,
feature_scope,
feature_scope_map,
n_samples_cat
):
N = len(instance_ids)
#
# LL is of shape D x N x L
L = LL.shape[2]
for n, d in zip(instance_ids, feature_ids):
if not feature_scope[d]:
continue
xnd = X[n, d]
dn = feature_scope_map[d]
Zn = Z[:, n]
if C[dn] == 1:
#
# real
aux = np.dot(Zn, Breal[dn])
ll = xpdf_re(xnd, 2 / (maxX[d] - meanX[d]),
meanX[d], aux, s2Y, s2U)
LL[d, n, 0] = ll
#
# int
aux = np.dot(Zn, Bint[dn])
ll = xpdf_int(xnd, Wint, theta_L[d], theta_H[d], aux, s2Y, s2U)
LL[d, n, 1] = ll
#
# pos
aux = np.dot(Zn, Bpos[dn])
ll = xpdf_pos(xnd, 2 / maxX[d], aux, s2Y, s2U)
LL[d, n, 2] = ll
elif C[dn] == 2:
#
# real
aux = np.dot(Zn, Breal[dn])
ll = xpdf_re(xnd, 2 / (maxX[d] - meanX[d]),
meanX[d], aux, s2Y, s2U)
LL[d, n, 0] = ll
#
# int
aux = np.dot(Zn, Bint[dn])
ll = xpdf_int(xnd, Wint, theta_L[d], theta_H[d], aux, s2Y, s2U)
LL[d, n, 1] = ll
elif C[dn] == 3:
continue
elif C[dn] == 4:
xnd = int(xnd)
r = int64(xnd - 1)
rr = int64(xnd - 2)
#
# cat
ll = cat_pdf_numba(dn, r, Bcat, Zn, u, n_samples_cat, R[dn])
LL[d, n, 3] = ll
#
# ord
aux = np.dot(Zn, Bord[dn])
if xnd == 1:
ll = np.log(phi(x=theta[d, r] - aux))
elif xnd == R[dn]:
ll = np.log(1 - phi(x=theta[d, rr] - aux))
else:
ll = np.log(phi(theta[d, r] - aux) -
phi(x=theta[d, rr] - aux))
LL[d, n, 4] = ll