-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTH_module.py
1498 lines (1135 loc) · 52.6 KB
/
BTH_module.py
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
################################################
# DESCRIPTION: tests for the BTH test using: #
################################################
# a) Sampling implementation with Laplace prior#
# b) INLA implementation with Laplace prior #
# c) INLA implementation with Cauchy prior #
# d) 2SLS (eLife) #
# e) Brown - Forsythe (0,1,2 only) #
# f) Levene (0,1,2 only) #
################################################
# includes the function special_shuffle #
# for computing nulls #
################################################
# Date July 29, 2015 #
# biancad, gdarnell, bee @ princeton.edu #
################################################
# requierements
import numpy as np
from numpy.random import randn
from scipy import stats
from scipy import optimize
import math
import random
import sys
import os
from scipy.stats import loglaplace
from scipy.stats import cauchy
# global hyperparams
global M, b, theta1, theta2, v, lam, logexp_param, snp, out, gamma
def set_global_params():
'''
sets up the global hyperparameters
'''
global theta1, theta2, v, lam, b, M, gamma
theta1 = 1.
theta2 = 2.
v = 5.
b = 10.
lam = 5.
M = 500
gamma = 1. # for Cauchy
return []
########## Cauchy Inla ##########
def INLACauchy_uniform_intercept(snp,out):
'''
inla approximation with Cauchy prior and uniform prior over the intercept
'''
set_global_params()
[numerator1, beta0_est, beta_est, alpha_est, sigma_est] = INLACauchy_log_Laplace( snp,out,theta1, theta2, v, b, 1,gamma)
[denominator, beta0_est_null, beta_est_null, alpha_est_null, sigma_est_null] = INLACauchy_log_Laplace(snp,out,theta1, theta2, v, b, 0,gamma) # double integrals
return [(numerator1 - denominator)* np.log10(np.e), beta0_est, beta_est, alpha_est, sigma_est, beta0_est_null, beta_est_null,alpha_est_null, sigma_est_null]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# helper functions for Cauchy, redundant # TO DO: consolidate with INLA Laplace
def INLACauchy_log_Laplace(snp, out,theta1, theta2, v,lam, int_type,gamma):
''' \int \int \int exp int_type can be 1, -1, 0 '''
N = len(snp)
if int_type == 1:
bound1 = 0.000001 # for Cauchy
bound2 = None # for Cauchy
elif int_type == -1:
bound1 = 1. # does not matter for Cauchy
bound2 = None # does not matter for Cauchy
else:
bound1 = None
bound2 = None
# MAP estimates
N = len(snp)
params = [snp, out, theta1, theta2, v, lam, N, int_type,gamma]
##############################################################
if int_type != 0:
# perform triple integral estimation with prior over alpha
if int_type == -1:
ans = optimize.fmin_tnc( lambda x: INLACauchy_h(x,params), [1.,1.,0.], fprime= lambda x: INLACauchy_hprime(x,params), \
bounds=((bound1, bound2),(0.00001,None),(None,None)),\
epsilon =1e-4,disp=False)
else:
#ans = optimize.fmin_tnc( lambda x: INLACauchy_h(x,params), [1.,1.,0.], fprime= lambda x: INLACauchy_hprime(x,params), \
# bounds=((bound1, bound2),(0.00001,None),(None,None)),\
# epsilon =1e-4)
ans = optimize.fmin_tnc( lambda x: INLACauchy_h(x,params), [1.,1.,0.], fprime= lambda x: INLACauchy_hprime(x,params), \
bounds=((bound1, bound2),(0.000001,None),(None,None)),\
epsilon =1e-4,disp=False)
[alpha_hat, sigma_hat, beta0_hat] = ans[0]
#print("alpha_hat:",ans[0][0]," sigma_hat: ",ans[0][1], " beta0_hat: ",ans[0][2])
evaluate_h = INLACauchy_h([alpha_hat, sigma_hat, beta0_hat], params)
evaluate_hess = INLACauchy_h_hess([alpha_hat, sigma_hat, beta0_hat], params) # fing the values of the hessian terms at MAP
d = 3.
S2 = sum([(snp[i]**2) * (alpha_hat ** snp[i]) for i in range(len(snp))])
S1 = sum([snp[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
Q1 = sum([snp[i] * out[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
beta_hat = 1./(v + 1./sigma_hat * S2) * 1./sigma_hat *(Q1 - beta0_hat*S1)
#print("beta_hat:",beta_hat)
#print('^ numerators ^')
else:
# perform double integral estimation with alpha = 1
ans = optimize.fmin_tnc( lambda x: INLACauchy_h(x,params), [1., 0.000001], fprime= lambda x: INLACauchy_hprime(x,params), \
bounds=((0.000001, None),(None, None)),epsilon =1e-5,disp=False)
#print("alpha =1, sigma*_hat, beta0*_hat")
[sigma_hat,beta0_hat,] = ans[0]
#print("alpha_hat:",1," sigma_hat: ",ans[0][0], " beta0_hat: ",ans[0][1])
evaluate_h = INLACauchy_h([sigma_hat, beta0_hat], params) # find the value of the h function at the MAP estimates
evaluate_hess = INLACauchy_h_hess([sigma_hat,beta0_hat],params) # fing the values of the hessian terms at MAP
d = 2.
alpha_hat = 1.
S2 = sum([(snp[i]**2) * (alpha_hat ** snp[i]) for i in range(len(snp))])
S1 = sum([snp[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
Q1 = sum([snp[i] * out[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
beta_hat = 1./(v + 1./sigma_hat * S2) * 1./sigma_hat *(Q1 - beta0_hat*S1)
log_laplace_term = (- N * evaluate_h) + d/2. * np.log(2*np.pi) - \
0.5 * np.log(abs(evaluate_hess)) - d/2. *np.log(N)
return [log_laplace_term, beta0_hat, beta_hat, alpha_hat,sigma_hat]
def INLACauchy_h(x,params):
'''
returns the evaluation of the main function h, that enters the Laplace approx in the term exp(- N * h)
'''
[snp, out, theta1, theta2, v, lam, N,int_type,gamma] = params
if int_type != 0:
alpha = x[0]
sigma = x[1]
beta0 = x[2]
if int_type == -1: #will not matter for Cauchy
prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (1./lam - 1.) * np.log(alpha)
#prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (1./lam ) * np.log(alpha)
else:
#prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (-1./lam - 1.) * np.log(alpha)
prior_on_alpha = - np.log(np.pi * gamma + np.pi / gamma * ((np.log(alpha))**2)) - np.log(alpha) # Cauchy
else:
alpha = 1.
sigma = x[0]
beta0 = x[1]
prior_on_alpha = 0.
#terms that i need in the evaluation of the function
####################################################
[S, R, Q, O] = INLACauchy_intermediary_params(x, snp, out,int_type)
[S0, S1, S2, S3, S4] = S
[R1, R2, R3] = R
[Q1,Q2,Q3] = Q
[O1,O2,O3] = O
####################################################
G = sigma * v + S2
prior_on_sigma = (-theta1 -1)*np.log(sigma) - theta2*1./sigma
if G ==0.:
#print('error at params, G, h')
#print(params)
G = 0.000001
if sigma == 0.:
#print('error at params, sigma, h')
#print(params)
sigma = 0.000001
L0 = (-N/2. + 0.5) * np.log(sigma) - 0.5 * np.log(G) + 0.5 * np.sum(snp) * np.log(alpha)
L1 = -1./(2 * sigma) *(R2 - Q1*Q1/G)
L2 = 1./sigma * beta0 *(R1 - Q1*S1/G)
L3 = - 1./(2 * sigma) *beta0 * beta0 * (S0 - S1*S1/G)
h = -1./N *(L0 + L1 + L2 + L3 + prior_on_alpha + prior_on_sigma)
return h
def INLACauchy_hprime(x,params):
''' evaluations of the first derivatives
'''
[snp, out, theta1, theta2, v, lam, N, int_type,gamma] = params
if int_type != 0:
alpha = x[0]
sigma = x[1]
beta0 = x[2]
if int_type == -1:
d_alpha_prior_on_alpha = (1./lam -1.)*1./alpha # not used for Cauchy
else:
#d_alpha_prior_on_alpha = (-1./lam -1)*1./alpha
d_alpha_prior_on_alpha = - (gamma**2 + (np.log(alpha))**2 + 2 * np.log(alpha))/( gamma**2 *alpha + alpha * (np.log(alpha))**2)#for Cauchy
else:
alpha = 1.
sigma = x[0]
beta0 = x[1]
[S, R, Q, O] = INLACauchy_intermediary_params(x, snp, out, int_type)
[S0, S1, S2, S3, S4] = S
[R1, R2, R3] = R
[Q1,Q2,Q3] = Q
[O1,O2,O3] = O
G = sigma * v + S2
if G ==0.:
#print('error at params, G, hprime')
#print(params)
G = 0.000001
if sigma ==0.:
#print('error at params, sigma, hprime')
#print(params)
sigma = 0.000001
if alpha ==0:
#print('error at alpha,hprime')
#print(params)
alpha = 0.000001
### d_sig_L0
d_sig_L0 = (-0.5*N + 0.5) * 1./sigma - 0.5 * v /G #OK
### d_sig_L1
d_sig_L1 = 1./(2* sigma*sigma)*(R2 - Q1*Q1/G) - 1./(2*sigma) * v * Q1*Q1/(G*G) #OK
### d_sig_L2
d_sig_L2 = -1./(sigma*sigma) *beta0 *( R1 - Q1*S1/G) + 1./sigma * beta0 * Q1* S1*v/ (G*G) #OK
### d_sig_L3
d_sig_L3 = 1./ (2* sigma*sigma) * beta0* beta0 *(S0 - S1*S1/G) - 1./(2*sigma) *beta0*beta0 *S1*S1*v/(G*G) #OK
### d_sig_prior_on_alpha
d_sig_prior_on_alpha = 0 #OK
### d_sig_prior_on_sigma
d_sig_prior_on_sigma = (-theta1 -1) *(1./sigma) + theta2/(sigma*sigma) #OK
### d_beta0_L0
d_beta0_L0 = 0 #OK
### d_beta0_L1
d_beta0_L1 = 0 #OK
### d_beta0_L2
d_beta0_L2 = 1./ sigma * (R1 - Q1*S1/G) #OK
### d_beta0_L3
d_beta0_L3 = -1./sigma * beta0 * (S0 - S1*S1/G) #OK
### d_beta0_prior_on_alpha
d_beta0_prior_on_alpha = 0 #OK
### d_beta0_prior_on_sigma
d_beta0_prior_on_sigma = 0 #OK
dhsigma = -1./N *(d_sig_L0 + d_sig_L1 + d_sig_L2 + d_sig_L3 + d_sig_prior_on_alpha + d_sig_prior_on_sigma) #OK
dhbeta0 = -1./N *(d_beta0_L0 + d_beta0_L1 + d_beta0_L2 + d_beta0_L3 + d_beta0_prior_on_alpha + d_beta0_prior_on_sigma)
if int_type ==0:
return np.array((dhsigma, dhbeta0))
else:
### d_alpha_L0
d_alpha_L0 = - 0.5 * 1./alpha * S3/G + 0.5 * np.sum(snp) * 1./alpha
### d_alpha_L1
d_alpha_L1 = -0.5 * 1./sigma * (1./alpha) *(O1 - 2*Q1*Q2/G + Q1*Q1*S3/(G*G))
### d_alpha_L2
d_alpha_L2 = 1./sigma * beta0 * 1./alpha * (Q1 - Q2 *S1/G - Q1*S2/G + Q1*S1*S3/(G*G))
### d_alpha_L3
d_alpha_L3 = -0.5* 1./sigma *beta0*beta0 *1./alpha * (S1 - 2*S1*S2/G + S1*S1*S3/(G*G))
### d_alpha_prior_on_alpha
### d_alpha_prior_on_sigma
d_alpha_prior_on_sigma = 0
dhalpha = -1./N *(d_alpha_L0 + d_alpha_L1 + d_alpha_L2 + d_alpha_L3 + d_alpha_prior_on_alpha + d_alpha_prior_on_sigma)
return np.array((dhalpha, dhsigma, dhbeta0))
def INLACauchy_hhprime(x,params):
[snp, out, theta1, theta2, v, lam, N, int_type,gamma] = params
if int_type ==0:
alpha = 1.
sigma = x[0]
beta0 = x[1]
else:
alpha = x[0]
sigma = x[1]
beta0 = x[2]
if int_type == -1: # won't get here for cauchy
daa_prior_on_alpha = - (1./lam -1)*1./(alpha* alpha)
else:
#daa_prior_on_alpha = - (-1./lam -1)* 1./(alpha*alpha)
T1 = gamma**2 * (gamma**2 - 2) + 2*(gamma**2 + 1)*(np.log(alpha)**2)
T1 = T1 + 2 * gamma**2 * np.log(alpha) + (np.log(alpha)**4)
T1 = T1 + 2. * (np.log(alpha)**3)
T2 = alpha**2 * ((gamma**2 + np.log(alpha)**2)**2)
daa_prior_on_alpha = T1/T2# <--for Cauchy
[S, R, Q, O] = INLACauchy_intermediary_params(x, snp, out, int_type)
[S0, S1, S2, S3, S4] = S
[R1, R2, R3] = R
[Q1,Q2,Q3] = Q
[O1,O2,O3] = O
G = sigma * v + S2
if G ==0.:
#print('error at params, G, hprime')
#print(params)
G = 0.000001
if sigma ==0.:
#print('error at params, sigma, hprime')
#print(params)
sigma = 0.000001
################# dss #################
### dss_L0
dss_L0 = -(-N/2. + 0.5)*1./(sigma**2) + 0.5 * (v**2)/(G**2)
### dss_L1 TYPO!!!
dss_L1 = -1./(sigma**3)*(R2 - Q1*Q1/G) + 1.0/(sigma**2) * (v * (Q1**2)/(G**2)) +\
1./sigma * (Q1**2) * (v**2)/(G**3)
### dss_L2
dss_L2 = 2./(sigma**3) * beta0 * (R1 - Q1*S1/G) - beta0/(sigma**2)*Q1*S1*v/(G**2) +\
(-1./(sigma**2)) * beta0 * Q1*S1*v/(G**2) + 1./sigma * beta0 * (-2) * Q1*S1*v*v/(G**3)
### dss_L3
dss_L3 = -1./(sigma**3) * (beta0**2) *(S0 - S1*S1/G) + 1./(sigma**2)*(beta0**2)*(S1**2)*v/(G**2)+\
+ 1./sigma * (beta0**2) * (S1**2)*v*v/(G**3)
### dss_prior_on_alpha
dss_prior_on_alpha = 0
### dss_prior_on_sigma
dss_prior_on_sigma = -(-theta1 -1)*1./(sigma**2) - 2* theta2/ (sigma**3)
################# dsb #################
### dsb_L0
dsb_L0 = 0
### dsb_L1
dsb_L1 = 0
### dsb_L2
dsb_L2 = -1./(sigma**2) * (R1 - Q1*S1/G) +1./sigma * Q1*S1*v/(G**2)
### dsb_L3
dsb_L3 = beta0/(sigma**2) * (S0 - S1**2/G) - (beta0/sigma) * (S1**2)*v/(G**2)
### dsb_prior_on_alpha
dsb_prior_on_alpha = 0
### dsb_prior_on_sigma
dsb_prior_on_sigma = 0
################# dbb #################
### dbb_L0
dbb_L0 = 0
### dbb_L1
dbb_L1 = 0
### dbb_L2
dbb_L2 = 0
### dbb_L3
dbb_L3 = -1./(sigma) * (S0 - S1*S1/G)
### dbb_prior_on_alpha
dbb_prior_on_alpha = 0
### dbb_prior_on_sigma
dbb_prior_on_sigma = 0
hss = -1./N *(dss_L0 + dss_L1 + dss_L2 + dss_L3 + dss_prior_on_alpha + dss_prior_on_sigma)
hsb = -1./N *(dsb_L0 + dsb_L1 + dsb_L2 + dsb_L3 + dsb_prior_on_alpha + dsb_prior_on_sigma)
hbb = -1./N *(dbb_L0 + dbb_L1 + dbb_L2 + dbb_L3 + dbb_prior_on_alpha + dbb_prior_on_sigma)
if int_type ==0:
return np.array((hss,hsb,hsb,hbb))
else:
################# daa #################
### daa_L0
daa_L0 = 1./(2*(alpha**2)) * 1./G *(S3 - S4) + 1./(2 * alpha**2) * (S3**2)/(G**2) - 1./(2* alpha**2)* np.sum(snp)
### daa_L1
P1 = O1 - 2*Q1*Q2/G + (Q1**2)*S3/(G**2)
P2 = O2 - (2* Q2**2 + 2* Q1 * Q3)/G + (4.* Q1*Q2*S3 + (Q1**2)*S4)/(G**2) - 2.*(Q1**2)*(S3**2)/(G**3)
## ##
daa_L1 = 0.5 * 1./sigma * 1./(alpha**2) * P1 - 0.5 *1./sigma * 1./(alpha**2)* P2
### daa_L2
P1 = Q1 - (Q2*S1 + Q1*S2)/G + (Q1*S1*S3)/(G**2)
P2 = Q2 - (Q3*S1 + Q2*S2 + Q2*S2 + Q1*S3)/G + (S3 * ( Q2*S1 + Q1*S2 ))/(G**2) +\
(Q2*S1*S3 + Q1 * S2*S3 + Q1*S1*S4 )/(G**2) - (2*Q1*S1 * (S3**2))/(G**3)
daa_L2 = -1./sigma * beta0 * 1./(alpha**2) * P1 + 1./sigma * beta0 * 1./(alpha**2) * P2
### daa_L3
P1 = S1 - 2* S1*S2/G + (S1**2)*S3/(G**2)
# oldP2 = S2 - 2* (S2**2 + S1*S3)/G - 2 * S1 * S2 * S3/ (G**2) + (2*S1 *S2 * S3 + S1**2 * S4)/(G**2) - (2* S1**2 * S3**2)/(G**3)
P2 = S2 - 2* (S2**2 + S1*S3)/G + 2 * S1 * S2 * S3/ (G**2) + (2*S1 *S2 * S3 + S1**2 * S4)/(G**2) - (2* S1**2 * S3**2)/(G**3)
daa_L3 = 1./ (2 * sigma) * (beta0**2)/ (alpha**2) * P1 - 1./(2*sigma)* (beta0**2)/(alpha**2) * P2
### daa_prior_on_alpha
### daa_prior_on_sigma
daa_prior_on_sigma = 0
################# das #################
### das_L0
das_L0 = 0.5 * 1./alpha * v * S3 / (G**2)
### das_L1
P1 = O1 - 2.* Q1 * Q2/G + (Q1**2) * S3 / (G**2)
P2 = 2* Q1*Q2*v/(G**2) - 2 * Q1**2 * S3 * v/ (G**3)
das_L1 = 0.5 * (1./(sigma**2)) * (1./alpha) * P1 - (0.5 * 1./sigma)*(1./alpha)* P2
### das_L2
P1 = Q1 - Q2 * S1/G - Q1 * S2/G + Q1 * S1 * S3/ (G**2)
P2 = v*(Q2 * S1 + Q1 * S2)/ (G**2) - 2 * (Q1 * S1 * S3 * v)/(G**3)
das_L2 = - beta0 / (alpha * (sigma**2)) * P1 + beta0/ (alpha * sigma) * P2
### das_L3
P1 = 1./alpha * (S1 - 2*S1*S2/G + S1**2 * S3 / (G**2))
P2 = 1./alpha * (2 * S1 * S2 *v/ (G**2) - 2 * S3 * (S1**2) * v/(G**3))
das_L3 = beta0**2/ (2 * sigma**2)* P1 - (beta0**2)/ (2*sigma) * P2
### das_prior_on_alpha
das_prior_on_alpha = 0
### das_prior_on_sigma
das_prior_on_sigma = 0
################# dab #################
### dab_L0
dab_L0 = 0
### dab_L1
dab_L1 = 0
### dab_L2
dab_L2 = 1./(sigma * alpha ) * (Q1 - Q2 * S1/ G - Q1 * S2 /G + Q1 * S1 * S3/ (G**2))
### dab_L3
dab_L3 = - beta0/(sigma * alpha) * (S1 - 2 * S1 * S2/G + S1**2 * S3/(G**2))
### dab_prior_on_alpha
dab_prior_on_alpha = 0
### dab_prior_on_sigma
dab_prior_on_sigma = 0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
haa = -1./N *(daa_L0 + daa_L1 + daa_L2 + daa_L3 + daa_prior_on_alpha + daa_prior_on_sigma)
has = -1./N *(das_L0 + das_L1 + das_L2 + das_L3 + das_prior_on_alpha + das_prior_on_sigma)
hab = -1./N *(dab_L0 + dab_L1 + dab_L2 + dab_L3 + dab_prior_on_alpha + dab_prior_on_sigma)
hsa = has
hba = hab
hbs = hsb
return np.array((haa, has, hab, hsa, hss, hsb, hba, hbs, hbb))
def INLACauchy_h_hess(x,params):
hess_entries = INLACauchy_hhprime(x,params)
if params[-1]==0:
Hess = np.resize(hess_entries,(2,2))
else:
Hess = np.resize(hess_entries,(3,3))
if (np.linalg.det(Hess)) == 0:
return 0.000001
big_sigma = np.linalg.det(Hess)
return big_sigma
def INLACauchy_intermediary_params(x, snp, out, int_type):
if int_type!= 0:
alpha = x[0]
#sigma = x[1]
#beta0 = x[2]
[S0,S1,S2,S3,S4,R1,R2,R3,Q1,Q2,Q3,O1,O2,O3] = np.zeros(14)
for i in range(len(snp)):
#print snp[i]
c = alpha**snp[i]
[S0,S1,S2,S3,S4] = [S0 + c, S1 + snp[i] *c, S2 + (snp[i]**2) *c, S3 + (snp[i]**3) *c, S4 + (snp[i]**4) *c]
[R1, R2, R3] = [R1 + (out[i]**1) *c, R2 + (out[i]**2) *c, R3 + (out[i]**3) *c]
[Q1, Q2, Q3] = [Q1 + (out[i]**1)*(snp[i]**1)*c, Q2 + (out[i]**1)*(snp[i]**2)*c, Q3 + (out[i]**1)*(snp[i]**3)*c]
[O1, O2, O3] = [O1 + (out[i]**2)*(snp[i]**1)*c, O2 + (out[i]**2)*(snp[i]**2)*c, O3 + (out[i]**2)*(snp[i]**3)*c]
else:
alpha = 1.
#sigma = x[0]
#beta0 = x[1]
[S0,S1,S2,S3,S4,R1,R2,R3,Q1,Q2,Q3,O1,O2,O3] = np.zeros(14)
for i in range(len(snp)):
c = 1
[S0,S1,S2,S3,S4] = [S0 + c, S1 + snp[i] *c, S2 + (snp[i]**2) *c, S3 + (snp[i]**3) *c, S4 + (snp[i]**4) *c]
[R1, R2, R3] = [R1 + (out[i]**1) *c, R2 + (out[i]**2) *c, R3 + (out[i]**3) *c]
[Q1, Q2, Q3] = [Q1 + (out[i]**1)*(snp[i]**1)*c, Q2 + (out[i]**1)*(snp[i]**2)*c, Q3 + (out[i]**1)*(snp[i]**3)*c]
[O1, O2, O3] = [O1 + (out[i]**2)*(snp[i]**1)*c, O2 + (out[i]**2)*(snp[i]**2)*c, O3 + (out[i]**2)*(snp[i]**3)*c]
S = [S0, S1, S2, S3, S4]
R = [R1, R2, R3]
Q = [Q1, Q2, Q3]
O = [O1, O2, O3]
return [S, R, Q, O]
################################## Laplace Sample ######################################
def SampleLaplace_uniform_intercept(snp,out):
'''
sampling version of the BTH test
'''
from scipy.stats import loglaplace
global theta1, theta2, v, lam, M
set_global_params()
M = 500
#data is assumed to be tested for correctness
#BF = 1/m \sum \frac{T(\alpha_m)}{T_1}
alphas = loglaplace.rvs(1./lam, size= M)
#print('alpha')
#print(alphas)
Theta1 = theta1
Theta2 = theta2
V = v
Lam = lam
# replace the above with the average
log10_T1 = log10_T_s(snp,out,Theta1, Theta2, V, Lam, M, 1)
#BF_terms= [[log10_T_s(snp,out,Theta1, Theta2, V, Lam, m, alpha) - log10_T1,alpha] for alpha in alphas]
#return [BF_terms,alphas]
BF_terms= [log10_T_s(snp,out,Theta1, Theta2, V, Lam, M, alpha) - log10_T1 for alpha in alphas]
return max(BF_terms) - np.log10(M)
# helper functions for Sample Laplace
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
def log10_T_s(snp,out,theta1, theta2,v,lam, M, alpha):
''' return the log factors representing the numerator and denominator
'''
N = len(snp)
if alpha==1.:
#ln alpha (0.5 * sum(snp)
res = 0.
else:
res = np.log(alpha) * (0.5 * np.sum(snp)) *np.log10(np.e)
Xo_alpha = float(sum([(snp[i]**2) * (alpha**snp[i]) for i in range(N)]))
Yo_alpha = float(sum([(out[i]**2) * (alpha**snp[i]) for i in range(N)]))
Zo_alpha = float(sum([(snp[i]*out[i]) * (alpha**snp[i]) for i in range(N)]))
To_alpha = float(sum([(alpha**snp[i]) for i in range(N)]))
TXo_alpha = float(sum([(snp[i]) * (alpha**snp[i]) for i in range(N)]))
TYo_alpha = float(sum([(out[i]) * (alpha**snp[i]) for i in range(N)]))
# MAP estimates
params = [N, Xo_alpha, Yo_alpha, Zo_alpha, To_alpha, TXo_alpha, TYo_alpha]
#print('alpha')
#print(alpha)
ans = optimize.fmin_tnc( lambda x: h_s(x,params), [0,1], fprime= lambda x: hprime_s(x,params), bounds=((None, None),(0.0001, None)),disp=False)
[beta0_hat,sigma_hat] = ans[0]
#print('estimates')
#print(ans[0])
##############################################################
evaluate_h = h_s([beta0_hat,sigma_hat], params) # find the value of the h function at the MAP estimates
#print('h at MAP')
#print(evaluate_h)
evaluate_hess = h_hess_s([beta0_hat,sigma_hat],params) # fing the values of the hessian terms at MAP
#print('hess at MAP')
#print(evaluate_hess)
d = 2. # dimension of the laplace approximation
laplace_term = (- N * evaluate_h)* np.log10(np.e) + d/2. * np.log10(2*np.pi) - \
0.5 * np.log10(0.0000000000000000000001 + abs(evaluate_hess)) - d/2. *np.log10(N) # edit such that it becomes non zero
return res + laplace_term
######################################
######################################
def h_s(x,params):
'''
the main function that enters the Laplace approx as exp(- N * h)
'''
[N, Xo_alpha, Yo_alpha, Zo_alpha, To_alpha, TXo_alpha, TYo_alpha] = params
beta0 = x[0]
sigma = x[1]
[A, B, C, D, G, betaC, betaD] = intermediary_params_s(x,params)
if G ==0.:
print('error at params, G, h')
print(params)
if sigma == 0.:
print('error at params, sigma, h')
print(params)
h = -1./N *( A * np.log(sigma) + 1./sigma * C + B * np.log(G) + D*1./G)
return h
######################################
######################################
def hprime_s(x,params):
''' derivative
'''
global theta2
global theta1
global v
[N, Xo_alpha, Yo_alpha, Zo_alpha, To_alpha, TXo_alpha, TYo_alpha] = params
beta0 = x[0]
sigma = x[1]
[A, B, C, D, G, betaC, betaD] = intermediary_params_s(x,params)
if G ==0.:
print('error at params, G, hprime')
print(params)
if sigma ==0.:
print('error at params, sigma, hprime')
print(params)
dhsigma = -1./N * (A/sigma - C/(sigma**2) + B*v/G - v* D/(G**2))
dhbeta0 = -1./N * (1./sigma * betaC + 1./G * betaD)
return np.array((dhbeta0, dhsigma))
######################################
######################################
def hhprime_s(x,params):
global theta2
global theta1
global v
[N, Xo_alpha, Yo_alpha, Zo_alpha, To_alpha, TXo_alpha, TYo_alpha] = params
beta0 = x[0]
sigma = x[1]
[A, B, C, D, G, betaC, betaD] = intermediary_params_s(x,params)
betaZ = - TXo_alpha
betabetaY = 2 * To_alpha
if Xo_alpha==0.:
print('error at params, Xo_alpha, hhprime')
print(params)
betabetaC = -0.5*(betabetaY - 1./Xo_alpha * 2.* (betaZ**2))
betabetaD = v* 1./Xo_alpha * (betaZ**2)
if G ==0.:
print('error at params, G,hhprime')
print(params)
if sigma ==0.:
print('error at params, sigma, hhprime')
print(params)
hbb = -1./N * ( 1./sigma * betabetaC + 1./G * betabetaD)
hsb = -1./N *( -1./(sigma**2) * betaC - v/(G**2) * betaD)
hss = -1./N *(-1./(sigma**2) + 2* C/(sigma**3) - B*(v**2)/(G**2) + 2* D*(v**2)/(G**3))
return np.array((hbb, hsb, hsb, hss))
######################################
######################################
def h_hess_s(x,params):
hess_entries = hhprime_s(x,params)
Hess = np.resize(hess_entries,(2,2))
if (np.linalg.det(Hess)) == 0:
return 0.000001
big_sigma = np.linalg.det(Hess)
return big_sigma
######################################
######################################
def intermediary_params_s(x,params):
global theta2
global theta1
global v
[N, Xo_alpha, Yo_alpha, Zo_alpha, To_alpha, TXo_alpha, TYo_alpha] = params
beta0 = x[0]
sigma = x[1]
A = -0.5 * (N-1) - theta1 - 1.
B = -0.5
Z_alpha = Zo_alpha - beta0 * TXo_alpha
Y_alpha = Yo_alpha + beta0**2 * To_alpha - 2.* beta0* TYo_alpha
if Xo_alpha==0:
print('error at params')
print(params)
C = - 0.5 *( Y_alpha - (Z_alpha**2)/Xo_alpha + 2. * theta2)
D = - 0.5 * v * (Z_alpha**2)/Xo_alpha # TO this is repetitive with h, so make separate function
betaY = 2.* beta0 * To_alpha - 2.* TYo_alpha
betaZ = - TXo_alpha
betaC = -0.5 *(betaY - 1./Xo_alpha * 2.* Z_alpha * betaZ)
betaD = - v * Z_alpha * betaZ/Xo_alpha
G = sigma *v + Xo_alpha
return [A, B, C, D, G, betaC, betaD]
######################################
######################################
########################################################################################
################################## Laplace INLA ########################################
########################################################################################
def INLALaplace_uniform_intercept(snp,out):
'''
integrated laplace for approximating BFs
'''
from scipy.stats import loglaplace
global theta1, theta2, v, b
set_global_params()
[numerator1, beta0_est, beta_est, alpha_est, sigma_est] = INLA_log_Laplace( snp,out,theta1, theta2, v, b, 1)
[numerator2, beta0_est_null, beta_est_null,alpha_est_null, sigma_est_null] = INLA_log_Laplace(snp, out,theta1, theta2, v, b, -1)
denominator = INLA_log_Laplace(snp,out,theta1, theta2, v, b,0) # double integrals
maxi = max(numerator1 - denominator, numerator2 - denominator)
mini = min(numerator1 - denominator, numerator2 - denominator)
if maxi > 500:
return maxi * np.log10(np.e)
elif mini < - 500:
mini = -500
return [np.log(np.e**maxi + np.e**mini) * np.log10(np.e), beta0_est, beta_est, alpha_est, sigma_est, beta0_est_null, beta_est_null,alpha_est_null, sigma_est_null]
# helper functions for Laplace INLA
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
def INLA_log_Laplace(snp, out,theta1, theta2, v,lam, int_type):
''' \int \int \int exp
int_type can be 1, -1, 0
'''
N = len(snp)
if int_type == 1:
bound1 = 0.
bound2 = 1. # edit july 8th, july 23
elif int_type == -1:
bound1 = 1. # edit july 8th
bound2 = None
else:
bound1 = None
bound2 = None
# MAP estimates
N = len(snp)
params = [snp, out, theta1, theta2, v, lam, N, int_type]
##############################################################
if int_type != 0:
# perform triple integral estimation with prior over alpha
if int_type == -1:
ans = optimize.fmin_tnc( lambda x: INLA_h(x,params), [1.,1.,0.], fprime= lambda x: INLA_hprime(x,params), \
bounds=((bound1, bound2),(0.00001,None),(None,None)),\
epsilon =1e-4,disp=False)
else:
ans = optimize.fmin_tnc( lambda x: INLA_h(x,params), [1.,1.,0.], fprime= lambda x: INLA_hprime(x,params), \
bounds=((bound1, bound2),(0.00001,None),(None,None)),\
epsilon =1e-4,disp=False)
# edit july 20: changed start guess to 1.1 for alpha such that it is symmetric to other integral
[alpha_hat, sigma_hat, beta0_hat] = ans[0]
evaluate_h = INLA_h([alpha_hat, sigma_hat, beta0_hat], params)
evaluate_hess = INLA_h_hess([alpha_hat, sigma_hat, beta0_hat], params) # fing the values of the hessian terms at MAP
d = 3.
S2 = sum([(snp[i]**2) * (alpha_hat ** snp[i]) for i in range(len(snp))])
S1 = sum([snp[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
Q1 = sum([snp[i] * out[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
beta_hat = 1./(v + 1./sigma_hat * S2) * 1./sigma_hat *(Q1 - beta0_hat*S1)
#print('^ numerators ^')
else:
# perform double integral estimation with alpha = 1
ans = optimize.fmin_tnc( lambda x: INLA_h(x,params), [1., 0.00001], fprime= lambda x: INLA_hprime(x,params), \
bounds=((0.00001, None),(None, None)),epsilon =1e-5,disp=False)
#print("alpha =1, sigma*_hat, beta0*_hat")
[sigma_hat,beta0_hat,] = ans[0]
#print("alpha_hat:",1," sigma_hat: ",ans[0][0], " beta0_hat: ",ans[0][1])
evaluate_h = INLA_h([sigma_hat, beta0_hat], params) # find the value of the h function at the MAP estimates
evaluate_hess = INLA_h_hess([sigma_hat,beta0_hat],params) # fing the values of the hessian terms at MAP
d = 2.
alpha_hat = 1.
S2 = sum([(snp[i]**2) * (alpha_hat ** snp[i]) for i in range(len(snp))])
S1 = sum([snp[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
Q1 = sum([snp[i] * out[i] * (alpha_hat ** snp[i]) for i in range(len(snp))])
beta_hat = 1./(v + 1./sigma_hat * S2) * 1./sigma_hat *(Q1 - beta0_hat*S1)
#print("beta_hat:",beta_hat)
#print('^denominator ^')
log_laplace_term = (- N * evaluate_h) + d/2. * np.log(2*np.pi) - \
0.5 * np.log(abs(evaluate_hess)) - d/2. *np.log(N)
return [log_laplace_term, beta0_hat, beta_hat,alpha_hat, sigma_hat]
################################################################################################
################################################################################################
def INLA_h(x,params):
'''
returns the evaluation of the main function h, that enters the Laplace approx in the term exp(- N * h)
'''
[snp, out, theta1, theta2, v, lam, N,int_type] = params
if int_type != 0:
alpha = x[0]
sigma = x[1]
beta0 = x[2]
if int_type == -1:
prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (1./lam - 1.) * np.log(alpha)
#prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (1./lam ) * np.log(alpha)
else:
prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (-1./lam - 1.) * np.log(alpha)
#prior_on_alpha = np.log(0.5 * 1./abs(lam)) + (-1./lam ) * np.log(alpha)
else:
alpha = 1.
sigma = x[0]
beta0 = x[1]
prior_on_alpha = 0.
#terms that i need in the evaluation of the function
####################################################
[S, R, Q, O] = INLA_intermediary_params(x, snp, out,int_type)
[S0, S1, S2, S3, S4] = S
[R1, R2, R3] = R
[Q1,Q2,Q3] = Q
[O1,O2,O3] = O
####################################################
G = sigma * v + S2
prior_on_sigma = (-theta1 -1)*np.log(sigma) - theta2*1./sigma
if G ==0.:
#print('error at params, G, h')
#print(params)
G = 0.000001
if sigma == 0.:
#print('error at params, sigma, h')
#print(params)
sigma = 0.000001
L0 = (-N/2. + 0.5) * np.log(sigma) - 0.5 * np.log(G) + 0.5 * np.sum(snp) * np.log(alpha)
L1 = -1./(2 * sigma) *(R2 - Q1*Q1/G)
L2 = 1./sigma * beta0 *(R1 - Q1*S1/G)
L3 = - 1./(2 * sigma) *beta0 * beta0 * (S0 - S1*S1/G)
h = -1./N *(L0 + L1 + L2 + L3 + prior_on_alpha + prior_on_sigma)
return h
################################################################################################
################################################################################################
def INLA_hprime(x,params):
''' evaluations of the first derivatives
'''
[snp, out, theta1, theta2, v, lam, N, int_type] = params
if int_type != 0:
alpha = x[0]
sigma = x[1]
beta0 = x[2]
if int_type == -1:
d_alpha_prior_on_alpha = (1./lam -1.)*1./alpha
else:
d_alpha_prior_on_alpha = (-1./lam -1)*1./alpha
else:
alpha = 1.
sigma = x[0]
beta0 = x[1]
[S, R, Q, O] = INLA_intermediary_params(x, snp, out, int_type)
[S0, S1, S2, S3, S4] = S
[R1, R2, R3] = R
[Q1,Q2,Q3] = Q
[O1,O2,O3] = O
G = sigma * v + S2
if G ==0.:
#print('error at params, G, hprime')
#print(params)
G = 0.000001
if sigma ==0.:
#print('error at params, sigma, hprime')
#print(params)
sigma = 0.000001
### d_sig_L0
d_sig_L0 = (-0.5*N + 0.5) * 1./sigma - 0.5 * v /G #OK
### d_sig_L1
d_sig_L1 = 1./(2* sigma*sigma)*(R2 - Q1*Q1/G) - 1./(2*sigma) * v * Q1*Q1/(G*G) #OK
### d_sig_L2
d_sig_L2 = -1./(sigma*sigma) *beta0 *( R1 - Q1*S1/G) + 1./sigma * beta0 * Q1* S1*v/ (G*G) #OK
### d_sig_L3
d_sig_L3 = 1./ (2* sigma*sigma) * beta0* beta0 *(S0 - S1*S1/G) - 1./(2*sigma) *beta0*beta0 *S1*S1*v/(G*G) #OK
### d_sig_prior_on_alpha
d_sig_prior_on_alpha = 0 #OK
### d_sig_prior_on_sigma
d_sig_prior_on_sigma = (-theta1 -1) *(1./sigma) + theta2/(sigma*sigma) #OK
### d_beta0_L0
d_beta0_L0 = 0 #OK
### d_beta0_L1
d_beta0_L1 = 0 #OK
### d_beta0_L2
d_beta0_L2 = 1./ sigma * (R1 - Q1*S1/G) #OK
### d_beta0_L3
d_beta0_L3 = -1./sigma * beta0 * (S0 - S1*S1/G) #OK
### d_beta0_prior_on_alpha
d_beta0_prior_on_alpha = 0 #OK
### d_beta0_prior_on_sigma
d_beta0_prior_on_sigma = 0 #OK
dhsigma = -1./N *(d_sig_L0 + d_sig_L1 + d_sig_L2 + d_sig_L3 + d_sig_prior_on_alpha + d_sig_prior_on_sigma) #OK
dhbeta0 = -1./N *(d_beta0_L0 + d_beta0_L1 + d_beta0_L2 + d_beta0_L3 + d_beta0_prior_on_alpha + d_beta0_prior_on_sigma) #OK