-
Notifications
You must be signed in to change notification settings - Fork 1
/
Source.cpp
1611 lines (1132 loc) · 50.3 KB
/
Source.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 <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <time.h>
#include "randlib.h"
#include <omp.h>
#include <vector>
#include <algorithm>
using namespace std;
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// // //
// #### // Setting up objects, //
// ## ## // declaring array sizes //
// ## ## // //
// ## ## // //
// #### // //
// // //
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// 0.1 Declare global variables of sizes of arrays
#define N_mcmc 10000000 // Number of MCMC iterations: indexed by mc
#define N_adapt 600000 // Number of MCMC iterations in adaptive phase
#define N_tune_start 10000 // Number of MCMC iterations in adaptive phase
#define N_tune_end 500000
#define N_data_cols 33 // Number of columns in data frame
#define N_part 194 // Number of participants to read in data from
#define N_negcon 270 // Number of negative control participants to read in data from
#define N_t 15 // Maximum number of data points per participant
#define N_loc_par 8 // Number of individual-level parameters
#define N_glob_par 8 // Number of population-level parameters
#define LARGE 1e12 // large number needed for priors
#define log2 0.69314718055995
#define sqrt2 1.414214
/////////////////////////////////////////////////////////////////
// 0.2 Create structure to hold data for participant n
// and local parameter estimates
struct part_n
{
//////////////////////////////////////
// Covariate information
int site; // 1 = Bichat; 2 = Strasbourg; 3 = Cochin; 4 = Thailand; 5 = Peru; 6 = EFS
int status; // negative = 1; positive = 2
int symptoms; // mild = 1; severe = 2
//////////////////////////////////////
// Antibody data
int N_sam; // Number of samples of antibody data
vector<double> AB;
vector<double> tt;
vector<double> lAB;
double AB_min; // Minimum antibody level for the individual
double AB_max; // Maximum antibody level for the individual
double lAB_min; // Log minimum antibody level for the individual
double lAB_max; // Log maximum antibody level for the individual
double lNC_max; // Log of maximum background antibody level (determined by negative controls)
//////////////////////////////////////
// Individual-level parameters
double Ab_bg; // background antibody level
double beta; // boost in ASCs - vaccine dose 1
double tau; // delay in boosting of antibody responses
double t_delta; // half-life of memory B cells
double t_short; // half-life of short-lived ASCs
double t_long; // half-life of long-lived ASCs
double t_IgG; // half-life of IgG molecules
double rho; // proportion of short-lived ASCs
double lAb_bg; // log(boost in antibody levels)
double lbeta; // log(boost in ASCs)
double ltau; // delay in boosting of antibody responses
double lt_delta; // half-life of memory B cells
double lt_short; // half-life of short-lived ASCs
double lt_long; // half-life of long-lived ASCs
double lt_IgG; // half-life of IgG molecules
double logitrho; // logit proportion of short-lived ASCs - vaccine dose 1
double r_delta; // drug decay rate
double r_short; // drug decay rate
double r_long; // drug decay rate
double r_IgG; // drug decay rate
//////////////////////////////////////
// Likelihood
double data_like; // data likelihood
double mix_like; // mixed-effects likelihood
double lhood; // individual-level likelihood
};
/////////////////////////////////////////////////////////////////
// 0.3 Create structure for global parameters to be estimated
struct params
{
/////////////////////////////////////////////////////////////
// Population-level parameters describing mixed effects
double mu_par[N_glob_par];
double tau_par[N_glob_par];
/////////////////////////////////////////////////////////////
// Maximum and minimum antibody levels
double AB_min; // Global minimum antibody level
double AB_max; // Global maximum antibody level
double lAB_min; // Log global minimum antibody level
double lAB_max; // Log global maximum antibody level
/////////////////////////////////////////////////////////////
// Parameter for observational error
double sig_obs; // precision of observational error
double log_sig_obs;
double sig_obs_scale;
/////////////////////////////////////////////////////////////
// Log likelihood and prior
double loglike;
double prior;
/////////////////////////////////////////////////////////////
// Prior distributions
double prior_MM[N_glob_par];
double prior_MM_CV[N_glob_par];
double prior_SIG[N_glob_par];
double prior_SIG_CV[N_glob_par];
double prior_LN_MM[N_glob_par];
double prior_LN_MM_CV[N_glob_par];
double prior_LN_SIG[N_glob_par];
double prior_LN_SIG_CV[N_glob_par];
double prior_mu[N_glob_par];
double prior_tau[N_glob_par];
double prior_k[N_glob_par];
double prior_theta[N_glob_par];
/////////////////////////////////////////////////////////////
// Individual-level parameter book-keeping
double Y_par[N_glob_par];
double Ymu2_par[N_glob_par];
};
/////////////////////////////////////////////////////////////////
// 0.4 Individual-level structure for MCMC tuning
struct part_n_MCMC
{
float par_vec[N_loc_par]; // Parameter vector (in float format for setgmn) (lAB_0, rr)
float par_vec_test[N_loc_par]; // Test parameter vector for MCMC update (in float format for setgmn)
float work[N_loc_par]; // Scratch vector for setgmn
double par_S1[N_loc_par]; // Sum of parameters
double par_S2[N_loc_par][N_loc_par]; // Sum of product of pairs
float COV_MAT[N_loc_par][N_loc_par]; // covariance matrix (in float format for setgmn)
float COV_MAT_dummy[N_loc_par][N_loc_par]; // dummy covariance matrix: setgmn gives back sqrt(COV_MAT) or similar so we feed it a dummy
float GMN_parm[(N_loc_par)*(N_loc_par + 3) / 2 + 1]; // array for setgmn output
int denom; // denominator for tracking SD calculations
double step_scale; // scalar for tuning acceptance rate
int accept; // number of accepted steps
};
////////////////////////////////////////////////////
// 0.5 Initialise functions
double data_like_n(part_n* p, params* theta);
double mix_like_n(part_n* p, params* theta);
double global_prior(params* priors);
double local_prior(part_n* p);
double rm_scale(double step_scale, int step, int N_step_adapt, double log_prob);
double gammln(const double xx);
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// // //
// ## // Initialise main object, read in data and //
// ### // fill out objects //
// ## // //
// ## // //
// #### // //
// // //
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// 1.1 Initialise main object - need to choose whether
// to run in console or as a .exe
int main(int argc, char** argv)
{
// do we have the correct command line?
if (argc != 5)
{
std::cout << "Incorrect command line.\n";
return 0;
}
char* AB_input_File = argv[1];
char* global_output_File = argv[2];
char* local_output_File = argv[3];
double long_half = atof(argv[4]);
//////////////////////////////////////////////////////
// 1.2 Declare seed, buffer for writing to and clock
setall(time(NULL), 7);
int cl = clock();
///////////////////////////////////////////////////////
// 1.3 Read in antibody data (infected individuals)
std::ifstream AB_Stream(AB_input_File);
if (AB_Stream.fail())
{
std::cout << "Failure reading in data." << endl;
}
vector<vector<double>> AB_data_read;
AB_data_read.resize(N_part+ N_negcon);
for (int i = 0; i < (N_part + N_negcon); i++)
{
AB_data_read[i].resize(N_data_cols);
}
for (int i = 0; i< (N_part + N_negcon); i++)
{
for (int j = 0; j<N_data_cols; j++)
{
AB_Stream >> AB_data_read[i][j];
}
}
AB_Stream.close();
//////////////////////////////////////////////////////
// 1.4 Create global parameter objects
params theta, theta_p1;
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// Priors on global parameter
////////////////////////////
// Ab_bg; background Ab level
theta.prior_MM[0] = 0.001; // Mean value of the parameter in the population
theta.prior_MM_CV[0] = 0.5; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[0] = 0.005; // Between Person standard deviation
theta.prior_SIG_CV[0] = 0.33; // Coefficient of variation in Between Person sd
////////////////////////////
// beta_mild; B cell boost
theta.prior_MM[1] = 0.001; // Mean value of the parameter in the population
theta.prior_MM_CV[1] = 2; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[1] = 0.01; // Between Person standard deviation
theta.prior_SIG_CV[1] = 0.5; // Coefficient of variation in Between Person sd
///////////////////////////
// tau; half-life of memory B cells
theta.prior_MM[2] = 9.6; // Mean value of the parameter in the population
theta.prior_MM_CV[2] = 0.25; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[2] = 4.2; // Between Person standard deviation
theta.prior_SIG_CV[2] = 0.33; // Coefficient of variation in Between Person sd
///////////////////////////
// t_delta; half-life of memory B cells
theta.prior_MM[3] = 2.0; // Mean value of the parameter in the population
theta.prior_MM_CV[3] = 0.25; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[3] = 1.8; // Between Person standard deviation
theta.prior_SIG_CV[3] = 0.33; // Coefficient of variation in Between Person sd
///////////////////////////
// t_short; half-life of short-lived component
theta.prior_MM[4] = 2.5; // Mean value of the parameter in the population
theta.prior_MM_CV[4] = 0.25; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[4] = 1.3; // Between Person standard deviation
theta.prior_SIG_CV[4] = 0.33; // Coefficient of variation in Between Person sd
////////////////////////////
// t_long; half-life of long-lived component
theta.prior_MM[5] = long_half; // Mean value of the parameter in the population
theta.prior_MM_CV[5] = 0.05; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[5] = 100.0; // Between Person standard deviation
theta.prior_SIG_CV[5] = 0.33; // Coefficient of variation in Between Person sd
////////////////////////////
// t_IgG; half-life of IgG
theta.prior_MM[6] = 21.0; // Mean value of the parameter in the population
theta.prior_MM_CV[6] = 0.02; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[6] = 3.0; // Between Person standard deviation
theta.prior_SIG_CV[6] = 0.33; // Coefficient of variation in Between Person sd
////////////////////////////
// rho; proportion of short-lived component at start
theta.prior_MM[7] = 0.9; // Mean value of the parameter in the population
theta.prior_MM_CV[7] = 0.075; // Coefficient of variation in estimate of population - level parameter
theta.prior_SIG[7] = 0.05; // Between Person standard deviation
theta.prior_SIG_CV[7] = 0.25; // Coefficient of variation in Between Person sd
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// Transformation of priors
for (int p = 0; p < 7; p++)
{
theta.prior_LN_MM[p] = log( theta.prior_MM[p] / sqrt(1.0 + pow(theta.prior_SIG[p] / theta.prior_MM[p], 2.0)) );
theta.prior_LN_MM_CV[p] = theta.prior_MM_CV[p];
theta.prior_LN_SIG[p] = sqrt(log( 1.0 + pow(theta.prior_SIG[p] / theta.prior_MM[p], 2.0) ));
theta.prior_LN_SIG_CV[p] = theta.prior_SIG_CV[p];
}
theta.prior_LN_MM[7] = 2.3046092;
theta.prior_LN_MM_CV[7] = 2.5*theta.prior_MM_CV[7];
theta.prior_LN_SIG[7] = 0.5386774;
theta.prior_LN_SIG_CV[7] = 2.5*theta.prior_SIG_CV[7];
for (int p = 0; p < N_glob_par; p++)
{
theta.prior_mu[p] = theta.prior_LN_MM[p];
theta.prior_tau[p] = 1.0 / pow(theta.prior_LN_MM[p] * theta.prior_LN_MM_CV[p], 2.0);
theta.prior_k[p] = 1.0 / pow(2.0*theta.prior_LN_SIG_CV[p], 2.0);
theta.prior_theta[p] = pow(2.0*theta.prior_LN_SIG_CV[p] / theta.prior_LN_SIG[p], 2.0);
}
for (int p = 0; p < N_glob_par; p++)
{
theta.mu_par[p] = genunf(0.9, 1.1)*theta.prior_mu[p];
theta.tau_par[p] = 0.1*genunf(0.9, 1.1)*theta.prior_k[p] * theta.prior_theta[p];
}
for (int p = 0; p < N_glob_par; p++)
{
cout << theta.mu_par[p] << "\t" << theta.tau_par[p] << endl;
}
cout << endl;
theta.sig_obs = genunf(0.9, 1.1); // Precision of observational error
theta.log_sig_obs = log(theta.sig_obs);
theta.sig_obs_scale = 1.0;
theta.AB_min = 1.95e-6;
theta.AB_max = 0.02;
theta.lAB_min = log(theta.AB_min);
theta.lAB_max = log(theta.AB_max);
//////////////////////////////////////////////////////////
// 1.5 Create individual-level objects for participant n
part_n* part;
part = new part_n[N_part + N_negcon];
//////////////////////////////////////////////////////////
// 1.6.1 Infected individuals
for (int n = 0; n<(N_part+N_negcon); n++)
{
part[n].site = AB_data_read[n][0];
part[n].status = AB_data_read[n][1];
part[n].symptoms = AB_data_read[n][2];
/////////////////////////////////////////////////
// Fill antibody data
part[n].N_sam = 0;
for (int j = 0; j<N_t; j++)
{
if (AB_data_read[n][3 + j] > -0.5)
{
part[n].tt.push_back(AB_data_read[n][3 + j]);
part[n].AB.push_back(AB_data_read[n][3 + N_t + j]);
part[n].lAB.push_back(log(AB_data_read[n][3 + N_t + j]));
part[n].N_sam = part[n].N_sam + 1;
}
}
part[n].AB_min = 1e10;
part[n].AB_max = 1e-10;
for (int j = 0; j < part[n].N_sam; j++)
{
if (part[n].AB[j] < part[n].AB_min)
{
part[n].AB_min = part[n].AB[j];
}
}
for (int j = 0; j < part[n].N_sam; j++)
{
if (part[n].AB[j] > part[n].AB_max)
{
part[n].AB_max = part[n].AB[j];
}
}
//cout << n << "\t" << part[n].AB_min << "\t" << part[n].AB_max << endl;
part[n].lAB_min = log(part[n].AB_min);
part[n].lAB_max = log(part[n].AB_max);
/////////////////////////////////////////////////
// Randomly assign individual-level parameters
if (part[n].status == 2)
{
part[n].Ab_bg = genunf(2e-5, 3e-5);
part[n].beta = genunf(0.00001, 0.00005);
part[n].tau = genunf(0.1, 5.0);
part[n].t_delta = genunf(5.0, 10.0);
part[n].t_short = genunf(5.0, 10.0);
part[n].t_long = genunf(500.0, 1500.0);
part[n].t_IgG = genunf(10.0, 30.0);
part[n].rho = genunf(0.8, 0.9);
part[n].lAb_bg = log(part[n].Ab_bg);
part[n].ltau = log(part[n].tau);
part[n].lbeta = log(part[n].beta);
part[n].lt_delta = log(part[n].t_delta);
part[n].lt_short = log(part[n].t_short);
part[n].lt_long = log(part[n].t_long);
part[n].lt_IgG = log(part[n].t_IgG);
part[n].r_delta = log2 / part[n].t_delta;
part[n].r_short = log2 / part[n].t_short;
part[n].r_long = log2 / part[n].t_long;
part[n].r_IgG = log2 / part[n].t_IgG;
part[n].logitrho = log(part[n].rho / (1.0 - part[n].rho));
}
if (part[n].status == 1)
{
part[n].Ab_bg = genunf(2e-5, 3e-5);
part[n].beta = 0.01;
part[n].tau = 2.0;
part[n].t_delta = 2.0;
part[n].t_short = 10.0;
part[n].t_long = 100.0;
part[n].t_IgG = 10.0;
part[n].rho = 0.2;
part[n].lAb_bg = log(part[n].Ab_bg);
part[n].ltau = log(part[n].tau);
part[n].lbeta = log(part[n].beta);
part[n].lt_delta = log(part[n].t_delta);
part[n].lt_short = log(part[n].t_short);
part[n].lt_long = log(part[n].t_long);
part[n].lt_IgG = log(part[n].t_IgG);
part[n].r_delta = log2 / part[n].t_delta;
part[n].r_short = log2 / part[n].t_short;
part[n].r_long = log2 / part[n].t_long;
part[n].r_IgG = log2 / part[n].t_IgG;
part[n].logitrho = log(part[n].rho / (1.0 - part[n].rho));
}
/////////////////////////////////////////////////
// Calculate individual-level likelihood
part[n].data_like = data_like_n(&part[n], &theta);
part[n].mix_like = mix_like_n(&part[n], &theta);
part[n].lhood = part[n].data_like + part[n].mix_like;
}
AB_data_read.clear();
double NC_max = 0.0;
for (int n = 0; n < (N_part + N_negcon); n++)
{
if (part[n].status == 1)
{
if (part[n].AB[0] > NC_max)
{
NC_max = part[n].AB[0];
}
}
}
for (int n = 0; n < (N_part + N_negcon); n++)
{
part[n].lNC_max = log(1.1*NC_max);
}
//////////////////////////////////////////////////////
// 1.7 Initialise adaptive MCMC object for individual-level parameters
// One object for each participant.
part_n_MCMC* part_MCMC;
part_MCMC = new part_n_MCMC[N_part + N_negcon];
for (int n = 0; n<(N_part + N_negcon); n++)
{
///////////////////////////////////
// Parameter vector for MVN update
part_MCMC[n].par_vec[0] = part[n].lAb_bg;
part_MCMC[n].par_vec[1] = part[n].lbeta;
part_MCMC[n].par_vec[2] = part[n].ltau;
part_MCMC[n].par_vec[3] = part[n].lt_delta;
part_MCMC[n].par_vec[4] = part[n].lt_short;
part_MCMC[n].par_vec[5] = part[n].lt_long;
part_MCMC[n].par_vec[6] = part[n].lt_IgG;
part_MCMC[n].par_vec[7] = part[n].logitrho;
/////////////////////////////
// Initialise diagonal covariance matrix
for (int p = 0; p<N_loc_par; p++)
{
for (int q = 0; q<N_loc_par; q++)
{
part_MCMC[n].COV_MAT[p][q] = 0.0;
}
}
part_MCMC[n].COV_MAT[0][0] = 0.2*0.2;
part_MCMC[n].COV_MAT[1][1] = 0.2*0.2;
part_MCMC[n].COV_MAT[2][2] = 0.2*0.2;
part_MCMC[n].COV_MAT[3][3] = 0.2*0.2;
part_MCMC[n].COV_MAT[4][4] = 0.2*0.2;
part_MCMC[n].COV_MAT[5][5] = 0.2*0.2;
part_MCMC[n].COV_MAT[6][6] = 0.2*0.2;
part_MCMC[n].COV_MAT[7][7] = 0.2*0.2;
/////////////////////////////
// Counting moments
for (int p = 0; p<N_loc_par; p++)
{
part_MCMC[n].par_S1[p] = part_MCMC[n].par_vec[p];
for (int q = 0; q<N_loc_par; q++)
{
part_MCMC[n].par_S2[p][q] = part_MCMC[n].par_vec[p] * part_MCMC[n].par_vec[q];
}
}
part_MCMC[n].denom = 1;
/////////////////////////////
// Set up dummy covariance matrix including
// step-size scaling
part_MCMC[n].step_scale = 0.1;
for (int p = 0; p<N_loc_par; p++)
{
for (int q = 0; q<N_loc_par; q++)
{
part_MCMC[n].COV_MAT_dummy[p][q] = part_MCMC[n].step_scale*part_MCMC[n].COV_MAT[p][q];
}
}
part_MCMC[n].accept = 0.0;
}
////////////////////////////////////////////////////////
// 1.8 Book-keeping
for (int p = 0; p < N_glob_par; p++)
{
theta.Y_par[p] = 0.0;
theta.Ymu2_par[p] = 0.0;
for (int n = 0; n < (N_part + N_negcon); n++)
{
theta.Y_par[p] = theta.Y_par[p] + part_MCMC[n].par_vec[p];
theta.Ymu2_par[p] = theta.Ymu2_par[p] + (part_MCMC[n].par_vec[p] - theta.mu_par[p])*(part_MCMC[n].par_vec[p] - theta.mu_par[p]);
}
}
theta_p1 = theta;
////////////////////////////////////////////////////////
// 1.9 Create objects for updating local parameters
part_n* part_p1;
part_p1 = new part_n[N_part + N_negcon];
for (int n = 0; n<(N_part + N_negcon); n++)
{
part_p1[n] = part[n];
}
///////////////////////////////////////////////////////////////////////////
// 1.10 Test output of likelihood
for (int n = 0; n < (N_part + N_negcon); n++)
{
cout << n << "\t" << "Ab_bg: " << part[n].lAb_bg << "\t" << "beta: " << part[n].lbeta << "\t"
<< "tau: " << part[n].ltau << "\t" << "t_delta: " << part[n].lt_delta << "\t" << "t_short: " << part[n].lt_short << "\t" << "t_long: " << part[n].lt_long << "\t" << "t_IgG: " << part[n].lt_IgG << "\t"
<< "rho: " << part[n].logitrho << "\t" << "logL: " << part[n].lhood << endl;
//cout << "N_sam = " << part[n].N_sam << "\t" << part[n].AB[0] << "\t" << part[n].lAB[0] << endl;
//system("PAUSE");
}
///////////////////////////////////////////////////////////////////////////
// 1.11 Initialise parameters for MCMC likelihood, Robbins-Munro
// acceptance and output
double loglike = global_prior(&theta);
for (int n = 0; n<(N_part + N_negcon); n++)
{
loglike = loglike + part[n].lhood;
}
double log_prob, loglike_p1;
double log_loc_prob[N_part + N_negcon];
int glob_out = max(2, (int)((int)N_mcmc) / 10000);
int loc_out = max(2, (int)((int)N_mcmc) / 1000);
vector<double> randomU(N_part + N_negcon);
vector<double> loglike_vec_p1(N_part + N_negcon);
///////////////////////////////////////////////////////////////////////////
// 1.12 Open file for output and write first line
std::cout << "START MCMC" << endl;
cout << 0 << "\t";
for (int p = 0; p < N_glob_par; p++)
{
cout << theta.mu_par[p] << "\t";
}
for (int p = 0; p < N_glob_par; p++)
{
cout << theta.tau_par[p] << "\t";
}
cout << theta.sig_obs << "\t" << loglike << "\t" << global_prior(&theta) << endl;
std::ofstream global_MCMC_Stream(global_output_File);
for (int p = 0; p < N_glob_par; p++)
{
global_MCMC_Stream << theta.mu_par[p] << "\t";
}
for (int p = 0; p < N_glob_par; p++)
{
global_MCMC_Stream << theta.tau_par[p] << "\t";
}
global_MCMC_Stream << theta.sig_obs << "\t" << loglike << "\t" << global_prior(&theta) << endl;
std::ofstream local_MCMC_Stream(local_output_File);
for (int n = 0; n<(N_part + N_negcon); n++)
{
local_MCMC_Stream << part[n].Ab_bg << "\t" << part[n].beta << "\t" <<
part[n].tau << "\t" << part[n].t_delta << "\t" << part[n].t_short << "\t" << part[n].t_long << "\t" << part[n].t_IgG << "\t" <<
part[n].rho << "\t" << part[n].lhood << "\t";
}
local_MCMC_Stream << endl;
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
// // //
// #### // Begin MCMC fitting procedure //
// ## ## // //
// ## // //
// ## // //
// ##### // //
// // //
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
for (int mc = 1; mc<N_mcmc; mc++)
{
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// // //
// 2.1 // UPDATE STAGE 1: INDIVIDUAL-LEVEL //
// // Metropolis-Hastings sampler //
// // //
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/////////////////////////////////////////////
// 2.1.1. Proposal step
for (int n = 0; n < (N_part + N_negcon); n++)
{
////////////////////////////////////////////////
// Update COV_MAT_dummay
for (int p = 0; p < N_loc_par; p++)
{
for (int q = 0; q < N_loc_par; q++)
{
part_MCMC[n].COV_MAT_dummy[p][q] = part_MCMC[n].step_scale*part_MCMC[n].COV_MAT[p][q];
}
}
///////////////////////////////////////////////
// Multi-variate Normal proposal step
setgmn(part_MCMC[n].par_vec, *part_MCMC[n].COV_MAT_dummy, N_loc_par, part_MCMC[n].GMN_parm);
genmn(part_MCMC[n].GMN_parm, part_MCMC[n].par_vec_test, part_MCMC[n].work);
part_p1[n].lAb_bg = part_MCMC[n].par_vec_test[0];
if( part[n].status == 2)
{
part_p1[n].lbeta = part_MCMC[n].par_vec_test[1];
part_p1[n].ltau = part_MCMC[n].par_vec_test[2];
part_p1[n].lt_delta = part_MCMC[n].par_vec_test[3];
part_p1[n].lt_short = part_MCMC[n].par_vec_test[4];
part_p1[n].lt_long = part_MCMC[n].par_vec_test[5];
part_p1[n].lt_IgG = part_MCMC[n].par_vec_test[6];
part_p1[n].logitrho = part_MCMC[n].par_vec_test[7];
}
randomU[n] = genunf(0.0, 1.0);
}
/////////////////////////////////////////////
// 2.1.2. Update step
//#pragma omp parallel for schedule(dynamic,4)
for (int n = 0; n < (N_part + N_negcon); n++)
{
////////////////////////////////////////////////////////
// 2.1.2.1. Only proceed if allowable parameters proposed
if (local_prior(&part_p1[n]) > -0.5*LARGE)
{
part_p1[n].Ab_bg = exp(part_p1[n].lAb_bg);
if (part[n].status == 2)
{
part_p1[n].beta = exp(part_p1[n].lbeta);
part_p1[n].tau = exp(part_p1[n].ltau);
part_p1[n].t_delta = exp(part_p1[n].lt_delta);
part_p1[n].r_delta = log2 / part_p1[n].t_delta;
part_p1[n].t_short = exp(part_p1[n].lt_short);
part_p1[n].r_short = log2 / part_p1[n].t_short;
part_p1[n].t_long = exp(part_p1[n].lt_long);
part_p1[n].r_long = log2 / part_p1[n].t_long;
part_p1[n].t_IgG = exp(part_p1[n].lt_IgG);
part_p1[n].r_IgG = log2 / part_p1[n].t_IgG;
part_p1[n].rho = exp(part_p1[n].logitrho) / (1.0 + exp(part_p1[n].logitrho));
}
part_p1[n].data_like = data_like_n(&part_p1[n], &theta);
part_p1[n].mix_like = mix_like_n(&part_p1[n], &theta);
part_p1[n].lhood = part_p1[n].data_like + part_p1[n].mix_like;
double log_prob_n = part_p1[n].lhood - part[n].lhood;
log_loc_prob[n] = _finite(log_prob_n) ? std::min(log_prob_n, 0.0) : -LARGE;
////////////////////////////////////////
// 2.1.2.2. Update if necessary
if (log(randomU[n]) < log_loc_prob[n])
{
part[n] = part_p1[n];
for (int p = 0; p < N_loc_par; p++)
{
part_MCMC[n].par_vec[p] = part_MCMC[n].par_vec_test[p];
}
part_MCMC[n].accept = part_MCMC[n].accept + 1;
}
////////////////////////////////////////
// 2.1.3. Adjust step-size with Robbins-Monro
// Only do this for a local step within allowed range
if (mc < N_adapt)
{
part_MCMC[n].step_scale = rm_scale(part_MCMC[n].step_scale, mc, N_adapt, log_loc_prob[n]);
}
}
////////////////////////////////////////////////////////////
// Running account of sums and sums of squares
if (mc < N_tune_end)
{
for (int p = 0; p < N_loc_par; p++)
{
part_MCMC[n].par_S1[p] = part_MCMC[n].par_S1[p] + part_MCMC[n].par_vec[p];
for (int q = 0; q < N_loc_par; q++)
{
part_MCMC[n].par_S2[p][q] = part_MCMC[n].par_S2[p][q] + part_MCMC[n].par_vec[p] * part_MCMC[n].par_vec[q];
}
}
part_MCMC[n].denom = part_MCMC[n].denom + 1;
}
////////////////////////////////////////////////////////////
// 2.1.4. TUNING STAGE 1
/////////////////////////////////
// Update covariance matrix
if ((mc >= N_tune_start) && (mc < N_tune_end))
{
for (int p = 0; p < N_loc_par; p++)
{
for (int q = 0; q < N_loc_par; q++)
{
if (part_MCMC[n].accept / part_MCMC[n].denom > 0.01)
{
part_MCMC[n].COV_MAT[p][q] = part_MCMC[n].par_S2[p][q] / (part_MCMC[n].denom) - part_MCMC[n].par_S1[p] * part_MCMC[n].par_S1[q] / (part_MCMC[n].denom*part_MCMC[n].denom);
}
}
}
}
}
//////////////////////////////////////////////////////////////
// 2.1.5. Update the total likelihood given the local updates
loglike = global_prior(&theta);
for (int n = 0; n < (N_part + N_negcon); n++)
{
loglike = loglike + part[n].lhood;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// // //
// 2.2 // UPDATE STAGE 2 - POPULATION-LEVEL //
// // Gibbs sampler //
// // //
///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Ab_bg
theta.Y_par[0] = 0.0;
for (int n = 0; n < (N_part + N_negcon); n++)
{
theta.Y_par[0] = theta.Y_par[0] + part_MCMC[n].par_vec[0];
}
theta.mu_par[0] = gennor( (theta.prior_tau[0] * theta.prior_mu[0] + theta.tau_par[0] * theta.Y_par[0]) / (theta.prior_tau[0] + (N_part + N_negcon)* theta.tau_par[0]),
1.0 / sqrt(theta.prior_tau[0] + (N_part + N_negcon)* theta.tau_par[0]));
theta.Ymu2_par[0] = 0.0;
for (int n = 0; n < (N_part + N_negcon); n++)
{
theta.Ymu2_par[0] = theta.Ymu2_par[0] + (part_MCMC[n].par_vec[0] - theta.mu_par[0])*(part_MCMC[n].par_vec[0] - theta.mu_par[0]);
}
theta.tau_par[0] = gengam( 1.0 / theta.prior_theta[0] + 0.5*theta.Ymu2_par[0],
0.5*(N_part + N_negcon) + theta.prior_k[0]);