-
Notifications
You must be signed in to change notification settings - Fork 1
/
MHupdates.cpp
1909 lines (1530 loc) · 72.6 KB
/
MHupdates.cpp
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
#include "global_defs.h"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <omp.h>
#define sd_prior_beta 1 // 1.8 sd of the normal prior for beta
#define mean_prior_beta -1 // -2 mean of the normal prior for beta
#define sd_prior_theta 1 // 1.8 sd of the normal prior for beta
#define mean_prior_theta -1 // -2 mean of the normal prior for beta
#define lambda 1 // parameter for the non informative dirichlet prior of allele frequencies
#define gamma 10 // parameter for the variance between delta and mu
#define epsilon 1e-6 // limit for allele frequencies (epsilon,1-epsilon)
#define PI 3.14159265358979
using namespace std;
//////////////////////////////////
// update ancestral allele frequencies
///////////////////////////////////
void update_ancestral_freq()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // forward and backward proposition probability
double old_freq; // store old allele freq if the move is rejected
double u; // proposal random value
//omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for SCHED_I private(r, A, FwPrp, BwPrp, old_freq, u)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
// store old value
old_freq=freq_ancestral[i];
// calculate forward proposition probability =q(p',p)
BwPrp=min(1.0-epsilon,old_freq+e_ancestral[i])-max(0.0+epsilon,old_freq-e_ancestral[i]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].randDblExc(BwPrp)+max(0.0+epsilon,old_freq-e_ancestral[i]);
if (u<=epsilon) u=epsilon;
if (u>=1-epsilon) u=1-epsilon;
freq_ancestral[i]=u;
// calculate backward proposition probability =q(p,p')
FwPrp=min(1.0-epsilon,freq_ancestral[i]+e_ancestral[i])-max(0.0+epsilon,freq_ancestral[i]-e_ancestral[i]);
// ratio A
A= log(BwPrp) - log(FwPrp);
/*+ (a_p-1)*log((1-freq_ancestral[i])/(1-old_freq))+(a_p-1)*log(freq_ancestral[i]/old_freq);*/
double phi;
for (int g=0;g<G;g++)
{
phi=exp(-(alpha[i]+beta[g]));
A+= gammaln(phi*old_freq) + gammaln(phi*(1-old_freq))
- gammaln(phi*freq_ancestral[i]) - gammaln(phi*(1-freq_ancestral[i]))
+ phi*(freq_ancestral[i]-old_freq)*(log(group[g].locus[i].p) - log(1-group[g].locus[i].p));
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
freq_ancestral[i]=old_freq;// restore old value of allele frequency
}
else
{
acc_freq_ancestral[i]++;
}
}
} // end of cycle over loci
}
/////////////////////////////////////
// update a_p (beta prior for p)
/////////////////////////////////////
void update_a_p()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double u;
double e;
double old_a; // old value of a
double m=0;
double s=1;
// keep the old value
old_a=a_p;
// make the move
e=randgen.randNorm(0,var_prop_a_p);
a_p=a_p*exp(e);
A= 0.5*((log(old_a)-m)/s)*((log(old_a)-m)/s)
- 0.5*((log(a_p)-m)/s)*((log(a_p)-m)/s);
A+=I*(gammaln(2.0*a_p)+2.0*gammaln(old_a)-gammaln(2.0*old_a)-2.0*gammaln(a_p));
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
A+=(a_p-old_a)*(log(freq_ancestral[i])+log(1-freq_ancestral[i]));
}
r=randgen.randDblExc();
// reject proposed value
if (log(r)>A)
{
a_p=old_a; // restore old value of sigma_square
}
else
{
acc_a_p++;
}
}
//////////////////////////////////
// update allele frequencies
///////////////////////////////////
void update_freq()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // forward and backward proposition probability
double FwPrp2,BwPrp2; // forward and backward proposition probability
// double old_log_likelihood; // old logikelihood
double diff_log_likelihood; // difference of the logikelihoods
double old_freq; // store old allele freq if the move is rejected
//double log_likelihood2;
double old_g,new_g; // store new and old phenotype frequency
double u; // proposal random value
double g;
#pragma omp parallel for SCHED_J reduction(+:log_likelihood) private(r, A, FwPrp, BwPrp, FwPrp2, BwPrp2, diff_log_likelihood, old_freq, old_g, new_g, u, g)
for (int j=0;j<J;j++) //cycle over populations
{
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
double e_small=e_freq[i][j]/100;
double prop_small=0.5;
// store old value
old_freq=pop[j].locus[i].p;
old_g= pop[j].locus[i].p*pop[j].locus[i].p
+2*pop[j].locus[i].p*(1-pop[j].locus[i].p)*(1-f[j])
+f[j]*pop[j].locus[i].p*(1-pop[j].locus[i].p);
// calculate forward proposition probability =q(p',p)
BwPrp=min(1.0-epsilon,old_freq+e_freq[i][j])-max(0.0+epsilon,old_freq-e_freq[i][j]);
BwPrp2=min(1.0-epsilon,old_freq+e_small)-max(0.0+epsilon,old_freq-e_small);
// propose the new values
if (randgen_parallel[omp_get_thread_num()].rand()<1-prop_small)
u=randgen_parallel[omp_get_thread_num()].rand(BwPrp)+max(0.0+epsilon,old_freq-e_freq[i][j]);
else
u=randgen_parallel[omp_get_thread_num()].rand(BwPrp2)+max(0.0+epsilon,old_freq-e_small);
if (u<=epsilon) u=epsilon;
if (u>=1-epsilon) u=1-epsilon;
pop[j].locus[i].p=u;
// calculate backward proposition probability =q(p,p')
FwPrp=min(1.0-epsilon,pop[j].locus[i].p+e_freq[i][j])-max(0.0+epsilon,pop[j].locus[i].p-e_freq[i][j]);
FwPrp2=min(1.0-epsilon,pop[j].locus[i].p+e_small)-max(0.0+epsilon,pop[j].locus[i].p-e_small);
// store the old logikelihood and calculate the new logikelihood
//old_log_likelihood=log_likelihood;
new_g= pop[j].locus[i].p*pop[j].locus[i].p
+2*pop[j].locus[i].p*(1-pop[j].locus[i].p)*(1-f[j])
+f[j]*pop[j].locus[i].p*(1-pop[j].locus[i].p);
diff_log_likelihood= pop[j].locus[i].nA1*(log(new_g)-log(old_g))
+ (pop[j].locus[i].n-pop[j].locus[i].nA1)*(log(1-new_g)-log(1-old_g));
//#pragma omp critical(likelihood)
//log_likelihood=old_log_likelihood+diff_log_likelihood;
double phi_sc=exp(-(eta2[pop[j].group][i]+theta[j]));
// ratio A
A= diff_log_likelihood
+(phi_sc*group[pop[j].group].locus[i].p-1)*(log(pop[j].locus[i].p)-log(old_freq))
+ (phi_sc*(1-group[pop[j].group].locus[i].p)-1)*(log(1-pop[j].locus[i].p)-log(1-old_freq))
+ log((1-prop_small)*BwPrp+prop_small*BwPrp2) - log((1-prop_small)*FwPrp+prop_small*FwPrp2);
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
//log_likelihood=old_log_likelihood; // restore likelihood
pop[j].locus[i].p=old_freq;// restore old value of allele frequency
}
else
{
acc_freq[i][j]++;
log_likelihood=log_likelihood+diff_log_likelihood;
}
}
} // end of cycle over loci
} // end of cycle over populations
}
//////////////////////////////////
// update group allele frequencies
///////////////////////////////////
void update_freq_group()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // forward and backward proposition probability
//double old_log_likelihood; // old logikelihood
//double diff_log_likelihood; // difference of the logikelihoods
double old_freq; // store old allele freq if the move is rejected
//double log_likelihood2;
double u; // proposal random value
//double theta; // store current theta value
//omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for SCHED_I private(r, A, FwPrp, BwPrp, old_freq, u)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
for (int g=0;g<G;g++) //cycle over populations
{
// store old value
old_freq=group[g].locus[i].p;
// calculate forward proposition probability =q(p',p)
BwPrp=min(1.0-epsilon,old_freq+e_freq_group[i][g])-max(0.0+epsilon,old_freq-e_freq_group[i][g]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].rand(BwPrp)+max(0.0+epsilon,old_freq-e_freq_group[i][g]);
if (u<=epsilon) u=epsilon;
if (u>=1-epsilon) u=epsilon;
group[g].locus[i].p=u;
// calculate backward proposition probability =q(p,p')
FwPrp=min(1.0-epsilon,group[g].locus[i].p+e_freq_group[i][g])-max(0.0+epsilon,group[g].locus[i].p-e_freq_group[i][g]);
A=0;
double phi_sc;
for (int j=0;j<group[g].member.size();j++)
{
int cur_pop=group[g].member[j];
phi_sc=exp(-(eta2[g][i]+theta[cur_pop]));
A+= gammaln(phi_sc*old_freq) + gammaln(phi_sc*(1-old_freq))
- gammaln(phi_sc*group[g].locus[i].p) - gammaln(phi_sc*(1-group[g].locus[i].p))
+ phi_sc*(group[g].locus[i].p-old_freq)*(log(pop[cur_pop].locus[i].p) - log(1-pop[cur_pop].locus[i].p));
}
// ratio A
double phi_ct=exp(-(alpha[i]+beta[g]));
A+= (phi_ct*freq_ancestral[i]-1)*(log(group[g].locus[i].p)-log(old_freq))
+ (phi_ct*(1-freq_ancestral[i])-1)*(log(1-group[g].locus[i].p)-log(1-old_freq))
+ log(BwPrp) - log(FwPrp);
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
// log_likelihood=old_log_likelihood; // restore likelihood
group[g].locus[i].p=old_freq;// restore old value of allele frequency
}
else
{
acc_freq_group[i][g]++;
}
}
} // end of cycle over loci
} // end of cycle over populations
}
//////////////////////////////////
// update fis randomly (NOT ESTIMATED)
///////////////////////////////////
void update_f_random()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // inverse of forward and backward proposition probability =1/q
double old_f; // store old f if the move is rejected
double u; // proposal random value
if (prior_fis_unif)
{
//omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for SCHED_J private(r, A, FwPrp, BwPrp, u,old_f)
for (int j=0;j<J;j++) // cycle over populations
{
// store old value
old_f=f[j];
// calculate forward proposition probability =q(p',p)
BwPrp=min(prior_fis_hb,old_f+e_f[j])-max(prior_fis_lb,old_f-e_f[j]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].randDblExc(BwPrp)+max(prior_fis_lb,old_f-e_f[j]);
f[j]=u;
// calculate backward proposition probability =q(p,p')
FwPrp=min(1.0,f[j]+e_f[j])-max(0.0,f[j]-e_f[j]);
// ratio A
A= log(BwPrp) - log(FwPrp);
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A) f[j]=old_f;// restore old value of allele frequency
else
{
//log_likelihood=allelecount_loglikelihood();
acc_f[j]++;
}
}
log_likelihood=allelecount_loglikelihood();
}
else // beta prior
{
#pragma omp parallel for private(r, A, FwPrp, BwPrp, u,old_f)
for (int j=0;j<J;j++) // cycle over populations
{
// store old value
old_f=f[j];
// calculate forward proposition probability =q(p',p)
BwPrp=min(1.0,old_f+e_f[j])-max(0.0,old_f-e_f[j]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].rand(BwPrp)+max(0.0,old_f-e_f[j]);
if (u<=0.0001) u=0.0001;
if (u>=0.9999) u=0.9999;
f[j]=u;
// calculate backward proposition probability =q(p,p')
FwPrp=min(1.0,f[j]+e_f[j])-max(0.0,f[j]-e_f[j]);
A= (prior_fis_a-1)*(log(f[j])-log(old_f))
+ (prior_fis_b-1)*(log(1-f[j])-log(1-old_f))
+ log(BwPrp) - log(FwPrp);
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A) f[j]=old_f;// restore old value of allele frequency
else
{
//log_likelihood=allelecount_loglikelihood();
acc_f[j]++;
}
}
log_likelihood=allelecount_loglikelihood();
}
}
//////////////////////////////////
// update theta
///////////////////////////////////
void update_theta()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double BwPrp,FwPrp;
double u;
double e; // proposal random value
double new_phi,old_phi; // new and old values of theta
double old_theta;
//omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for SCHED_J private(r, A, FwPrp, BwPrp, e, u,new_phi,old_phi,old_theta)
for (int j=0;j<J;j++) // cycle over populations
{
// store old value
old_theta=theta[j];
// make the move
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_theta[j]);
theta[j]=theta[j]+e;
// ratio A
A=(old_theta-theta[j])*(old_theta+theta[j]-2*mean_prior_theta)/(2*sd_prior_theta*sd_prior_theta);
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
{
// calculate old and new value of theta
new_phi=exp(-(eta2[pop[j].group][i]+theta[j]));
old_phi=exp(-(eta2[pop[j].group][i]+old_theta));
A+= gammaln(new_phi)-gammaln(old_phi)
- gammaln(new_phi*group[pop[j].group].locus[i].p) + gammaln(old_phi*group[pop[j].group].locus[i].p)
- gammaln(new_phi*(1-group[pop[j].group].locus[i].p)) + gammaln(old_phi*(1-group[pop[j].group].locus[i].p))
+ group[pop[j].group].locus[i].p*(new_phi-old_phi)*log(pop[j].locus[i].p)
+ (1-group[pop[j].group].locus[i].p)*(new_phi-old_phi)*log(1-pop[j].locus[i].p);
}
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
theta[j]=old_theta;// restore old value of beta
}
else
{
acc_theta[j]++;
}
} // end of cycle over populations
}
//////////////////////////////////
// update beta
///////////////////////////////////
void update_beta()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double old_beta; // store old allele freq if the move is rejected
double BwPrp,FwPrp;
double u;
double e; // proposal random value
double new_phi,old_phi; // new and old values of theta
// #pragma omp parallel for SCHED_G private(r, A, FwPrp, BwPrp, e, u,new_phi,old_phi,old_beta)
for (int g=0;g<G;g++) // cycle over populations
{
// store old value
old_beta=beta[g];
// make the move
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_beta[g]);
beta[g]=beta[g]+e;
// ratio A
A=(old_beta-beta[g])*(old_beta+beta[g]-2*mean_prior_beta)/(2*sd_prior_beta*sd_prior_beta);
#pragma omp parallel for SCHED_I reduction(+:A) private(new_phi,old_phi)
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
{
// calculate old and new value of theta
new_phi=exp(-(alpha[i]+beta[g]));
old_phi=exp(-(alpha[i]+old_beta));
A+= gammaln(new_phi)-gammaln(old_phi)
- gammaln(new_phi*freq_ancestral[i]) + gammaln(old_phi*freq_ancestral[i])
- gammaln(new_phi*(1-freq_ancestral[i])) + gammaln(old_phi*(1-freq_ancestral[i]))
+ freq_ancestral[i]*(new_phi-old_phi)*log(group[g].locus[i].p)
+ (1-freq_ancestral[i])*(new_phi-old_phi)*log(1-group[g].locus[i].p);
}
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
beta[g]=old_beta;// restore old value of beta
}
else
{
acc_beta[g]++;
}
} // end of cycle over populations
}
//////////////////////////////////
// update alpha i
///////////////////////////////////
void update_alpha()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double old_alpha; // store old allele freq if the move is rejected
double e; // proposal random value
double new_phi,old_phi; // new and old values of theta
#pragma omp parallel for SCHED_I /*reduction(+:alpha_updates)*/ private(r, A, old_alpha, e,new_phi,old_phi)
for (int i=0;i<I;i++)
{
if (!discarded_loci[i] && alpha_included[i])
{
//alpha_updates=alpha_updates+1;
// store old value
old_alpha=alpha[i];
// make the move
//std::cout << omp_get_thread_num();
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_alpha[i]);
alpha[i]=alpha[i]+e;
// ratio A
A= (old_alpha*old_alpha-alpha[i]*alpha[i])/(2*sd_prior_alpha*sd_prior_alpha);
// A=log_prior_alpha(alpha[i])-log_prior_alpha(old_alpha);
for (int g=0;g<G;g++)
{
// calculate old and new value of theta
new_phi=exp(-(alpha[i]+beta[g]));
old_phi=exp(-(old_alpha+beta[g]));
A+= gammaln(new_phi)-gammaln(old_phi)
- gammaln(new_phi*freq_ancestral[i]) + gammaln(old_phi*freq_ancestral[i])
- gammaln(new_phi*(1-freq_ancestral[i])) + gammaln(old_phi*(1-freq_ancestral[i]))
+ freq_ancestral[i]*(new_phi-old_phi)*log(group[g].locus[i].p)
+ (1-freq_ancestral[i])*(new_phi-old_phi)*log(1-group[g].locus[i].p);
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
alpha[i]=old_alpha;// restore old value of alpha
}
else
{
acc_alpha[i]++;
}
}
}
}
//////////////////////////////////
// update eta i g
///////////////////////////////////
void update_eta()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double old_eta; // store old eta if the move is rejected
double e; // proposal random value
double new_phi,old_phi; // new and old values of theta
#pragma omp parallel for SCHED_I private(r, A, old_eta, e, new_phi, old_phi)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
for (int p=0;p<P;p++) //cycle over populations
{
if (eta_included[p][i])
{
//eta_updates[g]++;
// store old value
old_eta=eta[p][i];
// make the move
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_eta[p][i]);
eta[p][i]=eta[p][i]+e;
// ratio A
A= (old_eta*old_eta-eta[p][i]*eta[p][i])/(2*sd_prior_alpha*sd_prior_alpha);
for (int g=0;g<pressure[p].member.size();g++)
{
int cur_group=pressure[p].member[g];
for (int j=0;j<group[cur_group].member.size();j++)
{
// calculate old and new value of theta
int cur_pop=group[cur_group].member[j];
new_phi=exp(-(eta[p][i]+theta[cur_pop]));
old_phi=exp(-(old_eta+theta[cur_pop]));
A+= gammaln(new_phi)-gammaln(old_phi)
- gammaln(new_phi*group[cur_group].locus[i].p) + gammaln(old_phi*group[cur_group].locus[i].p)
- gammaln(new_phi*(1-group[cur_group].locus[i].p)) + gammaln(old_phi*(1-group[cur_group].locus[i].p))
+ group[cur_group].locus[i].p*(new_phi-old_phi)*log(pop[cur_pop].locus[i].p)
+ (1-group[cur_group].locus[i].p)*(new_phi-old_phi)*log(1-pop[cur_pop].locus[i].p);
}
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
eta[p][i]=old_eta;// restore old value of alpha
}
else
{
acc_eta[p][i]++;
}
}
}
}
}
}
//////////////////////////////////
// update eta2 i g
///////////////////////////////////
void update_eta2()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double old_eta2; // store old eta if the move is rejected
double e; // proposal random value
double new_phi,old_phi; // new and old values of theta
#pragma omp parallel for SCHED_I private(r, A, old_eta2, e, new_phi, old_phi)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
for (int g=0;g<G;g++) //cycle over populations
{
if (eta2_included[g][i])
{
//eta_updates[g]++;
// store old value
old_eta2=eta2[g][i];
// make the move
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_eta2[g][i]);
eta2[g][i]=eta2[g][i]+e;
// ratio A
A= (old_eta2*old_eta2-eta2[g][i]*eta2[g][i])/(2*sd_prior_alpha*sd_prior_alpha);
for (int j=0;j<group[g].member.size();j++)
{
// calculate old and new value of theta
int cur_pop=group[g].member[j];
new_phi=exp(-(eta2[g][i]+theta[cur_pop]));
old_phi=exp(-(old_eta2+theta[cur_pop]));
A+= gammaln(new_phi)-gammaln(old_phi)
- gammaln(new_phi*group[g].locus[i].p) + gammaln(old_phi*group[g].locus[i].p)
- gammaln(new_phi*(1-group[g].locus[i].p)) + gammaln(old_phi*(1-group[g].locus[i].p))
+ group[g].locus[i].p*(new_phi-old_phi)*log(pop[cur_pop].locus[i].p)
+ (1-group[g].locus[i].p)*(new_phi-old_phi)*log(1-pop[cur_pop].locus[i].p);
}
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
eta2[g][i]=old_eta2;// restore old value of alpha
}
else
{
acc_eta2[g][i]++;
}
}
}
}
}
}
//////////////////////////////////////////////////////////
// For codominant markers
/////////////////////////////////////////////
//////////////////////////////////
// update allele frequencies in groups
///////////////////////////////////
void update_freq_group_codominant()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // inverse of forward and backward proposition probability =1/q
double old_m,old_n; // store old allele freq if the move is rejected
int m,n; // alleles to be changed, chosen at random
//double old_log_likelihood; // old logikelihood
double diff_log_likelihood; // difference of the logikelihood
//double log_likelihood2;
double u; // proposal random value
#pragma omp parallel for SCHED_I reduction(+:log_likelihood) private(r, A, FwPrp, BwPrp, old_m,old_n, m,n,u,diff_log_likelihood)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
for (int g=0;g<G;g++) // cycle over groups
{
if (freq_locus[i].ar==2) //case of SNPs, no need to choose allele at random
{
m=0;
n=1;
}
else
{
// choose at random the two allele frequences to change
m=randgen_parallel[omp_get_thread_num()].randInt(freq_locus[i].ar-1);
do
{
n=randgen_parallel[omp_get_thread_num()].randInt(freq_locus[i].ar-1);
}
while (n==m);
}
// store old values
old_m=group[g].locus[i].allele[m];
old_n=group[g].locus[i].allele[n];
// store old modify part of likelihood
// old_changed locus
double old_l=0;
for (int j=0;j<group[g].member.size();j++)
{
int cur_pop=group[g].member[j];
double phi_sc=exp(-(eta2[g][i]+theta[cur_pop]));
old_l+=gammaln(pop[cur_pop].locus[i].data_allele_count[m]+phi_sc*group[g].locus[i].allele[m])
-factln(pop[cur_pop].locus[i].data_allele_count[m])-gammaln(phi_sc*group[g].locus[i].allele[m]);
old_l+=gammaln(pop[cur_pop].locus[i].data_allele_count[n]+phi_sc*group[g].locus[i].allele[n])
-factln(pop[cur_pop].locus[i].data_allele_count[n])-gammaln(phi_sc*group[g].locus[i].allele[n]);
}
// calculate inverse of forward and backward proposition probability =1/q
BwPrp=min(old_m+old_n,old_m+e_freq_group[i][g])-max(0.0,old_m-e_freq_group[i][g]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].randDblExc(BwPrp)+max(0.0,old_m-e_freq_group[i][g]);
group[g].locus[i].allele[m]=u;
group[g].locus[i].allele[n]=old_n+(old_m-group[g].locus[i].allele[m]);
FwPrp=min(group[g].locus[i].allele[m]+group[g].locus[i].allele[n],group[g].locus[i].allele[m]+e_freq_group[i][g])-max(0.0,group[g].locus[i].allele[m]-e_freq_group[i][g]);
// store the old logikelihood and calculate the new logikelihood
//old_log_likelihood=log_likelihood;
double new_l=0;
// calculate new changed locus
for (int j=0;j<group[g].member.size();j++)
{
int cur_pop=group[g].member[j];
double phi_sc=exp(-(eta2[g][i]+theta[cur_pop]));
new_l+=gammaln(pop[cur_pop].locus[i].data_allele_count[m]+phi_sc*group[g].locus[i].allele[m])
-factln(pop[cur_pop].locus[i].data_allele_count[m])-gammaln(phi_sc*group[g].locus[i].allele[m]);
new_l+=gammaln(pop[cur_pop].locus[i].data_allele_count[n]+phi_sc*group[g].locus[i].allele[n])
-factln(pop[cur_pop].locus[i].data_allele_count[n])-gammaln(phi_sc*group[g].locus[i].allele[n]);
}
//log_likelihood=old_log_likelihood-old_l+new_l;
diff_log_likelihood=-old_l+new_l;
// ratio A
double phi_ct=exp(-(alpha[i]+beta[g]));
A= -old_l+new_l + log(BwPrp) - log(FwPrp)
// bug found : was lambda*... -> (lambda-1)*... is correct
// + (lambda-1.0)*(log(group[g].locus[i].allele[m])+log(group[g].locus[i].allele[n])-log(old_m)-log(old_n));
+ (phi_ct*freq_locus[i].allele[m]-1.0)* ( log(group[g].locus[i].allele[m])-log(old_m) )
+ (phi_ct*freq_locus[i].allele[n]-1.0)* ( log(group[g].locus[i].allele[n])-log(old_n) );
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
//log_likelihood=old_log_likelihood; // restore likelihood
group[g].locus[i].allele[m]=old_m; // and old values of allele frequencies
group[g].locus[i].allele[n]=old_n;
}
else
{
acc_freq_group[i][g]++;
log_likelihood=log_likelihood+diff_log_likelihood;
}
}
} // end of cycle over loci
} // end of cycle over groups
}
//////////////////////////////////
// update ancetral allele frequencies
///////////////////////////////////
void update_ancestral_freq_codominant()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double FwPrp,BwPrp; // inverse of forward and backward proposition probability =1/q
double old_m,old_n; // store old allele freq if the move is rejected
int m,n; // alleles to be changed, chosen at random
double phi;
double u; // proposal random value
#pragma omp parallel for SCHED_I private(r, A, FwPrp, BwPrp, old_m,old_n, m,n,phi,u)
for (int i=0;i<I;i++) // cycle over loci
{
if (!discarded_loci[i])
{
if (freq_locus[i].ar==2) //case of SNPs, no need to choose allele at random
{
m=0;
n=1;
}
else
{
// choose at random the two allele frequences to change
m=randgen_parallel[omp_get_thread_num()].randInt(freq_locus[i].ar-1);
do
{
n=randgen_parallel[omp_get_thread_num()].randInt(freq_locus[i].ar-1);
}
while (n==m);
}
// store old values
old_m=freq_locus[i].allele[m];
old_n=freq_locus[i].allele[n];
// store old modify part of likelihood
// old_changed locus
double old_l=0;
for (int g=0;g<G;g++)
{
phi=exp(-(alpha[i]+beta[g]));
for (int k=0;k<group[g].locus[i].ar;k++)
old_l-=gammaln(phi*freq_locus[i].allele[k]);
double temp=0;
for (int k=0;k<group[g].locus[i].ar;k++)
temp+=phi*freq_locus[i].allele[k];
old_l+=gammaln(temp);
for (int k=0;k<group[g].locus[i].ar;k++)
old_l+=(phi*freq_locus[i].allele[k]-1)*log(group[g].locus[i].allele[k]);
}
// calculate inverse of forward and backward proposition probability =1/q
BwPrp=min(old_m+old_n,old_m+e_ancestral[i])-max(0.0,old_m-e_ancestral[i]);
// propose the new values
u=randgen_parallel[omp_get_thread_num()].randDblExc(BwPrp)+max(0.0,old_m-e_ancestral[i]);
freq_locus[i].allele[m]=u;
freq_locus[i].allele[n]=old_n+(old_m-freq_locus[i].allele[m]);
FwPrp=min(freq_locus[i].allele[m]+freq_locus[i].allele[n],freq_locus[i].allele[m]+e_ancestral[i])-max(0.0,freq_locus[i].allele[m]-e_ancestral[i]);
double new_l=0;
// calculate new changed locus
for (int g=0;g<G;g++)
{
phi=exp(-(alpha[i]+beta[g]));
for (int k=0;k<group[g].locus[i].ar;k++)
new_l-=gammaln(phi*freq_locus[i].allele[k]);
double temp=0;
for (int k=0;k<group[g].locus[i].ar;k++)
temp+=phi*freq_locus[i].allele[k];
new_l+=gammaln(temp);
for (int k=0;k<group[g].locus[i].ar;k++)
new_l+=(phi*freq_locus[i].allele[k]-1)*log(group[g].locus[i].allele[k]);
}
// ratio A
A= -old_l+new_l + log(BwPrp) - log(FwPrp)
// bug found : was lambda*... -> (lambda-1)*... is correct
+ (lambda-1.0)*(log(freq_locus[i].allele[m])+log(freq_locus[i].allele[n])-log(old_m)-log(old_n));
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
freq_locus[i].allele[m]=old_m; // and old values of allele frequencies
freq_locus[i].allele[n]=old_n;
}
else
{
acc_freq_ancestral[i]++;
}
}
} // end of cycle over loci
}
//////////////////////////////////
// update alpha i
///////////////////////////////////
void update_alpha_codominant()
{
double r; // random value to accept/reject the move
double A; // log of A in MH algorithm : accept move with probability min(1,A)
double old_alpha; // store old allele freq if the move is rejected
double e; // proposal random value
double new_phi,old_phi; // new and old values of phi
#pragma omp parallel for SCHED_I /*reduction(+:alpha_updates)*/ private(r, A, old_alpha, e,new_phi,old_phi)
for (int i=0;i<I;i++)
{
if (!discarded_loci[i] && alpha_included[i])
{
//alpha_updates=alpha_updates+1;
// store old value
old_alpha=alpha[i];
double old_l=0;
for (int g=0;g<G;g++)
{
// calculate old and new value of theta
old_phi=exp(-(old_alpha+beta[g]));
for (int k=0;k<group[g].locus[i].ar;k++)
old_l-=gammaln(old_phi*freq_locus[i].allele[k]);
double temp=0;
for (int k=0;k<group[g].locus[i].ar;k++)
temp+=old_phi*freq_locus[i].allele[k];
old_l+=gammaln(temp);
for (int k=0;k<group[g].locus[i].ar;k++)
old_l+=(old_phi*freq_locus[i].allele[k]-1)*log(group[g].locus[i].allele[k]);
}
// make the move
e=randgen_parallel[omp_get_thread_num()].randNorm(0,var_prop_alpha[i]);
alpha[i]=alpha[i]+e;
double new_l=0;
for (int g=0;g<G;g++)
{
// calculate old and new value of theta
new_phi=exp(-(alpha[i]+beta[g]));
for (int k=0;k<group[g].locus[i].ar;k++)
new_l-=gammaln(new_phi*freq_locus[i].allele[k]);
double temp=0;
for (int k=0;k<group[g].locus[i].ar;k++)
temp+=new_phi*freq_locus[i].allele[k];
new_l+=gammaln(temp);
for (int k=0;k<group[g].locus[i].ar;k++)
new_l+=(new_phi*freq_locus[i].allele[k]-1)*log(group[g].locus[i].allele[k]);
}
// ratio A
A= -old_l+new_l +(old_alpha*old_alpha-alpha[i]*alpha[i])/(2*sd_prior_alpha*sd_prior_alpha);
r=randgen_parallel[omp_get_thread_num()].randDblExc();
// reject proposed value
if (log(r)>A)
{
alpha[i]=old_alpha;// restore old value of alpha
}