forked from StochasticBiology/hypertraps-ct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypertraps-r.cpp
More file actions
1360 lines (1210 loc) · 40.3 KB
/
hypertraps-r.cpp
File metadata and controls
1360 lines (1210 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Rcpp.h>
using namespace Rcpp;
#define _USE_CODE_FOR_R 1
#include "hypertraps.c"
// function definitions -- for content see comments preceding each function
List PosteriorAnalysis(List L,
Nullable<CharacterVector> featurenames_arg,
Nullable<NumericVector> startstate,
int use_regularised,
int limited_output,
int samples_per_row,
int outputtransitions);
List RegulariseR(int *matrix,
int len, int ntarg, double *ntrans, double *tau1s, double *tau2s, int model, int PLI,
int limited_output);
List OutputStatesR(double *ntrans, int LEN, int model);
List HyperTraPS(NumericMatrix obs,
Nullable<NumericMatrix> initialstates,
Nullable<NumericMatrix> priors,
Nullable<NumericVector> starttimes,
Nullable<NumericVector> endtimes,
NumericVector length,
NumericVector kernel,
NumericVector losses,
NumericVector apm,
NumericVector sa,
NumericVector sgd,
NumericVector sgdscale,
NumericVector seed,
NumericVector outputinput,
NumericVector regularise,
NumericVector penalty,
NumericVector lasso,
NumericVector model,
NumericVector pli,
NumericVector walkers,
NumericVector full_analysis,
NumericVector output_transitions,
Nullable<CharacterVector> featurenames);
// R version of detailed output
// given a parameter set "ntrans" for a model of structure "model" and "LEN" features
// return a named list containing (a) probabilities of occupancy of each state and (b) transition rates and fluxes for each edge
List OutputStatesR(double *ntrans, int LEN, int model)
{
int i, j, k, a;
int statedec;
int src, dest;
int state[LEN];
double rate, totrate;
int *active, *newactive;
double *probs;
int nactive, newnactive;
int level;
int found;
// vectors for output
NumericVector state_v, prob_v, prob_dt_v;
NumericVector from_v, to_v, edgeprob_v, flux_v;
// allocate memory for statistics
probs = (double*)malloc(sizeof(double)*mypow2(LEN));
active = (int*)malloc(sizeof(int)*mypow2(LEN));
newactive = (int*)malloc(sizeof(int)*mypow2(LEN));
// "probs" will store state probabilities; level the "level" of the hypercube we're currently at
// "active" tracks which paths are currently under active calculation
for(i = 0; i < mypow2(LEN); i++)
probs[i] = 0;
level = 0;
// start with probability in 0^L and a single active path
probs[0] = 1;
active[0] = 0;
nactive = 1;
// while we haven't crossed the whole cube
while(nactive > 0)
{
newnactive = 0;
// go through active paths
for(a = 0; a < nactive; a++)
{
// pull the state of this active path
src = active[a];
statedec = src;
for(j = LEN-1; j >= 0; j--)
{
if(statedec >= mypow2(j))
{
state[LEN-1-j] = 1;
statedec -= mypow2(j);
}
else
state[LEN-1-j] = 0;
}
// pull the transitions from this state
totrate = 0;
for(j = 0; j < LEN; j++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[j] == 0)
{
rate = RetrieveEdge(state, j, ntrans, LEN, model);
totrate += rate;
}
}
// go through each outgoing edge, outputting its transition rate (and the probability flux at this point)
// and spawning a new active path if the destination node doesn't already have one
for(j = 0; j < LEN; j++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[j] == 0)
{
dest = src+mypow2(LEN-1-j);
rate = RetrieveEdge(state, j, ntrans, LEN, model);
probs[dest] += probs[src] * rate/totrate;
from_v.push_back(src);
to_v.push_back(dest);
edgeprob_v.push_back(rate/totrate);
flux_v.push_back(probs[src]*rate/totrate);
found = 0;
for(k = 0; k < newnactive; k++)
{
if(newactive[k] == dest) { found = 1; break; }
}
if(found == 0)
newactive[newnactive++] = dest;
}
}
}
// update the list of active paths
for(a = 0; a < newnactive; a++)
active[a] = newactive[a];
nactive = newnactive;
level++;
}
// record the list of state occupancy probabilities
for(dest = 0; dest < mypow2(LEN); dest++)
{
state_v.push_back(dest);
prob_v.push_back(probs[dest]);
prob_dt_v.push_back(probs[dest]/(LEN+1));
}
// compile state occupancies into a named list
List L = List::create(Named("State") = state_v,
Named("Probability") = prob_v,
Named("Probability.DT") = prob_dt_v);
DataFrame Ldf(L);
// compile edge probabilities and fluxes into a named list
List Lflux = List::create(Named("From") = from_v,
Named("To") = to_v,
Named("Probability") = edgeprob_v,
Named("Flux") = flux_v);
DataFrame Lfluxdf(Lflux);
// compile overall list of results
List Lout = List::create(Named("states") = Ldf, Named("trans") = Lfluxdf);
free(active);
free(newactive);
free(probs);
return Lout;
}
// R version of stepwise regularisation
// stepwise regularise a parameter set by minimising likelihood loss as parameters are pruned
List RegulariseR(int *matrix, int len, int ntarg, double *ntrans, double *tau1s, double *tau2s, int model, int PLI, int limited_output)
{
int i, j;
int NVAL;
double lik, nlik;
double oldval;
int biggestindex;
double biggest;
int pcount;
double AIC, BIC, bestIC;
double *best;
double normedval;
if(model == -1) normedval = -20;
else normedval = 0;
// initialise the setup and estimate initial likelihood and ICs
NVAL = nparams(model, len);
best = (double*)malloc(sizeof(double)*NVAL);
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, ntrans, tau1s, tau2s, model, PLI);
AIC = 2*NVAL-2*lik;
BIC = log(ntarg)*NVAL-2*lik;
bestIC = AIC;
for(i = 0; i < NVAL; i++)
best[i] = ntrans[i];
// vectors for output of statistics
NumericVector NVAL_v, removed_v, lik_v, AIC_v, BIC_v;
NVAL_v.push_back(NVAL);
removed_v.push_back(-1);
lik_v.push_back(lik);
AIC_v.push_back(AIC);
BIC_v.push_back(BIC);
if(!limited_output)
Rprintf("Regularising...\npruning ");
// remove parameters stepwise
for(j = 0; j < NVAL; j++)
{
if(!limited_output)
Rprintf("%i of %i\n", j+1, NVAL);
// find parameter that retains highest likelihood when removed
biggest = 0;
for(i = 0; i < NVAL; i++)
{
if(ntrans[i] != normedval)
{
oldval = ntrans[i];
ntrans[i] = normedval;
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, ntrans, tau1s, tau2s, model, PLI);
ntrans[i] = oldval;
if((biggest == 0 || nlik > biggest))
{
biggest = nlik;
biggestindex = i;
}
}
}
// set this param to zero and count new param set
ntrans[biggestindex] = normedval;
pcount = 0;
for(i = 0; i < NVAL; i++)
{
if(ntrans[i] != normedval) pcount++;
}
// output
AIC = 2*pcount-2*biggest;
BIC = log(ntarg)*pcount-2*biggest;
NVAL_v.push_back(pcount);
removed_v.push_back(biggestindex);
lik_v.push_back(biggest);
AIC_v.push_back(AIC);
BIC_v.push_back(BIC);
if(AIC < bestIC)
{
bestIC = AIC;
for(i = 0; i < NVAL; i++)
best[i] = ntrans[i];
}
}
// compile statistics of the process into a named list
List Ldyn = List::create(Named("nparam") = NVAL_v,
Named("removed") = removed_v,
Named("lik") = lik_v,
Named("AIC") = AIC_v,
Named("BIC") = BIC_v);
DataFrame Ldyndf(Ldyn);
NumericVector best_v(NVAL);
for(i = 0; i < NVAL; i++)
{
best_v[i] = best[i];
ntrans[i] = best[i];
}
List Lout = List::create(Named("best") = best_v,
Named("lik.1") = GetLikelihoodCoalescentChange(matrix, len, ntarg, best, tau1s, tau2s, model, PLI),
Named("lik.2") = GetLikelihoodCoalescentChange(matrix, len, ntarg, best, tau1s, tau2s, model, PLI),
Named("reg.process") = Ldyndf);
free(best);
return Lout;
}
//' Runs HyperTraPS-related inference on a dataset of observations
//'
//' @param matrix A matrix of observations. Should contain 0s, 1s, and optional 2s for missing data. Should be $n \times L$, containing $n$ cross-sectional observations of length $L$.
//' @param initialstates An optional matrix of initial states. If we are using longitudinal observations, each row in this matrix gives the "before" state to the corresponding "after" state in the observations matrix. Omitting this matrix is equivalent to consider every observation to have a root "before" state. If specified, should be $n \times L$, containing $n$ cross-sectional observations of length $L$, to match the observations matrix.
//' @param start_times An optional vector of $n$ times describing the beginning of the observation time window for each observation. If empty, equivalent to this time window beginning at time 0. If specified, should be of length $n$.
//' @param end_times An optional vector of $n$ times describing the end of the observation time window for each observation. If empty, equivalent to this time window ending at time infinity. If specified, should be of length $n$.
//' @param length_index Length of MCMC chain
//' @param kernel_index Kernel index
//' @param losses Whether to consider accumulation of gains (0) or losses (1)
//' @param apm_type APM
//' @param sgd_scale SGD
//' @param seed Random number seed
//' @param outputinput Option to output the input data
//' @param regularise Regularise
//' @param model Model structure
//' @param pli Phenotype landscape inference
//' @return A named list of objects from the inference process, containing parameter samples from the inference process, the maximum likelihood parameterisation, likelihood samples, and the sampling times.
// [[Rcpp::export]]
List HyperTraPS(NumericMatrix obs, //NumericVector len_arg, NumericVector ntarg_arg,
Nullable<NumericMatrix> initialstates = R_NilValue,
Nullable<NumericMatrix> priors = R_NilValue,
Nullable<NumericVector> starttimes = R_NilValue,
Nullable<NumericVector> endtimes = R_NilValue,
NumericVector length = 3,
NumericVector kernel = 5,
NumericVector samplegap = -1,
NumericVector losses = 0,
NumericVector apm_type = 0,
NumericVector sa = 0,
NumericVector sgd = 0,
NumericVector sgd_scale = 0.01,
NumericVector seed = 1,
NumericVector outputinput = 0,
NumericVector regularise = 0,
NumericVector penalty = 0,
NumericVector lasso = 0,
NumericVector model = 2,
NumericVector pli = 0,
NumericVector walkers = 200,
NumericVector full_analysis = 1,
NumericVector limited_output = 0,
NumericVector output_transitions = 1,
NumericVector samples_per_row = 10,
Nullable<CharacterVector> featurenames = R_NilValue)
{
int *matrix;
int len, ntarg;
double *trans, *ntrans, *besttrans, *gradients;
int t;
int i, j;
double lik, nlik;
int maxt;
int _seed;
double DELTA, MU;
int NVAL;
int expt;
double acc, rej, lacc, lrej;
double *tmpmat;
double r;
double tau1s[_MAXN], tau2s[_MAXN];
int nancount = 0;
int spectrumtype;
double bestlik = 0;
double _lengthindex;
int _kernelindex;
int SAMPLE;
int _losses;
int apm_seed, old_apm_seed;
int _apm_type;
double testval;
char obsfile[1000], timefile[1000], endtimefile[1000], paramfile[1000];
int searchmethod;
int filelabel;
char labelstr[1000];
int _crosssectional;
time_t start_t, end_t;
double diff_t;
struct timeval t_stop, t_start;
int _outputinput;
double _sgdscale;
int _model;
int _regularise;
int _outputtransitions;
int readparams;
int _PLI;
int _limited_output;
int _samples_per_row;
double _penalty;
int _lasso;
int regterm;
double lassoterm;
int _samplegap;
// default values
num_error = 0;
spectrumtype = 0;
_lengthindex = length[0];
_kernelindex = kernel[0];
_losses = losses[0];
_apm_type = apm_type[0];
_sgdscale = sgd_scale[0];
_samplegap = samplegap[0];
filelabel = 0;
_seed = seed[0];
searchmethod = 0;
BANK = walkers[0];
_limited_output = limited_output[0];
_penalty = penalty[0];
_lasso = lasso[0];
if(sgd[0] == 1) searchmethod = 1;
if(sa[0] == 1) searchmethod = 2;
_outputinput = outputinput[0];
_regularise = regularise[0];
_model = model[0];
readparams = 0;
_PLI = pli[0];
_outputtransitions = output_transitions[0];
_samples_per_row = samples_per_row[0];
strcpy(obsfile, "rcpp");
strcpy(paramfile, "");
strcpy(timefile, "");
strcpy(endtimefile, "");
// basic input parsing
len = obs.ncol();
ntarg = obs.nrow()*2;
// construct internal observation matrix
matrix = (int*)malloc(sizeof(int)*len*ntarg);
// check to see if we're doing crosssectional analysis, and if not, if we've got appropriate initial state info
_crosssectional = 1;
if(initialstates.isUsable()) {
NumericMatrix _initialstates(initialstates);
_crosssectional = 0;
if(_initialstates.ncol() != len || _initialstates.nrow() != ntarg/2)
{
Rprintf("If specifying initial states, we need one initial state for each observation.");
myexit(0);
}
for(i = 0; i < ntarg/2; i++)
{
for(j = 0; j < len; j++)
matrix[i*(2*len)+j] = (_losses != 1 || _initialstates(i,j) == 2 ? _initialstates(i, j) : 1 - _initialstates(i,j));
for(j = 0; j < len; j++)
matrix[i*(2*len)+len+j] = (_losses != 1 || obs(i,j) == 2 ? obs(i,j) : 1 - obs(i,j));
}
}
else {
for(i = 0; i < ntarg/2; i++)
{
for(j = 0; j < len; j++)
matrix[i*(2*len)+j] = 0;
for(j = 0; j < len; j++)
matrix[i*(2*len)+len+j] = (_losses != 1 || obs(i,j) == 2 ? obs(i,j) : 1 - obs(i,j));
}
}
// populate timing vectors
if(starttimes.isUsable()) {
NumericVector _starttimes(starttimes);
if(_starttimes.length() != ntarg/2) {
Rprintf("If specifying start timings, we need one timing entry for each observation.");
myexit(0);
}
for(i = 0; i < ntarg/2; i++)
tau1s[i] = _starttimes[i];
spectrumtype = 1;
}
if(endtimes.isUsable()) {
NumericVector _endtimes(endtimes);
if(_endtimes.length() != ntarg/2) {
Rprintf("If specifying end timings, we need one timing entry for each observation.");
myexit(0);
}
for(i = 0; i < ntarg/2; i++)
tau2s[i] = _endtimes[i];
spectrumtype = 1;
}
if(spectrumtype == 1)
{
if(!starttimes.isNotNull()) {
Rprintf("End timings, but not start timings, specified. Assuming t = 0 starts.\n");
for(i = 0; i < ntarg/2; i++)
tau1s[i] = 0;
}
if(!endtimes.isNotNull()) {
Rprintf("Start timings, but not end timings, specified. Assuming that start times *are* end timings (i.e. precisely specified times).\n");
for(i = 0; i < ntarg/2; i++)
tau2s[i] = tau1s[i];
}
for(i = 0; i < ntarg/2; i++)
{
if(tau2s[i] < tau1s[i])
{
Rprintf("End time %i is less than start time!\n", i);
myexit(0);
}
}
}
else
{
for(i = 0; i < ntarg/2; i++)
{
tau1s[i] = 0;
tau2s[i] = INFINITY;
}
}
if(!_limited_output)
{
Rprintf("\nHyperTraPS(-CT)\n\nPlease cite Aga et al., PLoS Comput Biol 20 e1012393 (2024)\n\n");
if(_PLI == 1) {
Rprintf("Running Phenotype Landscape Inference with:\n[observations-file]: %s\n[start-timings-file]: %s\n[end-timings-file]: %s\n[random number seed]: %.2f\n[length index]: %i\n[kernel index]: %i\n[walkers]: %i\n[losses (1) or gains (0)]: %i\n[APM]: %i\n[model]: %i\n[penalty]: %.3e\n[lasso]: %i\n\n", obsfile, timefile, endtimefile, _seed, _lengthindex, _kernelindex, BANK, _losses, _apm_type, _model, _penalty, _lasso);
} else if(spectrumtype == 1) {
Rprintf("Running HyperTraPS-CT with:\n[observations-file]: %s\n[start-timings-file]: %s\n[end-timings-file]: %s\n[random number seed]: %.2f\n[length index]: %i\n[kernel index]: %i\n[walkers]: %i\n[losses (1) or gains (0)]: %i\n[APM]: %i\n[model]: %i\n[penalty]: %.3e\n[lasso]: %i\n\n", obsfile, timefile, endtimefile, _seed, _lengthindex, _kernelindex, BANK, _losses, _apm_type, _model, _penalty, _lasso);
} else {
Rprintf("Running HyperTraPS with:\n[observations-file]: %s\n[random number seed]: %.2f\n[length index]: %i\n[kernel index]: %i\n[walkers]: %i\n[losses (1) or gains (0)]: %i\n[APM]: %i\n[model]: %i\n[penalty]: %.3e\n[lasso]: %i\n\n", obsfile, _seed, _lengthindex, _kernelindex, BANK, _losses, _apm_type, _model, _penalty, _lasso);
}
if(_penalty != 0 && _lasso != 0) {
Rprintf("*** NOTE -- you have specified both a likelihood penalty and a LASSO regularisation -- you probably don't want to do both of these together!\n");
}
switch(searchmethod) {
case 0: Rprintf("Using MH MCMC\n"); break;
case 1: Rprintf("Using SGD\n"); break;
case 2: Rprintf("Using SA\n"); break;
}
}
// initialise and allocate
maxt = pow(10, _lengthindex);
if(_samplegap == -1) {
SAMPLE = 1000;
if(maxt <= 10000) SAMPLE = 100;
if(maxt <= 100) SAMPLE = 1;
} else {
SAMPLE = _samplegap;
}
if(_EVERYITERATION)
SAMPLE = 1;
RNDSEED(_seed);
// choose parameterisation based on command line
expt = _kernelindex;
switch(expt)
{
case 0: DELTA = 0; break;
case 1: DELTA = 0.005; MU = 0.1; break;
case 2: DELTA = 0.01; MU = 1.; break;
case 3: DELTA = 0.05; MU = 1.; break;
case 4: DELTA = 0.1; MU = 1.; break;
case 5: DELTA = 0.25; MU = 1.; break;
case 6: DELTA = 0.5; MU = 1.; break;
default: DELTA = 0.75; MU = 1.; break;
}
NVAL = nparams(_model, len);
NumericMatrix _priors(NVAL,2);
if(priors.isUsable())
{
NumericMatrix tmpM(priors);
if(tmpM.ncol() != 2 || tmpM.nrow() != NVAL)
{
Rprintf("Prior matrix has the wrong dimensions -- need 2 columns and NPARAM rows\n");
myexit(0);
}
_priors = tmpM;
}
else
{
NumericMatrix tmpM(NVAL, 2);
for(i = 0; i < NVAL; i++)
{
tmpM(i,0) = -10;
tmpM(i,1) = 10;
}
_priors = tmpM;
}
if(_outputinput)
{
Rprintf("Observed transitions:\n");
for(i = 0; i < ntarg/2; i++)
{
Rprintf("%i: ", i);
for(j = 0; j < len; j++) Rprintf("%i", matrix[2*len*i+j]);
Rprintf(" -> ");
for(j = 0; j < len; j++) Rprintf("%i", matrix[2*len*i+len+j]);
if(spectrumtype != 0)
Rprintf("(window %.3e-%.3e)", tau1s[i], tau2s[i]);
Rprintf("\n");
}
if(_losses == 1) Rprintf("(where 1 is absence)\n\n");
if(_losses == 0) Rprintf("(where 1 is presence)\n\n");
}
if(!_limited_output)
{
if(spectrumtype == 0)
{
Rprintf("Number of features is %i, I found %i observation pairs\n", len, ntarg/2);
}
else
{
Rprintf("Number of features is %i, I found %i observation pairs and %i timing pairs\n", len, ntarg/2, ntarg/2);
if(len > 30)
{
Rprintf("*** CAUTION: continuous time calculations sometimes fail to converge for large (>30) feature sets. This can lead to NaNs appearing, which will stop the simulation. Consider running without continuous time option.\n");
}
}
Rprintf("\n");
}
if(len > 15 && _outputtransitions == 1)
{
if(!_limited_output)
Rprintf("*** More than 15 features, meaning we'd need a lot of space to output transition and state information. I'm switching off this output.\n");
_outputtransitions = 0;
}
// allocate memory and initialise output file
trans = (double*)malloc(sizeof(double)*NVAL);
ntrans = (double*)malloc(sizeof(double)*NVAL);
besttrans = (double*)malloc(sizeof(double)*NVAL);
gradients = (double*)malloc(sizeof(double)*NVAL);
tmpmat = (double*)malloc(sizeof(double)*NVAL);
if(filelabel == 0)
{
sprintf(labelstr, "%s-%i-%i-%i-%.2f-%i-%i-%i", obsfile, spectrumtype, searchmethod, _seed, _lengthindex, _kernelindex, BANK, _apm_type);
}
// initialise with an agnostic transition matrix
if(readparams == 1)
{
if(!_limited_output)
Rprintf("Starting with supplied parameterisation\n");
ReadMatrix(trans, len, _model, paramfile);
}
else if(priors.isUsable())
{
if(!_limited_output)
Rprintf("Starting with centre of priors\n");
for(i = 0; i < NVAL; i++)
trans[i] = (_priors(i,0)+_priors(i,1))/2;
}
else {
if(!_limited_output)
Rprintf("Starting with simple initial param guess\n");
InitialMatrix(trans, len, _model, 0);
}
// compute initial likelihood given this matrix
time(&start_t);
gettimeofday(&t_start, NULL);
// count nonzero parameters for likelihood penalisation
regterm = lassoterm = 0;
for(i = 0; i < NVAL; i++)
{
regterm += (trans[i] != 0);
lassoterm += fabs(trans[i]);
}
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
time(&end_t);
gettimeofday(&t_stop, NULL);
diff_t = (t_stop.tv_sec - t_start.tv_sec) + (t_stop.tv_usec-t_start.tv_usec)/1.e6;
Rprintf("One likelihood estimation took %e seconds.\nInitial likelihood is %e\n", diff_t, lik);
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
Rprintf("Second guess is %e\n", lik);
// MCMC or simulated annealing
if(searchmethod == 0 || searchmethod == 2)
{
Rprintf("This code (%i steps) will probably take around %.3f seconds (%.3f hours) to complete.\n\n", maxt, diff_t*maxt, diff_t*maxt/3600.);
}
if(isinf(lik))
{
Rprintf("Start parameterisation gave a nonsensical likelihood. I'm going to try random alternatives.\n");
if(_PLI) {
Rprintf("With PLI, this often means we're not using enough random walkers to hit every datapoint on the hypercube. If this takes a while to find a suitable start parameterisation, consider re-running with more random walkers.\n");
}
i = 0;
do{
i++;
InitialMatrix(trans, len, _model, 1);
regterm = lassoterm = 0;
for(j = 0; j < NVAL; j++)
{
regterm += (trans[j] != 0);
lassoterm += fabs(trans[j]);
}
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
}while(isinf(lik) && i < 100);
if(i >= 100) {
Rprintf("I didn't find a sensible start within 100 steps. I suspect something's wrong numerically.\n");
myexit(0);
}
Rprintf("OK, starting with initial likelihood %e\n", lik);
}
// initialise counters for acceptance ratio
acc = rej = 0;
lacc = lrej = 0;
if(_apm_type == 1)
apm_seed = _seed;
int NSAMPLES;
if(searchmethod == 0)
NSAMPLES = (maxt-maxt/5)/SAMPLE-1;
else
NSAMPLES = 1;
NumericVector lik0_output, lik1_output, lik2_output, L_output, model_output, nparam_output, t_output;
NumericVector best_output(NVAL);
NumericMatrix posterior_output(NSAMPLES, NVAL);
int sampleref = 0;
List dynamics_output;
// run the chain
for(t = 0; t < maxt; t++)
{
// if we've got a new best likelihood, store it
if(lik > bestlik || t == 0)
{
for(i = 0; i < NVAL; i++)
best_output[i] = besttrans[i] = trans[i];
bestlik = lik;
if(_outputtransitions)
{
dynamics_output = OutputStatesR(besttrans, len, _model);
}
}
// output some info periodically
if(t % SAMPLE == 0)
Rprintf("%i - ", t);
if(t > maxt/5 && t % SAMPLE == 0)
{
regterm = lassoterm = 0;
// if we're burnt in, periodically sample the current parameterisation to an output file
// most appropriate for Bayesian MCMC but useful for all
for(i = 0; i < NVAL; i++)
{
posterior_output(sampleref, i) = trans[i];
regterm += (trans[i] != 0);
lassoterm += fabs(trans[i]);
}
// if MCMC, store a set of samples, otherwise the single best
if(searchmethod == 0)
sampleref++;
lik0_output.push_back(lik);
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
lik1_output.push_back(nlik);
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
lik2_output.push_back(nlik);
L_output.push_back(len);
model_output.push_back(_model);
nparam_output.push_back(regterm);
t_output.push_back(t);
}
// MCMC or simulated annealing
if(searchmethod == 0 || searchmethod == 2)
{
regterm = lassoterm = 0;
if(_apm_type == 0 || t%2 == 0)
{
// apply a perturbation to the existing parameterisation
// non-uniform priors can be employed here if desired
for(i = 0; i < NVAL; i++)
{
ntrans[i] = trans[i];
r = RND;
if(r < MU)
{
if((_penalty == 0 && _lasso == 0) || ntrans[i] != 0 || RND < 1./NVAL)
ntrans[i] += gsl_ran_gaussian(DELTA);
}
if((_penalty || _lasso) && RND < 1./NVAL)
ntrans[i] = 0;
if(ntrans[i] < _priors(i,0)) ntrans[i] = _priors(i,0);
if(ntrans[i] > _priors(i,1)) ntrans[i] = _priors(i,1);
regterm += (ntrans[i] != 0);
lassoterm += fabs(ntrans[i]);
}
if(APM_VERBOSE)
{
Rprintf("step 0 (change theta): apm_seed %i, ntrans[0] %f\n", apm_seed, ntrans[0]);
}
}
else
{
// change the random number seed and keep the parameterisation the same
old_apm_seed = apm_seed;
apm_seed = _seed+t;
for(i = 0; i < NVAL; i++)
ntrans[i] = trans[i];
if(APM_VERBOSE)
{
Rprintf("step 1 (change u): apm_seed %i, ntrans[0] %f\n", apm_seed, ntrans[0]);
}
}
// compute likelihood for the new parameterisation
if(_apm_type == 1)
{
RNDSEED(apm_seed);
if(APM_VERBOSE)
{
Rprintf("r seeded with %i, first call is %f\n", apm_seed, RND);
}
}
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, ntrans, tau1s, tau2s, _model, _PLI) - regterm*_penalty - lassoterm*_lasso;
if(APM_VERBOSE)
{
Rprintf("likelihood %f\n", nlik);
}
// keep track of NaNs in calculations
if(isnan(nlik))
{
nancount++;
}
testval = RND;
if(searchmethod == 2)
{
testval = 0.1*sqrt(sqrt(t));
}
// compare likelihood to previous
if(nlik >= lik || -(lik-nlik) > log(testval))
{
// accept this new parameterisation
lik = nlik;
if(_apm_type == 0 || t%2 == 0)
{
acc++; lacc++;
for(i = 0; i < NVAL; i++)
trans[i] = ntrans[i];
}
if(APM_VERBOSE)
{
Rprintf("accepted: apm_seed %i trans[0] %f\n\n", apm_seed, trans[0]);
}
}
else
{
// reject the change
if(_apm_type == 1 && t%2 == 1)
{
apm_seed = old_apm_seed;
}
else
{
rej++; lrej++;
}
if(APM_VERBOSE)
{
Rprintf("rejected: apm_seed %i trans[0] %f\n\n", apm_seed, trans[0]);
}
}
}
// gradient descent
if(searchmethod == 1)
{
time(&start_t);
gettimeofday(&t_start, NULL);
GetGradients(matrix, len, ntarg, trans, tau1s, tau2s, gradients, _sgdscale, _model, _PLI);
time(&end_t);
gettimeofday(&t_stop, NULL);
diff_t = (t_stop.tv_sec - t_start.tv_sec) + (t_stop.tv_usec-t_start.tv_usec)/1.e6;
if(t == 0 && !_limited_output)
Rprintf("Using SGD: one gradient calculation took %e seconds\n\n", diff_t);
for(i = 0; i < NVAL; i++)
{
trans[i] = trans[i]+gradients[i]*_sgdscale;
if(trans[i] < _priors(i,0)) trans[i] = _priors(i,0);
if(trans[i] > _priors(i,1)) trans[i] = _priors(i,1);
}
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, _model, _PLI);
if(!_limited_output)
Rprintf("Iteration %i likelihood %f previous-likelihood %f\n", t, nlik, lik);
lik = nlik;
}
// output information periodically
if(t % TMODULE == 0 && searchmethod != 1)
{
if(!_limited_output)
Rprintf("Iteration %i likelihood %f total-acceptance %f recent-acceptance %f trial-likelihood %f penalty %f,%f\n", t, lik, acc/(acc+rej), lacc/(lacc+lrej), nlik, regterm*_penalty, lassoterm*_lasso);
lacc = lrej = 0;
}
}
// compile named lists for output
List Lts = List::create(Named("Step") = t_output,
Named("L") = L_output,
Named("model") = model_output,
Named("nparam") = nparam_output,
Named("CurrentLogLikelihood") = lik0_output,
Named("LogLikelihood1") = lik1_output,
Named("LogLikelihood2") = lik2_output);
DataFrame Ltsdf(Lts);
List L = List::create(Named("label") = labelstr ,
Named("L") = len,
Named("model") = _model,
Named("best") = best_output,
Named("posterior.samples") = posterior_output,
Named("lik.traces") = Ltsdf);
if(_regularise)
{
List regL = RegulariseR(matrix, len, ntarg, besttrans, tau1s, tau2s, _model, _PLI, _limited_output);
L["regularisation"] = regL;
if(_outputtransitions)
{
dynamics_output = OutputStatesR(besttrans, len, _model);
}
}
if(_outputtransitions)
L["dynamics"] = dynamics_output;
if(full_analysis[0] == 0)
return L;
else
return PosteriorAnalysis(L, featurenames, R_NilValue, _regularise, _limited_output, _samples_per_row, _outputtransitions);
}
// R version of posterior analysis
//' Extracts information from HyperTraPS-related posterior samples
//'
//' @param L List output from HyperTraPS, containing posterior samples
//' @return Named list containing summary data for feature acquisition ordering ("bubbles"), time histograms, sampled accumulation routes, and timings of these sampled routes.
// [[Rcpp::export]]
List PosteriorAnalysis(List L,
Nullable<CharacterVector> featurenames = R_NilValue,
Nullable<NumericVector> startstate = R_NilValue,
int use_regularised = 0,
int limited_output = 0,
int samples_per_row = 10,
int outputtransitions = 0)
{
int *matrix;
int len, ntarg;
double *trans, *ntrans;
int t;
int prediction;
int i, j;
int *rec, *order;
double *drec, *sortdrec, *mean;
int allruns;
int seed = 0;
double tmp;
int change;
char names[200*FLEN];
int count;
double *meanstore, *fmeanstore;
double *ctrec, ctnorm;
double *times, *timediffs, *betas;
int *route;
int tlen;
int verbose;
double BINSCALE;
char postfile[1000];
int filelabel;
char labelstr[1000];
int NVAL;
int model;
int burnin, sampleperiod;
char labelfile[1000];
int *sstate;
double *predictrate, predictnorm = 0;
// default values
BINSCALE = 10;
verbose = 0;
filelabel = 0;
seed = 0;
prediction = 0;
model = L["model"];
burnin = 0;
sampleperiod = 0;
strcpy(postfile, "rcpp");
strcpy(labelfile, "");