[dfe06d]: / src / moves.cpp

Download this file

597 lines (361 with data), 17.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <Rcpp.h>
#include <Rmath.h>
#include "internals.h"
#include "likelihoods.h"
#include "priors.h"
// IMPORTANT: ON INDEXING VECTORS AND ANCESTRIES
// Most of the functions implemented here are susceptible to be called from R
// via Rcpp, and are therefore treated as interfaces. This causes a number of
// headaches when using indices of cases defined in R (1:N) to refer to elements
// in Rcpp / Cpp vectors (0:N-1). By convention, we store all data on the
// original scale (1:N), and modify indices whenever accessing elements of
// vectors. In other words, in an expression like 'alpha[j]', 'j' should always
// be on the internal scale (0:N-1).
// In all these functions, 'SEXP i' is an optional vector of case indices, on
// the 1:N scale.
// ---------------------------
// Movement of the mutation rate 'mu' is done using a dumb normal proposal. This
// is satisfying for now - we only reject a few non-sensical values outside the
// range [0;1]. The SD of the proposal (implicitely contained in rand$mu.rnorm1,
// but really provided through 'config', seems fine as the range of real values
// will never change much. Probably not much point in using auto-tuning here.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_mu(Rcpp::List param, Rcpp::List data, Rcpp::List config,
Rcpp::RObject custom_ll = R_NilValue,
Rcpp::RObject custom_prior = R_NilValue) {
// deep copy here for now, ultimately should be an arg.
Rcpp::List new_param = clone(param);
Rcpp::NumericVector mu = param["mu"];
Rcpp::NumericVector new_mu = new_param["mu"];
double sd_mu = static_cast<double>(config["sd_mu"]);
double old_logpost = 0.0, new_logpost = 0.0, p_accept = 0.0;
// proposal (normal distribution with SD: config$sd_mu)
new_mu[0] += R::rnorm(0.0, sd_mu); // new proposed value
// automatic rejection of negative mu
if (new_mu[0] < 0.0) {
return param;
}
// compute likelihoods
old_logpost = cpp_ll_genetic(data, param, R_NilValue, custom_ll);
new_logpost = cpp_ll_genetic(data, new_param, R_NilValue, custom_ll);
// compute priors
old_logpost += cpp_prior_mu(param, config, custom_prior);
new_logpost += cpp_prior_mu(new_param, config, custom_prior);
// acceptance term
p_accept = exp(new_logpost - old_logpost);
// acceptance: the new value is already in mu, so we only act if the move is
// rejected, in which case we restore the previous ('old') value
if (p_accept < unif_rand()) { // reject new values
return param;
}
return new_param;
}
// ---------------------------
// movement of the Reporting probability 'pi' is done using a dumb normal
// proposal. This is satisfying for now - we only reject a few non-sensical
// values outside the range [0;1]. The SD of the proposal (implicitely contained
// in rand$pi.rnorm1, but really provided through 'config', seems fine as the
// range of real values will never change much. Probably not much point in using
// auto-tuning here.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_pi(Rcpp::List param, Rcpp::List data, Rcpp::List config,
Rcpp::RObject custom_ll = R_NilValue,
Rcpp::RObject custom_prior = R_NilValue) {
// deep copy here for now, ultimately should be an arg.
Rcpp::List new_param = clone(param);
Rcpp::NumericVector pi = param["pi"]; // these are just pointers
Rcpp::NumericVector new_pi = new_param["pi"]; // these are just pointers
double sd_pi = static_cast<double>(config["sd_pi"]);
double old_logpost = 0.0, new_logpost = 0.0, p_accept = 0.0;
// proposal (normal distribution with SD: config$sd_pi)
new_pi[0] += R::rnorm(0.0, sd_pi); // new proposed value
// automatic rejection of pi outside [0;1]
if (new_pi[0] < 0.0 || new_pi[0] > 1.0) {
return param;
}
// compute likelihoods
old_logpost = cpp_ll_reporting(data, param, R_NilValue, custom_ll);
new_logpost = cpp_ll_reporting(data, new_param, R_NilValue, custom_ll);
// compute priors
old_logpost += cpp_prior_pi(param, config, custom_prior);
new_logpost += cpp_prior_pi(new_param, config, custom_prior);
// acceptance term
p_accept = exp(new_logpost - old_logpost);
// acceptance: the new value is already in pi, so we only act if the move is
// rejected, in which case we restore the previous ('old') value
if (p_accept < unif_rand()) { // reject new values
return param;
}
return new_param;
}
// ---------------------------
// movement of the contact reporting coverage 'eps' is done using a dumb normal
// proposal. This is satisfying for now - we only reject a few non-sensical
// values outside the range [0;1]. The SD of the proposal is provided through
// 'config'; this seems fine as the range of real values will never change
// much. Probably not much point in using auto-tuning here.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_eps(Rcpp::List param, Rcpp::List data, Rcpp::List config,
Rcpp::RObject custom_ll = R_NilValue,
Rcpp::RObject custom_prior = R_NilValue) {
// deep copy here for now, ultimately should be an arg.
Rcpp::List new_param = clone(param);
Rcpp::NumericVector eps = param["eps"]; // these are just pointers
Rcpp::NumericVector new_eps = new_param["eps"]; // these are just pointers
double sd_eps = static_cast<double>(config["sd_eps"]);
double old_logpost = 0.0, new_logpost = 0.0, p_accept = 0.0;
// proposal (normal distribution with SD: config$sd_eps)
new_eps[0] += R::rnorm(0.0, sd_eps); // new proposed value
// automatic rejection of eps outside [0;1]
if (new_eps[0] < 0.0 || new_eps[0] > 1.0) {
return param;
}
// compute likelihoods
old_logpost = cpp_ll_contact(data, param, R_NilValue, custom_ll);
new_logpost = cpp_ll_contact(data, new_param, R_NilValue, custom_ll);
// compute priors
old_logpost += cpp_prior_eps(param, config, custom_prior);
new_logpost += cpp_prior_eps(new_param, config, custom_prior);
// acceptance term
p_accept = exp(new_logpost - old_logpost);
// acceptance: the new value is already in eps, so we only act if the move is
// rejected, in which case we restore the previous ('old') value
if (p_accept < unif_rand()) { // reject new values
return param;
}
return new_param;
}
// ---------------------------
// movement of the non-infectious contact rate 'eps' is done using a dumb
// normal proposal. This is satisfying for now - we only reject a few
// non-sensical values outside the range [0;1]. The SD of the proposal is
// provided through 'config'; this seems fine as the range of real values will
// never change much. Probably not much point in using auto-tuning here.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_lambda(Rcpp::List param, Rcpp::List data, Rcpp::List config,
Rcpp::RObject custom_ll = R_NilValue,
Rcpp::RObject custom_prior = R_NilValue) {
// deep copy here for now, ultimately should be an arg.
Rcpp::List new_param = clone(param);
Rcpp::NumericVector lambda = param["lambda"]; // these are just pointers
Rcpp::NumericVector new_lambda = new_param["lambda"]; // these are just pointers
double sd_lambda = static_cast<double>(config["sd_lambda"]);
double old_logpost = 0.0, new_logpost = 0.0, p_accept = 0.0;
// proposal (normal distribution with SD: config$sd_lambda)
new_lambda[0] += R::rnorm(0.0, sd_lambda); // new proposed value
// automatic rejection of lambda outside [0;1]
if (new_lambda[0] < 0.0 || new_lambda[0] > 1.0) {
return param;
}
// compute likelihoods
old_logpost = cpp_ll_contact(data, param, R_NilValue, custom_ll);
new_logpost = cpp_ll_contact(data, new_param, R_NilValue, custom_ll);
// compute priors
old_logpost += cpp_prior_lambda(param, config, custom_prior);
new_logpost += cpp_prior_lambda(new_param, config, custom_prior);
// acceptance term
p_accept = exp(new_logpost - old_logpost);
// acceptance: the new value is already in lambda, so we only act if the move is
// rejected, in which case we restore the previous ('old') value
if (p_accept < unif_rand()) { // reject new values
return param;
}
return new_param;
}
// ---------------------------
// Movement of infection dates are +/- 1 from current states. These movements
// are currently vectorised, i.e. a bunch of dates are proposed all together;
// this may not be sustainable for larger datasets. The non-vectorised option
// will be slower and speed-up with C/C++ will be more substantial then.
// This version differs from the initial R implementation in several points:
// 1. all cases are moved
// 2. cases are moved one by one
// 3. movement for each case is +/- 1 time unit
// Notes
// - when computing the timing log-likelihood, the descendents of each
// case are also affected.
// - we generate a new vector 'new_t_inf', which will replace the
// previous pointer defining param["t_inf"].
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_t_inf(Rcpp::List param, Rcpp::List data,
Rcpp::RObject list_custom_ll = R_NilValue) {
// deep copy here for now, ultimately should be an arg.
Rcpp::List new_param = clone(param);
Rcpp::IntegerVector t_inf = param["t_inf"];
Rcpp::IntegerVector new_t_inf = new_param["t_inf"];
Rcpp::IntegerVector alpha = param["alpha"];
Rcpp::IntegerVector local_cases;
size_t N = static_cast<size_t>(data["N"]);
double old_loc_loglike = 0.0, new_loc_loglike = 0.0, p_loc_accept = 0.0;
for (size_t i = 0; i < N; i++) {
local_cases = cpp_find_descendents(param["alpha"], i+1);
// loglike with current value
old_loc_loglike = cpp_ll_timing(data, param, i+1, list_custom_ll); // term for case 'i' with offset
// term descendents of 'i'
if (local_cases.size() > 0) {
old_loc_loglike += cpp_ll_timing(data, param, local_cases, list_custom_ll);
}
// proposal (+/- 1)
new_t_inf[i] += unif_rand() > 0.5 ? 1 : -1; // new proposed value
// loglike with new value
new_loc_loglike = cpp_ll_timing(data, new_param, i+1, list_custom_ll); // term for case 'i' with offset
// term descendents of 'i'
if (local_cases.size() > 0) {
new_loc_loglike += cpp_ll_timing(data, new_param, local_cases, list_custom_ll);
}
// acceptance term
p_loc_accept = exp(new_loc_loglike - old_loc_loglike);
// acceptance: the new value is already in t_inf, so we only act if the move
// is rejected, in which case we restore the previous ('old') value
if (p_loc_accept < unif_rand()) { // reject new values
new_t_inf[i] = t_inf[i];
}
}
return new_param;
}
// ---------------------------
// Movement of ancestries ('alpha') is not vectorised, movements are made one
// case at a time. This procedure is simply about picking an infector at random
// amongst cases preceeding the case considered. The original version in
// 'outbreaker' used to move simultaneously 'alpha', 'kappa' and 't_inf', but
// current implementation is simpler and seems to mix at least as well. Proper
// movement of 'alpha' needs this procedure as well as a swapping procedure
// (swaps are not possible through move.alpha only); in all instances, 'alpha'
// is on the scale 1:N.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_alpha(Rcpp::List param, Rcpp::List data,
Rcpp::RObject list_custom_ll = R_NilValue) {
Rcpp::List new_param = clone(param);
Rcpp::IntegerVector alpha = param["alpha"]; // pointer to param$alpha
Rcpp::IntegerVector t_inf = param["t_inf"]; // pointer to param$t_inf
Rcpp::IntegerVector new_alpha = new_param["alpha"];
size_t N = static_cast<size_t>(data["N"]);
double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0;
for (size_t i = 0; i < N; i++) {
// only non-NA ancestries are moved, if there is at least 1 choice
if (alpha[i] != NA_INTEGER && sum(t_inf < t_inf[i]) > 0) {
// loglike with current value
// old_loglike = cpp_ll_all(data, param, R_NilValue);
old_loglike = cpp_ll_all(data, param, i+1, list_custom_ll); // offset
// proposal (+/- 1)
new_alpha[i] = cpp_pick_possible_ancestor(t_inf, i+1); // new proposed value (on scale 1 ... N)
// loglike with current value
new_loglike = cpp_ll_all(data, new_param, i+1, list_custom_ll);
// acceptance term
p_accept = exp(new_loglike - old_loglike);
// which case we restore the previous ('old') value
if (p_accept < unif_rand()) { // reject new values
new_alpha[i] = alpha[i];
} else {
alpha[i] = new_alpha[i];
}
}
}
return new_param;
}
// ---------------------------
// The basic movement of ancestries (picking an ancestor at random amongst in
// previous cases) makes swaps of ancestries (A->B) to (B->A) very
// difficult. This function addresses the issue. It is computer-intensive, but
// likely a determining factor for faster mixing. Unlike previous versions in
// the original 'outbreaker' package, all cases are 'moved' here. A swap is
// defined as:
// x -> a -> b becomes a -> x -> b
// Obviously cases are moved one at a time. We need to use local likelihood
// changes for this move to scale well with outbreak size. The complicated bit
// is that the move impacts all descendents from 'a' as well as 'x'.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_swap_cases(Rcpp::List param, Rcpp::List data,
Rcpp::RObject list_custom_ll = R_NilValue) {
Rcpp::List new_param = clone(param);
Rcpp::IntegerVector alpha = param["alpha"]; // pointer to param$alpha
Rcpp::IntegerVector t_inf = param["t_inf"]; // pointer to param$t_inf
Rcpp::List swapinfo; // contains alpha and t_inf
Rcpp::IntegerVector local_cases;
size_t N = static_cast<size_t>(data["N"]);
int N_int = data["N"];
double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0;
// Shuffle indices to make equal cases equally likely
Rcpp::IntegerVector idx = Rcpp::seq(0, N-1);
idx = Rcpp::sample(idx, N_int, false);
for (size_t j = 0; j < N; ++j) {
size_t i = (size_t)idx[j];
// only non-NA ancestries are moved, if there is at least 1 choice
if (alpha[i] != NA_INTEGER && sum(t_inf < t_inf[i]) > 0) {
// The local likelihood is defined as the likelihood computed for the
// cases affected by the swap; these include:
// - 'i'
// - the descendents of 'i'
// - 'alpha[i]'
// - the descendents of 'alpha[i]' (other than 'i')
local_cases = cpp_find_local_cases(param["alpha"], i+1);
// loglike with current parameters
old_loglike = cpp_ll_all(data, param, local_cases, list_custom_ll); // offset
// proposal: swap case 'i' and its ancestor
swapinfo = cpp_swap_cases(param, i+1);
new_param["alpha"] = swapinfo["alpha"];
new_param["t_inf"] = swapinfo["t_inf"];
new_param["kappa"] = swapinfo["kappa"];
// loglike with new parameters
new_loglike = cpp_ll_all(data, new_param, local_cases, list_custom_ll);
// acceptance term
p_accept = exp(new_loglike - old_loglike);
// acceptance: change param only if new values is accepted
if (p_accept >= unif_rand()) { // accept new parameters
param["alpha"] = new_param["alpha"];
param["t_inf"] = new_param["t_inf"];
param["kappa"] = new_param["kappa"];
}
}
}
return param;
}
// ---------------------------
// Movement of the number of generations on transmission chains ('kappa') is
// done for one ancestry at a time. As for infection times ('t_inf') we use a
// dumb, symmetric +/- 1 proposal. But because values are typically in a short
// range (e.g. [1-3]) we probably propose more dumb values here. We may
// eventually want to bounce back or use and correct for assymetric proposals.
// [[Rcpp::export(rng = true)]]
Rcpp::List cpp_move_kappa(Rcpp::List param, Rcpp::List data, Rcpp::List config,
Rcpp::RObject list_custom_ll = R_NilValue) {
Rcpp::List new_param = clone(param);
Rcpp::IntegerVector alpha = param["alpha"]; // pointer to param$alpha
Rcpp::IntegerVector kappa = param["kappa"]; // pointer to param$kappa
Rcpp::IntegerVector t_inf = param["t_inf"]; // pointer to param$t_inf
Rcpp::IntegerVector new_kappa = new_param["kappa"];
size_t N = static_cast<size_t>(data["N"]);
size_t K = static_cast<size_t>(config["max_kappa"]);
size_t jump;
double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0;
for (size_t i = 0; i < N; i++) {
// only non-NA ancestries are moved
if (alpha[i] != NA_INTEGER) {
// propose new kappa
jump = (unif_rand() > 0.5) ? 1 : -1;
new_kappa[i] = kappa[i] + jump;
// only look into this move if new kappa is positive and smaller than the
// maximum value; if not, remember to reset the value of new_kappa to that
// of kappa, otherwise we implicitely accept stupid moves automatically
if (new_kappa[i] < 1 || new_kappa[i] > K) {
new_kappa[i] = kappa[i];
} else {
// loglike with current parameters
old_loglike = cpp_ll_all(data, param, i+1, list_custom_ll);
// loglike with new parameters
new_loglike = cpp_ll_all(data, new_param, i+1, list_custom_ll);
// acceptance term
p_accept = exp(new_loglike - old_loglike);
// acceptance: change param only if new values is accepted
if (p_accept >= unif_rand()) { // accept new parameters
// Rprintf("\naccepting kappa:%d (p: %f old ll: %f new ll: %f",
// new_kappa[i], p_accept, old_loglike, new_loglike);
kappa[i] = new_kappa[i];
} else {
new_kappa[i] = kappa[i];
}
}
}
}
return param;
}