1 /*
2
3 PhyML: a program that computes maximum likelihood phylogenies from
4 DNA or AA homologous sequences.
5
6 Copyright (C) Stephane Guindon. Oct 2003 onward.
7
8 All parts of the source except where indicated are distributed under
9 the GNU public licence. See http://www.opensource.org for details.
10
11 */
12
13 #include "mcmc.h"
14
15
16 //////////////////////////////////////////////////////////////
17 //////////////////////////////////////////////////////////////
18
MCMC_Single_Param_Generic(phydbl * val,phydbl lim_inf,phydbl lim_sup,int move_num,phydbl * lnPrior,phydbl * lnLike,phydbl (* prior_func)(t_edge *,t_tree *,supert_tree *),phydbl (* like_func)(t_edge *,t_tree *,supert_tree *),int move_type,int _log,t_edge * branch,t_tree * tree,supert_tree * stree)19 void MCMC_Single_Param_Generic(phydbl *val,
20 phydbl lim_inf,
21 phydbl lim_sup,
22 int move_num,
23 phydbl *lnPrior,
24 phydbl *lnLike,
25 phydbl (*prior_func)(t_edge *,t_tree *,supert_tree *),
26 phydbl (*like_func)(t_edge *,t_tree *,supert_tree *),
27 int move_type,
28 int _log, /* _log == YES: the model describes the distribution of log(val) but the move applies to val. Need a correction factor */
29 t_edge *branch, t_tree *tree, supert_tree *stree)
30 {
31 phydbl cur_val,new_val,new_lnLike,new_lnPrior,cur_lnLike,cur_lnPrior;
32 phydbl u,alpha,ratio;
33 phydbl K;
34 phydbl new_lnval, cur_lnval;
35
36 Record_Br_Len(tree);
37
38 cur_val = *val;
39 new_val = -1.0;
40 ratio = 0.0;
41 K = tree->mcmc->tune_move[move_num];
42 cur_lnval = log(*val);
43 new_lnval = cur_lnval;
44
45 if(lnLike)
46 {
47 cur_lnLike = *lnLike;
48 new_lnLike = *lnLike;
49 }
50 else
51 {
52 cur_lnLike = 0.0;
53 new_lnLike = 0.0;
54 }
55
56 if(lnPrior)
57 {
58 cur_lnPrior = *lnPrior;
59 new_lnPrior = *lnPrior;
60 }
61 else
62 {
63 cur_lnPrior = 0.0;
64 new_lnPrior = 0.0;
65 }
66
67 MCMC_Make_Move(&cur_val,&new_val,lim_inf,lim_sup,&ratio,K,move_type);
68
69 if(new_val < lim_sup && new_val > lim_inf)
70 {
71 *val = new_val;
72
73 if(tree->rates) RATES_Update_Cur_Bl(tree);
74
75 if(_log == YES) ratio += (cur_lnval - new_lnval);
76
77 if(prior_func) /* Prior ratio */
78 {
79 new_lnPrior = (*prior_func)(branch,tree,stree);
80 ratio += (new_lnPrior - cur_lnPrior);
81 }
82
83 if(like_func) /* Likelihood ratio */
84 {
85 new_lnLike = (*like_func)(branch,tree,stree);
86 ratio += (new_lnLike - cur_lnLike);
87 }
88
89 /* printf("\n. %s cur_val: %f new_val:%f cur_lnL: %f new_lnL: %f cur_lnPrior: %f new_lnPrior: %f ratio: %f", */
90 /* tree->mcmc->move_name[move_num], */
91 /* cur_val, */
92 /* new_val, */
93 /* cur_lnLike, */
94 /* new_lnLike, */
95 /* cur_lnPrior, */
96 /* new_lnPrior, */
97 /* ratio); */
98
99 ratio = exp(ratio);
100 alpha = MIN(1.,ratio);
101
102 /* Always accept move */
103 if(tree->mcmc->always_yes == YES && new_lnLike > UNLIKELY) alpha = 1.0;
104
105 u = Uni();
106
107 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
108
109 if(u > alpha) /* Reject */
110 {
111 *val = cur_val;
112 new_val = cur_val;
113 if(lnPrior) *lnPrior = cur_lnPrior;
114 if(lnLike) *lnLike = cur_lnLike;
115 Restore_Br_Len(tree);
116 if(tree->mod && tree->mod->update_eigen)
117 {
118 if(!Update_Eigen(tree->mod))
119 {
120 PhyML_Fprintf(stderr,"\n. Problem in move %s",tree->mcmc->move_name[tree->mcmc->move_idx]);
121 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
122 }
123 }
124 }
125 else /* Accept */
126 {
127 tree->mcmc->acc_move[move_num]++;
128 if(lnPrior) *lnPrior = new_lnPrior;
129 if(lnLike) *lnLike = new_lnLike;
130 }
131 }
132
133 tree->mcmc->run_move[move_num]++;
134 }
135
136 //////////////////////////////////////////////////////////////
137 //////////////////////////////////////////////////////////////
138 /* Formula from ``Markov chain Monte Carlo in practice: a roundtable discussion'',
139 Kass, Robert E and Carlin, Bradley P and Gelman, Andrew and Neal, Radford M
140 The American Statistician, 1998
141 */
MCMC_Update_Effective_Sample_Size(int move_num,t_mcmc * mcmc,t_tree * tree)142 void MCMC_Update_Effective_Sample_Size(int move_num, t_mcmc *mcmc, t_tree *tree)
143 {
144 int i,N,lag;
145 phydbl rho,mean,var,old_rho,act,ess;
146 int burnin;
147
148 N = mcmc->sample_num+1;
149
150 burnin = (int)(0.1*N);
151 if(burnin < 1) return;
152
153 N -= burnin;
154
155 mean = Weighted_Mean(mcmc->sampled_val+move_num*mcmc->sample_size+burnin,NULL,N);
156 var = Variance(mcmc->sampled_val+move_num*mcmc->sample_size+burnin,N);
157
158 /* if(move_num == tree->mcmc->num_move_phyrex_sigsq) */
159 /* printf("\n. %d %f %f\n",N,mean,var); */
160
161 act = -1.0;
162 old_rho = 1.0;
163 For(lag,MIN(N,mcmc->max_lag))
164 {
165 rho = 0.0;
166 for(i=0;i<N-lag;i++) rho +=
167 (mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i] - mean) *
168 (mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i+lag] - mean) ;
169
170 rho /= (N - lag)*var;
171
172 if(old_rho + rho < 0.0)
173 {
174 break; /* Geyer (1992) stopping criterion */
175 }
176
177 old_rho = rho;
178
179 act += 2.*rho;
180 }
181
182 if(act > 0.0) ess = N/act;
183 else ess = 0.0;
184
185 mcmc->ess[move_num] = ess;
186
187 }
188
189 //////////////////////////////////////////////////////////////
190 //////////////////////////////////////////////////////////////
191
MCMC_Update_Mode(int move_num,t_mcmc * mcmc,t_tree * tree)192 void MCMC_Update_Mode(int move_num, t_mcmc *mcmc, t_tree *tree)
193 {
194 int i,j,N,best_bin;
195 int burnin,breaks,*bin_score,best_score;
196 phydbl min,max;
197
198 breaks = 100;
199 bin_score = (int *)mCalloc(breaks,sizeof(int));
200
201 N = mcmc->sample_num+1;
202
203 burnin = (int)(0.1*N);
204 if(burnin < 1) return;
205
206 N -= burnin;
207
208 min = +INFINITY;
209 for(i=0;i<N;i++)
210 if(mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i] < min)
211 min = mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i];
212
213 max = -INFINITY;
214 for(i=0;i<N;i++)
215 if(mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i] > max)
216 max = mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i];
217
218
219 for(i=0;i<N;i++)
220 {
221 for(j=1;j<breaks;j++)
222 if(min+j*(max-min)/breaks > mcmc->sampled_val[move_num*mcmc->sample_size+burnin+i])
223 {
224 bin_score[j-1]++;
225 break;
226 }
227 }
228
229 best_score = 0;
230 best_bin = 0;
231 for(j=0;j<breaks;j++)
232 if(bin_score[j] > best_score)
233 {
234 best_score = bin_score[j];
235 best_bin = j;
236 }
237
238 mcmc->mode[move_num] = min + best_bin*(max-min)/breaks;
239
240 Free(bin_score);
241 }
242
243 //////////////////////////////////////////////////////////////
244 //////////////////////////////////////////////////////////////
245
MCMC_Clock_R(t_tree * tree)246 void MCMC_Clock_R(t_tree *tree)
247 {
248 /* t_tree *tree; */
249
250 /* tree = mixt_tree; */
251 /* do */
252 /* { */
253 /* MCMC_Single_Param_Generic(&(tree->rates->clock_r), */
254 /* mixt_tree->rates->min_clock, */
255 /* mixt_tree->rates->max_clock, */
256 /* mixt_tree->mcmc->num_move_clock_r, */
257 /* NULL,&(mixt_tree->c_lnL), */
258 /* NULL,Wrap_Lk, */
259 /* mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_clock_r], */
260 /* NO,NULL,mixt_tree,NULL); */
261
262 /* tree = tree->next; */
263 /* } */
264 /* while(tree); */
265
266 phydbl new_lnL_data, cur_lnL_data;
267 phydbl new_lnL_rate, cur_lnL_rate;
268 phydbl u, ratio, alpha;
269 phydbl new_clock_r, cur_clock_r;
270 phydbl min, max;
271 int move_num;
272 phydbl K;
273
274
275 /* PhyML_Printf("\n. %d %d",tree->eval_alnL,tree->mod->s_opt->opt_clock_r); */
276 if(tree->mod->s_opt->opt_clock_r == NO) return;
277
278 new_clock_r = tree->rates->clock_r;
279 cur_clock_r = tree->rates->clock_r;
280 min = tree->rates->min_clock;
281 max = tree->rates->max_clock;
282 ratio = 0.0;
283 move_num = tree->mcmc->num_move_clock_r;
284 K = tree->mcmc->tune_move[move_num];
285 cur_lnL_data = tree->c_lnL;
286 new_lnL_data = tree->c_lnL;
287 cur_lnL_rate = tree->rates->c_lnL_rates;
288 new_lnL_rate = tree->rates->c_lnL_rates;
289
290 MCMC_Make_Move(&cur_clock_r,&new_clock_r,min,max,&ratio,K,tree->mcmc->move_type[move_num]);
291
292 if(new_clock_r > min && new_clock_r < max)
293 {
294 tree->rates->clock_r = new_clock_r;
295
296 RATES_Update_Cur_Bl(tree);
297
298 if(tree->eval_alnL == YES) new_lnL_data = Lk(NULL,tree);
299 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
300
301 ratio += (new_lnL_data - cur_lnL_data);
302 ratio += (new_lnL_rate - cur_lnL_rate);
303
304 ratio = exp(ratio);
305 alpha = MIN(1.,ratio);
306
307 u = Uni();
308
309 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
310
311 if(u > alpha) /* Reject */
312 {
313 /* PhyML_Printf("\n. reject"); */
314 tree->rates->clock_r = cur_clock_r;
315 tree->c_lnL = cur_lnL_data;
316 tree->rates->c_lnL_rates = cur_lnL_rate;
317 RATES_Update_Cur_Bl(tree);
318 }
319 else
320 {
321 /* PhyML_Printf("\n. accept"); */
322 tree->mcmc->acc_move[move_num]++;
323 }
324 }
325 tree->mcmc->run_move[move_num]++;
326 }
327
328 //////////////////////////////////////////////////////////////
329 //////////////////////////////////////////////////////////////
330
331 #ifdef GEO
332 // Sample dispersal parameter from a phylogeo model
MCMC_GEO_Sigma(t_tree * mixt_tree)333 void MCMC_GEO_Sigma(t_tree *mixt_tree)
334 {
335 t_tree *tree;
336
337 tree = mixt_tree;
338 do
339 {
340 MCMC_Single_Param_Generic(&(tree->geo->sigma),
341 mixt_tree->geo->min_sigma,
342 mixt_tree->geo->max_sigma,
343 mixt_tree->mcmc->num_move_geo_sigma,
344 NULL,&(mixt_tree->geo->c_lnL),
345 NULL,GEO_Wrap_Lk,
346 mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_geo_sigma],
347 NO,NULL,mixt_tree,NULL);
348
349 GEO_Update_Fmat(tree->geo);
350 tree = tree->next;
351 }
352 while(tree);
353 }
354 #endif
355
356 //////////////////////////////////////////////////////////////
357 //////////////////////////////////////////////////////////////
358
359 #ifdef GEO
360 // Sample competition parameter from a phylogeo model
MCMC_GEO_Lbda(t_tree * mixt_tree)361 void MCMC_GEO_Lbda(t_tree *mixt_tree)
362 {
363 t_tree *tree;
364
365 tree = mixt_tree;
366 do
367 {
368 MCMC_Single_Param_Generic(&(tree->geo->lbda),
369 mixt_tree->geo->min_lbda,
370 mixt_tree->geo->max_lbda,
371 mixt_tree->mcmc->num_move_geo_lambda,
372 NULL,&(mixt_tree->geo->c_lnL),
373 NULL,GEO_Wrap_Lk,
374 mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_geo_lambda],
375 NO,NULL,mixt_tree,NULL);
376
377 tree = tree->next;
378 }
379 while(tree);
380 }
381 #endif
382
383 //////////////////////////////////////////////////////////////
384 //////////////////////////////////////////////////////////////
385
386 #ifdef GEO
387 // Sample global migration rate parameter from a phylogeo model
MCMC_GEO_Tau(t_tree * mixt_tree)388 void MCMC_GEO_Tau(t_tree *mixt_tree)
389 {
390 t_tree *tree;
391
392 tree = mixt_tree;
393 do
394 {
395 MCMC_Single_Param_Generic(&(tree->geo->tau),
396 mixt_tree->geo->min_tau,
397 mixt_tree->geo->max_tau,
398 mixt_tree->mcmc->num_move_geo_tau,
399 NULL,&(mixt_tree->geo->c_lnL),
400 NULL,GEO_Wrap_Lk,
401 mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_geo_tau],
402 NO,NULL,mixt_tree,NULL);
403
404 tree = tree->next;
405 }
406 while(tree);
407 }
408 #endif
409
410 //////////////////////////////////////////////////////////////
411 //////////////////////////////////////////////////////////////
412
413 #ifdef GEO
MCMC_GEO_Dum(t_tree * mixt_tree)414 void MCMC_GEO_Dum(t_tree *mixt_tree)
415 {
416 t_tree *tree;
417
418 tree = mixt_tree;
419 do
420 {
421 MCMC_Single_Param_Generic(&(tree->geo->dum),
422 mixt_tree->geo->min_dum,
423 mixt_tree->geo->max_dum,
424 mixt_tree->mcmc->num_move_geo_dum,
425 NULL,&(mixt_tree->geo->c_lnL),
426 NULL,GEO_Wrap_Lk,
427 mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_geo_dum],
428 NO,NULL,mixt_tree,NULL);
429
430 tree = tree->next;
431 }
432 while(tree);
433 }
434 #endif
435
436 //////////////////////////////////////////////////////////////
437 //////////////////////////////////////////////////////////////
438
439 #ifdef GEO
MCMC_GEO_Loc(t_tree * tree)440 void MCMC_GEO_Loc(t_tree *tree)
441 {
442 int target;
443 int *rec_loc; // recorded locations
444 int i;
445 phydbl cur_lnL, new_lnL;
446 phydbl u, ratio, alpha;
447 phydbl sum;
448 phydbl *probs;
449
450 cur_lnL = tree->geo->c_lnL;
451
452 rec_loc = (int *)mCalloc(2*tree->n_otu-1,sizeof(int));
453
454 For(i,2*tree->n_otu-1) rec_loc[i] = tree->geo->idx_loc[i];
455
456 // Choose an internal node (including the root) at random
457 target = Rand_Int(tree->n_otu,2*tree->n_otu-2);
458
459 target = 2*tree->n_otu-2;
460
461 // Root node is special. Select new location uniformly at random
462 if(tree->a_nodes[target] == tree->n_root)
463 {
464 probs = (phydbl *)mCalloc(tree->geo->ldscape_sz,sizeof(phydbl));
465
466 sum = 0.0;
467 for(i=0;i<tree->geo->ldscape_sz;i++) sum += tree->geo->idx_loc_beneath[tree->n_root->num * tree->geo->ldscape_sz + i];
468 for(i=0;i<tree->geo->ldscape_sz;i++) probs[i] = tree->geo->idx_loc_beneath[tree->n_root->num * tree->geo->ldscape_sz + i]/sum;
469
470 tree->geo->idx_loc[tree->n_root->num] = Sample_i_With_Proba_pi(probs,tree->geo->ldscape_sz);
471
472 Free(probs);
473 }
474
475
476 // Randomize the locations below the selected node
477 GEO_Randomize_Locations(tree->a_nodes[target],
478 tree->geo,
479 tree);
480
481 new_lnL = GEO_Lk(tree->geo,tree);
482
483 ratio = (new_lnL - cur_lnL);
484 ratio = exp(ratio);
485 alpha = MIN(1.,ratio);
486 u = Uni();
487
488 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
489
490 if(u > alpha) /* Reject */
491 {
492 For(i,2*tree->n_otu-1) tree->geo->idx_loc[i] = rec_loc[i];
493 tree->geo->c_lnL = GEO_Lk(tree->geo,tree); // TO DO: you only need to update the occupation vector here...
494 }
495
496 Free(rec_loc);
497 }
498 #endif
499
500 //////////////////////////////////////////////////////////////
501 //////////////////////////////////////////////////////////////
502
MCMC_Sample_Joint_Rates_Prior(t_tree * tree)503 void MCMC_Sample_Joint_Rates_Prior(t_tree *tree)
504 {
505 int i,dim;
506 phydbl T;
507 phydbl *r,*t,*lambda;
508 phydbl *min_r,*max_r;
509 phydbl k;
510
511 dim = 2*tree->n_otu-2;
512 lambda = tree->rates->_2n_vect1;
513 min_r = tree->rates->_2n_vect2;
514 max_r = tree->rates->_2n_vect3;
515 r = tree->rates->br_r;
516 t = tree->times->nd_t;
517
518 for(i=0;i<dim;i++) tree->rates->mean_r[i] = 1.0;
519
520 RATES_Fill_Lca_Table(tree);
521 RATES_Covariance_Mu(tree);
522
523 T = .0;
524 for(i=0;i<dim;i++) T += (t[tree->a_nodes[i]->num] - t[tree->a_nodes[i]->anc->num]);
525 for(i=0;i<dim;i++) lambda[i] = (t[tree->a_nodes[i]->num] - t[tree->a_nodes[i]->anc->num])/T;
526 for(i=0;i<dim;i++) r[i] = 1.0;
527 for(i=0;i<dim;i++) min_r[i] = tree->rates->min_rate;
528 for(i=0;i<dim;i++) max_r[i] = tree->rates->max_rate;
529
530 k = 1.; /* We want \sum_i lambda[i] r[i] = 1 */
531
532 Rnorm_Multid_Trunc_Constraint(tree->rates->mean_r,
533 tree->rates->cov_r,
534 min_r,max_r,
535 lambda,
536 k,
537 r,
538 dim);
539 }
540
541 //////////////////////////////////////////////////////////////
542 //////////////////////////////////////////////////////////////
543
MCMC_Rates_All(t_tree * tree)544 void MCMC_Rates_All(t_tree *tree)
545 {
546 Set_Both_Sides(NO,tree);
547 if(tree->eval_alnL == YES) Lk(NULL,tree);
548 MCMC_One_Rate(tree->n_root,tree->n_root->v[1],YES,tree);
549 Update_Partial_Lk(tree,tree->e_root,tree->n_root->v[1]);
550 MCMC_One_Rate(tree->n_root,tree->n_root->v[2],YES,tree);
551 }
552
553 //////////////////////////////////////////////////////////////
554 //////////////////////////////////////////////////////////////
555
MCMC_One_Rate(t_node * a,t_node * d,int traversal,t_tree * tree)556 void MCMC_One_Rate(t_node *a, t_node *d, int traversal, t_tree *tree)
557 {
558 t_edge *b;
559 int i;
560 phydbl u;
561 phydbl new_lnL_seq, cur_lnL_seq, new_lnL_rate, cur_lnL_rate;
562 phydbl ratio, alpha;
563 phydbl new_mu, cur_mu;
564 phydbl r_min, r_max;
565 int move_num,err;
566 phydbl K;
567
568 if(tree->rates->model == STRICTCLOCK) return;
569 if(tree->rates->model == GUINDON) return;
570
571 b = NULL;
572 if(a == tree->n_root) b = tree->e_root;
573 else for(i=0;i<3;i++) if(d->v[i] == a) { b = d->b[i]; break; }
574
575 cur_mu = tree->rates->br_r[d->num];
576 r_min = tree->rates->min_rate;
577 r_max = tree->rates->max_rate;
578 ratio = 0.0;
579 move_num = tree->mcmc->num_move_br_r;
580 K = tree->mcmc->tune_move[move_num];
581 cur_lnL_seq = tree->c_lnL;
582 new_lnL_seq = tree->c_lnL;
583 cur_lnL_rate = tree->rates->c_lnL_rates;
584 new_lnL_rate = tree->rates->c_lnL_rates;
585
586 /* MCMC_Make_Move(&cur_mu,&new_mu,r_min,r_max,&ratio,K,tree->mcmc->move_type[move_num]); */
587
588 /* new_mu = Rnorm_Trunc(1.0,3.*tree->rates->nu,r_min,r_max,&err); */
589 /* ratio -= Log_Dnorm_Trunc(new_mu,1.0,3.*tree->rates->nu,r_min,r_max,&err); */
590 /* ratio += Log_Dnorm_Trunc(cur_mu,1.0,3.*tree->rates->nu,r_min,r_max,&err); */
591
592 err = NO;
593 new_mu = Rnorm_Trunc(cur_mu,K*tree->rates->nu,r_min,r_max,&err);
594
595 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
596
597 ratio -= Log_Dnorm_Trunc(new_mu,cur_mu,K*tree->rates->nu,r_min,r_max,&err);
598 ratio += Log_Dnorm_Trunc(cur_mu,new_mu,K*tree->rates->nu,r_min,r_max,&err);
599
600 if(new_mu > r_min && new_mu < r_max)
601 {
602 tree->rates->br_r[d->num] = new_mu;
603 tree->rates->br_do_updt[d->num] = YES;
604
605 if(tree->eval_alnL == YES) new_lnL_seq = Lk(b,tree);
606 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
607
608 ratio += (new_lnL_seq - cur_lnL_seq);
609 ratio += (new_lnL_rate - cur_lnL_rate);
610
611 ratio = exp(ratio);
612 alpha = MIN(1.,ratio);
613
614 u = Uni();
615
616 /* PhyML_Printf("\n. %f->%f %f->%f %f->%f ratio:%f", */
617 /* cur_mu,new_mu, */
618 /* cur_lnL_seq,new_lnL_seq, */
619 /* cur_lnL_rate,new_lnL_rate, */
620 /* ratio); */
621
622 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
623
624 if(u > alpha) /* Reject */
625 {
626 tree->rates->br_r[d->num] = cur_mu;
627 tree->c_lnL = cur_lnL_seq;
628 tree->rates->c_lnL_rates = cur_lnL_rate;
629
630 RATES_Update_Cur_Bl(tree);
631 Update_PMat_At_Given_Edge(b,tree);
632 }
633 else
634 {
635 tree->mcmc->acc_move[move_num]++;
636 }
637 }
638
639 tree->mcmc->run_move[move_num]++;
640
641 if(traversal == YES)
642 {
643 if(d->tax == YES) return;
644 else
645 {
646 for(i=0;i<3;++i)
647 if(d->v[i] != a && d->b[i] != tree->e_root)
648 {
649 Update_Partial_Lk(tree,d->b[i],d);
650 MCMC_One_Rate(d,d->v[i],YES,tree);
651 }
652 Update_Partial_Lk(tree,b,d);
653 }
654 }
655 }
656
657 //////////////////////////////////////////////////////////////
658 //////////////////////////////////////////////////////////////
659
MCMC_One_Node_Rate(t_node * a,t_node * d,int traversal,t_tree * tree)660 void MCMC_One_Node_Rate(t_node *a, t_node *d, int traversal, t_tree *tree)
661 {
662 t_edge *b;
663 int i;
664
665 b = NULL;
666 if(a == tree->n_root) b = tree->e_root;
667 else
668 for(i=0;i<3;i++) if(d->v[i] == a) { b = d->b[i]; break; }
669
670 /* Only the log_RANDWALK move seems to work here. Change with caution then. */
671 tree->rates->br_do_updt[d->num] = YES;
672 MCMC_Single_Param_Generic(&(tree->rates->nd_r[d->num]),
673 tree->rates->min_rate,
674 tree->rates->max_rate,
675 tree->mcmc->num_move_nd_r,
676 &(tree->rates->c_lnL_rates),NULL,
677 Wrap_Lk_Rates,NULL,
678 tree->mcmc->move_type[tree->mcmc->num_move_nd_r],
679 NO,NULL,tree,NULL);
680
681
682 Update_PMat_At_Given_Edge(b,tree);
683
684 if(traversal == YES)
685 {
686 if(d->tax == YES) return;
687 else
688 {
689 for(i=0;i<3;i++)
690 if(d->v[i] != a && d->b[i] != tree->e_root)
691 {
692 MCMC_One_Node_Rate(d,d->v[i],YES,tree);
693 }
694 }
695 }
696 }
697
698 //////////////////////////////////////////////////////////////
699 //////////////////////////////////////////////////////////////
700
MCMC_Times_And_Rates_All(t_tree * tree)701 void MCMC_Times_And_Rates_All(t_tree *tree)
702 {
703 MCMC_Times_And_Rates_Root(tree);
704 MCMC_Times_And_Rates_Recur(tree->n_root,tree->n_root->v[1],YES,tree);
705 MCMC_Times_And_Rates_Recur(tree->n_root,tree->n_root->v[2],YES,tree);
706 }
707
708 //////////////////////////////////////////////////////////////
709 //////////////////////////////////////////////////////////////
710
MCMC_Times_And_Rates_Root(t_tree * tree)711 void MCMC_Times_And_Rates_Root(t_tree *tree)
712 {
713 phydbl u;
714 phydbl t_min,t_max;
715 phydbl r_min,r_max;
716 phydbl t1_cur, t1_new;
717 phydbl cur_lnL_rate, new_lnL_rate;
718 phydbl cur_lnL_time, new_lnL_time;
719 phydbl cur_lnL_seq, new_lnL_seq;
720 phydbl ratio,alpha;
721 phydbl t0,t2,t3;
722 t_node *v2,*v3;
723 phydbl K;
724 int move_num;
725 t_node *root;
726 phydbl r2_cur,r3_cur;
727 phydbl r2_new,r3_new;
728
729 root = tree->n_root;
730
731 if(FABS(tree->times->t_prior_min[root->num] - tree->times->t_prior_max[root->num]) < 1.E-10) return;
732
733 move_num = tree->mcmc->num_move_times_and_rates_root;
734 K = tree->mcmc->tune_move[move_num];
735 t1_cur = tree->times->nd_t[root->num];
736 ratio = 0.0;
737 cur_lnL_rate = tree->rates->c_lnL_rates;
738 new_lnL_rate = tree->rates->c_lnL_rates;
739 cur_lnL_time = tree->times->c_lnL_times;
740 new_lnL_time = tree->times->c_lnL_times;
741 cur_lnL_seq = tree->c_lnL;
742 new_lnL_seq = tree->c_lnL;
743 r_min = tree->rates->min_rate;
744 r_max = tree->rates->max_rate;
745
746 v2 = root->v[2];
747 v3 = root->v[1];
748
749 t0 = tree->times->t_prior_min[root->num];
750 t2 = tree->times->nd_t[v2->num];
751 t3 = tree->times->nd_t[v3->num];
752
753 t_min = -INFINITY;
754 t_max = MIN(t2,t3);
755
756 t_min += tree->rates->min_dt;
757 t_max -= tree->rates->min_dt;
758
759 u = Uni();
760 t1_new = t_max - (t_max - t1_cur)*exp(K*(u-.5));
761 ratio += log((t1_new - t_max) / (t1_cur - t_max));
762
763 r2_cur = r3_cur = -1.0;
764
765 if(tree->rates->model == THORNE ||
766 tree->rates->model == LOGNORMAL ||
767 tree->rates->model == STRICTCLOCK)
768 {
769 r2_cur = tree->rates->br_r[v2->num];
770 r3_cur = tree->rates->br_r[v3->num];
771 }
772 else if(tree->rates->model == GUINDON)
773 {
774 r2_cur = tree->rates->nd_r[v2->num];
775 r3_cur = tree->rates->nd_r[v3->num];
776 }
777 else assert(FALSE);
778
779
780 r2_new = r2_cur * (t2 - t1_cur) / (t2 - t1_new);
781 r3_new = r3_cur * (t3 - t1_cur) / (t3 - t1_new);
782
783
784 if(t_min > t_max)
785 {
786 PhyML_Fprintf(stderr,"\n. glnL:%f",TIMES_Lk_Times(NO,tree));
787 PhyML_Fprintf(stderr,"\n. t:%f",tree->times->nd_t[tree->n_root->num]);
788 PhyML_Fprintf(stderr,"\n. t0 = %f t2 = %f t3 = %f",t0,t2,t3);
789 PhyML_Fprintf(stderr,"\n. t_min = %f t_max = %f",t_min,t_max);
790 PhyML_Fprintf(stderr,"\n. prior_min = %f prior_max = %f",tree->times->t_prior_min[root->num],tree->times->t_prior_max[root->num]);
791 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
792 }
793
794
795
796 if(t1_new > t_min &&
797 t1_new < t_max &&
798 r2_new > r_min &&
799 r2_new < r_max &&
800 r3_new > r_min &&
801 r3_new < r_max)
802 {
803 RATES_Record_Times(tree);
804
805 tree->times->nd_t[root->num] = t1_new;
806
807 if(tree->rates->model == THORNE ||
808 tree->rates->model == LOGNORMAL ||
809 tree->rates->model == STRICTCLOCK)
810 {
811 tree->rates->br_r[v2->num] = r2_new;
812 tree->rates->br_r[v3->num] = r3_new;
813 }
814 else if(tree->rates->model == GUINDON)
815 {
816 tree->rates->nd_r[v2->num] = r2_new;
817 tree->rates->nd_r[v3->num] = r3_new;
818 }
819 else assert(FALSE);
820
821 RATES_Update_Cur_Bl(tree);
822
823 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
824 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
825
826 if(tree->rates->model == GUINDON)
827 {
828 RATES_Update_Cur_Bl(tree);
829 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
830 }
831 else new_lnL_seq = cur_lnL_seq;
832
833 ratio += (new_lnL_rate - cur_lnL_rate);
834 ratio += (new_lnL_time - cur_lnL_time);
835 ratio += (new_lnL_seq - cur_lnL_seq);
836
837 ratio = exp(ratio);
838 alpha = MIN(1.,ratio);
839 u = Uni();
840
841 /* printf("\n. alnL:%f->%f tlnL: %f->%f rlnL: %f->%f ratio: %f", */
842 /* cur_lnL_seq,new_lnL_seq, */
843 /* cur_lnL_time,new_lnL_time, */
844 /* cur_lnL_rate,new_lnL_rate, */
845 /* ratio); */
846
847 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
848
849 if(u > alpha) /* Reject */
850 {
851 tree->times->nd_t[root->num] = t1_cur;
852
853 if(tree->rates->model == THORNE ||
854 tree->rates->model == LOGNORMAL ||
855 tree->rates->model == STRICTCLOCK)
856 {
857 tree->rates->br_r[v2->num] = r2_cur;
858 tree->rates->br_r[v3->num] = r3_cur;
859 }
860 else if(tree->rates->model == GUINDON)
861 {
862 tree->rates->nd_r[v2->num] = r2_cur;
863 tree->rates->nd_r[v3->num] = r3_cur;
864 }
865 else assert(FALSE);
866
867 RATES_Update_Cur_Bl(tree);
868
869 tree->rates->c_lnL_rates = cur_lnL_rate;
870 tree->times->c_lnL_times = cur_lnL_time;
871 tree->c_lnL = cur_lnL_seq;
872
873 }
874 else
875 {
876 tree->mcmc->acc_move[move_num]+=1;
877 }
878
879 // Ignore boundaries when updating tuning parameter
880 tree->mcmc->run_move[move_num]+=1;
881
882 }
883 }
884
885 //////////////////////////////////////////////////////////////
886 //////////////////////////////////////////////////////////////
887
MCMC_Times_And_Rates_Recur(t_node * a,t_node * d,int traversal,t_tree * tree)888 void MCMC_Times_And_Rates_Recur(t_node *a, t_node *d, int traversal, t_tree *tree)
889 {
890 phydbl u;
891 phydbl t_min,t_max;
892 phydbl r_min,r_max;
893 phydbl t1_cur, t1_new;
894 phydbl cur_lnL_rate, new_lnL_rate;
895 phydbl cur_lnL_time, new_lnL_time;
896 phydbl cur_lnL_seq, new_lnL_seq;
897 phydbl ratio,alpha;
898 int i;
899 phydbl t0,t2,t3;
900 t_node *v2,*v3;
901 int move_num;
902 phydbl r1_cur,r2_cur,r3_cur;
903 phydbl r1_new,r2_new,r3_new;
904
905 if(d->tax) return; /* Won't change time at tip */
906
907 move_num = tree->mcmc->num_move_times_and_rates;
908 t1_cur = tree->times->nd_t[d->num];
909 ratio = 0.0;
910 cur_lnL_rate = tree->rates->c_lnL_rates;
911 new_lnL_rate = tree->rates->c_lnL_rates;
912 cur_lnL_time = tree->times->c_lnL_times;
913 new_lnL_time = tree->times->c_lnL_times;
914 cur_lnL_seq = tree->c_lnL;
915 new_lnL_seq = tree->c_lnL;
916 r_min = tree->rates->min_rate;
917 r_max = tree->rates->max_rate;
918 r2_cur = -1.0;
919 r1_cur = -1.0;
920 r3_cur = -1.0;
921
922
923 v2 = v3 = NULL;
924 for(i=0;i<3;++i)
925 if((d->v[i] != a) && (d->b[i] != tree->e_root))
926 {
927 if(!v2) { v2 = d->v[i]; }
928 else { v3 = d->v[i]; }
929 }
930
931 if(tree->rates->model == THORNE ||
932 tree->rates->model == LOGNORMAL ||
933 tree->rates->model == STRICTCLOCK)
934 {
935 r1_cur = tree->rates->br_r[d->num];
936 r2_cur = tree->rates->br_r[v2->num];
937 r3_cur = tree->rates->br_r[v3->num];
938 }
939 else if(tree->rates->model == GUINDON)
940 {
941 r1_cur = tree->rates->nd_r[d->num];
942 r2_cur = tree->rates->nd_r[v2->num];
943 r3_cur = tree->rates->nd_r[v3->num];
944 }
945 else assert(FALSE);
946
947 t0 = tree->times->nd_t[a->num];
948 t2 = tree->times->nd_t[v2->num];
949 t3 = tree->times->nd_t[v3->num];
950
951 t_min = MAX(t0,tree->times->t_prior_min[d->num]);
952 t_max = MIN(tree->times->t_prior_max[d->num],MIN(t2,t3));
953
954 t_min += tree->rates->min_dt;
955 t_max -= tree->rates->min_dt;
956
957 t1_new = Uni()*(t_max - t_min) + t_min;
958
959 r1_new = r1_cur * (t1_cur - t0) / (t1_new - t0);
960 r2_new = r2_cur * (t2 - t1_cur) / (t2 - t1_new);
961 r3_new = r3_cur * (t3 - t1_cur) / (t3 - t1_new);
962
963 ratio +=
964 log((t1_cur - t0) / (t1_new - t0) *
965 (t2 - t1_cur) / (t2 - t1_new) *
966 (t3 - t1_cur) / (t3 - t1_new));
967
968 if(t1_new > t_min && t1_new < t_max &&
969 r1_new > r_min && r1_new < r_max &&
970 r2_new > r_min && r2_new < r_max &&
971 r3_new > r_min && r3_new < r_max)
972 {
973 RATES_Record_Times(tree);
974
975 tree->times->nd_t[d->num] = t1_new;
976
977 if(tree->rates->model == THORNE ||
978 tree->rates->model == LOGNORMAL ||
979 tree->rates->model == STRICTCLOCK)
980 {
981 tree->rates->br_r[d->num] = r1_new;
982 tree->rates->br_r[v2->num] = r2_new;
983 tree->rates->br_r[v3->num] = r3_new;
984 }
985 else if(tree->rates->model == GUINDON)
986 {
987 tree->rates->nd_r[d->num] = r1_new;
988 tree->rates->nd_r[v2->num] = r2_new;
989 tree->rates->nd_r[v3->num] = r3_new;
990 }
991 else assert(FALSE);
992
993 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
994 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
995
996 if(tree->rates->model == GUINDON)
997 {
998 RATES_Update_Cur_Bl(tree);
999 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1000 }
1001 else new_lnL_seq = cur_lnL_seq;
1002
1003 ratio += (new_lnL_time - cur_lnL_time);
1004 ratio += (new_lnL_rate - cur_lnL_rate);
1005 ratio += (new_lnL_seq - cur_lnL_seq);
1006
1007
1008 ratio = exp(ratio);
1009 alpha = MIN(1.,ratio);
1010 u = Uni();
1011
1012 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1013
1014 if(u > alpha) /* Reject */
1015 {
1016 /* printf("\n. rej"); */
1017 RATES_Reset_Times(tree);
1018
1019 if(tree->rates->model == THORNE ||
1020 tree->rates->model == LOGNORMAL ||
1021 tree->rates->model == STRICTCLOCK)
1022 {
1023 tree->rates->br_r[d->num] = r1_cur;
1024 tree->rates->br_r[v2->num] = r2_cur;
1025 tree->rates->br_r[v3->num] = r3_cur;
1026 }
1027 else if(tree->rates->model == GUINDON)
1028 {
1029 tree->rates->nd_r[d->num] = r1_cur;
1030 tree->rates->nd_r[v2->num] = r2_cur;
1031 tree->rates->nd_r[v3->num] = r3_cur;
1032 }
1033 else assert(FALSE);
1034
1035 RATES_Update_Cur_Bl(tree);
1036
1037 tree->rates->c_lnL_rates = cur_lnL_rate;
1038 tree->times->c_lnL_times = cur_lnL_time;
1039 tree->c_lnL = cur_lnL_seq;
1040 }
1041 else
1042 {
1043 /* printf("\n. acc"); */
1044 tree->mcmc->acc_move[move_num]++;
1045 }
1046 }
1047
1048 tree->mcmc->run_move[move_num]++;
1049
1050 if(d->tax == YES) return;
1051 else
1052 {
1053 for(i=0;i<3;i++)
1054 if(d->v[i] != a && d->b[i] != tree->e_root)
1055 {
1056 MCMC_Times_And_Rates_Recur(d,d->v[i],YES,tree);
1057 }
1058 }
1059 }
1060
1061 //////////////////////////////////////////////////////////////
1062 //////////////////////////////////////////////////////////////
1063
MCMC_Times_All(t_tree * tree)1064 void MCMC_Times_All(t_tree *tree)
1065 {
1066 // Down partials may not be up to date.
1067 Set_Both_Sides(YES,tree);
1068 if(tree->eval_alnL == YES) Lk(NULL,tree);
1069 Set_Both_Sides(NO,tree);
1070 MCMC_Root_Time(tree);
1071 MCMC_Time_Recur(tree->n_root,tree->n_root->v[1],YES,tree);
1072 if(tree->eval_alnL == YES) Update_Partial_Lk(tree,tree->e_root,tree->n_root->v[1]);
1073 MCMC_Time_Recur(tree->n_root,tree->n_root->v[2],YES,tree);
1074 }
1075
1076 //////////////////////////////////////////////////////////////
1077 //////////////////////////////////////////////////////////////
1078
MCMC_Time_Recur(t_node * a,t_node * d,int traversal,t_tree * tree)1079 void MCMC_Time_Recur(t_node *a, t_node *d, int traversal, t_tree *tree)
1080 {
1081 phydbl u;
1082 phydbl t_min,t_max;
1083 phydbl t1_cur, t1_new;
1084 phydbl cur_lnL_seq, new_lnL_seq;
1085 phydbl cur_lnL_rate, new_lnL_rate;
1086 phydbl cur_lnL_time, new_lnL_time;
1087 /* phydbl K; */
1088 phydbl ratio,alpha;
1089 t_edge *b1,*b2,*b3;
1090 int i;
1091 phydbl t0,t2,t3;
1092 t_node *v2,*v3;
1093 int move_num;
1094
1095 if(d->tax) return; /* Won't change time at tip */
1096
1097 move_num = tree->mcmc->num_move_times;
1098 t1_cur = tree->times->nd_t[d->num];
1099 ratio = 0.0;
1100 cur_lnL_seq = tree->c_lnL;
1101 new_lnL_seq = tree->c_lnL;
1102 cur_lnL_rate = tree->rates->c_lnL_rates;
1103 new_lnL_rate = tree->rates->c_lnL_rates;
1104 cur_lnL_time = tree->times->c_lnL_times;
1105 new_lnL_time = tree->times->c_lnL_times;
1106
1107
1108 v2 = v3 = NULL;
1109 for(i=0;i<3;i++)
1110 if((d->v[i] != a) && (d->b[i] != tree->e_root))
1111 {
1112 if(!v2) { v2 = d->v[i]; }
1113 else { v3 = d->v[i]; }
1114 }
1115
1116
1117 b1 = NULL;
1118 if(a == tree->n_root) b1 = tree->e_root;
1119 else for(i=0;i<3;i++) if(d->v[i] == a) { b1 = d->b[i]; break; }
1120
1121 b2 = b3 = NULL;
1122 for(i=0;i<3;i++)
1123 if((d->v[i] != a) && (d->b[i] != tree->e_root))
1124 {
1125 if(!b2) { b2 = d->b[i]; }
1126 else { b3 = d->b[i]; }
1127 }
1128
1129 t0 = tree->times->nd_t[a->num];
1130 t2 = tree->times->nd_t[v2->num];
1131 t3 = tree->times->nd_t[v3->num];
1132
1133 t_min = MAX(t0,tree->times->t_prior_min[d->num]);
1134 t_max = MIN(tree->times->t_prior_max[d->num],MIN(t2,t3));
1135
1136 t_min += tree->rates->min_dt;
1137 t_max -= tree->rates->min_dt;
1138
1139 /* K = tree->mcmc->tune_move[tree->mcmc->num_move_times] * (t_max - t_min); */
1140 /* MCMC_Make_Move(&t1_cur,&t1_new,t_min,t_max,&ratio,K,tree->mcmc->move_type[move_num]); */
1141 t1_new = Uni()*(t_max - t_min) + t_min;
1142
1143 if(t1_new > t_min && t1_new < t_max)
1144 {
1145 RATES_Record_Times(tree);
1146
1147 tree->times->nd_t[d->num] = t1_new;
1148
1149 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1150 ratio += (new_lnL_time - cur_lnL_time);
1151
1152 if(new_lnL_time > UNLIKELY)
1153 {
1154 RATES_Update_Cur_Bl(tree);
1155
1156 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1157 ratio += (new_lnL_rate - cur_lnL_rate);
1158
1159 if(tree->eval_alnL == YES && tree->io->lk_approx == EXACT)
1160 {
1161 Update_PMat_At_Given_Edge(b1,tree);
1162 Update_PMat_At_Given_Edge(b2,tree);
1163 Update_PMat_At_Given_Edge(b3,tree);
1164 Update_Partial_Lk(tree,b1,d);
1165 }
1166 if(tree->eval_alnL == YES) new_lnL_seq = Lk(b1,tree);
1167
1168 ratio += (new_lnL_seq - cur_lnL_seq);
1169 }
1170
1171 /* printf("\n. One_Time cur_t: %f new_t: %f ratio: %f alnL:%f->%f tlnL: %f->%f rlnL: %f->%f", */
1172 /* t1_cur,t1_new, */
1173 /* ratio, */
1174 /* cur_lnL_seq,new_lnL_seq, */
1175 /* cur_lnL_time,new_lnL_time, */
1176 /* cur_lnL_rate,new_lnL_rate); */
1177
1178 ratio = exp(ratio);
1179 alpha = MIN(1.,ratio);
1180 u = Uni();
1181
1182 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1183
1184 if(u > alpha) /* Reject */
1185 {
1186 /* printf("\n. rej"); */
1187 RATES_Reset_Times(tree);
1188 RATES_Update_Cur_Bl(tree);
1189
1190 if(tree->eval_alnL == YES && tree->io->lk_approx == EXACT)
1191 {
1192 Update_PMat_At_Given_Edge(b1,tree);
1193 Update_PMat_At_Given_Edge(b2,tree);
1194 Update_PMat_At_Given_Edge(b3,tree);
1195 Update_Partial_Lk(tree,b1,d);
1196 }
1197
1198 if(isinf(FABS(new_lnL_time)) == YES || isnan(new_lnL_time) == YES)
1199 {
1200 Print_Node(tree->n_root,tree->n_root->v[1],tree);
1201 Print_Node(tree->n_root,tree->n_root->v[2],tree);
1202 assert(FALSE);
1203 }
1204
1205 tree->c_lnL = cur_lnL_seq;
1206 tree->rates->c_lnL_rates = cur_lnL_rate;
1207 tree->times->c_lnL_times = cur_lnL_time;
1208
1209 if(Are_Equal(tree->times->c_lnL_times,cur_lnL_time,1.E-3) == NO)
1210 {
1211 PhyML_Fprintf(stderr,"\n\n");
1212 PhyML_Fprintf(stderr,"\n. moved node %d from %f to %f\n",d->num,t1_cur,t1_new);
1213 Print_Node(tree->n_root,tree->n_root->v[1],tree);
1214 Print_Node(tree->n_root,tree->n_root->v[2],tree);
1215 PhyML_Fprintf(stderr,"\n. new_glnL: %f cur_glnL: %f",tree->times->c_lnL_times,cur_lnL_time);
1216 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1217 }
1218
1219 /* new_lnL_seq = Lk(NULL,tree); /\* Not necessary. Remove once tested *\/ */
1220 /* if(Are_Equal(new_lnL_seq,cur_lnL_seq,1.E-3) == NO) */
1221 /* { */
1222 /* PhyML_Printf("\n. a: %d d: %d v2: %d v3: %d",a->num,d->num,v2->num,v3->num); */
1223 /* PhyML_Printf("\n. t1_cur: %f t1_new: %f",t1_cur,t1_new); */
1224 /* PhyML_Printf("\n. new_alnL: %f cur_alnL: %f",new_lnL_seq,cur_lnL_seq); */
1225 /* Generic_Exit(__FILE__,__LINE__,__FUNCTION__); */
1226 /* } */
1227 }
1228 else
1229 {
1230 /* cur_lnL_seq = Lk(NULL,tree); /\* Not necessary. Remove once tested *\/ */
1231 /* if(Are_Equal(new_lnL_seq,cur_lnL_seq,1.E-3) == NO) */
1232 /* { */
1233 /* PhyML_Printf("\n. a: %d d: %d v2: %d v3: %d",a->num,d->num,v2->num,v3->num); */
1234 /* PhyML_Printf("\n. t1_cur: %f t1_new: %f",t1_cur,t1_new); */
1235 /* PhyML_Printf("\n. new_alnL: %f cur_alnL: %f",new_lnL_seq,cur_lnL_seq); */
1236 /* Generic_Exit(__FILE__,__LINE__,__FUNCTION__); */
1237 /* } */
1238 tree->mcmc->acc_move[move_num]++;
1239 }
1240
1241 /* printf("\n. %f",new_lnL_seq); fflush(NULL); */
1242
1243 if(t1_new < t0)
1244 {
1245 t1_new = t0+1.E-4;
1246 PhyML_Fprintf(stderr,"\n");
1247 PhyML_Fprintf(stderr,"\n. a is root -> %s",(a == tree->n_root)?("YES"):("NO"));
1248 PhyML_Fprintf(stderr,"\n. t0 = %f t1_new = %f",t0,t1_new);
1249 PhyML_Fprintf(stderr,"\n. t_min=%f t_max=%f",t_min,t_max);
1250 PhyML_Fprintf(stderr,"\n. (t1-t0)=%f (t2-t1)=%f",t1_cur-t0,t2-t1_cur);
1251 PhyML_Fprintf(stderr,"\n. Err. in file %s at line %d\n",__FILE__,__LINE__);
1252 /* Exit("\n"); */
1253 }
1254 if(t1_new > MIN(t2,t3))
1255 {
1256 PhyML_Fprintf(stderr,"\n");
1257 PhyML_Fprintf(stderr,"\n. a is root -> %s",(a == tree->n_root)?("YES"):("NO"));
1258 PhyML_Fprintf(stderr,"\n. t0 = %f t1_new = %f t1 = %f t2 = %f t3 = %f MIN(t2,t3)=%f",t0,t1_new,t1_cur,t2,t3,MIN(t2,t3));
1259 PhyML_Fprintf(stderr,"\n. t_min=%f t_max=%f",t_min,t_max);
1260 PhyML_Fprintf(stderr,"\n. (t1-t0)=%f (t2-t1)=%f",t1_cur-t0,t2-t1_cur);
1261 PhyML_Fprintf(stderr,"\n. Err. in file %s at line %d\n",__FILE__,__LINE__);
1262 /* Exit("\n"); */
1263 }
1264
1265 if(isnan(t1_new))
1266 {
1267 PhyML_Fprintf(stderr,"\n. run=%d",tree->mcmc->run);
1268 PhyML_Fprintf(stderr,"\n. Err. in file %s at line %d\n",__FILE__,__LINE__);
1269 }
1270 }
1271
1272 tree->mcmc->run_move[move_num]++;
1273
1274 if(traversal == YES)
1275 {
1276 if(d->tax == YES) return;
1277 else
1278 {
1279 for(i=0;i<3;i++)
1280 if(d->v[i] != a && d->b[i] != tree->e_root)
1281 {
1282 if(tree->eval_alnL == YES) Update_Partial_Lk(tree,d->b[i],d);
1283 MCMC_Time_Recur(d,d->v[i],YES,tree);
1284 }
1285 if(tree->eval_alnL == YES) Update_Partial_Lk(tree,b1,d);
1286 }
1287 }
1288 }
1289
1290
1291 //////////////////////////////////////////////////////////////
1292 //////////////////////////////////////////////////////////////
1293
MCMC_Root_Time(t_tree * tree)1294 void MCMC_Root_Time(t_tree *tree)
1295 {
1296 phydbl u;
1297 phydbl t_min,t_max;
1298 phydbl t1_cur, t1_new;
1299 phydbl cur_lnL_seq, new_lnL_seq;
1300 phydbl cur_lnL_rate, new_lnL_rate;
1301 phydbl cur_lnL_time, new_lnL_time;
1302 phydbl ratio,alpha;
1303 t_edge *b1;
1304 phydbl t0,t2,t3;
1305 t_node *v2,*v3;
1306 phydbl K;
1307 int move_num;
1308 t_node *root;
1309
1310 /* printf("\n IN ROOT: %f",tree->times->c_lnL_times); */
1311
1312 root = tree->n_root;
1313
1314 if(FABS(tree->times->t_prior_min[root->num] - tree->times->t_prior_max[root->num]) < 1.E-10) return;
1315
1316 move_num = tree->mcmc->num_move_root_time;
1317 K = tree->mcmc->tune_move[move_num];
1318 t1_cur = tree->times->nd_t[root->num];
1319 ratio = 0.0;
1320 cur_lnL_seq = tree->c_lnL;
1321 new_lnL_seq = tree->c_lnL;
1322 cur_lnL_rate = tree->rates->c_lnL_rates;
1323 new_lnL_rate = tree->rates->c_lnL_rates;
1324 cur_lnL_time = tree->times->c_lnL_times;
1325 new_lnL_time = tree->times->c_lnL_times;
1326
1327 v2 = root->v[2];
1328 v3 = root->v[1];
1329
1330 b1 = tree->e_root;
1331
1332 t0 = tree->times->t_prior_min[root->num];
1333 t2 = tree->times->nd_t[v2->num];
1334 t3 = tree->times->nd_t[v3->num];
1335
1336 t_min = -INFINITY;
1337 t_max = MIN(t2,t3);
1338
1339 t_min += tree->rates->min_dt;
1340 t_max -= tree->rates->min_dt;
1341
1342 if(t_min > t_max)
1343 {
1344 PhyML_Fprintf(stderr,"\n. glnL:%f",TIMES_Lk_Times(NO,tree));
1345 PhyML_Fprintf(stderr,"\n. t:%f",tree->times->nd_t[tree->n_root->num]);
1346 PhyML_Fprintf(stderr,"\n. t0 = %f t2 = %f t3 = %f",t0,t2,t3);
1347 PhyML_Fprintf(stderr,"\n. t_min = %f t_max = %f",t_min,t_max);
1348 PhyML_Fprintf(stderr,"\n. prior_min = %f prior_max = %f",tree->times->t_prior_min[root->num],tree->times->t_prior_max[root->num]);
1349 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1350 }
1351
1352 u = Uni();
1353 t1_new = t_max - (t_max - t1_cur)*exp(K*(u-.5));
1354 ratio += log((t1_new - t_max) / (t1_cur - t_max));
1355 /* MCMC_Make_Move(&t1_cur,&t1_new,t_min,t_max,&ratio,K,tree->mcmc->move_type[move_num]); */
1356 /* t1_new = Uni()*(t_max-t_min) + t_min; */
1357
1358 /* PhyML_Printf("\n. K: %f t1: %f->%f acc.num: %d run: %d acc.rate: %f", */
1359 /* K,t1_cur,t1_new, */
1360 /* tree->mcmc->acc_move[move_num], */
1361 /* tree->mcmc->run_move[move_num], */
1362 /* tree->mcmc->run_move[move_num] > 0 ? (phydbl)tree->mcmc->acc_move[move_num]/tree->mcmc->run_move[move_num] : -1.); */
1363
1364 if(t1_new > t_min && t1_new < t_max)
1365 {
1366 RATES_Record_Times(tree);
1367
1368 tree->times->nd_t[root->num] = t1_new;
1369
1370 RATES_Update_Cur_Bl(tree);
1371
1372 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1373
1374 if(new_lnL_time > UNLIKELY)
1375 {
1376 if(tree->eval_alnL == YES) new_lnL_seq = Lk(b1,tree);
1377 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1378 }
1379
1380 ratio += (new_lnL_seq - cur_lnL_seq);
1381 ratio += (new_lnL_rate - cur_lnL_rate);
1382 ratio += (new_lnL_time - cur_lnL_time);
1383
1384 ratio = exp(ratio);
1385 alpha = MIN(1.,ratio);
1386 u = Uni();
1387
1388 /* printf("\n. alnL:%f->%f tlnL: %f->%f rlnL: %f->%f ratio: %f", */
1389 /* cur_lnL_seq,new_lnL_seq, */
1390 /* cur_lnL_time,new_lnL_time, */
1391 /* cur_lnL_rate,new_lnL_rate, */
1392 /* ratio); */
1393
1394 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1395
1396 if(u > alpha) /* Reject */
1397 {
1398 RATES_Reset_Times(tree);
1399 RATES_Update_Cur_Bl(tree);
1400 if(tree->eval_alnL == YES) Update_PMat_At_Given_Edge(b1,tree);
1401
1402 tree->c_lnL = cur_lnL_seq;
1403 tree->rates->c_lnL_rates = cur_lnL_rate;
1404 tree->times->c_lnL_times = cur_lnL_time;
1405
1406 /* if(Are_Equal(tree->times->c_lnL_times,cur_lnL_time,1.E-3) == NO) */
1407 /* { */
1408 /* PhyML_Printf("\n\n"); */
1409 /* Print_Node(tree->n_root,tree->n_root->v[1],tree); */
1410 /* Print_Node(tree->n_root,tree->n_root->v[2],tree); */
1411 /* PhyML_Printf("\n. new_glnL: %f cur_glnL: %f",tree->times->c_lnL_times,cur_lnL_time); */
1412 /* Generic_Exit(__FILE__,__LINE__,__FUNCTION__); */
1413 /* } */
1414 }
1415 else
1416 {
1417 tree->mcmc->acc_move[move_num]++;
1418 }
1419
1420 // Ignore boundaries when updating tuning parameter
1421 tree->mcmc->run_move[move_num]+=1;
1422
1423 }
1424 }
1425
1426 //////////////////////////////////////////////////////////////
1427 //////////////////////////////////////////////////////////////
1428
MCMC_Tree_Height(t_tree * tree)1429 void MCMC_Tree_Height(t_tree *tree)
1430 {
1431 phydbl K,mult,u,alpha,ratio;
1432 phydbl cur_lnL_seq,new_lnL_seq;
1433 phydbl cur_lnL_rate,new_lnL_rate;
1434 phydbl cur_lnL_time,new_lnL_time;
1435 phydbl floor;
1436 int n_nodes;
1437
1438 if(FABS(tree->times->t_prior_max[tree->n_root->num] - tree->times->t_prior_min[tree->n_root->num]) < 1.E-10) return;
1439
1440 RATES_Record_Times(tree);
1441
1442 K = tree->mcmc->tune_move[tree->mcmc->num_move_tree_height];
1443 ratio = 0.0;
1444 cur_lnL_seq = tree->c_lnL;
1445 new_lnL_seq = tree->c_lnL;
1446 cur_lnL_rate = tree->rates->c_lnL_rates;
1447 new_lnL_rate = tree->rates->c_lnL_rates;
1448 cur_lnL_time = tree->times->c_lnL_times;
1449 new_lnL_time = tree->times->c_lnL_times;
1450
1451 u = Uni();
1452 mult = exp(K*(u-0.5));
1453
1454 floor = 0.0;
1455 Scale_Subtree_Height(tree->n_root,mult,floor,&n_nodes,tree);
1456
1457 RATES_Update_Cur_Bl(tree);
1458
1459 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1460
1461 if(new_lnL_time > UNLIKELY)
1462 {
1463 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1464 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1465 }
1466
1467 ratio += (phydbl)(n_nodes)*log(mult);
1468
1469 ratio += (new_lnL_seq - cur_lnL_seq);
1470 ratio += (new_lnL_rate - cur_lnL_rate);
1471 ratio += (new_lnL_time - cur_lnL_time);
1472
1473 /* printf("\n. seq: %f %f times: %f %f rates: %f %f", */
1474 /* new_lnL_seq,cur_lnL_seq, */
1475 /* new_lnL_time,cur_lnL_time, */
1476 /* new_lnL_rate,cur_lnL_rate); */
1477
1478
1479 ratio = exp(ratio);
1480 alpha = MIN(1.,ratio);
1481 u = Uni();
1482
1483 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1484
1485 if(u > alpha)
1486 {
1487 RATES_Reset_Times(tree);
1488 RATES_Update_Cur_Bl(tree);
1489 tree->times->c_lnL_times = TIMES_Lk_Times(NO,tree); // Required in order to set t_prior_min/max to their original values
1490 tree->c_lnL = cur_lnL_seq;
1491 tree->rates->c_lnL_rates = cur_lnL_rate;
1492
1493 if(Are_Equal(tree->times->c_lnL_times,cur_lnL_time,1.E-3) == NO)
1494 {
1495 PhyML_Fprintf(stderr,"\n. new_glnL: %f cur_glnL: %f",tree->times->c_lnL_times,cur_lnL_time);
1496 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1497 }
1498 }
1499 else
1500 {
1501 tree->mcmc->acc_move[tree->mcmc->num_move_tree_height]++;
1502 }
1503
1504 tree->mcmc->run_move[tree->mcmc->num_move_tree_height]++;
1505 }
1506
1507 //////////////////////////////////////////////////////////////
1508 //////////////////////////////////////////////////////////////
1509
MCMC_Updown_T_Cr(t_tree * tree)1510 void MCMC_Updown_T_Cr(t_tree *tree)
1511 {
1512 /*! TO DO: make sure to change the values of clock_r across
1513 the different seq partitions
1514 */
1515
1516 int i;
1517 phydbl K,mult,u,alpha,ratio;
1518 phydbl cur_lnL_rate,new_lnL_rate;
1519 phydbl cur_lnL_time,new_lnL_time;
1520 phydbl cur_lnL_seq,new_lnL_seq;
1521 phydbl floor;
1522 int n_nodes;
1523
1524 /*! Check that sequences are isochronous. */
1525 for(i=0;i<tree->n_otu-1;i++) if(!Are_Equal(tree->times->nd_t[i+1],tree->times->nd_t[i],1.E-10)) return;
1526
1527 if(FABS(tree->times->t_prior_max[tree->n_root->num] - tree->times->t_prior_min[tree->n_root->num]) < 1.E-10) return;
1528
1529 RATES_Record_Times(tree);
1530
1531 K = tree->mcmc->tune_move[tree->mcmc->num_move_updown_t_cr];
1532 ratio = 0.0;
1533 cur_lnL_rate = tree->rates->c_lnL_rates;
1534 new_lnL_rate = tree->rates->c_lnL_rates;
1535 cur_lnL_time = tree->times->c_lnL_times;
1536 new_lnL_time = tree->times->c_lnL_times;
1537 cur_lnL_seq = tree->c_lnL;
1538 new_lnL_seq = tree->c_lnL;
1539
1540
1541 u = Uni();
1542 mult = exp(K*(u-0.5));
1543
1544 floor = 0.0;
1545 Scale_Subtree_Height(tree->n_root,mult,floor,&n_nodes,tree);
1546
1547 if(tree->mod->s_opt->opt_clock_r == YES)
1548 {
1549 tree->rates->clock_r /= mult;
1550 if(tree->rates->clock_r < tree->rates->min_clock || tree->rates->clock_r > tree->rates->max_clock)
1551 {
1552 tree->rates->clock_r *= mult;
1553 RATES_Reset_Times(tree);
1554 tree->mcmc->run_move[tree->mcmc->num_move_updown_t_cr]++;
1555 return;
1556 }
1557 }
1558
1559 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1560 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1561
1562 /* The Hastings ratio is actually mult^(n) when changing the absolute
1563 node heights. When considering the relative heights, this ratio combined
1564 to the Jacobian for the change of variable ends up to being equal to mult.
1565 */
1566 ratio += (n_nodes - 1)*log(mult);
1567
1568 ratio += (new_lnL_rate - cur_lnL_rate);
1569 ratio += (new_lnL_time - cur_lnL_time);
1570
1571
1572 if(tree->rates->model == GUINDON)
1573 {
1574 RATES_Update_Cur_Bl(tree);
1575 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1576 }
1577 else new_lnL_seq = cur_lnL_seq;
1578
1579 ratio += (new_lnL_seq - cur_lnL_seq);
1580
1581 ratio = exp(ratio);
1582 alpha = MIN(1.,ratio);
1583 u = Uni();
1584
1585 /* printf("\n. t_old = %f t_new = %f cr_old = %f cr_new = %f", */
1586 /* tree->times->nd_t[tree->n_root->num]/mult, */
1587 /* tree->times->nd_t[tree->n_root->num], */
1588 /* tree->rates->clock_r*mult, */
1589 /* tree->rates->clock_r); */
1590
1591 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1592
1593 if(u > alpha)
1594 {
1595 /* printf(" reject"); */
1596 RATES_Reset_Times(tree);
1597 if(tree->mod->s_opt->opt_clock_r == YES) tree->rates->clock_r *= mult;
1598 tree->rates->c_lnL_rates = cur_lnL_rate;
1599 tree->times->c_lnL_times = cur_lnL_time;
1600 if(tree->rates->model == GUINDON && tree->eval_alnL == YES)
1601 {
1602 RATES_Update_Cur_Bl(tree);
1603 tree->c_lnL = cur_lnL_seq;
1604 }
1605 }
1606 else
1607 {
1608 /* printf("\ accept"); */
1609 tree->mcmc->acc_move[tree->mcmc->num_move_updown_t_cr]++;
1610 }
1611
1612 tree->mcmc->run_move[tree->mcmc->num_move_updown_t_cr]++;
1613 }
1614
1615 //////////////////////////////////////////////////////////////
1616 //////////////////////////////////////////////////////////////
1617
MCMC_Updown_T_Br(t_tree * tree)1618 void MCMC_Updown_T_Br(t_tree *tree)
1619 {
1620
1621 int i;
1622 phydbl K,mult,u,alpha,ratio;
1623 phydbl cur_lnL_seq,new_lnL_seq;
1624 phydbl cur_lnL_rate,new_lnL_rate;
1625 phydbl cur_lnL_time,new_lnL_time;
1626 phydbl floor;
1627 int n_nodes, target;
1628
1629 /*! Check that sequences are isochronous. */
1630 for(i=0;i<tree->n_otu-1;i++) if(!Are_Equal(tree->times->nd_t[i+1],tree->times->nd_t[i],1.E-10)) return;
1631
1632 if(FABS(tree->times->t_prior_max[tree->n_root->num] - tree->times->t_prior_min[tree->n_root->num]) < 1.E-10) return;
1633
1634 RATES_Record_Times(tree);
1635
1636 K = tree->mcmc->tune_move[tree->mcmc->num_move_updown_t_br];
1637 ratio = 0.0;
1638 cur_lnL_seq = tree->c_lnL;
1639 new_lnL_seq = tree->c_lnL;
1640 cur_lnL_rate = tree->rates->c_lnL_rates;
1641 new_lnL_rate = tree->rates->c_lnL_rates;
1642 cur_lnL_time = tree->times->c_lnL_times;
1643 new_lnL_time = tree->times->c_lnL_times;
1644
1645 u = Uni();
1646 mult = exp(K*(u-0.5));
1647
1648 target = Rand_Int(tree->n_otu,2*tree->n_otu-3);
1649 floor = 0.0;
1650 Scale_Subtree_Height(tree->a_nodes[target],mult,floor,&n_nodes,tree);
1651
1652 for(i=0;i<2*tree->n_otu-1;++i)
1653 {
1654 if(tree->times->nd_t[i] > tree->times->t_prior_max[i] ||
1655 tree->times->nd_t[i] < tree->times->t_prior_min[i])
1656 {
1657 RATES_Reset_Times(tree);
1658 tree->mcmc->run_move[tree->mcmc->num_move_updown_t_br]++;
1659 return;
1660 }
1661 }
1662
1663 if(RATES_Check_Node_Times(tree)) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1664
1665 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1666 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1667 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1668
1669 ratio += n_nodes*log(mult);
1670
1671 /* Likelihood ratio */
1672 ratio += (new_lnL_seq - cur_lnL_seq);
1673 ratio += (new_lnL_rate - cur_lnL_rate);
1674 ratio += (new_lnL_time - cur_lnL_time);
1675
1676 ratio = exp(ratio);
1677 alpha = MIN(1.,ratio);
1678 u = Uni();
1679
1680 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1681
1682 if(u > alpha)
1683 {
1684 RATES_Reset_Times(tree);
1685 tree->c_lnL = cur_lnL_seq;
1686 tree->rates->c_lnL_rates = cur_lnL_rate;
1687 tree->times->c_lnL_times = cur_lnL_time;
1688 }
1689 else
1690 {
1691 tree->mcmc->acc_move[tree->mcmc->num_move_updown_t_br]++;
1692 }
1693
1694 tree->mcmc->run_move[tree->mcmc->num_move_updown_t_br]++;
1695 }
1696
1697 //////////////////////////////////////////////////////////////
1698 //////////////////////////////////////////////////////////////
1699
MCMC_Subtree_Height(t_tree * tree)1700 void MCMC_Subtree_Height(t_tree *tree)
1701 {
1702 phydbl K,mult,u,alpha,ratio;
1703 phydbl cur_lnL_seq,new_lnL_seq;
1704 phydbl cur_lnL_rate,new_lnL_rate;
1705 phydbl cur_lnL_time,new_lnL_time;
1706 phydbl floor;
1707 int target;
1708 int n_nodes;
1709
1710 RATES_Record_Times(tree);
1711
1712 K = tree->mcmc->tune_move[tree->mcmc->num_move_subtree_height];
1713 ratio = 0.0;
1714 cur_lnL_seq = tree->c_lnL;
1715 new_lnL_seq = tree->c_lnL;
1716 cur_lnL_rate = tree->rates->c_lnL_rates;
1717 new_lnL_rate = tree->rates->c_lnL_rates;
1718 cur_lnL_time = tree->times->c_lnL_times;
1719 new_lnL_time = tree->times->c_lnL_times;
1720
1721 u = Uni();
1722 mult = exp(K*(u-0.5));
1723
1724 target = Rand_Int(tree->n_otu,2*tree->n_otu-3);
1725 floor = 0.0;
1726 if(tree->a_nodes[target] == tree->n_root) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1727
1728 if(!Scale_Subtree_Height(tree->a_nodes[target],mult,floor,&n_nodes,tree))
1729 {
1730 RATES_Reset_Times(tree);
1731 tree->mcmc->run_move[tree->mcmc->num_move_subtree_height]++;
1732 return;
1733 }
1734
1735 if(RATES_Check_Node_Times(tree)) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1736
1737 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
1738
1739 if(new_lnL_time > UNLIKELY)
1740 {
1741 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1742 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1743 }
1744
1745 ratio += (phydbl)(n_nodes)*log(mult);
1746 ratio += (new_lnL_seq - cur_lnL_seq);
1747 ratio += (new_lnL_rate - cur_lnL_rate);
1748 ratio += (new_lnL_time - cur_lnL_time);
1749
1750 ratio = exp(ratio);
1751 alpha = MIN(1.,ratio);
1752 u = Uni();
1753
1754 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1755
1756 if(u > alpha)
1757 {
1758 RATES_Reset_Times(tree);
1759 RATES_Update_Cur_Bl(tree);
1760 tree->c_lnL = cur_lnL_seq;
1761 tree->rates->c_lnL_rates = cur_lnL_rate;
1762 tree->times->c_lnL_times = cur_lnL_time;
1763 }
1764 else
1765 {
1766 tree->mcmc->acc_move[tree->mcmc->num_move_subtree_height]++;
1767 }
1768
1769 tree->mcmc->run_move[tree->mcmc->num_move_subtree_height]++;
1770
1771 }
1772
1773 //////////////////////////////////////////////////////////////
1774 //////////////////////////////////////////////////////////////
1775
MCMC_Tree_Rates(t_tree * tree)1776 void MCMC_Tree_Rates(t_tree *tree)
1777 {
1778 phydbl K,mult,u,alpha,ratio;
1779 phydbl cur_lnL_rate,new_lnL_rate;
1780 phydbl cur_lnL_seq,new_lnL_seq;
1781 int n_nodes;
1782 phydbl init_clock;
1783
1784 if(tree->eval_alnL == NO) return;
1785 if(tree->rates->model == STRICTCLOCK) return;
1786
1787 RATES_Record_Rates(tree);
1788
1789 tree->mcmc->run_move[tree->mcmc->num_move_tree_rates]++;
1790
1791 K = tree->mcmc->tune_move[tree->mcmc->num_move_tree_rates];
1792 cur_lnL_rate = tree->rates->c_lnL_rates;
1793 new_lnL_rate = UNLIKELY;
1794 init_clock = tree->rates->clock_r;
1795 ratio = 0.0;
1796 cur_lnL_seq = tree->c_lnL;
1797 new_lnL_seq = UNLIKELY;
1798
1799 u = Uni();
1800 mult = exp(K*(u-0.5));
1801
1802 /* Multiply branch rates (or add to log of rates) */
1803 if(!Scale_Subtree_Rates(tree->n_root,mult,&n_nodes,tree))
1804 {
1805 RATES_Reset_Rates(tree);
1806 return;
1807 }
1808
1809 if(n_nodes != 2*tree->n_otu-2) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
1810
1811 /* Divide clock_r */
1812 if(tree->mod->s_opt->opt_clock_r == YES)
1813 {
1814 tree->rates->clock_r /= mult;
1815 if(tree->rates->clock_r < tree->rates->min_clock || tree->rates->clock_r > tree->rates->max_clock)
1816 {
1817 tree->rates->clock_r = init_clock;
1818 RATES_Reset_Rates(tree);
1819 return;
1820 }
1821 }
1822
1823 if((tree->rates->model == GUINDON) ||
1824 (tree->times->is_asynchronous == YES) ||
1825 (tree->mod->s_opt->opt_clock_r == NO))
1826 {
1827 RATES_Update_Cur_Bl(tree);
1828 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1829 }
1830 else
1831 new_lnL_seq = cur_lnL_seq;
1832
1833 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1834
1835 ratio += (n_nodes-1)*log(mult);
1836 ratio += (new_lnL_rate - cur_lnL_rate);
1837 ratio += (new_lnL_seq - cur_lnL_seq);
1838
1839 ratio = exp(ratio);
1840 alpha = MIN(1.,ratio);
1841 u = Uni();
1842
1843 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1844
1845 if(u > alpha)
1846 {
1847 tree->rates->clock_r = init_clock;
1848 RATES_Reset_Rates(tree);
1849 tree->rates->c_lnL_rates = cur_lnL_rate;
1850 if(tree->rates->model == GUINDON)
1851 {
1852 RATES_Update_Cur_Bl(tree);
1853 tree->c_lnL = cur_lnL_seq;
1854 }
1855 }
1856 else
1857 {
1858 tree->mcmc->acc_move[tree->mcmc->num_move_tree_rates]++;
1859 }
1860 }
1861
1862 //////////////////////////////////////////////////////////////
1863 //////////////////////////////////////////////////////////////
1864
MCMC_Subtree_Rates(t_tree * tree)1865 void MCMC_Subtree_Rates(t_tree *tree)
1866 { phydbl K,mult,u,alpha,ratio;
1867 phydbl cur_lnL_rate,new_lnL_rate;
1868 phydbl cur_lnL_seq,new_lnL_seq;
1869 int target;
1870 int n_nodes;
1871
1872 if(tree->rates->model == STRICTCLOCK) return;
1873
1874 RATES_Record_Rates(tree);
1875 Record_Br_Len(tree);
1876
1877 K = tree->mcmc->tune_move[tree->mcmc->num_move_subtree_rates];
1878 cur_lnL_rate = tree->rates->c_lnL_rates;
1879 new_lnL_rate = cur_lnL_rate;
1880 cur_lnL_seq = tree->c_lnL;
1881 new_lnL_seq = cur_lnL_seq;
1882 ratio = 0.0;
1883
1884 u = Uni();
1885 mult = exp(K*(u-0.5));
1886 /* mult = Rgamma(1./K,K); */
1887
1888 target = Rand_Int(tree->n_otu,2*tree->n_otu-3);
1889
1890 /* Multiply branch rates */
1891 if(!Scale_Subtree_Rates(tree->a_nodes[target],mult,&n_nodes,tree))
1892 {
1893 RATES_Reset_Rates(tree);
1894 tree->mcmc->run_move[tree->mcmc->num_move_subtree_rates]++;
1895 return;
1896 }
1897
1898 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1899 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1900
1901 /* Proposal ratio: 2n-2=> number of multiplications, 1=>number of divisions */
1902 ratio += (+n_nodes)*log(mult);
1903 /* ratio += (n_nodes-2)*log(mult) + log(Dgamma(1./mult,1./K,K)/Dgamma(mult,1./K,K)); */
1904
1905
1906 ratio += (new_lnL_rate - cur_lnL_rate);
1907 ratio += (new_lnL_seq - cur_lnL_seq);
1908
1909
1910 ratio = exp(ratio);
1911 alpha = MIN(1.,ratio);
1912 u = Uni();
1913
1914 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
1915
1916 if(u > alpha)
1917 {
1918 RATES_Reset_Rates(tree);
1919 Restore_Br_Len(tree);
1920 tree->rates->c_lnL_rates = cur_lnL_rate;
1921 tree->c_lnL = cur_lnL_seq;
1922 }
1923 else
1924 {
1925 tree->mcmc->acc_move[tree->mcmc->num_move_subtree_rates]++;
1926 }
1927
1928 tree->mcmc->run_move[tree->mcmc->num_move_subtree_rates]++;
1929 }
1930
1931 //////////////////////////////////////////////////////////////
1932 //////////////////////////////////////////////////////////////
1933
MCMC_Swing(t_tree * tree)1934 void MCMC_Swing(t_tree *tree)
1935 {
1936 int i;
1937 phydbl K,mult,u,alpha,ratio;
1938 phydbl cur_lnL_seq,new_lnL_seq;
1939 phydbl cur_lnL_rate,new_lnL_rate;
1940
1941 if(tree->rates->model == STRICTCLOCK) return;
1942
1943 RATES_Record_Times(tree);
1944 RATES_Record_Rates(tree);
1945 Record_Br_Len(tree);
1946
1947 K = 3.;
1948 cur_lnL_seq = tree->c_lnL;
1949 new_lnL_seq = cur_lnL_seq;
1950 cur_lnL_rate = tree->rates->c_lnL_rates;
1951 new_lnL_rate = cur_lnL_rate;
1952 ratio = 0.0;
1953
1954 u = Uni();
1955 /* mult = exp(K*(u-0.5)); */
1956 mult = u*(K - 1./K) + 1./K;
1957
1958
1959 For(i,2*tree->n_otu-1)
1960 {
1961 if(tree->a_nodes[i]->tax == NO)
1962 {
1963 tree->times->nd_t[i] *= mult;
1964 }
1965
1966 if(tree->times->nd_t[i] > tree->times->t_prior_max[i] ||
1967 tree->times->nd_t[i] < tree->times->t_prior_min[i])
1968 {
1969 RATES_Reset_Times(tree);
1970 Restore_Br_Len(tree);
1971 return;
1972 }
1973 }
1974
1975 For(i,2*tree->n_otu-2)
1976 {
1977 tree->rates->br_r[i] /= mult;
1978
1979 if(tree->rates->br_r[i] > tree->rates->max_rate ||
1980 tree->rates->br_r[i] < tree->rates->min_rate)
1981 {
1982 RATES_Reset_Times(tree);
1983 RATES_Reset_Rates(tree);
1984 Restore_Br_Len(tree);
1985 return;
1986 }
1987 }
1988
1989 RATES_Update_Cur_Bl(tree);
1990 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
1991 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
1992
1993 ratio += (-(tree->n_otu-1.)-2.)*log(mult);
1994 ratio += (new_lnL_rate - cur_lnL_rate);
1995 ratio += (new_lnL_seq - cur_lnL_seq);
1996
1997 ratio = exp(ratio);
1998 alpha = MIN(1.,ratio);
1999 u = Uni();
2000
2001 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
2002
2003 if(u > alpha)
2004 {
2005 RATES_Reset_Times(tree);
2006 RATES_Reset_Rates(tree);
2007 Restore_Br_Len(tree);
2008 tree->c_lnL = cur_lnL_seq;
2009 tree->rates->c_lnL_rates = cur_lnL_rate;
2010 /* printf("\n. Reject %8f",mult); */
2011 }
2012 else
2013 {
2014 /* printf("\n. Accept %8f",mult); */
2015 }
2016
2017
2018 }
2019
2020 //////////////////////////////////////////////////////////////
2021 //////////////////////////////////////////////////////////////
2022
MCMC_Updown_Nu_Cr(t_tree * tree)2023 void MCMC_Updown_Nu_Cr(t_tree *tree)
2024 {
2025 phydbl K,mult,u,alpha,ratio;
2026 phydbl cur_lnL_rate,new_lnL_rate;
2027 phydbl cur_lnL_seq,new_lnL_seq;
2028
2029 RATES_Record_Rates(tree);
2030 Record_Br_Len(tree);
2031
2032 K = tree->mcmc->tune_move[tree->mcmc->num_move_updown_nu_cr];
2033 cur_lnL_seq = tree->c_lnL;
2034 new_lnL_seq = tree->c_lnL;
2035 cur_lnL_rate = tree->rates->c_lnL_rates;
2036 new_lnL_rate = tree->rates->c_lnL_rates;
2037
2038 u = Uni();
2039 mult = exp(K*(u-0.5));
2040
2041 /* Multiply branch rates */
2042 /* if(!Scale_Subtree_Rates(tree->n_root,mult,&n_nodes,tree)) */
2043 /* { */
2044 /* RATES_Reset_Rates(tree); */
2045 /* Restore_Br_Len(tree); */
2046 /* tree->mcmc->run_move[tree->mcmc->num_move_updown_nu_cr]++; */
2047 /* return; */
2048 /* } */
2049
2050
2051 if(tree->mod->s_opt->opt_clock_r == YES)
2052 {
2053 tree->rates->clock_r /= mult;
2054 if(tree->rates->clock_r < tree->rates->min_clock || tree->rates->clock_r > tree->rates->max_clock)
2055 {
2056 tree->rates->clock_r *= mult;
2057 RATES_Reset_Rates(tree);
2058 Restore_Br_Len(tree);
2059 tree->mcmc->run_move[tree->mcmc->num_move_updown_nu_cr]++;
2060 return;
2061 }
2062 }
2063
2064 tree->rates->nu *= mult;
2065 if(tree->rates->nu < tree->rates->min_nu || tree->rates->nu > tree->rates->max_nu)
2066 {
2067 tree->rates->nu /= mult;
2068 tree->rates->clock_r *= mult;
2069 RATES_Reset_Rates(tree);
2070 Restore_Br_Len(tree);
2071 tree->mcmc->run_move[tree->mcmc->num_move_updown_nu_cr]++;
2072 return;
2073 }
2074
2075 RATES_Update_Cur_Bl(tree);
2076 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
2077 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
2078
2079 ratio = 0.0;
2080 /* Proposal ratio: 2n-2=> number of multiplications, 1=>number of divisions */
2081 /* ratio += n_nodes*log(mult); /\* (1-1)*log(mult); *\/ */
2082 ratio += 0.0*log(mult); /* (1-1)*log(mult); */
2083 /* Prior density ratio */
2084 ratio += (new_lnL_rate - cur_lnL_rate);
2085 /* Likelihood density ratio */
2086 ratio += (new_lnL_seq - cur_lnL_seq);
2087
2088 ratio = exp(ratio);
2089 alpha = MIN(1.,ratio);
2090 u = Uni();
2091
2092 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
2093
2094 if(u > alpha)
2095 {
2096 if(tree->mod->s_opt->opt_clock_r == YES) tree->rates->clock_r *= mult;
2097 tree->rates->nu /= mult;
2098 RATES_Reset_Rates(tree);
2099 Restore_Br_Len(tree);
2100 tree->rates->c_lnL_rates = cur_lnL_rate;
2101 tree->c_lnL = cur_lnL_seq;
2102 }
2103 else
2104 {
2105 tree->mcmc->acc_move[tree->mcmc->num_move_updown_nu_cr]++;
2106 }
2107
2108 tree->mcmc->run_move[tree->mcmc->num_move_updown_nu_cr]++;
2109 }
2110
2111 //////////////////////////////////////////////////////////////
2112 //////////////////////////////////////////////////////////////
2113
MCMC_Time_Slice(t_tree * tree)2114 void MCMC_Time_Slice(t_tree *tree)
2115 {
2116 int i;
2117 phydbl K,mult,u,alpha,ratio;
2118 phydbl cur_lnL_seq,new_lnL_seq;
2119 phydbl cur_lnL_rate,new_lnL_rate;
2120 phydbl cur_lnL_time,new_lnL_time;
2121 phydbl floor;
2122 int n_nodes;
2123
2124 /*! Check that sequences are isochronous. */
2125 for(i=0;i<tree->n_otu-1;i++) if(!Are_Equal(tree->times->nd_t[i+1],tree->times->nd_t[i],1.E-10)) return;
2126
2127 RATES_Record_Times(tree);
2128
2129 K = tree->mcmc->tune_move[tree->mcmc->num_move_time_slice];
2130 cur_lnL_seq = tree->c_lnL;
2131 new_lnL_seq = UNLIKELY;
2132 cur_lnL_rate = tree->rates->c_lnL_rates;
2133 new_lnL_rate = UNLIKELY;
2134 cur_lnL_time = tree->times->c_lnL_times;
2135 new_lnL_time = UNLIKELY;
2136 ratio = 0.0;
2137
2138 u = Uni();
2139 mult = exp(K*(u-0.5));
2140
2141 floor = Uni()*tree->times->nd_t[tree->n_root->num];
2142
2143 Scale_Subtree_Height(tree->n_root,mult,floor,&n_nodes,tree);
2144
2145 if(TIMES_Check_Node_Height_Ordering(tree) != NO)
2146 {
2147 RATES_Update_Cur_Bl(tree);
2148
2149 new_lnL_time = TIMES_Lk_Times(NO,tree);
2150
2151 if(new_lnL_time > UNLIKELY)
2152 {
2153 new_lnL_seq = Lk(NULL,tree);
2154 new_lnL_rate = RATES_Lk_Rates(tree);
2155 }
2156
2157 ratio += (phydbl)(n_nodes)*log(mult);
2158
2159 /* Likelihood ratio */
2160 ratio += (new_lnL_seq - cur_lnL_seq);
2161
2162 /* Prior ratio */
2163 ratio += (new_lnL_rate - cur_lnL_rate);
2164 ratio += (new_lnL_time - cur_lnL_time);
2165
2166 /* printf("\n. seq: %f<-%f times: %f<-%f rates: %f<-%f acc.: [%d/%d] k: %f t: %f x: %f", */
2167 /* new_lnL_seq,cur_lnL_seq, */
2168 /* new_lnL_time,cur_lnL_time, */
2169 /* new_lnL_rate,cur_lnL_rate, */
2170 /* tree->mcmc->acc_move[tree->mcmc->num_move_time_slice], */
2171 /* tree->mcmc->run_move[tree->mcmc->num_move_time_slice], */
2172 /* K,floor,mult); */
2173
2174 ratio = exp(ratio);
2175 }
2176 else
2177 {
2178 ratio = 0.0;
2179 }
2180
2181 alpha = MIN(1.,ratio);
2182 u = Uni();
2183
2184 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
2185
2186 if(u > alpha)
2187 {
2188 RATES_Reset_Times(tree);
2189 RATES_Update_Cur_Bl(tree);
2190 tree->times->c_lnL_times = TIMES_Lk_Times(NO,tree); // Required in order to set t_prior_min/max to their original values
2191 tree->c_lnL = cur_lnL_seq;
2192 tree->rates->c_lnL_rates = cur_lnL_rate;
2193
2194 if(Are_Equal(tree->times->c_lnL_times,cur_lnL_time,1.E-3) == NO)
2195 {
2196 PhyML_Fprintf(stderr,"\n. new_glnL: %f cur_glnL: %f",tree->times->c_lnL_times,cur_lnL_time);
2197 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
2198 }
2199 }
2200 else
2201 {
2202 tree->mcmc->acc_move[tree->mcmc->num_move_time_slice]++;
2203 }
2204
2205 tree->mcmc->run_move[tree->mcmc->num_move_time_slice]++;
2206 }
2207
2208 //////////////////////////////////////////////////////////////
2209 //////////////////////////////////////////////////////////////
2210
MCMC_Print_Param_Stdin(t_mcmc * mcmc,t_tree * tree)2211 void MCMC_Print_Param_Stdin(t_mcmc *mcmc, t_tree *tree)
2212 {
2213 time_t cur_time;
2214
2215 time(&cur_time);
2216
2217
2218
2219 if(mcmc->run == 1)
2220 {
2221 PhyML_Printf("\n\n");
2222 PhyML_Printf("%9s","Run");
2223 PhyML_Printf(" %5s","Time");
2224 PhyML_Printf(" %10s","Likelihood");
2225 PhyML_Printf(" %10s","Prior");
2226 PhyML_Printf(" %19s","SubstRate[ ESS ]");
2227 PhyML_Printf(" %17s","TreeHeight[ ESS ]");
2228 if(tree->rates->model == THORNE || tree->rates->model == GUINDON) PhyML_Printf(" %16s","AutoCor[ ESS ]");
2229 else PhyML_Printf(" %16s","RateVar[ ESS ]");
2230 PhyML_Printf(" %15s","BirthR[ ESS ]");
2231 PhyML_Printf(" %8s","MinESS");
2232 }
2233
2234 if((cur_time - mcmc->t_last_print) > mcmc->print_every)
2235 {
2236 mcmc->t_last_print = cur_time;
2237 PhyML_Printf("\n");
2238 PhyML_Printf("%9d",tree->mcmc->run);
2239 PhyML_Printf(" %5d",(int)(cur_time-mcmc->t_beg));
2240 PhyML_Printf(" %10.2f",tree->c_lnL);
2241 PhyML_Printf(" %10.2f",(tree->rates ? tree->rates->c_lnL_rates+tree->times->c_lnL_times : +1));
2242 PhyML_Printf(" %12.6f[%5.0f]",RATES_Average_Substitution_Rate(tree),tree->mcmc->ess[tree->mcmc->num_move_clock_r]);
2243 /* PhyML_Printf("\t%12.6f[%5.0f]",tree->rates->clock_r,tree->mcmc->ess[tree->mcmc->num_move_clock_r]); */
2244 PhyML_Printf(" %9f[%5.0f]",
2245 (tree->rates ? tree->rates->nu : -1.),
2246 tree->mcmc->ess[tree->mcmc->num_move_nu]);
2247 PhyML_Printf(" %8f[%5.0f]",
2248 (tree->rates ? tree->times->birth_rate : -1.),
2249 tree->mcmc->ess[tree->mcmc->num_move_birth_rate]);
2250 }
2251 }
2252
2253 //////////////////////////////////////////////////////////////
2254 //////////////////////////////////////////////////////////////
2255
MCMC_Print_Param(t_mcmc * mcmc,t_tree * tree)2256 void MCMC_Print_Param(t_mcmc *mcmc, t_tree *tree)
2257 {
2258 int i;
2259 FILE *fp;
2260 char *s;
2261 int orig_approx;
2262 phydbl orig_lnL;
2263 char *s_tree;
2264
2265 if(tree->mcmc->run > mcmc->chain_len) return;
2266
2267 s = (char *)mCalloc(100,sizeof(char));
2268
2269 fp = mcmc->out_fp_stats;
2270
2271 /* if(tree->mcmc->run == 0) */
2272 /* { */
2273 /* PhyML_Fprintf(stdout," ["); */
2274 /* fflush(NULL); */
2275 /* } */
2276
2277 /* if(!(mcmc->run%(mcmc->chain_len/10))) */
2278 /* { */
2279 /* PhyML_Fprintf(stdout,"."); */
2280 /* fflush(NULL); */
2281 /* } */
2282
2283 /* if(tree->mcmc->run == mcmc->chain_len) */
2284 /* { */
2285 /* PhyML_Fprintf(stdout,"]"); */
2286 /* fflush(NULL); */
2287 /* } */
2288
2289
2290 /* MCMC_Print_Means(mcmc,tree); */
2291 /* MCMC_Print_Last(mcmc,tree); */
2292
2293
2294
2295 if(!(mcmc->run%mcmc->sample_interval))
2296 {
2297 MCMC_Copy_To_New_Param_Val(tree->mcmc,tree);
2298
2299 for(i=0;i<tree->mcmc->n_moves;i++)
2300 {
2301 if((tree->mcmc->acc_rate[i] > .1) && (tree->mcmc->start_ess[i] == NO)) tree->mcmc->start_ess[i] = YES;
2302 if(tree->mcmc->start_ess[i] == YES) MCMC_Update_Effective_Sample_Size(i,tree->mcmc,tree);
2303 if(tree->mcmc->run > (int) tree->mcmc->chain_len * 0.1) tree->mcmc->adjust_tuning[i] = NO;
2304 }
2305
2306 if(tree->mcmc->run == 0)
2307 {
2308 time(&(mcmc->t_beg));
2309 time(&(mcmc->t_last_print));
2310
2311 PhyML_Fprintf(fp,"# Random seed: %d",tree->io->r_seed);
2312 PhyML_Fprintf(fp,"\n");
2313 PhyML_Fprintf(fp,"Run\t");
2314 /* PhyML_Fprintf(fp,"Time\t"); */
2315 /* PhyML_Fprintf(fp,"MeanRate\t"); */
2316 /* PhyML_Fprintf(fp,"NormFact\t"); */
2317
2318 /* for(i=0;i<mcmc->n_moves;i++) */
2319 /* { */
2320 /* strcpy(s,"Acc."); */
2321 /* PhyML_Fprintf(fp,"%s%d\t",strcat(s,mcmc->move_name[i]),i); */
2322 /* } */
2323
2324 /* for(i=0;i<mcmc->n_moves;i++) */
2325 /* { */
2326 /* strcpy(s,"Tune."); */
2327 /* PhyML_Fprintf(fp,"%s%d\t",strcat(s,mcmc->move_name[i]),i); */
2328 /* } */
2329
2330 /* for(i=0;i<mcmc->n_moves;i++) */
2331 /* { */
2332 /* strcpy(s,"Run."); */
2333 /* PhyML_Fprintf(fp,"%s\t",strcat(s,mcmc->move_name[i])); */
2334 /* } */
2335
2336 PhyML_Fprintf(fp,"LnLike[Exact]\t");
2337 PhyML_Fprintf(fp,"LnLike[Approx]\t");
2338 PhyML_Fprintf(fp,"LnRates\t");
2339 PhyML_Fprintf(fp,"LnTimes\t");
2340 PhyML_Fprintf(fp,"LnPosterior\t");
2341 PhyML_Fprintf(fp,"ClockRate\t");
2342 PhyML_Fprintf(fp,"EvolRate\t");
2343 PhyML_Fprintf(fp,"Nu\t");
2344 PhyML_Fprintf(fp,"BirthRate\t");
2345 PhyML_Fprintf(fp,"TsTv\t");
2346
2347
2348 if(tree->mod->ras->n_catg > 1)
2349 {
2350 if(tree->mod->ras->free_mixt_rates == NO) PhyML_Fprintf(fp,"Alpha\t");
2351 else
2352 {
2353 for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"p%d\t",i);
2354 for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"r%d\t",i);
2355 }
2356 }
2357
2358
2359 if(tree->mod->m4mod->n_h > 1 && tree->mod->use_m4mod == YES)
2360 {
2361 for(i=0;i<tree->mod->m4mod->n_h;i++) PhyML_Fprintf(fp,"cov_p%d\t",i);
2362 for(i=0;i<tree->mod->m4mod->n_h;i++) PhyML_Fprintf(fp,"cov_r%d\t",i);
2363 PhyML_Fprintf(fp,"cov_switch\t");
2364 }
2365
2366
2367 if(fp != stdout)
2368 {
2369 for(i=tree->n_otu;i<2*tree->n_otu-1;i++)
2370 {
2371 if(tree->a_nodes[i] == tree->n_root->v[2])
2372 PhyML_Fprintf(fp,"T%d%s\t",i,"[LeftRoot]");
2373 else if(tree->a_nodes[i] == tree->n_root->v[1])
2374 PhyML_Fprintf(fp,"T%d%s\t",i,"[RightRoot]");
2375 else if(tree->a_nodes[i] == tree->n_root)
2376 PhyML_Fprintf(fp,"T%d%s\t",i,"[Root]");
2377 else
2378 PhyML_Fprintf(fp,"T%d[%d]\t",i,tree->a_nodes[i]->anc->num);
2379 }
2380 }
2381
2382
2383 /* if(fp != stdout) */
2384 /* { */
2385 /* For(i,2*tree->n_otu-1) */
2386 /* { */
2387 /* if(tree->a_nodes[i] == tree->n_root->v[2]) */
2388 /* PhyML_Fprintf(fp,"R%d[LeftRoot]\t",i); */
2389 /* else if(tree->a_nodes[i] == tree->n_root->v[1]) */
2390 /* PhyML_Fprintf(fp,"R%d[RightRoot]\t",i); */
2391 /* else if(tree->a_nodes[i] != tree->n_root) */
2392 /* PhyML_Fprintf(fp," R%d[%d]\t",i,tree->a_nodes[i]->anc->num); */
2393 /* else */
2394 /* PhyML_Fprintf(fp," R%d[Root]\t",i); */
2395
2396 /* /\* PhyML_Fprintf(fp," R%d[%f]\t",i,tree->rates->mean_l[i]); *\/ */
2397 /* } */
2398 /* } */
2399
2400
2401 if(fp != stdout)
2402 {
2403 For(i,2*tree->n_otu-2)
2404 {
2405 if(tree->a_nodes[i] == tree->n_root->v[2])
2406 PhyML_Fprintf(fp,"B%d[LeftRoot]\t",i);
2407 else if(tree->a_nodes[i] == tree->n_root->v[1])
2408 PhyML_Fprintf(fp,"B%d[RightRoot]\t",i);
2409 else
2410 PhyML_Fprintf(fp," B%d[%d]\t",i,tree->a_nodes[i]->anc->num);
2411
2412 /* PhyML_Fprintf(fp," R%d[%f]\t",i,tree->rates->mean_l[i]); */
2413 }
2414 }
2415
2416
2417 /* if(fp != stdout) */
2418 /* { */
2419 /* For(i,2*tree->n_otu-2) */
2420 /* { */
2421 /* if(tree->a_nodes[i] == tree->n_root->v[2]) */
2422 /* PhyML_Fprintf(fp,"G%d[LeftRoot]\t",i); */
2423 /* else if(tree->a_nodes[i] == tree->n_root->v[1]) */
2424 /* PhyML_Fprintf(fp,"G%d[RightRoot]\t",i); */
2425 /* else */
2426 /* PhyML_Fprintf(fp," G%d[%f]\t",i,tree->rates->ml_l[i]); */
2427
2428
2429 /* /\* PhyML_Fprintf(fp," R%d[%f]\t",i,tree->rates->mean_l[i]); *\/ */
2430 /* } */
2431 /* } */
2432
2433
2434 /* if(fp != stdout) */
2435 /* { */
2436 /* For(i,2*tree->n_otu-3) */
2437 /* { */
2438 /* if(tree->a_edges[i] == tree->e_root) */
2439 /* PhyML_Fprintf(fp,"*L[%f]%d\t",i,tree->rates->u_ml_l[i]); */
2440 /* else */
2441 /* PhyML_Fprintf(fp," L[%f]%d\t",i,tree->rates->u_ml_l[i]); */
2442 /* } */
2443 /* } */
2444
2445
2446 PhyML_Fprintf(mcmc->out_fp_trees,"#NEXUS\n");
2447 PhyML_Fprintf(mcmc->out_fp_trees,"BEGIN TREES;\n");
2448 PhyML_Fprintf(mcmc->out_fp_trees,"\tTRANSLATE\n");
2449 for(i=0;i<tree->n_otu-1;i++) PhyML_Fprintf(mcmc->out_fp_trees,"\t%3d\t%s,\n",tree->a_nodes[i]->num+1,tree->a_nodes[i]->name);
2450 PhyML_Fprintf(mcmc->out_fp_trees,"\t%3d\t%s;\n",tree->a_nodes[i]->num+1,tree->a_nodes[i]->name);
2451 tree->write_tax_names = NO;
2452 }
2453
2454 PhyML_Fprintf(fp,"\n");
2455
2456 PhyML_Fprintf(fp,"%6d\t",tree->mcmc->run);
2457
2458 /* time(&mcmc->t_cur); */
2459 /* PhyML_Fprintf(fp,"%6d\t",(int)(mcmc->t_cur-mcmc->t_beg)); */
2460
2461 /* RATES_Update_Cur_Bl(tree); */
2462 /* PhyML_Fprintf(fp,"%f\t",RATES_Check_Mean_Rates(tree)); */
2463
2464 /* PhyML_Fprintf(fp,"%f\t",tree->rates->norm_fact); */
2465 /* for(i=0;i<tree->mcmc->n_moves;i++) PhyML_Fprintf(fp,"%f\t",tree->mcmc->acc_rate[i]); */
2466 /* for(i=0;i<tree->mcmc->n_moves;i++) PhyML_Fprintf(fp,"%f\t",(phydbl)(tree->mcmc->tune_move[i])); */
2467 /* for(i=0;i<tree->mcmc->n_moves;i++) PhyML_Fprintf(fp,"%d\t",(int)(tree->mcmc->run_move[i])); */
2468
2469 orig_approx = tree->io->lk_approx;
2470 orig_lnL = tree->c_lnL;
2471 tree->io->lk_approx = EXACT;
2472 Lk(NULL,tree);
2473 PhyML_Fprintf(fp,"%.1f\t",tree->c_lnL);
2474 tree->io->lk_approx = NORMAL;
2475 tree->c_lnL = 0.0;
2476 Lk(NULL,tree);
2477 PhyML_Fprintf(fp,"%.1f\t",tree->c_lnL);
2478 tree->io->lk_approx = orig_approx;
2479 tree->c_lnL = orig_lnL;
2480
2481 /* PhyML_Fprintf(fp,"0\t0\t"); */
2482
2483 PhyML_Fprintf(fp,"%G\t",tree->rates->c_lnL_rates);
2484 PhyML_Fprintf(fp,"%G\t",tree->times->c_lnL_times);
2485 PhyML_Fprintf(fp,"%G\t",tree->c_lnL+tree->rates->c_lnL_rates+tree->times->c_lnL_times);
2486 PhyML_Fprintf(fp,"%G\t",tree->rates->clock_r);
2487 PhyML_Fprintf(fp,"%G\t",RATES_Average_Substitution_Rate(tree));
2488 PhyML_Fprintf(fp,"%G\t",tree->rates->nu);
2489 PhyML_Fprintf(fp,"%G\t",tree->times->birth_rate);
2490 PhyML_Fprintf(fp,"%G\t",tree->mod->kappa->v);
2491
2492 if(tree->mod->ras->n_catg > 1)
2493 {
2494 if(tree->mod->ras->free_mixt_rates == NO)
2495 PhyML_Fprintf(fp,"%G\t",tree->mod->ras->alpha->v);
2496 else
2497 {
2498 for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->ras->gamma_r_proba->v[i]);
2499 for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->ras->gamma_rr->v[i]);
2500 /* for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->ras->gamma_r_proba_unscaled[i]); */
2501 /* for(i=0;i<tree->mod->ras->n_catg;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->ras->gamma_rr_unscaled[i]); */
2502 }
2503 }
2504
2505 if(tree->mod->m4mod->n_h > 1 && tree->mod->use_m4mod == YES)
2506 {
2507 for(i=0;i<tree->mod->m4mod->n_h;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->m4mod->h_fq[i]);
2508 for(i=0;i<tree->mod->m4mod->n_h;i++) PhyML_Fprintf(fp,"%G\t",tree->mod->m4mod->multipl[i]);
2509 PhyML_Fprintf(fp,"%G\t",tree->mod->m4mod->delta);
2510 }
2511
2512
2513 char *format = (char *)mCalloc(100,sizeof(char));
2514 sprintf(format,"%%.%df\t",tree->mcmc->nd_t_digits);
2515 for(i=tree->n_otu;i<2*tree->n_otu-1;i++) PhyML_Fprintf(fp,format,tree->times->nd_t[i]);
2516 Free(format);
2517
2518 /* for(i=0;i<2*tree->n_otu-1;i++) PhyML_Fprintf(fp,"%.4f\t",log(tree->rates->nd_r[i])); */
2519
2520 // Average rate along edges: length divided by elapsed time
2521 For(i,2*tree->n_otu-2)
2522 PhyML_Fprintf(fp,"%.4f\t",
2523 tree->rates->cur_l[i]/(tree->times->nd_t[tree->a_nodes[i]->num] - tree->times->nd_t[tree->a_nodes[i]->anc->num]));
2524
2525 /* fp_pred = fopen("predict.txt","a"); */
2526 /* for(i=0;i<2*tree->n_otu-2;i++) */
2527 /* PhyML_Fprintf(fp_pred,"B%d\t%12f\t%12f\t%4d\n",i,exp(tree->rates->br_r[i]),tree->times->nd_t[i],tree->times->has_survived[i]); */
2528 /* fclose(fp_pred); */
2529
2530
2531 /* phydbl p,sd,mean; */
2532
2533 /* for(i=0;i<2*tree->n_otu-2;i++) */
2534 /* { */
2535 /* sd = tree->rates->nu * (tree->times->nd_t[i] - tree->times->nd_t[tree->a_nodes[i]->anc->num]); */
2536 /* mean = tree->rates->br_r[tree->a_nodes[i]->anc->num] - .5*sd*sd; */
2537 /* p = Pnorm(tree->rates->br_r[i],mean,sd); */
2538 /* PhyML_Fprintf(fp,"%f\t",p); */
2539 /* tree->rates->mean_r[i] += p; */
2540 /* } */
2541
2542 /* for(i=0;i<2*tree->n_otu-2;i++) PhyML_Fprintf(fp,"%.4f\t",tree->rates->cur_gamma_prior_mean[i]); */
2543 /* if(fp != stdout) for(i=tree->n_otu;i<2*tree->n_otu-1;i++) PhyML_Fprintf(fp,"%G\t",tree->times->t_prior[i]); */
2544 /* For(i,2*tree->n_otu-3) PhyML_Fprintf(fp,"%f\t",exp(tree->a_edges[i]->l->v)); */
2545
2546
2547 /* RATES_Update_Cur_Bl(tree); */
2548 /* For(i,2*tree->n_otu-3) PhyML_Fprintf(fp,"%f\t",tree->a_edges[i]->l->v); */
2549
2550
2551 for(i=0;i<2*tree->n_otu-2;++i) tree->rates->mean_r[i] = exp(tree->rates->br_r[i]);
2552 for(i=0;i<2*tree->n_otu-1;++i) tree->times->mean_t[i] = tree->times->nd_t[i];
2553
2554 /* Time_To_Branch(tree); */
2555 /* char *s; */
2556 /* s = (char *)mCalloc(T_MAX_NAME,sizeof(char)); */
2557 /* strcpy(s,mcmc->io->in_align_file); */
2558 /* strcat(s,"_"); */
2559 /* strcat(s,mcmc->out_filename); */
2560 /* strcat(s,".ps"); */
2561 /* DR_Draw_Tree(s,tree); */
2562 /* Free(s); */
2563
2564 /* FILE *fp; */
2565 /* int j; */
2566 /* t_node *d, *v1, *v2; */
2567 /* int n1, n2; */
2568 /* phydbl r1, r2; */
2569
2570 /* s = (char *)mCalloc(T_MAX_NAME,sizeof(char)); */
2571 /* strcpy(s,mcmc->io->in_align_file); */
2572 /* strcat(s,"_"); */
2573 /* strcat(s,mcmc->out_filename); */
2574 /* strcat(s,".corr"); */
2575 /* fp = fopen(s,"w"); */
2576
2577 /* n1 = n2 = 0; */
2578 /* r1 = r2 = 0.f; */
2579 /* For(i,2*tree->n_otu-3) */
2580 /* { */
2581 /* if(tree->a_nodes[i]->tax == NO) */
2582 /* { */
2583 /* d = tree->a_nodes[i]; */
2584 /* v1 = v2 = NULL; */
2585 /* for(j=0;j<3;j++) */
2586 /* { */
2587 /* if(d->v[j] != d->anc && d->b[j] != tree->e_root) */
2588 /* { */
2589 /* if(!v1) v1 = d->v[j]; */
2590 /* else v2 = d->v[j]; */
2591 /* } */
2592 /* } */
2593
2594 /* for(j=0;j<3;j++) */
2595 /* { */
2596 /* if(v1->v[j] && v1->v[j] == d) */
2597 /* { */
2598 /* n1 = v1->bip_size[j]; */
2599 /* /\* r1 = RATES_Get_Mean_Rate_In_Subtree(v1,tree); *\/ */
2600 /* r1 = exp(tree->rates->br_r[v1->num]); */
2601 /* break; */
2602 /* } */
2603 /* } */
2604
2605
2606 /* for(j=0;j<3;j++) */
2607 /* { */
2608 /* if(v2->v[j] && v2->v[j] == d) */
2609 /* { */
2610 /* n2 = v2->bip_size[j]; */
2611 /* /\* r2 = RATES_Get_Mean_Rate_In_Subtree(v2,tree); *\/ */
2612 /* r2 = exp(tree->rates->br_r[v2->num]); */
2613 /* break; */
2614 /* } */
2615 /* } */
2616
2617 /* fprintf(fp,"\n%4d %4d %15f %15f",n1,n2,r1,r2); */
2618 /* } */
2619 /* } */
2620
2621 /* fclose(fp); */
2622
2623 // TREES
2624 TIMES_Time_To_Bl(tree);
2625 tree->bl_ndigits = 3;
2626 s_tree = Write_Tree(tree);
2627 tree->bl_ndigits = 7;
2628 PhyML_Fprintf(mcmc->out_fp_trees,"TREE %8d [%f] = [&R] %s\n",mcmc->run,tree->c_lnL,s_tree);
2629 Free(s_tree);
2630 }
2631
2632 if(tree->mcmc->run == mcmc->chain_len) PhyML_Fprintf(mcmc->out_fp_trees,"END;\n");
2633
2634 fflush(NULL);
2635
2636 Free(s);
2637
2638 }
2639
2640 //////////////////////////////////////////////////////////////
2641 //////////////////////////////////////////////////////////////
2642
2643
MCMC_Print_Means(t_mcmc * mcmc,t_tree * tree)2644 void MCMC_Print_Means(t_mcmc *mcmc, t_tree *tree)
2645 {
2646
2647 if(!(mcmc->run%mcmc->sample_interval))
2648 {
2649 int i;
2650 char *s;
2651
2652 s = (char *)mCalloc(T_MAX_FILE,sizeof(char));
2653
2654 strcpy(s,tree->mcmc->out_filename);
2655 strcat(s,".means");
2656
2657 fclose(mcmc->out_fp_means);
2658
2659 mcmc->out_fp_means = fopen(s,"w");
2660
2661 PhyML_Fprintf(mcmc->out_fp_means,"#");
2662 for(i=tree->n_otu;i<2*tree->n_otu-1;i++) PhyML_Fprintf(mcmc->out_fp_means,"T%d\t",i);
2663
2664 PhyML_Fprintf(mcmc->out_fp_means,"\n");
2665
2666 for(i=tree->n_otu;i<2*tree->n_otu-1;i++) tree->times->t_mean[i] *= (phydbl)(mcmc->run / mcmc->sample_interval);
2667
2668 for(i=tree->n_otu;i<2*tree->n_otu-1;i++)
2669 {
2670 tree->times->t_mean[i] += tree->times->nd_t[i];
2671 tree->times->t_mean[i] /= (phydbl)(mcmc->run / mcmc->sample_interval + 1);
2672
2673 /* PhyML_Fprintf(tree->mcmc->out_fp_means,"%d\t",tree->mcmc->run / tree->mcmc->sample_interval); */
2674 PhyML_Fprintf(tree->mcmc->out_fp_means,"%.1f\t",tree->times->t_mean[i]);
2675 }
2676
2677 PhyML_Fprintf(tree->mcmc->out_fp_means,"\n");
2678 fflush(NULL);
2679
2680 Free(s);
2681 }
2682 }
2683
2684 //////////////////////////////////////////////////////////////
2685 //////////////////////////////////////////////////////////////
2686
2687
MCMC_Print_Last(t_mcmc * mcmc,t_tree * tree)2688 void MCMC_Print_Last(t_mcmc *mcmc, t_tree *tree)
2689 {
2690
2691 if(!(mcmc->run%mcmc->sample_interval))
2692 {
2693 int i;
2694 char *s;
2695
2696 s = (char *)mCalloc(T_MAX_FILE,sizeof(char));
2697
2698 strcpy(s,tree->mcmc->out_filename);
2699 strcat(s,".lasts");
2700
2701 fclose(mcmc->out_fp_last);
2702
2703 mcmc->out_fp_last = fopen(s,"w");
2704
2705 /* rewind(mcmc->out_fp_last); */
2706
2707 PhyML_Fprintf(mcmc->out_fp_last,"#");
2708 PhyML_Fprintf(tree->mcmc->out_fp_last,"Time\t");
2709
2710 for(i=tree->n_otu;i<2*tree->n_otu-1;i++)
2711 PhyML_Fprintf(tree->mcmc->out_fp_last,"T%d\t",i);
2712
2713 PhyML_Fprintf(tree->mcmc->out_fp_last,"\n");
2714
2715 if(mcmc->run)
2716 {
2717 time(&(mcmc->t_cur));
2718 PhyML_Fprintf(tree->mcmc->out_fp_last,"%d\t",(int)(mcmc->t_cur-mcmc->t_beg));
2719 /* PhyML_Fprintf(tree->mcmc->out_fp_last,"%d\t",(int)(mcmc->t_beg)); */
2720 }
2721
2722 for(i=tree->n_otu;i<2*tree->n_otu-1;i++) PhyML_Fprintf(tree->mcmc->out_fp_last,"%.1f\t",tree->times->nd_t[i]);
2723
2724 PhyML_Fprintf(tree->mcmc->out_fp_last,"\n");
2725 fflush(NULL);
2726
2727 Free(s);
2728 }
2729 }
2730
2731
2732 //////////////////////////////////////////////////////////////
2733 //////////////////////////////////////////////////////////////
2734
MCMC_Pause(t_mcmc * mcmc)2735 void MCMC_Pause(t_mcmc *mcmc)
2736 {
2737 char choice;
2738 char *s;
2739 int len;
2740
2741 s = (char *)mCalloc(100,sizeof(char));
2742
2743
2744 if(!(mcmc->run%mcmc->chain_len) && (mcmc->is_burnin == NO))
2745 {
2746 PhyML_Printf("\n. Do you wish to stop the analysis [N/y] ");
2747 if(!scanf("%c",&choice)) Exit("\n");
2748 if(choice == '\n') choice = 'N';
2749 else getchar(); /* \n */
2750
2751 Uppercase(&choice);
2752
2753 switch(choice)
2754 {
2755 case 'N':
2756 {
2757 len = 1E+4;
2758 PhyML_Printf("\n. How many extra generations is required [default: 1E+4] ");
2759 Getstring_Stdin(s);
2760 if(s[0] == '\0') len = 1E+4;
2761 else len = (int)atof(s);
2762
2763 if(len < 0)
2764 {
2765 PhyML_Fprintf(stderr,"\n. The value entered must be an integer greater than 0.\n");
2766 Exit("\n");
2767 }
2768 mcmc->chain_len += len;
2769 break;
2770 }
2771
2772 case 'Y':
2773 {
2774 PhyML_Printf("\n. Ok. Done.\n");
2775 Exit("\n");
2776 break;
2777 }
2778
2779 default:
2780 {
2781 PhyML_Printf("\n. Please enter 'Y' or 'N'.\n");
2782 Exit("\n");
2783 }
2784 }
2785 }
2786
2787 Free(s);
2788
2789 }
2790
2791 //////////////////////////////////////////////////////////////
2792 //////////////////////////////////////////////////////////////
2793
MCMC_Terminate()2794 void MCMC_Terminate()
2795 {
2796 char choice;
2797 PhyML_Printf("\n\n. Do you really want to terminate [Y/n]: ");
2798 if(!scanf("%c",&choice)) Exit("\n");
2799 if(choice == '\n') choice = 'Y';
2800 else getchar(); /* \n */
2801 Uppercase(&choice);
2802 if(choice == 'Y') raise(SIGTERM);
2803 }
2804
2805
2806 //////////////////////////////////////////////////////////////
2807 //////////////////////////////////////////////////////////////
2808
2809 //////////////////////////////////////////////////////////////
2810 //////////////////////////////////////////////////////////////
2811
2812
MCMC_Copy_MCMC_Struct(t_mcmc * ori,t_mcmc * cpy,char * filename)2813 void MCMC_Copy_MCMC_Struct(t_mcmc *ori, t_mcmc *cpy, char *filename)
2814 {
2815 int pid;
2816 int i;
2817
2818 cpy->sample_interval = ori->sample_interval ;
2819 cpy->chain_len = ori->chain_len ;
2820 cpy->randomize = ori->randomize ;
2821 cpy->norm_freq = ori->norm_freq ;
2822 cpy->n_moves = ori->n_moves ;
2823 cpy->max_tune = ori->max_tune ;
2824 cpy->min_tune = ori->min_tune ;
2825 cpy->print_every = ori->print_every ;
2826 cpy->is_burnin = ori->is_burnin ;
2827 cpy->is = ori->is ;
2828 cpy->io = ori->io ;
2829 cpy->in_fp_par = ori->in_fp_par ;
2830 cpy->nd_t_digits = ori->nd_t_digits ;
2831 cpy->max_lag = ori->max_lag ;
2832 cpy->move_idx = ori->move_idx ;
2833
2834 for(i=0;i<cpy->n_moves;i++)
2835 {
2836 cpy->start_ess[i] = ori->start_ess[i];
2837 cpy->ess_run[i] = ori->ess_run[i];
2838 cpy->ess[i] = ori->ess[i];
2839 cpy->move_weight[i] = ori->move_weight[i];
2840 cpy->run_move[i] = ori->run_move[i];
2841 cpy->acc_move[i] = ori->acc_move[i];
2842 cpy->prev_run_move[i] = ori->prev_run_move[i];
2843 cpy->prev_acc_move[i] = ori->prev_acc_move[i];
2844 cpy->acc_rate[i] = ori->acc_rate[i];
2845 cpy->tune_move[i] = ori->tune_move[i];
2846 strcpy(cpy->move_name[i],ori->move_name[i]);
2847 cpy->adjust_tuning[i] = ori->adjust_tuning[i];
2848 }
2849
2850
2851
2852 if(filename)
2853 {
2854 char *s;
2855
2856 s = (char *)mCalloc(T_MAX_NAME,sizeof(char));
2857
2858 strcpy(cpy->out_filename,filename);
2859 pid = getpid();
2860 sprintf(cpy->out_filename+strlen(cpy->out_filename),"_%d",pid);
2861
2862 strcpy(s,cpy->io->in_align_file);
2863 strcat(s,"_");
2864 strcat(s,cpy->out_filename);
2865 strcat(s,"_stats");
2866 cpy->out_fp_stats = fopen(s,"w");
2867
2868 strcpy(s,cpy->io->in_align_file);
2869 strcat(s,"_");
2870 strcat(s,cpy->out_filename);
2871 strcat(s,"_trees");
2872 cpy->out_fp_trees = fopen(s,"w");
2873
2874 strcpy(s,cpy->io->in_align_file);
2875 strcat(s,"_");
2876 strcat(s,cpy->out_filename);
2877 strcat(s,"_constree");
2878 cpy->out_fp_constree = fopen(s,"w");
2879
2880 Free(s);
2881 }
2882 else
2883 {
2884 cpy->out_fp_stats = stderr;
2885 cpy->out_fp_trees = stderr;
2886 }
2887 }
2888
2889
2890 //////////////////////////////////////////////////////////////
2891 //////////////////////////////////////////////////////////////
2892
2893
MCMC_Close_MCMC(t_mcmc * mcmc)2894 void MCMC_Close_MCMC(t_mcmc *mcmc)
2895 {
2896 fclose(mcmc->out_fp_trees);
2897 fclose(mcmc->out_fp_stats);
2898 fclose(mcmc->out_fp_constree);
2899 /* fclose(mcmc->out_fp_means); */
2900 /* fclose(mcmc->out_fp_last); */
2901 }
2902
2903 //////////////////////////////////////////////////////////////
2904 //////////////////////////////////////////////////////////////
2905
MCMC_Randomize_Kappa(t_tree * tree)2906 void MCMC_Randomize_Kappa(t_tree *tree)
2907 {
2908 tree->mod->kappa->v = Uni()*5.;
2909 }
2910
2911 //////////////////////////////////////////////////////////////
2912 //////////////////////////////////////////////////////////////
2913
MCMC_Randomize_Rate_Across_Sites(t_tree * tree)2914 void MCMC_Randomize_Rate_Across_Sites(t_tree *tree)
2915 {
2916 if(tree->mod->ras->n_catg == 1) return;
2917
2918 if(tree->mod->ras->free_mixt_rates == YES)
2919 {
2920 int i;
2921 for(i=0;i<tree->mod->ras->n_catg-1;i++) tree->mod->ras->gamma_r_proba_unscaled->v[i] = Uni();
2922 tree->mod->ras->gamma_r_proba_unscaled->v[tree->mod->ras->n_catg-1] = 1.;
2923 for(i=0;i<tree->mod->ras->n_catg-1;i++) tree->mod->ras->gamma_rr_unscaled->v[i] = (phydbl)i+0.1; /* Do not randomize those as their ordering matter */
2924 tree->mod->ras->gamma_rr_unscaled->v[tree->mod->ras->n_catg-1] = (phydbl)tree->mod->ras->n_catg;
2925 }
2926 else
2927 {
2928 tree->mod->ras->alpha->v = Uni()*5.;
2929 }
2930 }
2931
2932 //////////////////////////////////////////////////////////////
2933 //////////////////////////////////////////////////////////////
2934
2935
MCMC_Randomize_Covarion_Rates(t_tree * tree)2936 void MCMC_Randomize_Covarion_Rates(t_tree *tree)
2937 {
2938 if(tree->mod->use_m4mod == NO) return;
2939
2940 if(tree->mod->m4mod->n_h == 1) return;
2941
2942 int i;
2943
2944 for(i=0;i<tree->mod->m4mod->n_h;i++)
2945 {
2946 tree->mod->m4mod->multipl_unscaled[i] = (phydbl)i+1.;
2947 tree->mod->m4mod->h_fq_unscaled[i] = Uni()*(100.-0.01) + 0.01;
2948 }
2949 }
2950
2951 //////////////////////////////////////////////////////////////
2952 //////////////////////////////////////////////////////////////
2953
2954
MCMC_Randomize_Covarion_Switch(t_tree * tree)2955 void MCMC_Randomize_Covarion_Switch(t_tree *tree)
2956 {
2957 if(tree->mod->use_m4mod == NO) return;
2958
2959 tree->mod->m4mod->delta = Uni()*(10.-0.01)+0.01;
2960 }
2961
2962 //////////////////////////////////////////////////////////////
2963 //////////////////////////////////////////////////////////////
2964
MCMC_Randomize_Branch_Lengths(t_tree * tree)2965 void MCMC_Randomize_Branch_Lengths(t_tree *tree)
2966 {
2967 int i;
2968
2969 if(tree->mod->log_l == NO)
2970 For(i,2*tree->n_otu-3) tree->a_edges[i]->l->v = Rexp(10.);
2971 else
2972 For(i,2*tree->n_otu-3) tree->a_edges[i]->l->v = -4* Uni();
2973 }
2974
2975 //////////////////////////////////////////////////////////////
2976 //////////////////////////////////////////////////////////////
2977
MCMC_Randomize_Node_Rates(t_tree * tree)2978 void MCMC_Randomize_Node_Rates(t_tree *tree)
2979 {
2980
2981 int i,err;
2982 phydbl mean_r, var_r;
2983 phydbl min_r, max_r;
2984
2985 mean_r = 1.0;
2986 var_r = 0.5;
2987 min_r = tree->rates->min_rate;
2988 max_r = tree->rates->max_rate;
2989
2990 for(i=0;i<2*tree->n_otu-2;++i)
2991 if(tree->a_nodes[i] != tree->n_root)
2992 {
2993 tree->rates->nd_r[i] = Rnorm_Trunc(mean_r,SQRT(var_r),min_r,max_r,&err);
2994 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
2995 }
2996 }
2997
2998 //////////////////////////////////////////////////////////////
2999 //////////////////////////////////////////////////////////////
3000
MCMC_Randomize_Rates(t_tree * tree)3001 void MCMC_Randomize_Rates(t_tree *tree)
3002 {
3003
3004 /* Should be called once t_node times have been determined */
3005
3006 int i;
3007
3008 For(i,2*tree->n_otu-2) tree->rates->br_r[i] = 1.0;
3009
3010 /* For(i,2*tree->n_otu-2) */
3011 /* { */
3012 /* u = Uni(); */
3013 /* u = u * (r_max-r_min) + r_min; */
3014 /* tree->rates->br_r[i] = u; */
3015
3016 /* if(tree->rates->br_r[i] < tree->rates->min_rate) tree->rates->br_r[i] = tree->rates->min_rate; */
3017 /* if(tree->rates->br_r[i] > tree->rates->max_rate) tree->rates->br_r[i] = tree->rates->max_rate; */
3018 /* } */
3019
3020 MCMC_Randomize_Rates_Pre(tree->n_root,tree->n_root->v[2],tree);
3021 MCMC_Randomize_Rates_Pre(tree->n_root,tree->n_root->v[1],tree);
3022 }
3023
3024 //////////////////////////////////////////////////////////////
3025 //////////////////////////////////////////////////////////////
3026
MCMC_Randomize_Rates_Pre(t_node * a,t_node * d,t_tree * tree)3027 void MCMC_Randomize_Rates_Pre(t_node *a, t_node *d, t_tree *tree)
3028 {
3029 int i;
3030 phydbl mean_r, sd_r;
3031 phydbl min_r, max_r;
3032 int err;
3033
3034 mean_r = 1.0;
3035 sd_r = 1.0;
3036 min_r = tree->rates->min_rate;
3037 max_r = tree->rates->max_rate;
3038 err = -1;
3039
3040 tree->rates->br_r[d->num] = Rnorm_Trunc(mean_r,sd_r,min_r,max_r,&err);
3041 if(err == YES)
3042 {
3043 PhyML_Fprintf(stderr,"\n. mean_r: %f sd_r: %f min_r: %f max_r: %f",
3044 mean_r,
3045 sd_r,
3046 min_r,
3047 max_r);
3048 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3049 }
3050 if(d->tax) return;
3051 else
3052 {
3053 for(i=0;i<3;++i)
3054 if(d->v[i] != a && d->b[i] != tree->e_root)
3055 MCMC_Randomize_Rates_Pre(d,d->v[i],tree);
3056 }
3057 }
3058
3059 //////////////////////////////////////////////////////////////
3060 //////////////////////////////////////////////////////////////
3061
MCMC_Randomize_Birth(t_tree * tree)3062 void MCMC_Randomize_Birth(t_tree *tree)
3063 {
3064 phydbl min_b,max_b;
3065 phydbl u;
3066
3067 min_b = tree->times->birth_rate_min;
3068 max_b = MIN(0.5,tree->times->birth_rate_max);
3069
3070 u = Uni();
3071 tree->times->birth_rate = (max_b - min_b) * u + min_b;
3072 }
3073
3074 //////////////////////////////////////////////////////////////
3075 //////////////////////////////////////////////////////////////
3076
MCMC_Randomize_Death(t_tree * tree)3077 void MCMC_Randomize_Death(t_tree *tree)
3078 {
3079 phydbl min_d,max_d;
3080 phydbl u;
3081
3082 min_d = tree->times->death_rate_min;
3083 max_d = MIN(MIN(0.5,tree->times->death_rate_min),tree->times->birth_rate);
3084
3085 u = Uni();
3086 tree->times->death_rate = (max_d - min_d) * u + min_d;
3087 }
3088
3089 //////////////////////////////////////////////////////////////
3090 //////////////////////////////////////////////////////////////
3091
3092
MCMC_Randomize_Nu(t_tree * tree)3093 void MCMC_Randomize_Nu(t_tree *tree)
3094 {
3095 phydbl min_nu,max_nu;
3096 phydbl u;
3097
3098 /* It is preferable to start with small values of nu
3099 as if is difficult for the MCMC sampler to sample
3100 equal rates on edge (i.e., molecular clock) since
3101 such combination of rate lies on the boundary of
3102 the space of all edge rate combination. We give
3103 here a bit of help to the sampler by considering
3104 starting points close to the molecular clock
3105 constraint.
3106 */
3107 min_nu = tree->rates->min_nu;
3108 max_nu = tree->rates->max_nu/10.;
3109
3110 u = Uni();
3111 tree->rates->nu = (max_nu - min_nu) * u + min_nu;
3112 }
3113
3114 //////////////////////////////////////////////////////////////
3115 //////////////////////////////////////////////////////////////
3116
3117
MCMC_Randomize_Clock_Rate(t_tree * tree)3118 void MCMC_Randomize_Clock_Rate(t_tree *tree)
3119 {
3120 phydbl u;
3121 u = Uni();
3122 if(tree->mod->s_opt->opt_clock_r == YES) tree->rates->clock_r = u * (1.0 - tree->rates->min_clock) + tree->rates->min_clock;
3123 }
3124
3125 //////////////////////////////////////////////////////////////
3126 //////////////////////////////////////////////////////////////
3127
3128
MCMC_Randomize_Alpha(t_tree * tree)3129 void MCMC_Randomize_Alpha(t_tree *tree)
3130 {
3131 phydbl u;
3132
3133 u = Uni();
3134 tree->rates->alpha = u*6.0+1.0;
3135 }
3136
3137 //////////////////////////////////////////////////////////////
3138 //////////////////////////////////////////////////////////////
3139
MCMC_Randomize_Node_Times(t_tree * tree)3140 void MCMC_Randomize_Node_Times(t_tree *tree)
3141 {
3142 phydbl t_sup, t_inf;
3143 phydbl u;
3144 int iter;
3145 int i;
3146 phydbl dt,min_dt;
3147 int min_node;
3148
3149 t_inf = tree->times->t_prior_min[tree->n_root->num];
3150 t_sup = tree->times->t_prior_max[tree->n_root->num];
3151
3152 u = Uni();
3153 u *= (t_sup - t_inf);
3154 u += t_inf;
3155
3156 tree->times->nd_t[tree->n_root->num] = u;
3157
3158 printf("\n. ROOT: %f %f %d",t_inf,t_sup,tree->n_root->num);
3159 printf("\n. ROOT: %f",u);
3160
3161 MCMC_Randomize_Node_Times_Top_Down(tree->n_root,tree->n_root->v[2],tree);
3162 MCMC_Randomize_Node_Times_Top_Down(tree->n_root,tree->n_root->v[1],tree);
3163
3164 min_node = -1;
3165 iter = 0;
3166 do
3167 {
3168 min_dt = MDBL_MAX;
3169 For(i,2*tree->n_otu-2)
3170 {
3171 dt = tree->times->nd_t[i] - tree->times->nd_t[tree->a_nodes[i]->anc->num];
3172 if(dt < min_dt)
3173 {
3174 min_dt = dt;
3175 min_node = i;
3176 }
3177 }
3178
3179 if(min_dt > 0.01 * FABS(tree->times->nd_t[tree->n_root->num])/(phydbl)(tree->n_otu-1)) break;
3180
3181 RATES_Record_Times(tree);
3182 For(i,2*tree->n_otu-1)
3183 {
3184 if(tree->a_nodes[i]->tax == NO)
3185 tree->times->nd_t[i] -= 0.1*FABS(tree->times->nd_t[tree->n_root->num])/(phydbl)(tree->n_otu-1);
3186
3187 if(tree->times->nd_t[i] < tree->times->t_prior_min[i] ||
3188 tree->times->nd_t[i] > tree->times->t_prior_max[i])
3189 {
3190 RATES_Reset_Times(tree);
3191 break;
3192 }
3193 }
3194
3195 MCMC_Randomize_Node_Times_Bottom_Up(tree->n_root,tree->n_root->v[2],tree);
3196 MCMC_Randomize_Node_Times_Bottom_Up(tree->n_root,tree->n_root->v[1],tree);
3197
3198 iter++;
3199 }
3200 while(iter < 1000);
3201
3202 if(iter == 1000)
3203 {
3204 PhyML_Fprintf(stderr,"\n. min_dt = %f",min_dt);
3205 PhyML_Fprintf(stderr,"\n. min->t=%f min->anc->t=%f",tree->times->nd_t[min_node],tree->times->nd_t[tree->a_nodes[min_node]->anc->num]);
3206 PhyML_Fprintf(stderr,"\n. d up=%f down=%f",tree->times->t_prior_min[min_node],tree->times->t_prior_max[min_node]);
3207 PhyML_Fprintf(stderr,"\n. a up=%f down=%f",tree->times->t_prior_min[tree->a_nodes[min_node]->anc->num],tree->times->t_prior_max[tree->a_nodes[min_node]->anc->num]);
3208 PhyML_Fprintf(stderr,"\n. up=%f down=%f",tree->times->t_prior_min[min_node],tree->times->t_floor[tree->a_nodes[min_node]->anc->num]);
3209 PhyML_Fprintf(stderr,"\n. min_node = %d",min_node);
3210 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3211 }
3212
3213
3214 /* PhyML_Printf("\n. Needed %d iterations to randomize node heights.",iter); */
3215 /* TIMES_Print_Node_Times(tree->n_root,tree->n_root->v[2],tree); */
3216 /* TIMES_Print_Node_Times(tree->n_root,tree->n_root->v[1],tree); */
3217
3218 if(RATES_Check_Node_Times(tree)) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3219 }
3220
3221 //////////////////////////////////////////////////////////////
3222 //////////////////////////////////////////////////////////////
3223
3224
MCMC_Randomize_Node_Times_Bottom_Up(t_node * a,t_node * d,t_tree * tree)3225 void MCMC_Randomize_Node_Times_Bottom_Up(t_node *a, t_node *d, t_tree *tree)
3226 {
3227 if(d->tax) return;
3228 else
3229 {
3230 int i;
3231 phydbl u;
3232 phydbl t_inf, t_sup;
3233 t_node *v1, *v2;
3234
3235
3236 for(i=0;i<3;i++)
3237 {
3238 if((d->v[i] != a) && (d->b[i] != tree->e_root))
3239 {
3240 MCMC_Randomize_Node_Times_Bottom_Up(d,d->v[i],tree);
3241 }
3242 }
3243
3244 v1 = v2 = NULL;
3245 for(i=0;i<3;i++)
3246 {
3247 if(d->v[i] != a && d->b[i] != tree->e_root)
3248 {
3249 if(!v1) v1 = d->v[i];
3250 else v2 = d->v[i];
3251 }
3252 }
3253
3254 t_sup = MIN(tree->times->nd_t[v1->num],tree->times->nd_t[v2->num]);
3255 t_inf = tree->times->nd_t[a->num];
3256
3257 u = Uni();
3258 u *= (t_sup - t_inf);
3259 u += t_inf;
3260
3261
3262 if(u > tree->times->t_prior_min[d->num] && u < tree->times->t_prior_max[d->num])
3263 tree->times->nd_t[d->num] = u;
3264 }
3265 }
3266
3267 //////////////////////////////////////////////////////////////
3268 //////////////////////////////////////////////////////////////
3269
3270
MCMC_Randomize_Node_Times_Top_Down(t_node * a,t_node * d,t_tree * tree)3271 void MCMC_Randomize_Node_Times_Top_Down(t_node *a, t_node *d, t_tree *tree)
3272 {
3273 if(d->tax) return;
3274 else
3275 {
3276 int i;
3277 phydbl u;
3278 phydbl t_inf, t_sup;
3279
3280 t_inf = MAX(tree->times->nd_t[a->num],tree->times->t_prior_min[d->num]);
3281 t_sup = tree->times->t_prior_max[d->num];
3282
3283 u = Uni();
3284 u *= (t_sup - t_inf);
3285 u += t_inf;
3286
3287 tree->times->nd_t[d->num] = u;
3288
3289 for(i=0;i<3;i++)
3290 {
3291 if((d->v[i] != a) && (d->b[i] != tree->e_root))
3292 {
3293 MCMC_Randomize_Node_Times_Top_Down(d,d->v[i],tree);
3294 }
3295 }
3296 }
3297 }
3298
3299 //////////////////////////////////////////////////////////////
3300 //////////////////////////////////////////////////////////////
3301
MCMC_Get_Acc_Rates(t_mcmc * mcmc)3302 void MCMC_Get_Acc_Rates(t_mcmc *mcmc)
3303 {
3304 int i;
3305 phydbl eps;
3306 int lag;
3307
3308
3309 lag = 100;
3310
3311 eps = 1.E-6;
3312
3313 for(i=0;i<mcmc->n_moves;i++)
3314 {
3315 if(mcmc->run_move[i] - mcmc->prev_run_move[i] > lag)
3316 {
3317 mcmc->acc_rate[i] =
3318 (phydbl)(mcmc->acc_move[i] - mcmc->prev_acc_move[i] + eps) /
3319 (phydbl)(mcmc->run_move[i] - mcmc->prev_run_move[i] + eps) ;
3320
3321
3322 mcmc->prev_run_move[i] = mcmc->run_move[i];
3323 mcmc->prev_acc_move[i] = mcmc->acc_move[i];
3324
3325 MCMC_Adjust_Tuning_Parameter(i,mcmc);
3326 }
3327 }
3328 }
3329
3330 //////////////////////////////////////////////////////////////
3331 //////////////////////////////////////////////////////////////
3332
3333
MCMC_Adjust_Tuning_Parameter(int move,t_mcmc * mcmc)3334 void MCMC_Adjust_Tuning_Parameter(int move, t_mcmc *mcmc)
3335 {
3336 if(mcmc->adjust_tuning[move] == YES)
3337 {
3338 phydbl scale;
3339 phydbl rate;
3340 phydbl rate_inf,rate_sup;
3341
3342 if(mcmc->run < (int)(0.01*mcmc->chain_len)) scale = 1.5;
3343 else scale = 1.2;
3344
3345 if(!strcmp(mcmc->move_name[move],"tree_height"))
3346 {
3347 rate_inf = 0.234;
3348 rate_sup = 0.234;
3349 }
3350 else if(!strcmp(mcmc->move_name[move],"subtree_height"))
3351 {
3352 rate_inf = 0.2;
3353 rate_sup = 0.2;
3354 }
3355 else if(!strcmp(mcmc->move_name[move],"updown_t_cr"))
3356 {
3357 rate_inf = 0.1;
3358 rate_sup = 0.1;
3359 }
3360 else if(!strcmp(mcmc->move_name[move],"clock"))
3361 {
3362 rate_inf = 0.234;
3363 rate_sup = 0.234;
3364 }
3365
3366 /* if(!strcmp(mcmc->move_name[move],"tree_rates")) */
3367 /* { */
3368 /* rate_inf = 0.05; */
3369 /* rate_sup = 0.05; */
3370 /* } */
3371 else if(!strcmp(mcmc->move_name[move],"phyrex_lbda"))
3372 {
3373 rate_inf = 0.234;
3374 rate_sup = 0.234;
3375 }
3376 else if(!strcmp(mcmc->move_name[move],"phyrex_mu"))
3377 {
3378 rate_inf = 0.234;
3379 rate_sup = 0.234;
3380 }
3381 else if(!strcmp(mcmc->move_name[move],"phyrex_rad"))
3382 {
3383 rate_inf = 0.234;
3384 rate_sup = 0.234;
3385 }
3386 else if(!strcmp(mcmc->move_name[move],"phyrex_ldsk_and_disk"))
3387 {
3388 rate_inf = 0.234;
3389 rate_sup = 0.234;
3390 }
3391 else if(!strcmp(mcmc->move_name[move],"phyrex_ldsk_multi"))
3392 {
3393 rate_inf = 0.234;
3394 rate_sup = 0.234;
3395 }
3396 else if(!strcmp(mcmc->move_name[move],"phyrex_disk_multi"))
3397 {
3398 rate_inf = 0.234;
3399 rate_sup = 0.234;
3400 }
3401 else if(!strcmp(mcmc->move_name[move],"phyrex_indel_disk"))
3402 {
3403 rate_inf = 0.234;
3404 rate_sup = 0.234;
3405 }
3406 else if(!strcmp(mcmc->move_name[move],"phyrex_indel_hit"))
3407 {
3408 rate_inf = 0.234;
3409 rate_sup = 0.234;
3410 }
3411 else if(!strcmp(mcmc->move_name[move],"phyrex_scale_times"))
3412 {
3413 rate_inf = 0.234;
3414 rate_sup = 0.234;
3415 }
3416 else if(!strcmp(mcmc->move_name[move],"phyrex_ldsk_given_disk"))
3417 {
3418 rate_inf = 0.234;
3419 rate_sup = 0.234;
3420 }
3421 else if(!strcmp(mcmc->move_name[move],"phyrex_disk_given_ldsk"))
3422 {
3423 rate_inf = 0.234;
3424 rate_sup = 0.234;
3425 }
3426 else
3427 {
3428 rate_inf = 0.234; // Gareth Robert's magic number !
3429 rate_sup = 0.234;
3430 }
3431
3432
3433 rate = mcmc->acc_rate[move];
3434
3435 if(rate < rate_inf)
3436 {
3437 mcmc->tune_move[move] /= scale;
3438 }
3439
3440 else if(rate > rate_sup)
3441 {
3442 mcmc->tune_move[move] *= scale;
3443 }
3444
3445 if(mcmc->tune_move[move] > mcmc->max_tune) mcmc->tune_move[move] = mcmc->max_tune;
3446 if(mcmc->tune_move[move] < mcmc->min_tune) mcmc->tune_move[move] = mcmc->min_tune;
3447 }
3448 }
3449
3450 //////////////////////////////////////////////////////////////
3451 //////////////////////////////////////////////////////////////
3452
MCMC_One_Length(t_edge * b,t_tree * tree)3453 void MCMC_One_Length(t_edge *b, t_tree *tree)
3454 {
3455 phydbl u;
3456 phydbl new_lnL_seq, cur_lnL_seq;
3457 phydbl ratio, alpha;
3458 phydbl new_l, cur_l;
3459 phydbl K,mult;
3460
3461
3462 cur_l = b->l->v;
3463 cur_lnL_seq = tree->c_lnL;
3464 new_lnL_seq = tree->c_lnL;
3465 K = 0.1;
3466
3467 u = Uni();
3468 mult = exp(K*(u-0.5));
3469 /* mult = u*(K-1./K)+1./K; */
3470 new_l = cur_l * mult;
3471
3472 if(new_l < tree->mod->l_min || new_l > tree->mod->l_max) return;
3473
3474 b->l->v = new_l;
3475 if(tree->eval_alnL == YES) new_lnL_seq = Lk(b,tree);
3476
3477 ratio =
3478 (new_lnL_seq - cur_lnL_seq) +
3479 (log(mult));
3480
3481
3482 ratio = exp(ratio);
3483 alpha = MIN(1.,ratio);
3484
3485 u = Uni();
3486
3487 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
3488
3489 if(u > alpha) /* Reject */
3490 {
3491 b->l->v = cur_l;
3492 Update_PMat_At_Given_Edge(b,tree);
3493 tree->c_lnL = cur_lnL_seq;
3494 }
3495
3496 }
3497
3498 //////////////////////////////////////////////////////////////
3499 //////////////////////////////////////////////////////////////
3500
MCMC_Scale_Br_Lens(t_tree * tree)3501 void MCMC_Scale_Br_Lens(t_tree *tree)
3502 {
3503 phydbl u;
3504 phydbl new_lnL_seq, cur_lnL_seq;
3505 phydbl ratio, alpha;
3506 phydbl K,mult;
3507 int i;
3508
3509 Record_Br_Len(tree);
3510
3511 cur_lnL_seq = tree->c_lnL;
3512 new_lnL_seq = tree->c_lnL;
3513 K = 1.2;
3514
3515 u = Uni();
3516 mult = u*(K-1./K)+1./K;
3517
3518 for(i=0;i<2*tree->n_otu-3;++i)
3519 {
3520 tree->a_edges[i]->l->v *= mult;
3521 if(tree->a_edges[i]->l->v < tree->mod->l_min ||
3522 tree->a_edges[i]->l->v > tree->mod->l_max) return;
3523 }
3524
3525 Set_Both_Sides(NO,tree);
3526 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
3527
3528 ratio =
3529 (new_lnL_seq - cur_lnL_seq) +
3530 (2*tree->n_otu-5) * (log(mult));
3531
3532 ratio = exp(ratio);
3533 alpha = MIN(1.,ratio);
3534
3535 u = Uni();
3536
3537 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
3538
3539 if(u > alpha) /* Reject */
3540 {
3541 Restore_Br_Len(tree);
3542 tree->c_lnL = cur_lnL_seq;
3543 }
3544 }
3545
3546 //////////////////////////////////////////////////////////////
3547 //////////////////////////////////////////////////////////////
3548
3549
MCMC_Br_Lens(t_tree * tree)3550 void MCMC_Br_Lens(t_tree *tree)
3551 {
3552 MCMC_Br_Lens_Pre(tree->a_nodes[0],
3553 tree->a_nodes[0]->v[0],
3554 tree->a_nodes[0]->b[0],tree);
3555
3556 /* int i; */
3557 /* For(i,2*tree->n_otu-3) */
3558 /* { */
3559 /* MCMC_One_Length(tree->a_edges[Rand_Int(0,2*tree->n_otu-4)],acc,run,tree); */
3560 /* } */
3561 }
3562
3563 //////////////////////////////////////////////////////////////
3564 //////////////////////////////////////////////////////////////
3565
3566
MCMC_Br_Lens_Pre(t_node * a,t_node * d,t_edge * b,t_tree * tree)3567 void MCMC_Br_Lens_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree)
3568 {
3569 int i;
3570
3571 if(a == tree->n_root || d == tree->n_root) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3572
3573 MCMC_One_Length(b,tree);
3574 if(d->tax) return;
3575 else
3576 {
3577 for(i=0;i<3;i++)
3578 if(d->v[i] != a)
3579 {
3580 Update_Partial_Lk(tree,d->b[i],d);
3581 MCMC_Br_Lens_Pre(d,d->v[i],d->b[i],tree);
3582 }
3583 Update_Partial_Lk(tree,b,d);
3584 }
3585 }
3586
3587 //////////////////////////////////////////////////////////////
3588 //////////////////////////////////////////////////////////////
3589 // Potentially one kappa parameter for each tree in a mixture -> update each of them
MCMC_Kappa(t_tree * mixt_tree)3590 void MCMC_Kappa(t_tree *mixt_tree)
3591 {
3592 t_tree *tree;
3593 phydbl cur_kappa,new_kappa;
3594 phydbl u,alpha,ratio;
3595 phydbl min_kappa,max_kappa;
3596 phydbl K;
3597 phydbl cur_lnL_seq, new_lnL_seq;
3598
3599 if(mixt_tree->eval_alnL == NO) return;
3600
3601 Set_Update_Eigen(YES,mixt_tree->mod);
3602
3603 tree = mixt_tree;
3604
3605 do
3606 {
3607 if(tree->is_mixt_tree == YES) tree = tree->next;
3608
3609 if(!(tree->mod->whichmodel == HKY85 || tree->mod->whichmodel == K80 || tree->mod->whichmodel == TN93)) tree = tree->next;
3610
3611 if(tree == NULL) return;
3612
3613 cur_kappa = -1.0;
3614 new_kappa = -1.0;
3615 ratio = 0.0;
3616
3617 K = mixt_tree->mcmc->tune_move[mixt_tree->mcmc->num_move_kappa];
3618
3619 cur_lnL_seq = mixt_tree->c_lnL;
3620 new_lnL_seq = mixt_tree->c_lnL;
3621 cur_kappa = tree->mod->kappa->v;
3622
3623 min_kappa = 0.1;
3624 max_kappa = 100.;
3625
3626 MCMC_Make_Move(&cur_kappa,&new_kappa,min_kappa,max_kappa,&ratio,K,mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_kappa]);
3627
3628 if(new_kappa < max_kappa && new_kappa > min_kappa)
3629 {
3630 tree->mod->kappa->v = new_kappa;
3631
3632 if(mixt_tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,mixt_tree);
3633
3634 ratio += (new_lnL_seq - cur_lnL_seq);
3635
3636 ratio = exp(ratio);
3637 alpha = MIN(1.,ratio);
3638
3639 u = Uni();
3640
3641 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
3642
3643 if(u > alpha) /* Reject */
3644 {
3645 tree->mod->kappa->v = cur_kappa;
3646 mixt_tree->c_lnL = cur_lnL_seq;
3647 if(!Set_Model_Parameters(mixt_tree->mod))
3648 {
3649 PhyML_Fprintf(stderr,"\n. Problem in move %s",tree->mcmc->move_name[tree->mcmc->move_idx]);
3650 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3651 }
3652 }
3653 else
3654 {
3655 mixt_tree->mcmc->acc_move[mixt_tree->mcmc->num_move_kappa]++;
3656 }
3657
3658 /* PhyML_Printf("\n. MCMC cur_k: %f new_k: %f cur: %f new: %f k: %f -> %f",cur_kappa,new_kappa,cur_lnL_seq,new_lnL_seq,tree->mod->kappa->v,mixt_tree->c_lnL); */
3659
3660 }
3661 mixt_tree->mcmc->run_move[mixt_tree->mcmc->num_move_kappa]++;
3662
3663 tree = tree->next;
3664 }
3665 while(tree != NULL);
3666
3667 Set_Update_Eigen(NO,mixt_tree->mod);
3668
3669 }
3670
3671 //////////////////////////////////////////////////////////////
3672 //////////////////////////////////////////////////////////////
3673 // Potentially one set of relative rate parameters for each tree in a mixture -> update each of them
3674
MCMC_RR(t_tree * mixt_tree)3675 void MCMC_RR(t_tree *mixt_tree)
3676 {
3677 t_tree *tree;
3678 phydbl cur_rr,new_rr;
3679 phydbl u,alpha,ratio;
3680 int n_r_mat,*permut;
3681 phydbl K;
3682 phydbl cur_lnL_seq, new_lnL_seq;
3683 t_rmat **r_mat;
3684 int i;
3685
3686 if(mixt_tree->eval_alnL == NO) return;
3687
3688 Set_Update_Eigen(YES,mixt_tree->mod);
3689
3690 tree = mixt_tree;
3691 n_r_mat = 0;
3692 r_mat = NULL;
3693 permut = NULL;
3694
3695 do
3696 {
3697 if(tree->is_mixt_tree == YES) tree = tree->next;
3698
3699 if(!(tree->mod->whichmodel == GTR || tree->mod->whichmodel == CUSTOM)) tree = tree->next;
3700
3701 if(tree == NULL) return;
3702
3703 for(i=0;i<n_r_mat;i++) if(tree->mod->r_mat == r_mat[i]) break;
3704
3705 if(i == n_r_mat &&
3706 (tree->mod->whichmodel == GTR || tree->mod->whichmodel == CUSTOM) &&
3707 tree->mod->r_mat->n_diff_rr > 1)
3708 {
3709 permut = Permutate(tree->mod->r_mat->n_diff_rr);
3710
3711 for(i=0;i<tree->mod->r_mat->n_diff_rr;++i)
3712 {
3713 cur_rr = -1.0;
3714 new_rr = -1.0;
3715 ratio = 0.0;
3716 K = 1.0;
3717 cur_lnL_seq = mixt_tree->c_lnL;
3718 new_lnL_seq = UNLIKELY;
3719
3720 mixt_tree->mcmc->run_move[mixt_tree->mcmc->num_move_rr]++;
3721
3722 cur_rr = tree->mod->r_mat->rr_val->v[permut[i]];
3723
3724 MCMC_Make_Move(&cur_rr,&new_rr,UNSCALED_RR_MIN,UNSCALED_RR_MAX,&ratio,K,mixt_tree->mcmc->move_type[mixt_tree->mcmc->num_move_rr]);
3725
3726 if(new_rr < UNSCALED_RR_MAX && new_rr > UNSCALED_RR_MIN)
3727 {
3728 tree->mod->r_mat->rr_val->v[permut[i]] = new_rr;
3729
3730 new_lnL_seq = Lk(NULL,mixt_tree);
3731
3732 ratio += (new_lnL_seq - cur_lnL_seq);
3733 ratio += log(new_rr) - log(cur_rr); /* Because we are updating log(rr) instead of rr */
3734
3735
3736 ratio = exp(ratio);
3737 alpha = MIN(1.,ratio);
3738
3739 u = Uni();
3740
3741 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
3742
3743 if(u > alpha) /* Reject */
3744 {
3745 tree->mod->r_mat->rr_val->v[permut[i]] = cur_rr;
3746 mixt_tree->c_lnL = cur_lnL_seq;
3747 if(!Set_Model_Parameters(mixt_tree->mod))
3748 {
3749 PhyML_Fprintf(stderr,"\n. Problem in move %s",tree->mcmc->move_name[tree->mcmc->move_idx]);
3750 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3751 }
3752 }
3753 else
3754 {
3755 mixt_tree->mcmc->acc_move[mixt_tree->mcmc->num_move_rr]++;
3756 }
3757
3758 /* PhyML_Printf("\n. MCMC cur_rr: %f new_rr: %f cur: %f new: %f",cur_rr,new_rr,cur_lnL_seq,new_lnL_seq); */
3759 }
3760 }
3761 Free(permut);
3762 }
3763 tree = tree->next;
3764 }
3765 while(tree != NULL);
3766
3767 Set_Update_Eigen(NO,mixt_tree->mod);
3768 }
3769
3770 //////////////////////////////////////////////////////////////
3771 //////////////////////////////////////////////////////////////
3772
MCMC_Rate_Across_Sites(t_tree * tree)3773 void MCMC_Rate_Across_Sites(t_tree *tree)
3774 {
3775 int i;
3776
3777 if(tree->mod->ras->n_catg == 1) return;
3778
3779 for(i=0;i<tree->mod->ras->n_catg;i++)
3780 {
3781 if(tree->mod->ras->free_mixt_rates == YES) MCMC_Free_Mixt_Rate(tree);
3782 else MCMC_Alpha(tree);
3783 }
3784 }
3785
3786 //////////////////////////////////////////////////////////////
3787 //////////////////////////////////////////////////////////////
3788
MCMC_Alpha(t_tree * tree)3789 void MCMC_Alpha(t_tree *tree)
3790 {
3791 int i;
3792
3793 For(i,2*tree->n_otu-2) tree->rates->br_do_updt[i] = NO;
3794 MCMC_Single_Param_Generic(&(tree->mod->ras->alpha->v),0.,100.,tree->mcmc->num_move_ras,
3795 NULL,&(tree->c_lnL),
3796 NULL,Wrap_Lk,tree->mcmc->move_type[tree->mcmc->num_move_ras],NO,NULL,tree,NULL);
3797 }
3798
3799 //////////////////////////////////////////////////////////////
3800 //////////////////////////////////////////////////////////////
3801
MCMC_Free_Mixt_Rate(t_tree * mixt_tree)3802 void MCMC_Free_Mixt_Rate(t_tree *mixt_tree)
3803 {
3804 t_tree *tree;
3805 phydbl *u,*v;
3806 phydbl *f;
3807 phydbl cur_val;
3808 phydbl cur_A, new_A;
3809 phydbl low_bound,up_bound;
3810 unsigned int k,i,idx;
3811 phydbl hr,z;
3812 int n_moves;
3813 phydbl cur_lnL_seq, new_lnL_seq;
3814 phydbl ratio,alpha;
3815
3816 tree = mixt_tree;
3817
3818 do
3819 {
3820
3821 tree->mod->ras->sort_rate_classes = YES;
3822 tree->mod->ras->normalise_rr = YES;
3823
3824 cur_lnL_seq = mixt_tree->c_lnL;
3825 new_lnL_seq = mixt_tree->c_lnL;
3826
3827 k = tree->mod->ras->n_catg;
3828
3829 f = tree->mod->ras->gamma_r_proba->v;
3830
3831 u = tree->mod->ras->gamma_r_proba_unscaled->v;
3832 v = tree->mod->ras->gamma_rr_unscaled->v;
3833
3834
3835 // Setting the values of the unscaled rr freq as is done below
3836 // does not lead to changing the likelihood.
3837 u[0] = f[0];
3838 for(i=1;i<k;++i) u[i] = u[i-1] + f[i];
3839
3840
3841 n_moves = 0;
3842 do
3843 {
3844 n_moves++;
3845
3846 z = Uni();
3847
3848 if(z < 0.5)
3849 {
3850 // Update frequencies
3851
3852 cur_A = 0.0;
3853 for(i=0;i<k;++i) cur_A += f[i]*v[i];
3854
3855 hr = +2.*log(cur_A);
3856 for(i=0;i<k-1;++i) hr -= log(cur_A - f[i]*v[i]);
3857
3858 idx = Rand_Int(0,k-2);
3859
3860 // Proposal is uniform. Determine upper and lower bounds.
3861 z = Uni();
3862 low_bound = (idx==0)?(.0):(u[idx-1]);
3863 up_bound = u[idx+1];
3864 cur_val = u[idx];
3865 u[idx] = low_bound + z*(up_bound - low_bound);
3866
3867 Update_RAS(tree->mod);
3868 for(i=0;i<k;++i)
3869 {
3870 if(u[i] < GAMMA_RR_UNSCALED_MIN || u[i] > GAMMA_RR_UNSCALED_MAX) break;
3871 if(v[i] < GAMMA_R_PROBA_UNSCALED_MIN || v[i] > GAMMA_R_PROBA_UNSCALED_MAX) break;
3872 }
3873 if(i != k) new_lnL_seq = -INFINITY;
3874 else new_lnL_seq = Lk(NULL,mixt_tree);
3875
3876 new_A = 0.0;
3877 for(i=0;i<k;++i) new_A += f[i]*v[i];
3878
3879 hr -= 2.*log(new_A);
3880 for(i=0;i<k-1;++i) hr += log(new_A - f[i]*v[i]);
3881
3882 /* Metropolis-Hastings step */
3883 ratio = 0.;
3884 ratio += (new_lnL_seq - cur_lnL_seq);
3885 ratio += hr;
3886 ratio = exp(ratio);
3887 alpha = MIN(1.,ratio);
3888
3889 /* printf("\nf class=%d new_val=%f cur_val=%f cur: %f -> new: %f",idx,u[idx],cur_val,cur_lnL_seq,new_lnL_seq); */
3890
3891 z = Uni();
3892 if(z > alpha) /* Reject */
3893 {
3894 u[idx] = cur_val;
3895
3896 if(!Set_Model_Parameters(mixt_tree->mod))
3897 {
3898 PhyML_Fprintf(stderr,"\n. Problem in move %s",tree->mcmc->move_name[tree->mcmc->move_idx]);
3899 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3900 }
3901
3902 /* phydbl cur_lk = cur_lnL_seq; */
3903 /* phydbl new_lk = Lk(NULL,mixt_tree); */
3904 /* if(Are_Equal(cur_lk,new_lk,1.E-5) == NO) */
3905 /* { */
3906 /* PhyML_Printf("\n. new: %f cur: %f",new_lk,cur_lk); */
3907 /* assert(FALSE); */
3908 /* } */
3909
3910 mixt_tree->c_lnL = cur_lnL_seq;
3911 }
3912 else /* Accept */
3913 {
3914 cur_lnL_seq = new_lnL_seq;
3915 }
3916
3917 }
3918 else
3919 {
3920 // Update rates
3921
3922 cur_A = 0.0;
3923 for(i=0;i<k;++i) cur_A += f[i]*v[i];
3924
3925 hr = +2.*log(cur_A);
3926 for(i=0;i<k-1;++i) hr -= log(cur_A - f[i]*v[i]);
3927
3928 idx = Rand_Int(0,k-2);
3929
3930 // Proposal is uniform. Determine upper and lower bounds.
3931 z = Uni();
3932 low_bound = (idx==0)?(.0):(v[idx-1]);
3933 up_bound = v[idx+1];
3934 cur_val = v[idx];
3935 v[idx] = low_bound + z*(up_bound - low_bound);
3936
3937 Update_RAS(tree->mod);
3938 for(i=0;i<k;++i)
3939 {
3940 if(u[i] < GAMMA_RR_UNSCALED_MIN || u[i] > GAMMA_RR_UNSCALED_MAX) break;
3941 if(v[i] < GAMMA_R_PROBA_UNSCALED_MIN || v[i] > GAMMA_R_PROBA_UNSCALED_MAX) break;
3942 }
3943 if(i != k) new_lnL_seq = -INFINITY;
3944 else new_lnL_seq = Lk(NULL,mixt_tree);
3945
3946 new_A = 0.0;
3947 for(i=0;i<k;++i) new_A += f[i]*v[i];
3948
3949 hr -= 2.*log(new_A);
3950 for(i=0;i<k-1;++i) hr += log(new_A - f[i]*v[i]);
3951
3952
3953 /* Metropolis-Hastings step */
3954 ratio = 0.;
3955 ratio += (new_lnL_seq - cur_lnL_seq);
3956 ratio += hr;
3957 ratio = exp(ratio);
3958 alpha = MIN(1.,ratio);
3959
3960 /* printf("\nr class=%d new_val=%f cur_val=%f cur: %f -> new: %f",idx,v[idx],cur_val,cur_lnL_seq,new_lnL_seq); */
3961
3962
3963 z = Uni();
3964 if(z > alpha) /* Reject */
3965 {
3966 v[idx] = cur_val;
3967
3968 if(!Set_Model_Parameters(mixt_tree->mod))
3969 {
3970 PhyML_Fprintf(stderr,"\n. Problem in move %s",tree->mcmc->move_name[tree->mcmc->move_idx]);
3971 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
3972 }
3973
3974
3975 /* phydbl cur_lk = cur_lnL_seq; */
3976 /* phydbl new_lk = Lk(NULL,mixt_tree); */
3977 /* if(Are_Equal(cur_lk,new_lk,1.E-5) == NO) */
3978 /* { */
3979 /* PhyML_Printf("\n. new: %f cur: %f",new_lk,cur_lk); */
3980 /* assert(FALSE); */
3981 /* } */
3982
3983 mixt_tree->c_lnL = cur_lnL_seq;
3984 }
3985 else /* Accept */
3986 {
3987 cur_lnL_seq = new_lnL_seq;
3988 }
3989
3990 }
3991 }
3992 while(n_moves != k);
3993
3994 tree = tree->next_mixt;
3995 }
3996 while(tree);
3997
3998 }
3999
4000 //////////////////////////////////////////////////////////////
4001 //////////////////////////////////////////////////////////////
4002
MCMC_Covarion_Rates(t_tree * tree)4003 void MCMC_Covarion_Rates(t_tree *tree)
4004 {
4005 int i, class;
4006 phydbl u;
4007 phydbl min,max;
4008
4009 if(tree->mod->use_m4mod == NO) return;
4010
4011 Set_Update_Eigen(YES,tree->mod);
4012
4013 For(i,2*tree->n_otu-2) tree->rates->br_do_updt[i] = YES;
4014
4015 class = Rand_Int(0,tree->mod->m4mod->n_h-1);
4016
4017
4018 min = 0.01;
4019 max = 100.;
4020 u = Uni();
4021 if(u < .5)
4022 {
4023 if(!class)
4024 {
4025 min = 0.01;
4026 max = tree->mod->m4mod->multipl_unscaled[1];
4027 }
4028 else if(class == tree->mod->m4mod->n_h-1)
4029 {
4030 min = tree->mod->m4mod->multipl_unscaled[tree->mod->m4mod->n_h-2];
4031 max = +100.;
4032 }
4033 else
4034 {
4035 min = MIN(tree->mod->m4mod->multipl_unscaled[class-1],tree->mod->m4mod->multipl_unscaled[class+1]);
4036 max = MAX(tree->mod->m4mod->multipl_unscaled[class-1],tree->mod->m4mod->multipl_unscaled[class+1]);
4037 }
4038
4039 MCMC_Single_Param_Generic(&(tree->mod->m4mod->multipl_unscaled[class]),min,max,tree->mcmc->num_move_cov_rates+class+tree->mod->m4mod->n_h,
4040 NULL,&(tree->c_lnL),
4041 NULL,Wrap_Lk,tree->mcmc->move_type[tree->mcmc->num_move_cov_rates+class+tree->mod->m4mod->n_h],NO,NULL,tree,NULL);
4042 }
4043 else
4044 {
4045 MCMC_Single_Param_Generic(&(tree->mod->m4mod->h_fq_unscaled[class]),0.01,+100.,tree->mcmc->num_move_cov_rates+class,
4046 NULL,&(tree->c_lnL),
4047 NULL,Wrap_Lk,tree->mcmc->move_type[tree->mcmc->num_move_cov_rates+class],NO,NULL,tree,NULL);
4048 }
4049
4050 Set_Update_Eigen(NO,tree->mod);
4051
4052 }
4053
4054 //////////////////////////////////////////////////////////////
4055 //////////////////////////////////////////////////////////////
4056
4057
MCMC_Covarion_Switch(t_tree * tree)4058 void MCMC_Covarion_Switch(t_tree *tree)
4059 {
4060 if(tree->mod->use_m4mod == NO) return;
4061
4062 Set_Update_Eigen(YES,tree->mod);
4063 MCMC_Single_Param_Generic(&(tree->mod->m4mod->delta),0.01,+100.,tree->mcmc->num_move_cov_switch,
4064 NULL,&(tree->c_lnL),
4065 NULL,Wrap_Lk,tree->mcmc->move_type[tree->mcmc->num_move_cov_switch],NO,NULL,tree,NULL);
4066 Set_Update_Eigen(NO,tree->mod);
4067 }
4068
4069 //////////////////////////////////////////////////////////////
4070 //////////////////////////////////////////////////////////////
4071
MCMC_Birth_Rate(t_tree * tree)4072 void MCMC_Birth_Rate(t_tree *tree)
4073 {
4074 /* MCMC_Single_Param_Generic(&(tree->times->birth_rate), */
4075 /* tree->times->birth_rate_min, */
4076 /* tree->times->birth_rate_max, */
4077 /* tree->mcmc->num_move_birth_rate, */
4078 /* &(tree->times->c_lnL_times),NULL, */
4079 /* Wrap_Lk_Times,NULL,tree->mcmc->move_type[tree->mcmc->num_move_birth_rate],NO,NULL,tree,NULL); */
4080
4081 phydbl cur_birth_rate,new_birth_rate;
4082 phydbl cur_lnL_time,new_lnL_time;
4083 phydbl cur_lnL_time_ghost,new_lnL_time_ghost;
4084 /* phydbl cur_lnL_time_pivot,new_lnL_time_pivot; */
4085 phydbl u,alpha,ratio;
4086 phydbl birth_rate_min,birth_rate_max;
4087 phydbl K;
4088 int i,n_mcmc_steps,move;
4089
4090 cur_birth_rate = -1.0;
4091 new_birth_rate = -1.0;
4092 ratio = 0.0;
4093 n_mcmc_steps = tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] == 1 ? 1000 : 100;
4094 move = -1;
4095
4096 K = tree->mcmc->tune_move[tree->mcmc->num_move_birth_rate];
4097
4098 cur_lnL_time = tree->times->c_lnL_times;
4099 new_lnL_time = tree->times->c_lnL_times;
4100
4101 cur_lnL_time_ghost = UNLIKELY;
4102 new_lnL_time_ghost = UNLIKELY;
4103
4104 /* cur_lnL_time_pivot = UNLIKELY; */
4105 /* new_lnL_time_pivot = UNLIKELY; */
4106
4107 cur_birth_rate = tree->times->birth_rate;
4108
4109 birth_rate_min = MAX(tree->times->birth_rate_min,tree->times->death_rate);
4110 birth_rate_max = tree->times->birth_rate_max;
4111
4112 MCMC_Make_Move(&cur_birth_rate,&new_birth_rate,birth_rate_min,birth_rate_max,&ratio,K,tree->mcmc->move_type[tree->mcmc->num_move_birth_rate]);
4113 /* new_birth_rate = Uni()*(birth_rate_max - birth_rate_min) + birth_rate_min; */
4114
4115 if(new_birth_rate < birth_rate_max && new_birth_rate > birth_rate_min && new_birth_rate > tree->times->death_rate)
4116 {
4117 tree->times->birth_rate = new_birth_rate;
4118 new_lnL_time = TIMES_Lk_Times(NO,tree);
4119 ratio += (new_lnL_time - cur_lnL_time);
4120
4121 /* printf("\n. b :%4d: %12G -> %12G ratio : %12G [real: %12G %12G]", */
4122 /* tree->mcmc->run_move[tree->mcmc->num_move_birth_rate], */
4123 /* cur_birth_rate, */
4124 /* new_birth_rate, */
4125 /* ratio, */
4126 /* cur_lnL_time, */
4127 /* new_lnL_time); */
4128
4129
4130 if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] == 500)
4131 {
4132 tree->times->birth_rate_pivot = tree->times->birth_rate;
4133 tree->times->death_rate_pivot = tree->times->death_rate;
4134 }
4135
4136 if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 0)
4137 {
4138 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 500) */
4139 /* { */
4140 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4141 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4142 /* cur_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4143 /* } */
4144
4145 /* tree->extra_tree->times->birth_rate = cur_birth_rate; */
4146 /* tree->extra_tree->times->death_rate = tree->times->death_rate; */
4147 /* cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree); */
4148
4149 /* Copy_Tree(tree->extra_tree,tree->extra_tree->extra_tree); */
4150 /* RATES_Copy_Rate_Struct(tree->extra_tree->rates,tree->extra_tree->extra_tree->rates,tree->n_otu); */
4151 /* DATE_Assign_Primary_Calibration(tree->extra_tree->extra_tree); */
4152
4153
4154
4155 tree->extra_tree->eval_alnL = NO;
4156 tree->extra_tree->eval_rlnL = NO;
4157 tree->extra_tree->eval_glnL = YES;
4158 tree->extra_tree->times->birth_rate = new_birth_rate;
4159 tree->extra_tree->rates->c_lnL_rates = UNLIKELY;
4160 tree->extra_tree->c_lnL = UNLIKELY;
4161
4162 TIMES_Randomize_Tree_With_Time_Constraints(tree->extra_tree->times->a_cal[0],tree->extra_tree);
4163 tree->extra_tree->times->birth_rate = new_birth_rate;
4164 tree->extra_tree->times->death_rate = tree->times->death_rate;
4165 TIMES_Lk_Times(NO,tree->extra_tree);
4166
4167 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4168 {
4169 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4170 PhyML_Fprintf(stderr,"\n. birth=%G death=%G [%G]",new_birth_rate,tree->times->death_rate,tree->extra_tree->times->death_rate);
4171 TIMES_Lk_Times(YES,tree->extra_tree);
4172 assert(FALSE);
4173 }
4174
4175 i = 0;
4176 do
4177 {
4178 u = Uni();
4179 for(move=0;move<tree->mcmc->n_moves;move++) if(tree->mcmc->move_weight[move] > u-1.E-10) break;
4180
4181
4182 /* if(!(i%10)) PhyML_Printf("\n<< %5d Move '%20s' %12f",i,tree->mcmc->move_name[move],tree->extra_tree->times->c_lnL_times); */
4183
4184 if(!strcmp(tree->mcmc->move_name[move],"tree_height")) { MCMC_Tree_Height(tree->extra_tree); i++; }
4185 if(!strcmp(tree->mcmc->move_name[move],"times")) { MCMC_Times_All(tree->extra_tree); i++; }
4186 if(!strcmp(tree->mcmc->move_name[move],"spr")) { MCMC_Prune_Regraft(tree->extra_tree); i++; }
4187 if(!strcmp(tree->mcmc->move_name[move],"spr_local")) { MCMC_Prune_Regraft_Local(tree->extra_tree); i++; }
4188
4189 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4190 {
4191 PhyML_Fprintf(stderr,"\n. move: %s",tree->mcmc->move_name[move]);
4192 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4193 TIMES_Lk_Times(YES,tree->extra_tree);
4194 assert(FALSE);
4195 }
4196
4197 /* PhyML_Printf("\n.> %4d %15f",i,tree->extra_tree->times->c_lnL_times); */
4198 }
4199 while(i < n_mcmc_steps);
4200
4201 tree->extra_tree->times->birth_rate = cur_birth_rate;
4202 tree->extra_tree->times->death_rate = tree->times->death_rate;
4203 cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4204
4205 tree->extra_tree->times->birth_rate = new_birth_rate;
4206 tree->extra_tree->times->death_rate = tree->times->death_rate;
4207 new_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4208
4209 ratio += (cur_lnL_time_ghost - new_lnL_time_ghost);
4210
4211 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 500) */
4212 /* { */
4213 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4214 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4215 /* new_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4216 /* ratio += (new_lnL_time_pivot - cur_lnL_time_pivot); */
4217 /* } */
4218 }
4219
4220 ratio = exp(ratio);
4221 alpha = MIN(1.,ratio);
4222
4223 /* printf("\n. b :%4d: %12G -> %12G ratio : %12G [ghost: %12G %12G -- real: %12G %12G]", */
4224 /* tree->mcmc->run_move[tree->mcmc->num_move_birth_rate], */
4225 /* cur_birth_rate, */
4226 /* new_birth_rate, */
4227 /* ratio, */
4228 /* cur_lnL_time_ghost, */
4229 /* new_lnL_time_ghost, */
4230 /* cur_lnL_time, */
4231 /* new_lnL_time); */
4232
4233 u = Uni();
4234
4235 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4236
4237 if(u > alpha) /* Reject */
4238 {
4239 /* PhyML_Printf(" reject"); */
4240 tree->times->birth_rate = cur_birth_rate;
4241 tree->times->c_lnL_times = cur_lnL_time;
4242
4243 /* Copy_Tree(tree->extra_tree->extra_tree,tree->extra_tree); */
4244 /* RATES_Copy_Rate_Struct(tree->extra_tree->extra_tree->rates,tree->extra_tree->rates,tree->n_otu); */
4245 /* DATE_Assign_Primary_Calibration(tree->extra_tree); */
4246 }
4247 else
4248 {
4249 /* PhyML_Printf(" accept"); */
4250 tree->mcmc->acc_move[tree->mcmc->num_move_birth_rate]++;
4251 }
4252 }
4253 tree->mcmc->run_move[tree->mcmc->num_move_birth_rate]++;
4254 }
4255
4256 //////////////////////////////////////////////////////////////
4257 //////////////////////////////////////////////////////////////
4258
MCMC_Death_Rate(t_tree * tree)4259 void MCMC_Death_Rate(t_tree *tree)
4260 {
4261
4262 /* MCMC_Single_Param_Generic(&(tree->times->death_rate), */
4263 /* 0.0, // instead of tree->times->death_rate_min as death rate can be equal to 0 (Yule model) */
4264 /* tree->times->death_rate_min, */
4265 /* tree->mcmc->num_move_death_rate, */
4266 /* &(tree->times->c_lnL_times),NULL, */
4267 /* Wrap_Lk_Times,NULL,tree->mcmc->move_type[tree->mcmc->num_move_death_rate],NO,NULL,tree,NULL); */
4268
4269 phydbl cur_death_rate,new_death_rate;
4270 phydbl cur_lnL_time,new_lnL_time;
4271 phydbl cur_lnL_time_ghost,new_lnL_time_ghost;
4272 /* phydbl cur_lnL_time_pivot,new_lnL_time_pivot; */
4273 phydbl u,alpha,ratio;
4274 phydbl death_rate_min,death_rate_max;
4275 phydbl K;
4276 int i,n_mcmc_steps,move;
4277
4278 cur_death_rate = -1.0;
4279 new_death_rate = -1.0;
4280 ratio = 0.0;
4281 n_mcmc_steps = tree->mcmc->run_move[tree->mcmc->num_move_death_rate] == 1 ? 1000 : 100;
4282 move = -1;
4283
4284 K = tree->mcmc->tune_move[tree->mcmc->num_move_death_rate];
4285
4286 cur_lnL_time = tree->times->c_lnL_times;
4287 new_lnL_time = tree->times->c_lnL_times;
4288
4289 cur_lnL_time_ghost = UNLIKELY;
4290 new_lnL_time_ghost = UNLIKELY;
4291
4292 /* cur_lnL_time_pivot = UNLIKELY; */
4293 /* new_lnL_time_pivot = UNLIKELY; */
4294
4295 cur_death_rate = tree->times->death_rate;
4296
4297 death_rate_min = tree->times->death_rate_min;
4298 death_rate_max = MIN(tree->times->death_rate_min,tree->times->birth_rate);
4299
4300 MCMC_Make_Move(&cur_death_rate,&new_death_rate,death_rate_min,death_rate_max,&ratio,K,tree->mcmc->move_type[tree->mcmc->num_move_death_rate]);
4301 /* new_death_rate = Uni()*(death_rate_max - death_rate_min) + death_rate_min; */
4302
4303 if(new_death_rate < death_rate_max && new_death_rate > death_rate_min && new_death_rate < tree->times->birth_rate)
4304 {
4305 tree->times->death_rate = new_death_rate;
4306 new_lnL_time = TIMES_Lk_Times(NO,tree);
4307 ratio += (new_lnL_time - cur_lnL_time);
4308
4309 if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 0)
4310 {
4311 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 500) */
4312 /* { */
4313 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4314 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4315 /* cur_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4316 /* } */
4317
4318 /* tree->extra_tree->times->death_rate = cur_death_rate; */
4319 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate; */
4320 /* cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree); */
4321
4322 /* Copy_Tree(tree->extra_tree,tree->extra_tree->extra_tree); */
4323 /* RATES_Copy_Rate_Struct(tree->extra_tree->rates,tree->extra_tree->extra_tree->rates,tree->n_otu); */
4324 /* DATE_Assign_Primary_Calibration(tree->extra_tree->extra_tree); */
4325
4326
4327
4328 tree->extra_tree->eval_alnL = NO;
4329 tree->extra_tree->eval_rlnL = NO;
4330 tree->extra_tree->eval_glnL = YES;
4331 tree->extra_tree->rates->c_lnL_rates = UNLIKELY;
4332 tree->extra_tree->c_lnL = UNLIKELY;
4333
4334 TIMES_Randomize_Tree_With_Time_Constraints(tree->extra_tree->times->a_cal[0],tree->extra_tree);
4335 tree->extra_tree->times->birth_rate = tree->times->birth_rate;
4336 tree->extra_tree->times->death_rate = new_death_rate;
4337 TIMES_Lk_Times(NO,tree->extra_tree);
4338
4339 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4340 {
4341 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4342 PhyML_Fprintf(stderr,"\n. death=%G birth=%G [%G]",new_death_rate,tree->times->birth_rate,tree->extra_tree->times->birth_rate);
4343 PhyML_Fprintf(stderr,"\n");
4344 for(int i=0;i<tree->extra_tree->times->n_cal;++i)
4345 {
4346 t_cal *cal = tree->extra_tree->times->a_cal[i];
4347 PhyML_Fprintf(stderr,"\n. calibration %s applies to clade %s\t",cal->id,cal->clade_list[cal->current_clade_idx]->id);
4348 for(int j=0;j<cal->clade_list_size;++j)
4349 {
4350 t_clad *clade = cal->clade_list[j];
4351 PhyML_Fprintf(stderr,"time:%G\t",tree->extra_tree->times->nd_t[clade->target_nd->num]);
4352 }
4353 }
4354
4355 PhyML_Fprintf(stderr,"\n");
4356 TIMES_Lk_Times(YES,tree->extra_tree);
4357 assert(FALSE);
4358 }
4359
4360 i = 0;
4361 do
4362 {
4363 u = Uni();
4364 for(move=0;move<tree->mcmc->n_moves;move++) if(tree->mcmc->move_weight[move] > u-1.E-10) break;
4365
4366 /* if(!(i%10)) PhyML_Printf("\n>> %5d Move '%20s' %12f",i,tree->mcmc->move_name[move],tree->extra_tree->times->c_lnL_times); */
4367
4368 if(!strcmp(tree->mcmc->move_name[move],"tree_height")) { MCMC_Tree_Height(tree->extra_tree); i++; }
4369 if(!strcmp(tree->mcmc->move_name[move],"times")) { MCMC_Times_All(tree->extra_tree); i++; }
4370 if(!strcmp(tree->mcmc->move_name[move],"spr")) { MCMC_Prune_Regraft(tree->extra_tree); i++; }
4371 if(!strcmp(tree->mcmc->move_name[move],"spr_local")) { MCMC_Prune_Regraft_Local(tree->extra_tree); i++; }
4372
4373 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4374 {
4375 PhyML_Fprintf(stderr,"\n. move: %s",tree->mcmc->move_name[move]);
4376 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4377 TIMES_Lk_Times(YES,tree->extra_tree);
4378 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
4379 }
4380 }
4381 while(i < n_mcmc_steps);
4382
4383 tree->extra_tree->times->death_rate = cur_death_rate;
4384 tree->extra_tree->times->birth_rate = tree->times->birth_rate;
4385 cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4386
4387 tree->extra_tree->times->birth_rate = tree->times->birth_rate;
4388 tree->extra_tree->times->death_rate = new_death_rate;
4389 new_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4390
4391 ratio += (cur_lnL_time_ghost - new_lnL_time_ghost);
4392
4393 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 500) */
4394 /* { */
4395 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4396 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4397 /* new_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4398 /* ratio += (new_lnL_time_pivot - cur_lnL_time_pivot); */
4399 /* } */
4400 }
4401
4402 ratio = exp(ratio);
4403 alpha = MIN(1.,ratio);
4404
4405 /* printf("\n. d :%4d: %12G -> %12G ratio : %12G [ghost: %12G %12G -- real: %12G %12G]", */
4406 /* tree->mcmc->run_move[tree->mcmc->num_move_death_rate], */
4407 /* cur_death_rate, */
4408 /* new_death_rate, */
4409 /* ratio, */
4410 /* cur_lnL_time_ghost, */
4411 /* new_lnL_time_ghost, */
4412 /* cur_lnL_time, */
4413 /* new_lnL_time); */
4414
4415
4416 u = Uni();
4417
4418 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4419
4420 if(u > alpha) /* Reject */
4421 {
4422 /* PhyML_Printf(" reject"); */
4423 tree->times->death_rate = cur_death_rate;
4424 tree->times->c_lnL_times = cur_lnL_time;
4425
4426 /* Copy_Tree(tree->extra_tree->extra_tree,tree->extra_tree); */
4427 /* RATES_Copy_Rate_Struct(tree->extra_tree->extra_tree->rates,tree->extra_tree->rates,tree->n_otu); */
4428 /* DATE_Assign_Primary_Calibration(tree->extra_tree); */
4429 }
4430 else
4431 {
4432 /* PhyML_Printf(" accept"); */
4433 tree->mcmc->acc_move[tree->mcmc->num_move_death_rate]++;
4434 }
4435 }
4436 tree->mcmc->run_move[tree->mcmc->num_move_death_rate]++;
4437 }
4438
4439 //////////////////////////////////////////////////////////////
4440 //////////////////////////////////////////////////////////////
4441
MCMC_Birth_Death_Updown(t_tree * tree)4442 void MCMC_Birth_Death_Updown(t_tree *tree)
4443 {
4444 phydbl cur_death_rate,new_death_rate;
4445 phydbl cur_birth_rate,new_birth_rate;
4446 phydbl cur_lnL_time,new_lnL_time;
4447 phydbl cur_lnL_time_ghost,new_lnL_time_ghost;
4448 /* phydbl cur_lnL_time_pivot,new_lnL_time_pivot; */
4449 phydbl u,alpha,ratio;
4450 phydbl death_rate_min,death_rate_max;
4451 phydbl birth_rate_min,birth_rate_max;
4452 phydbl K,scale;
4453 int i,n_mcmc_steps,move;
4454
4455 cur_death_rate = -1.0;
4456 new_death_rate = -1.0;
4457 cur_birth_rate = -1.0;
4458 new_birth_rate = -1.0;
4459 ratio = 0.0;
4460 n_mcmc_steps = tree->mcmc->run_move[tree->mcmc->num_move_birth_death_updown] == 1 ? 1000 : 100;
4461 move = -1;
4462
4463 K = tree->mcmc->tune_move[tree->mcmc->num_move_birth_death_updown];
4464
4465 cur_lnL_time = tree->times->c_lnL_times;
4466 new_lnL_time = tree->times->c_lnL_times;
4467
4468 cur_lnL_time_ghost = UNLIKELY;
4469 new_lnL_time_ghost = UNLIKELY;
4470
4471 /* cur_lnL_time_pivot = UNLIKELY; */
4472 /* new_lnL_time_pivot = UNLIKELY; */
4473
4474 cur_death_rate = tree->times->death_rate;
4475 cur_birth_rate = tree->times->birth_rate;
4476
4477 death_rate_min = tree->times->death_rate_min;
4478 death_rate_max = tree->times->death_rate_min;
4479
4480 birth_rate_min = tree->times->birth_rate_min;
4481 birth_rate_max = tree->times->birth_rate_max;
4482
4483 scale = exp(K*(Uni()-.5));
4484 new_birth_rate = cur_birth_rate * scale;
4485 new_death_rate = cur_death_rate * scale;
4486 ratio += 2.*log(scale);
4487
4488 if(new_death_rate < death_rate_max && new_death_rate > death_rate_min &&
4489 new_birth_rate < birth_rate_max && new_birth_rate > birth_rate_min &&
4490 new_death_rate < new_birth_rate)
4491 {
4492 tree->times->death_rate = new_death_rate;
4493 tree->times->birth_rate = new_birth_rate;
4494 new_lnL_time = TIMES_Lk_Times(NO,tree);
4495 ratio += (new_lnL_time - cur_lnL_time);
4496
4497 if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 0)
4498 {
4499 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] >= 500) */
4500 /* { */
4501 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4502 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4503 /* cur_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4504 /* } */
4505
4506 /* tree->extra_tree->times->death_rate = cur_death_rate; */
4507 /* tree->extra_tree->times->birth_rate = cur_birth_rate; */
4508 /* cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree); */
4509
4510 /* Copy_Tree(tree->extra_tree,tree->extra_tree->extra_tree); */
4511 /* RATES_Copy_Rate_Struct(tree->extra_tree->rates,tree->extra_tree->extra_tree->rates,tree->n_otu); */
4512 /* DATE_Assign_Primary_Calibration(tree->extra_tree->extra_tree); */
4513
4514 tree->extra_tree->eval_alnL = NO;
4515 tree->extra_tree->eval_rlnL = NO;
4516 tree->extra_tree->eval_glnL = YES;
4517 tree->extra_tree->rates->c_lnL_rates = UNLIKELY;
4518 tree->extra_tree->c_lnL = UNLIKELY;
4519
4520 TIMES_Randomize_Tree_With_Time_Constraints(tree->extra_tree->times->a_cal[0],tree->extra_tree);
4521 tree->extra_tree->times->death_rate = new_death_rate;
4522 tree->extra_tree->times->birth_rate = new_birth_rate;
4523 TIMES_Lk_Times(NO,tree->extra_tree);
4524
4525 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4526 {
4527 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4528 PhyML_Fprintf(stderr,"\n. death=%G birth=%G [%G]",new_death_rate,tree->times->birth_rate,tree->extra_tree->times->birth_rate);
4529 TIMES_Lk_Times(YES,tree->extra_tree);
4530 assert(FALSE);
4531 }
4532
4533
4534 i = 0;
4535 do
4536 {
4537 u = Uni();
4538 for(move=0;move<tree->mcmc->n_moves;move++) if(tree->mcmc->move_weight[move] > u-1.E-10) break;
4539
4540 /* PhyML_Printf("\n>> Move '%s' %f %f b:%f d:%f-%f", */
4541 /* tree->mcmc->move_name[move], */
4542 /* tree->extra_tree->times->c_lnL_times, */
4543 /* tree->times->c_lnL_times, */
4544 /* tree->times->birth_rate, */
4545 /* cur_death_rate, */
4546 /* new_death_rate); */
4547
4548 if(!strcmp(tree->mcmc->move_name[move],"tree_height")) MCMC_Tree_Height(tree->extra_tree);
4549 if(!strcmp(tree->mcmc->move_name[move],"times")) MCMC_Times_All(tree->extra_tree);
4550 if(!strcmp(tree->mcmc->move_name[move],"spr")) MCMC_Prune_Regraft(tree->extra_tree);
4551 if(!strcmp(tree->mcmc->move_name[move],"spr_local")) MCMC_Prune_Regraft_Local(tree->extra_tree);
4552
4553 if(!(tree->extra_tree->times->c_lnL_times > UNLIKELY))
4554 {
4555 PhyML_Fprintf(stderr,"\n. move: %s",tree->mcmc->move_name[move]);
4556 PhyML_Fprintf(stderr,"\n. glnL=%f",tree->extra_tree->times->c_lnL_times);
4557 TIMES_Lk_Times(YES,tree->extra_tree);
4558 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
4559 }
4560 i++;
4561 }
4562 while(i < n_mcmc_steps);
4563
4564 tree->extra_tree->times->death_rate = cur_death_rate;
4565 tree->extra_tree->times->birth_rate = cur_birth_rate;
4566 cur_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4567
4568 tree->extra_tree->times->death_rate = new_death_rate;
4569 tree->extra_tree->times->birth_rate = new_birth_rate;
4570 new_lnL_time_ghost = TIMES_Lk_Times(NO,tree->extra_tree);
4571
4572 ratio += (cur_lnL_time_ghost - new_lnL_time_ghost);
4573
4574 /* if(tree->mcmc->run_move[tree->mcmc->num_move_birth_rate] == 500) */
4575 /* { */
4576 /* tree->extra_tree->times->death_rate = tree->times->death_rate_pivot; */
4577 /* tree->extra_tree->times->birth_rate = tree->times->birth_rate_pivot; */
4578 /* new_lnL_time_pivot = TIMES_Lk_Times(NO,tree->extra_tree); */
4579 /* ratio += (new_lnL_time_pivot - cur_lnL_time_pivot); */
4580 /* } */
4581 }
4582
4583 ratio = exp(ratio);
4584 alpha = MIN(1.,ratio);
4585
4586 /* printf("\n. bd :%4d: %12G -> %12G ratio : %12G [ghost: %12G %12G -- real: %12G %12G]", */
4587 /* tree->mcmc->run_move[tree->mcmc->num_move_birth_death_updown], */
4588 /* cur_death_rate, */
4589 /* new_death_rate, */
4590 /* ratio, */
4591 /* cur_lnL_time_ghost, */
4592 /* new_lnL_time_ghost, */
4593 /* cur_lnL_time, */
4594 /* new_lnL_time); */
4595
4596
4597 u = Uni();
4598
4599 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4600
4601 if(u > alpha) /* Reject */
4602 {
4603 /* PhyML_Printf(" reject"); */
4604 tree->times->death_rate = cur_death_rate;
4605 tree->times->birth_rate = cur_birth_rate;
4606 tree->times->c_lnL_times = cur_lnL_time;
4607
4608 /* Copy_Tree(tree->extra_tree->extra_tree,tree->extra_tree); */
4609 /* RATES_Copy_Rate_Struct(tree->extra_tree->extra_tree->rates,tree->extra_tree->rates,tree->n_otu); */
4610 /* DATE_Assign_Primary_Calibration(tree->extra_tree); */
4611 }
4612 else
4613 {
4614 /* PhyML_Printf(" accept"); */
4615 tree->mcmc->acc_move[tree->mcmc->num_move_birth_death_updown]++;
4616 }
4617 }
4618 tree->mcmc->run_move[tree->mcmc->num_move_birth_death_updown]++;
4619 }
4620
4621 //////////////////////////////////////////////////////////////
4622 //////////////////////////////////////////////////////////////
4623
MCMC_Nu(t_tree * tree)4624 void MCMC_Nu(t_tree *tree)
4625 {
4626 phydbl cur_nu,new_nu,cur_lnL_rate,new_lnL_rate;
4627 phydbl u,alpha,ratio;
4628 phydbl min_nu,max_nu;
4629 phydbl K;
4630 phydbl cur_lnL_seq, new_lnL_seq;
4631
4632 cur_nu = -1.0;
4633 new_nu = -1.0;
4634 ratio = 0.0;
4635
4636 K = tree->mcmc->tune_move[tree->mcmc->num_move_nu];
4637
4638 cur_lnL_rate = tree->rates->c_lnL_rates;
4639 new_lnL_rate = tree->rates->c_lnL_rates;
4640
4641 cur_lnL_seq = tree->c_lnL;
4642 new_lnL_seq = tree->c_lnL;
4643
4644 cur_nu = tree->rates->nu;
4645
4646 min_nu = tree->rates->min_nu;
4647 max_nu = tree->rates->max_nu;
4648
4649 MCMC_Make_Move(&cur_nu,&new_nu,min_nu,max_nu,&ratio,K,tree->mcmc->move_type[tree->mcmc->num_move_nu]);
4650
4651 if(new_nu < max_nu && new_nu > min_nu)
4652 {
4653 tree->rates->nu = new_nu;
4654
4655 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
4656
4657 if(tree->rates->model == GUINDON)
4658 {
4659 RATES_Update_Cur_Bl(tree);
4660 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
4661 }
4662 else
4663 new_lnL_seq = cur_lnL_seq;
4664
4665 ratio += (new_lnL_rate - cur_lnL_rate);
4666 ratio += (new_lnL_seq - cur_lnL_seq);
4667
4668 ratio = exp(ratio);
4669 alpha = MIN(1.,ratio);
4670
4671 u = Uni();
4672
4673 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4674
4675 if(u > alpha) /* Reject */
4676 {
4677 tree->rates->nu = cur_nu;
4678 tree->rates->c_lnL_rates = cur_lnL_rate;
4679 tree->c_lnL = cur_lnL_seq;
4680 if(tree->rates->model == GUINDON && tree->eval_alnL == YES) RATES_Update_Cur_Bl(tree);
4681 }
4682 else
4683 {
4684 tree->mcmc->acc_move[tree->mcmc->num_move_nu]++;
4685 }
4686 }
4687 tree->mcmc->run_move[tree->mcmc->num_move_nu]++;
4688 }
4689
4690 //////////////////////////////////////////////////////////////
4691 //////////////////////////////////////////////////////////////
4692
MCMC_Clade_Change(t_tree * tree)4693 void MCMC_Clade_Change(t_tree *tree)
4694 {
4695 phydbl u,alpha,ratio;
4696 phydbl cur_lnL_calib, new_lnL_calib;
4697 phydbl cur_lnL_time, new_lnL_time;
4698 int target_cal_idx,current_clade_idx,new_clade_idx;
4699 t_cal *cal;
4700
4701 ratio = 0.0;
4702 cal = NULL;
4703 cur_lnL_calib = DATE_Lk_Calib(tree);
4704
4705 new_lnL_time = tree->times->c_lnL_times;
4706 cur_lnL_time = tree->times->c_lnL_times;
4707
4708
4709
4710 // Choose a calibration uniformly at random
4711 target_cal_idx = Rand_Int(0,tree->times->n_cal-1);
4712 cal = tree->times->a_cal[target_cal_idx];
4713
4714 /* printf("\n. CURRENT cal: %s clade: %s target: %d",cal->id,cal->clade_list[cal->current_clade_idx]->id,cal->clade_list[cal->current_clade_idx]->target_nd->num); */
4715
4716 // Choose a new clade uniformly at random
4717 current_clade_idx = cal->current_clade_idx;
4718 new_clade_idx = Rand_Int(0,cal->clade_list_size-1);
4719 cal->current_clade_idx = new_clade_idx;
4720
4721 new_lnL_time = TIMES_Lk_Times(NO,tree);
4722 ratio += new_lnL_time - cur_lnL_time;
4723
4724 /* printf("\n. NEW cal: %s clade: %s target: %d",cal->id,cal->clade_list[cal->current_clade_idx]->id,cal->clade_list[cal->current_clade_idx]->target_nd->num); */
4725
4726 new_lnL_calib = DATE_Lk_Calib(tree);
4727 ratio += new_lnL_calib - cur_lnL_calib;
4728
4729 ratio = exp(ratio);
4730 alpha = MIN(1.,ratio);
4731
4732 u = Uni();
4733
4734 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4735
4736 if(u > alpha) /* Reject */
4737 {
4738 /* printf("\n. reject\n"); */
4739 cal->current_clade_idx = current_clade_idx;
4740 tree->times->c_lnL_times = cur_lnL_time;
4741 TIMES_Lk_Times(NO,tree); // Required in order to update node targeted by selected calibration
4742 }
4743 else
4744 {
4745 /* printf("\n. accept\n"); */
4746 tree->mcmc->acc_move[tree->mcmc->num_move_clade_change]++;
4747 }
4748 tree->mcmc->run_move[tree->mcmc->num_move_clade_change]++;
4749 }
4750
4751
4752 //////////////////////////////////////////////////////////////
4753 //////////////////////////////////////////////////////////////
4754
MCMC_All_Rates(t_tree * tree)4755 void MCMC_All_Rates(t_tree *tree)
4756 {
4757 phydbl cur_lnL_seq, new_lnL_seq, cur_lnL_rate;
4758 phydbl u, ratio, alpha;
4759
4760 new_lnL_seq = tree->c_lnL;
4761 cur_lnL_seq = tree->c_lnL;
4762 cur_lnL_rate = tree->rates->c_lnL_rates;
4763 ratio = 0.0;
4764
4765 Record_Br_Len(tree);
4766 RATES_Record_Rates(tree);
4767
4768 MCMC_Sim_Rate(tree->n_root,tree->n_root->v[2],tree);
4769 MCMC_Sim_Rate(tree->n_root,tree->n_root->v[1],tree);
4770
4771 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
4772
4773 ratio += (new_lnL_seq - cur_lnL_seq);
4774 ratio = exp(ratio);
4775
4776 alpha = MIN(1.,ratio);
4777 u = Uni();
4778
4779 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
4780
4781 if(u > alpha) /* Reject */
4782 {
4783 Restore_Br_Len(tree);
4784 RATES_Reset_Rates(tree);
4785 tree->rates->c_lnL_rates = cur_lnL_rate;
4786 tree->c_lnL = cur_lnL_seq;
4787 }
4788 else
4789 {
4790 tree->rates->c_lnL_rates = RATES_Lk_Rates(tree);;
4791 }
4792 }
4793
4794 //////////////////////////////////////////////////////////////
4795 //////////////////////////////////////////////////////////////
4796
4797
4798 /* Only works when simulating from prior */
MCMC_Sim_Rate(t_node * a,t_node * d,t_tree * tree)4799 void MCMC_Sim_Rate(t_node *a, t_node *d, t_tree *tree)
4800 {
4801 int err;
4802 phydbl mean,sd,br_r_a,dt_d;
4803
4804 br_r_a = tree->rates->br_r[a->num];
4805 dt_d = tree->times->nd_t[d->num] - tree->times->nd_t[a->num];
4806 sd = SQRT(dt_d*tree->rates->nu);
4807
4808 mean = br_r_a;
4809
4810 if(tree->rates->model == STRICTCLOCK) tree->rates->br_r[d->num] = 1.0;
4811 else
4812 {
4813 tree->rates->br_r[d->num] = Rnorm_Trunc(mean,sd,tree->rates->min_rate,tree->rates->max_rate,&err);
4814 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
4815 }
4816
4817 if(d->tax) return;
4818 else
4819 {
4820 int i;
4821
4822 for(i=0;i<3;i++)
4823 if(d->v[i] != a && d->b[i] != tree->e_root)
4824 MCMC_Sim_Rate(d,d->v[i],tree);
4825 }
4826 }
4827
4828 //////////////////////////////////////////////////////////////
4829 //////////////////////////////////////////////////////////////
4830
MCMC_Prune_Regraft(t_tree * tree)4831 void MCMC_Prune_Regraft(t_tree *tree)
4832 {
4833 phydbl u,alpha,ratio;
4834 phydbl t_min,t_max;
4835 phydbl cur_lnL_seq,new_lnL_seq;
4836 phydbl cur_lnL_rate,new_lnL_rate;
4837 phydbl cur_lnL_time,new_lnL_time;
4838 phydbl new_t;
4839 int i,prune_idx,n_iter,n_regraft_nd,regraft_idx,dir_prune;
4840 phydbl *times;
4841 int rnd_dir,dir_v1,dir_v2;
4842 t_node *prune,*prune_daughter,*new_regraft_nd,*cur_regraft_nd;
4843 t_ll *regraft_nd_list;
4844 t_edge *target, *ori_target, *residual,*regraft_edge;
4845 phydbl regraft_t_min,regraft_t_max;
4846 /* phydbl *prob_idx; */
4847 /* phydbl r,sum,prob_select_fwd,prob_select_bwd; */
4848
4849 n_iter = MAX(1,(int)(tree->n_otu/5));
4850 /* n_iter = tree->n_otu-2; */
4851 times = tree->times->nd_t;
4852
4853 /* prob_idx = (phydbl *)mCalloc(tree->n_otu-1,sizeof(phydbl)); */
4854
4855 // Shallowest node has probability proportional to r to be
4856 // to be selected as prune node while deepest a proba prop to 1.
4857 /* r = 0.5; */
4858 /* for(i=0;i<tree->n_otu-1;i++) prob_idx[i] = i*(r-1.0)/(tree->n_otu-2) + 1.0; */
4859
4860 /* sum = .0; */
4861 /* for(i=0;i<tree->n_otu-1;i++) sum += prob_idx[i]; */
4862 /* for(i=0;i<tree->n_otu-1;i++) prob_idx[i] /= sum; */
4863
4864
4865 while(n_iter--)
4866 {
4867
4868 TIMES_Update_Node_Ordering(tree);
4869
4870 /* if(tree->eval_alnL == YES) Lk(NULL,tree); */
4871
4872 tree->mcmc->run_move[tree->mcmc->num_move_spr]++;
4873
4874 RATES_Record_Times(tree);
4875
4876 cur_lnL_seq = tree->c_lnL;
4877 new_lnL_seq = tree->c_lnL;
4878 cur_lnL_rate = tree->rates->c_lnL_rates;
4879 new_lnL_rate = tree->rates->c_lnL_rates;
4880 cur_lnL_time = tree->times->c_lnL_times;
4881 new_lnL_time = tree->times->c_lnL_times;
4882
4883 ratio = 0.0;
4884 regraft_edge = NULL;
4885 new_regraft_nd = NULL;
4886 cur_regraft_nd = NULL;
4887 new_t = 0.0;
4888
4889 // Select prune node (any internal node)
4890 prune_idx = Rand_Int(tree->n_otu,2*tree->n_otu-2);
4891
4892 /* prune_idx = Sample_i_With_Proba_pi(prob_idx,tree->n_otu-1); */
4893 /* prob_select_fwd = prob_idx[prune_idx]; */
4894 /* ratio -= log(prob_select_fwd); */
4895 /* prune_idx = tree->times->t_rank[prune_idx]; */
4896
4897 prune = tree->a_nodes[prune_idx];
4898
4899
4900 assert(prune && prune->tax == NO);
4901
4902 // Select a daughter of prune node
4903 dir_v1 = dir_v2 = -1;
4904 for(i=0;i<3;i++)
4905 if(prune->v[i] != prune->anc && prune->b[i] != tree->e_root)
4906 {
4907 if(dir_v1 < 0) dir_v1 = i;
4908 else dir_v2 = i;
4909 }
4910
4911 u = Uni();
4912 if(u < 0.5) rnd_dir = dir_v1;
4913 else rnd_dir = dir_v2;
4914
4915 prune_daughter = prune->v[rnd_dir];
4916 cur_regraft_nd = prune->v[rnd_dir == dir_v1 ? dir_v2 : dir_v1];
4917
4918 if(prune == tree->n_root)
4919 {
4920 if(prune_daughter == prune->v[dir_v1] && prune->v[dir_v2]->tax == YES)
4921 {
4922 prune_daughter = prune->v[dir_v2];
4923 cur_regraft_nd = prune->v[dir_v1];
4924 }
4925
4926 if(prune_daughter == prune->v[dir_v2] && prune->v[dir_v1]->tax == YES)
4927 {
4928 prune_daughter = prune->v[dir_v1];
4929 cur_regraft_nd = prune->v[dir_v2];
4930 }
4931 }
4932
4933 if(prune_daughter->anc != prune) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
4934
4935 dir_prune = -1;
4936 for(i=0;i<3;i++)
4937 {
4938 if(prune_daughter->v[i] == prune || prune_daughter->b[i] == tree->e_root)
4939 {
4940 dir_prune = i;
4941 break;
4942 }
4943 }
4944 assert(dir_prune > -1);
4945
4946
4947 // Get the list of potential regraft nodes (oldest node on regraft edge)
4948 regraft_nd_list = DATE_List_Of_Regraft_Nodes(prune_daughter->v[dir_prune],prune_daughter,®raft_t_min,®raft_t_max,NO,tree);
4949 assert(regraft_nd_list);
4950 if(prune == tree->n_root) Push_Bottom_Linked_List(prune,®raft_nd_list,YES);
4951
4952 // Number of regraft nodes
4953 n_regraft_nd = Linked_List_Len(regraft_nd_list);
4954
4955 if(!(n_regraft_nd > 0)) // Should be at least 1, since original graft site is in the list
4956 {
4957 printf("\n\n. prune: %d [%d-%d-%d] [%s-%s-%s] [%f-%f;%f] prune_daughter: %d [%f-%f;%f] prune->anc: %d [%f-%f;%f] effective: %d glnL: %f",
4958 prune->num,
4959 prune->v[0] ? prune->v[0]->num : -1,
4960 prune->v[1]->num,
4961 prune->v[2]->num,
4962 prune->v[0] ? prune->v[0]->tax ? prune->v[0]->name : "XXX" : "XXX",
4963 prune->v[1]->tax ? prune->v[1]->name : "XXX",
4964 prune->v[2]->tax ? prune->v[2]->name : "XXX",
4965 tree->times->nd_t[prune->num],
4966 tree->times->t_prior_min[prune->num],
4967 tree->times->t_prior_max[prune->num],
4968 prune_daughter->num,
4969 tree->times->nd_t[prune_daughter->num],
4970 tree->times->t_prior_min[prune_daughter->num],
4971 tree->times->t_prior_max[prune_daughter->num],
4972 prune->anc ? prune->anc->num : -1,
4973 prune->anc ? tree->times->nd_t[prune->anc->num] : -1,
4974 prune->anc ? tree->times->t_prior_min[prune->anc->num] : -1,
4975 prune->anc ? tree->times->t_prior_max[prune->anc->num] : -1,
4976 prune_daughter->v[dir_prune]->num,
4977 tree->times->c_lnL_times); fflush(NULL);
4978
4979 regraft_nd_list = DATE_List_Of_Regraft_Nodes(prune_daughter->v[dir_prune],prune_daughter,®raft_t_min,®raft_t_max,YES,tree);
4980
4981 assert(FALSE);
4982 }
4983
4984 // Randomly select one (uniform)
4985 regraft_idx = Rand_Int(0,n_regraft_nd-1);
4986 new_regraft_nd = Linked_List_Elem(regraft_idx,regraft_nd_list);
4987 Free_Linked_List(regraft_nd_list);
4988
4989
4990 // Time of regraft node and corresponding (partial) Hastings ratio
4991 t_max = MIN(times[prune_daughter->num],times[cur_regraft_nd->num]);
4992 if(prune == tree->n_root) t_min = 10.0*t_max;
4993 else t_min = times[prune->anc->num];
4994 t_min = MAX(t_min,regraft_t_min);
4995 ratio += log(1./(t_max - t_min));
4996
4997 t_max = MIN(times[prune_daughter->num],times[new_regraft_nd->num]);
4998 if(new_regraft_nd == tree->n_root) t_min = 10.0*t_max;
4999 else t_min = times[new_regraft_nd->anc->num];
5000 t_min = MAX(t_min,regraft_t_min);
5001 ratio -= log(1./(t_max - t_min));
5002
5003 new_t = Uni()*(t_max-t_min) + t_min;
5004
5005 // Age of root node changes when pruned subtree is on one side of that node
5006 // Change here, not after the prune and regraft move
5007 /* if(prune == tree->n_root) */
5008 /* { */
5009 /* if(prune_daughter == tree->n_root->v[1]) */
5010 /* times[tree->n_root->num] = times[tree->n_root->v[2]->num]; */
5011 /* else if(prune_daughter == tree->n_root->v[2]) */
5012 /* times[tree->n_root->num] = times[tree->n_root->v[1]->num]; */
5013 /* else assert(false); */
5014 /* } */
5015
5016
5017 // New age
5018 if(prune == tree->n_root || new_regraft_nd == tree->n_root)
5019 {
5020 if(prune == tree->n_root)
5021 {
5022 if(prune_daughter == tree->n_root->v[1]) times[tree->n_root->num] = times[tree->n_root->v[2]->num];
5023 else times[tree->n_root->num] = times[tree->n_root->v[1]->num];
5024 times[prune_daughter->v[dir_prune]->num] = new_t;
5025 }
5026 if(new_regraft_nd == tree->n_root)
5027 {
5028 times[prune_daughter->v[dir_prune]->num] = times[tree->n_root->num];
5029 times[tree->n_root->num] = new_t;
5030 }
5031 }
5032 else
5033 {
5034 times[prune->num] = new_t;
5035 }
5036
5037
5038 // Prune
5039 target = residual = NULL;
5040 Prune_Subtree(prune_daughter->v[dir_prune],
5041 prune_daughter,
5042 &target,&residual,tree);
5043 ori_target = target;
5044
5045
5046 // Regraft edge is the one sitting above regraft_nd
5047 if(new_regraft_nd == tree->n_root->v[1] ||
5048 new_regraft_nd == tree->n_root->v[2] ||
5049 new_regraft_nd == tree->n_root) regraft_edge = tree->e_root;
5050 else
5051 {
5052 for(i=0;i<3;i++) if(new_regraft_nd->v[i] == new_regraft_nd->anc) break;
5053 assert(i!=3);
5054 regraft_edge = new_regraft_nd->b[i];
5055 }
5056
5057 assert(regraft_edge);
5058 assert(residual->left != residual->rght);
5059 assert(regraft_edge->left != prune_daughter->v[dir_prune]);
5060 assert(regraft_edge->rght != prune_daughter->v[dir_prune]);
5061
5062
5063 // Regraft
5064 Graft_Subtree(regraft_edge,
5065 prune_daughter->v[dir_prune],
5066 prune_daughter,
5067 residual,
5068 new_regraft_nd,tree);
5069
5070
5071 /* TIMES_Update_Node_Ordering(tree); */
5072 /* For(i,2*tree->n_otu-2) if(prune->num == tree->times->t_rank[i]) break; */
5073 /* prob_select_bwd = prob_idx[i]; */
5074 /* ratio += log(prob_select_bwd); */
5075
5076 if(!TIMES_Check_Node_Height_Ordering(tree))
5077 {
5078 printf("\n. prune[%d]->t:%.3f daughter[%d]->t:%.3f prune_anc[%d]->t:%.3f regraft[%d]->t:%.3f regraft_anc[%d]->t:%.3f [effective:%d] t_prior_min/max: [prune:[%.3f %.3f] regraft:[%.3f %.3f]] ",
5079 prune->num,
5080 times[prune->num],
5081 prune_daughter->num,
5082 times[prune_daughter->num],
5083 prune->anc ? prune->anc->num : -1,
5084 prune->anc ? times[prune->anc->num] : -1.,
5085 new_regraft_nd->num,
5086 times[new_regraft_nd->num],
5087 new_regraft_nd->anc ? new_regraft_nd->anc->num : -1,
5088 new_regraft_nd->anc ? times[new_regraft_nd->anc->num] : +1.,
5089 prune->num,
5090 tree->times->t_prior_min[prune->num],
5091 tree->times->t_prior_max[prune->num],
5092 tree->times->t_prior_min[new_regraft_nd->num],
5093 tree->times->t_prior_max[new_regraft_nd->num]);
5094 PhyML_Fprintf(stderr,"\n. root: %d %d %d",tree->n_root->num,tree->n_root->v[1]->num,tree->n_root->v[2]->num);
5095 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
5096 }
5097
5098 DATE_Assign_Primary_Calibration(tree);
5099 RATES_Update_Cur_Bl(tree);
5100
5101 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
5102
5103 if(new_lnL_time > UNLIKELY)
5104 {
5105 Set_Both_Sides(NO,tree);
5106 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
5107 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
5108 }
5109
5110 ratio += (new_lnL_seq - cur_lnL_seq);
5111 ratio += (new_lnL_rate - cur_lnL_rate);
5112 ratio += (new_lnL_time - cur_lnL_time);
5113
5114 ratio = exp(ratio);
5115 alpha = MIN(1.,ratio);
5116
5117 /* Always accept move */
5118 if(tree->mcmc->always_yes == YES && new_lnL_time > UNLIKELY) alpha = 1.0;
5119
5120 u = Uni();
5121
5122 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
5123
5124 if(u > alpha)
5125 {
5126 // Reject
5127 Prune_Subtree(prune_daughter->v[dir_prune],
5128 prune_daughter,
5129 &target,&residual,tree);
5130 assert(residual->left != residual->rght);
5131 assert(ori_target->left != prune_daughter->v[dir_prune]);
5132 assert(ori_target->rght != prune_daughter->v[dir_prune]);
5133 Graft_Subtree(ori_target,
5134 prune_daughter->v[dir_prune],
5135 prune_daughter,residual,prune == tree->n_root ? tree->n_root : cur_regraft_nd,tree);
5136
5137 RATES_Reset_Times(tree);
5138 RATES_Update_Cur_Bl(tree);
5139 DATE_Assign_Primary_Calibration(tree);
5140
5141 new_lnL_time = TIMES_Lk_Times(NO,tree);
5142 if(Are_Equal(new_lnL_time,cur_lnL_time,1.E-5) == NO)
5143 {
5144 PhyML_Printf("\n. new_lnL_time: %f cur_lnL_time: %f",new_lnL_time,cur_lnL_time);
5145 assert(FALSE);
5146 }
5147
5148 /* if(tree->eval_alnL == YES) */
5149 /* { */
5150 /* new_lnL_seq = Lk(NULL,tree); */
5151 /* if(Are_Equal(new_lnL_seq,cur_lnL_seq,1.E-5) == NO) */
5152 /* { */
5153 /* PhyML_Printf("\n. new: %f cur: %f",new_lnL_seq,cur_lnL_seq); */
5154 /* assert(FALSE); */
5155 /* } */
5156 /* } */
5157
5158 if(!(tree->times->c_lnL_times > UNLIKELY))
5159 {
5160 printf("\n. time prune: %f",times[prune->num]);
5161 printf("\n. time prune_daughter: %f",times[prune_daughter->num]);
5162 printf("\n. prune: %d prune_daughter: %d prune_daughter->v[dir_prune]: %d cur_regraft_nd: %d new_regraft_nd: %d",
5163 prune->num,
5164 prune_daughter->num,
5165 prune_daughter->v[dir_prune]->num,
5166 cur_regraft_nd->num,
5167 new_regraft_nd->num);
5168 TIMES_Lk_Times(YES,tree);
5169 fflush(NULL);
5170 }
5171 assert(tree->times->c_lnL_times > UNLIKELY);
5172
5173 tree->c_lnL = cur_lnL_seq;
5174 tree->times->c_lnL_times = cur_lnL_time;
5175 tree->rates->c_lnL_rates = cur_lnL_rate;
5176 }
5177 else
5178 {
5179 tree->mcmc->acc_move[tree->mcmc->num_move_spr]++;
5180 }
5181 }
5182 /* Free(prob_idx); */
5183 }
5184
5185 //////////////////////////////////////////////////////////////
5186 //////////////////////////////////////////////////////////////
5187
MCMC_Prune_Regraft_Weighted(t_tree * tree)5188 void MCMC_Prune_Regraft_Weighted(t_tree *tree)
5189 {
5190 phydbl u,alpha,ratio;
5191 phydbl cur_lnL_seq,new_lnL_seq;
5192 phydbl cur_lnL_rate,new_lnL_rate;
5193 phydbl cur_lnL_time,new_lnL_time;
5194 phydbl new_t;
5195 int i,prune_idx,n_iter,dir_prune;
5196 phydbl *times;
5197 int rnd_dir,dir_v1,dir_v2;
5198 t_node *prune,*prune_daughter,*new_regraft_nd,*cur_regraft_nd;
5199 t_edge *target, *ori_target, *residual,*regraft_edge;
5200 phydbl radius;
5201
5202 n_iter = MAX(1,(int)(tree->n_otu/5));
5203 times = tree->times->nd_t;
5204
5205 while(n_iter--)
5206 {
5207 tree->mcmc->run_move[tree->mcmc->num_move_spr_weighted]++;
5208
5209 RATES_Record_Times(tree);
5210
5211 cur_lnL_seq = tree->c_lnL;
5212 new_lnL_seq = tree->c_lnL;
5213 cur_lnL_rate = tree->rates->c_lnL_rates;
5214 new_lnL_rate = tree->rates->c_lnL_rates;
5215 cur_lnL_time = tree->times->c_lnL_times;
5216 new_lnL_time = tree->times->c_lnL_times;
5217
5218 ratio = 0.0;
5219 regraft_edge = NULL;
5220 new_regraft_nd = NULL;
5221 cur_regraft_nd = NULL;
5222 new_t = 0.0;
5223
5224 // Select prune node (any internal node)
5225 prune_idx = Rand_Int(tree->n_otu,2*tree->n_otu-2);
5226 prune = tree->a_nodes[prune_idx];
5227
5228 assert(prune && prune->tax == NO);
5229
5230 // Select a daughter of prune node
5231 dir_v1 = dir_v2 = -1;
5232 for(i=0;i<3;i++)
5233 if(prune->v[i] != prune->anc && prune->b[i] != tree->e_root)
5234 {
5235 if(dir_v1 < 0) dir_v1 = i;
5236 else dir_v2 = i;
5237 }
5238
5239 u = Uni();
5240 if(u < 0.5) rnd_dir = dir_v1;
5241 else rnd_dir = dir_v2;
5242
5243 prune_daughter = prune->v[rnd_dir];
5244 cur_regraft_nd = prune->v[rnd_dir == dir_v1 ? dir_v2 : dir_v1];
5245
5246 if(prune == tree->n_root)
5247 {
5248 if(prune_daughter == prune->v[dir_v1] && prune->v[dir_v2]->tax == YES)
5249 {
5250 prune_daughter = prune->v[dir_v2];
5251 cur_regraft_nd = prune->v[dir_v1];
5252 }
5253
5254 if(prune_daughter == prune->v[dir_v2] && prune->v[dir_v1]->tax == YES)
5255 {
5256 prune_daughter = prune->v[dir_v1];
5257 cur_regraft_nd = prune->v[dir_v2];
5258 }
5259 }
5260
5261 assert(prune_daughter->anc == prune);
5262
5263 dir_prune = -1;
5264 for(i=0;i<3;i++)
5265 {
5266 if(prune_daughter->v[i] == prune || prune_daughter->b[i] == tree->e_root)
5267 {
5268 dir_prune = i;
5269 break;
5270 }
5271 }
5272 assert(dir_prune > -1);
5273
5274
5275 radius = Rnorm(0.0,0.05);
5276 radius = fabs(radius);
5277 radius += tree->rates->cur_l[prune_daughter->num]; // valid too when prune == tree->n_root
5278
5279 /* printf("\n. Init rad: %G l: %G %G root: %d",radius,tree->rates->cur_l[prune_daughter->num],prune_daughter->b[dir_prune]->l->v,prune_daughter->b[dir_prune] == tree->e_root); */
5280 // Get the list of potential regraft nodes (oldest node on regraft edge)
5281 Random_Walk_Along_Tree_On_Radius(prune_daughter,
5282 prune_daughter->v[dir_prune],
5283 prune_daughter->b[dir_prune],
5284 &radius,
5285 ®raft_edge,
5286 &new_regraft_nd,
5287 &new_t,
5288 tree);
5289
5290
5291 /* printf("\n. new_regraft_edge: %d, new_regraft_nd: %d time: %f cur_regraft_nd: %d prune: %d prune_daughter: %d", */
5292 /* regraft_edge ? regraft_edge->num : -1, */
5293 /* new_regraft_nd ? new_regraft_nd->num : -1, */
5294 /* new_t, */
5295 /* cur_regraft_nd->num, */
5296 /* prune->num, */
5297 /* prune_daughter->num); fflush(NULL); */
5298
5299 if(new_regraft_nd == NULL) continue;
5300 if(new_regraft_nd == prune) continue;
5301 if(new_regraft_nd == cur_regraft_nd) continue;
5302 if(new_t > times[prune_daughter->num]) continue;
5303 assert(new_regraft_nd != prune_daughter);
5304
5305 // New age
5306 if(prune == tree->n_root || new_regraft_nd == tree->n_root)
5307 {
5308 if(prune == tree->n_root)
5309 {
5310 if(prune_daughter == tree->n_root->v[1]) times[tree->n_root->num] = times[tree->n_root->v[2]->num];
5311 else times[tree->n_root->num] = times[tree->n_root->v[1]->num];
5312 times[prune_daughter->v[dir_prune]->num] = new_t;
5313 }
5314 if(new_regraft_nd == tree->n_root)
5315 {
5316 times[prune_daughter->v[dir_prune]->num] = times[tree->n_root->num];
5317 times[tree->n_root->num] = new_t;
5318 }
5319 }
5320 else
5321 {
5322 times[prune->num] = new_t;
5323 }
5324
5325
5326 // Prune
5327 target = residual = NULL;
5328 Prune_Subtree(prune_daughter->v[dir_prune],
5329 prune_daughter,
5330 &target,&residual,tree);
5331 ori_target = target;
5332
5333
5334 // Regraft edge is the one sitting above new_regraft_nd
5335 if(new_regraft_nd == tree->n_root->v[1] ||
5336 new_regraft_nd == tree->n_root->v[2] ||
5337 new_regraft_nd == tree->n_root) regraft_edge = tree->e_root;
5338 else
5339 {
5340 for(i=0;i<3;++i) if(new_regraft_nd->v[i] == new_regraft_nd->anc) break;
5341 assert(i!=3);
5342 regraft_edge = new_regraft_nd->b[i];
5343 }
5344
5345 assert(regraft_edge);
5346 assert(residual->left != residual->rght);
5347 assert(regraft_edge->left != prune_daughter->v[dir_prune]);
5348 assert(regraft_edge->rght != prune_daughter->v[dir_prune]);
5349
5350 // Regraft
5351 Graft_Subtree(regraft_edge,
5352 prune_daughter->v[dir_prune],
5353 prune_daughter,
5354 residual,
5355 new_regraft_nd,tree);
5356
5357
5358 if(!TIMES_Check_Node_Height_Ordering(tree))
5359 {
5360 PhyML_Fprintf(stderr,"\n. prune[%d]->t:%.3f daughter[%d]->t:%.3f prune_anc[%d]->t:%.3f regraft[%d]->t:%.3f regraft_anc[%d]->t:%.3f [effective:%d] t_prior_min/max: [prune:[%.3f %.3f] regraft:[%.3f %.3f]] ",
5361 prune->num,
5362 times[prune->num],
5363 prune_daughter->num,
5364 times[prune_daughter->num],
5365 prune->anc ? prune->anc->num : -1,
5366 prune->anc ? times[prune->anc->num] : -1.,
5367 new_regraft_nd->num,
5368 times[new_regraft_nd->num],
5369 new_regraft_nd->anc ? new_regraft_nd->anc->num : -1,
5370 new_regraft_nd->anc ? times[new_regraft_nd->anc->num] : +1.,
5371 prune->num,
5372 tree->times->t_prior_min[prune->num],
5373 tree->times->t_prior_max[prune->num],
5374 tree->times->t_prior_min[new_regraft_nd->num],
5375 tree->times->t_prior_max[new_regraft_nd->num]);
5376 PhyML_Fprintf(stderr,"\n. root: %d %d %d",tree->n_root->num,tree->n_root->v[1]->num,tree->n_root->v[2]->num);
5377 assert(FALSE);
5378 }
5379
5380 RATES_Update_Cur_Bl(tree);
5381 DATE_Assign_Primary_Calibration(tree);
5382 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
5383
5384 if(new_lnL_time > UNLIKELY)
5385 {
5386 Set_Both_Sides(NO,tree);
5387 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
5388 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
5389 }
5390
5391 ratio += (new_lnL_seq - cur_lnL_seq);
5392 ratio += (new_lnL_rate - cur_lnL_rate);
5393 ratio += (new_lnL_time - cur_lnL_time);
5394
5395 ratio = exp(ratio);
5396 alpha = MIN(1.,ratio);
5397
5398 /* Always accept move */
5399 if(tree->mcmc->always_yes == YES && new_lnL_time > UNLIKELY) alpha = 1.0;
5400
5401 u = Uni();
5402
5403 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
5404
5405 if(u > alpha)
5406 {
5407 // Reject
5408 Prune_Subtree(prune_daughter->v[dir_prune],
5409 prune_daughter,
5410 &target,&residual,tree);
5411 assert(residual->left != residual->rght);
5412 assert(ori_target->left != prune_daughter->v[dir_prune]);
5413 assert(ori_target->rght != prune_daughter->v[dir_prune]);
5414 Graft_Subtree(ori_target,
5415 prune_daughter->v[dir_prune],
5416 prune_daughter,residual,prune == tree->n_root ? tree->n_root : cur_regraft_nd,tree);
5417
5418 RATES_Reset_Times(tree);
5419 RATES_Update_Cur_Bl(tree);
5420 DATE_Assign_Primary_Calibration(tree);
5421 new_lnL_time = TIMES_Lk_Times(NO,tree);
5422 if(Are_Equal(new_lnL_time,cur_lnL_time,1.E-5) == NO)
5423 {
5424 PhyML_Printf("\n. new_lnL_time: %f cur_lnL_time: %f",new_lnL_time,cur_lnL_time);
5425 assert(FALSE);
5426 }
5427
5428 /* new_lnL_seq = Lk(NULL,tree); */
5429 /* if(Are_Equal(new_lnL_seq,cur_lnL_seq,1.E-5) == NO) */
5430 /* { */
5431 /* PhyML_Printf("\n. new: %f cur: %f",new_lnL_seq,cur_lnL_seq); */
5432 /* assert(FALSE); */
5433 /* } */
5434
5435 if(!(tree->times->c_lnL_times > UNLIKELY))
5436 {
5437 printf("\n. time prune: %f",times[prune->num]);
5438 printf("\n. time prune_daughter: %f",times[prune_daughter->num]);
5439 printf("\n. prune: %d prune_daughter: %d prune_daughter->v[dir_prune]: %d cur_regraft_nd: %d new_regraft_nd: %d",
5440 prune->num,
5441 prune_daughter->num,
5442 prune_daughter->v[dir_prune]->num,
5443 cur_regraft_nd->num,
5444 new_regraft_nd->num);
5445 TIMES_Lk_Times(YES,tree);
5446 fflush(NULL);
5447 }
5448 assert(tree->times->c_lnL_times > UNLIKELY);
5449
5450 tree->c_lnL = cur_lnL_seq;
5451 tree->times->c_lnL_times = cur_lnL_time;
5452 tree->rates->c_lnL_rates = cur_lnL_rate;
5453 }
5454 else
5455 {
5456 tree->mcmc->acc_move[tree->mcmc->num_move_spr_weighted]++;
5457 }
5458 }
5459 }
5460
5461 //////////////////////////////////////////////////////////////
5462 //////////////////////////////////////////////////////////////
5463
MCMC_Prune_Regraft_Local(t_tree * tree)5464 void MCMC_Prune_Regraft_Local(t_tree *tree)
5465 {
5466 phydbl u,alpha,ratio;
5467 phydbl t_min,t_max;
5468 phydbl cur_lnL_seq,new_lnL_seq;
5469 phydbl cur_lnL_rate,new_lnL_rate;
5470 phydbl cur_lnL_time,new_lnL_time;
5471 phydbl new_t;
5472 int i,prune_idx,n_iter,n_regraft_nd,regraft_idx,dir_prune;
5473 phydbl *times;
5474 int rnd_dir,dir_v1,dir_v2;
5475 t_node *prune,*prune_daughter,*new_regraft_nd,*cur_regraft_nd;
5476 t_ll *regraft_nd_list;
5477 t_edge *target, *ori_target, *residual,*regraft_edge;
5478 t_node *a, *b, *c, *d, *e;
5479
5480 n_iter = MAX(1,(int)(tree->n_otu/5));
5481 times = tree->times->nd_t;
5482
5483 while(n_iter--)
5484 {
5485 tree->mcmc->run_move[tree->mcmc->num_move_spr_local]++;
5486
5487 RATES_Record_Times(tree);
5488
5489 cur_lnL_seq = tree->c_lnL;
5490 new_lnL_seq = tree->c_lnL;
5491 cur_lnL_rate = tree->rates->c_lnL_rates;
5492 new_lnL_rate = tree->rates->c_lnL_rates;
5493 cur_lnL_time = tree->times->c_lnL_times;
5494 new_lnL_time = tree->times->c_lnL_times;
5495
5496 ratio = 0.0;
5497 regraft_edge = NULL;
5498 new_regraft_nd = NULL;
5499 cur_regraft_nd = NULL;
5500 new_t = 0.0;
5501
5502 // Select prune node (any internal node)
5503 prune_idx = Rand_Int(tree->n_otu,2*tree->n_otu-2);
5504 /* prune_idx = tree->n_otu+n_iter; */
5505
5506 prune = tree->a_nodes[prune_idx];
5507
5508 assert(prune && prune->tax == NO);
5509
5510 // Select a daughter of prune node
5511 dir_v1 = dir_v2 = -1;
5512 for(i=0;i<3;i++)
5513 if(prune->v[i] != prune->anc && prune->b[i] != tree->e_root)
5514 {
5515 if(dir_v1 < 0) dir_v1 = i;
5516 else dir_v2 = i;
5517 }
5518
5519 u = Uni();
5520 if(u < 0.5) rnd_dir = dir_v1;
5521 else rnd_dir = dir_v2;
5522
5523 prune_daughter = prune->v[rnd_dir];
5524 cur_regraft_nd = prune->v[rnd_dir == dir_v1 ? dir_v2 : dir_v1];
5525
5526 if(prune == tree->n_root)
5527 {
5528 if(prune_daughter == prune->v[dir_v1] && prune->v[dir_v2]->tax == YES)
5529 {
5530 prune_daughter = prune->v[dir_v2];
5531 cur_regraft_nd = prune->v[dir_v1];
5532 }
5533
5534 if(prune_daughter == prune->v[dir_v2] && prune->v[dir_v1]->tax == YES)
5535 {
5536 prune_daughter = prune->v[dir_v1];
5537 cur_regraft_nd = prune->v[dir_v2];
5538 }
5539 }
5540
5541 assert(prune_daughter->anc == prune);
5542
5543 dir_prune = -1;
5544 for(i=0;i<3;i++)
5545 {
5546 if(prune_daughter->v[i] == prune || prune_daughter->b[i] == tree->e_root)
5547 {
5548 dir_prune = i;
5549 break;
5550 }
5551 }
5552 assert(dir_prune > -1);
5553
5554 /*
5555
5556 |
5557 |
5558 d
5559 / \
5560 / \c
5561 / \
5562 e / \prune_daughter
5563 /\
5564 / \
5565 a b
5566
5567 */
5568
5569
5570 // Probability of forward move
5571 regraft_nd_list = NULL;
5572 a = b = c = d = e = NULL;
5573 e = cur_regraft_nd;
5574
5575 if(e->tax == NO && times[e->num] < times[prune_daughter->num])
5576 {
5577 for(i=0;i<3;i++)
5578 {
5579 if(e->v[i] != e->anc && e->b[i] != tree->e_root)
5580 {
5581 if(a == NULL) a = e->v[i];
5582 else b = e->v[i];
5583 }
5584 }
5585 Push_Bottom_Linked_List(a,®raft_nd_list,YES);
5586 Push_Bottom_Linked_List(b,®raft_nd_list,YES);
5587 }
5588
5589 if(prune_daughter->anc != tree->n_root)
5590 {
5591 d = prune_daughter->anc->anc;
5592
5593 for(i=0;i<3;i++)
5594 {
5595 if(d->v[i] != d->anc && d->b[i] != tree->e_root && d->v[i] != prune)
5596 {
5597 c = d->v[i];
5598 break;
5599 }
5600 }
5601 Push_Bottom_Linked_List(c,®raft_nd_list,YES);
5602 Push_Bottom_Linked_List(d,®raft_nd_list,YES);
5603 }
5604
5605 n_regraft_nd = Linked_List_Len(regraft_nd_list);
5606
5607 if(n_regraft_nd == 0)
5608 {
5609 Free_Linked_List(regraft_nd_list);
5610 continue;
5611 }
5612
5613 assert(n_regraft_nd > 0);
5614 ratio -= log(1./n_regraft_nd);
5615
5616 // Randomly select one node among potential regraft sites (uniform)
5617 regraft_idx = Rand_Int(0,n_regraft_nd-1);
5618 new_regraft_nd = Linked_List_Elem(regraft_idx,regraft_nd_list);
5619 Free_Linked_List(regraft_nd_list);
5620
5621
5622 // Time of regraft node and corresponding (partial) Hastings ratio
5623 t_max = MIN(times[prune_daughter->num],times[cur_regraft_nd->num]);
5624 if(prune == tree->n_root) t_min = 10.0*t_max;
5625 else t_min = times[prune->anc->num];
5626 ratio += log(1./(t_max - t_min));
5627
5628
5629 t_max = MIN(times[prune_daughter->num],times[new_regraft_nd->num]);
5630 if(new_regraft_nd == tree->n_root) t_min = 10.0*t_max;
5631 else t_min = times[new_regraft_nd->anc->num];
5632 ratio -= log(1./(t_max - t_min));
5633
5634 new_t = Uni()*(t_max-t_min) + t_min;
5635
5636
5637 // Age of root node changes when pruned subtree is on one side of that node
5638 // Change here, not after the prune and regraft move
5639 /* if(prune == tree->n_root) */
5640 /* { */
5641 /* if(prune_daughter == tree->n_root->v[1]) */
5642 /* times[tree->n_root->num] = times[tree->n_root->v[2]->num]; */
5643 /* else if(prune_daughter == tree->n_root->v[2]) */
5644 /* times[tree->n_root->num] = times[tree->n_root->v[1]->num]; */
5645 /* else assert(false); */
5646 /* } */
5647
5648 /* printf("\n-> prune: %d prune_daughter: %d prune_daughter->v[dir_prune]: %d cur_regraft_nd: %d new_regraft_nd: %d", */
5649 /* prune->num, */
5650 /* prune_daughter->num, */
5651 /* prune_daughter->v[dir_prune]->num, */
5652 /* cur_regraft_nd->num, */
5653 /* new_regraft_nd->num); */
5654
5655 /* printf("\n. a:%d b:%d c:%d d:%d e:%d", */
5656 /* a ? a->num : -1, */
5657 /* b ? b->num : -1, */
5658 /* c ? c->num : -1, */
5659 /* d ? d->num : -1, */
5660 /* e ? e->num : -1); */
5661 /* fflush(NULL); */
5662
5663
5664 // New age
5665 if(prune == tree->n_root || new_regraft_nd == tree->n_root)
5666 {
5667 if(prune == tree->n_root)
5668 {
5669 if(prune_daughter == tree->n_root->v[1]) times[tree->n_root->num] = times[tree->n_root->v[2]->num];
5670 else times[tree->n_root->num] = times[tree->n_root->v[1]->num];
5671 times[prune_daughter->v[dir_prune]->num] = new_t;
5672 }
5673 if(new_regraft_nd == tree->n_root)
5674 {
5675 times[prune_daughter->v[dir_prune]->num] = times[tree->n_root->num];
5676 times[tree->n_root->num] = new_t;
5677 }
5678 }
5679 else
5680 {
5681 times[prune->num] = new_t;
5682 }
5683
5684
5685
5686 // Prune
5687 target = residual = NULL;
5688 Prune_Subtree(prune_daughter->v[dir_prune],
5689 prune_daughter,
5690 &target,&residual,tree);
5691 ori_target = target;
5692
5693 // Regraft edge is the one sitting above regraft_nd
5694 if(new_regraft_nd == tree->n_root->v[1] ||
5695 new_regraft_nd == tree->n_root->v[2] ||
5696 new_regraft_nd == tree->n_root) regraft_edge = tree->e_root;
5697 else
5698 {
5699 for(i=0;i<3;i++) if(new_regraft_nd->v[i] == new_regraft_nd->anc) break;
5700 assert(i!=3);
5701 regraft_edge = new_regraft_nd->b[i];
5702 }
5703
5704 assert(regraft_edge);
5705 assert(residual->left != residual->rght);
5706 assert(regraft_edge->left != prune_daughter->v[dir_prune]);
5707 assert(regraft_edge->rght != prune_daughter->v[dir_prune]);
5708
5709 // Regraft
5710 Graft_Subtree(regraft_edge,
5711 prune_daughter->v[dir_prune],
5712 prune_daughter,
5713 residual,
5714 new_regraft_nd,tree);
5715
5716
5717 a = b = c = d = e = NULL;
5718 regraft_nd_list = NULL;
5719 e = new_regraft_nd;
5720 if(new_regraft_nd == tree->n_root)
5721 {
5722 if(prune_daughter == tree->n_root->v[1])
5723 e = tree->n_root->v[2];
5724 else if(prune_daughter == tree->n_root->v[2])
5725 e = tree->n_root->v[1];
5726 else assert(false);
5727 }
5728
5729 if(e->tax == NO && times[e->num] < times[prune_daughter->num])
5730 {
5731 for(i=0;i<3;i++)
5732 {
5733 if(e->v[i] != e->anc && e->b[i] != tree->e_root)
5734 {
5735 if(a == NULL) a = e->v[i];
5736 else b = e->v[i];
5737 }
5738 }
5739 Push_Bottom_Linked_List(a,®raft_nd_list,YES);
5740 Push_Bottom_Linked_List(b,®raft_nd_list,YES);
5741 }
5742
5743 // prune is different from prune_daughter->anc here if prune == tree->n_root
5744 if(prune_daughter->anc != tree->n_root)
5745 {
5746 d = prune_daughter->anc->anc;
5747
5748 for(i=0;i<3;i++)
5749 {
5750 if(d->v[i] != d->anc && d->b[i] != tree->e_root && d->v[i] != prune)
5751 {
5752 c = d->v[i];
5753 break;
5754 }
5755 }
5756 Push_Bottom_Linked_List(c,®raft_nd_list,YES);
5757 Push_Bottom_Linked_List(d,®raft_nd_list,YES);
5758 }
5759
5760 /* printf("\n+ a:%d b:%d c:%d d:%d e:%d", */
5761 /* a ? a->num : -1, */
5762 /* b ? b->num : -1, */
5763 /* c ? c->num : -1, */
5764 /* d ? d->num : -1, */
5765 /* e ? e->num : -1); */
5766 /* fflush(NULL); */
5767
5768 // Number of regraft nodes
5769 n_regraft_nd = Linked_List_Len(regraft_nd_list);
5770 assert(n_regraft_nd > 0);
5771 ratio += log(1./n_regraft_nd);
5772 Free_Linked_List(regraft_nd_list);
5773
5774 // New age
5775 /* if(new_regraft_nd == tree->n_root) */
5776 /* { */
5777 /* if(prune_daughter == tree->n_root->v[1]) */
5778 /* times[tree->n_root->v[2]->num] = times[tree->n_root->num]; */
5779 /* else if(prune_daughter == tree->n_root->v[2]) */
5780 /* times[tree->n_root->v[1]->num] = times[tree->n_root->num]; */
5781 /* else assert(false); */
5782
5783 /* times[tree->n_root->num] = new_t; */
5784 /* } */
5785 /* else if(prune == tree->n_root) */
5786 /* { */
5787 /* times[prune_daughter->v[dir_prune]->num] = new_t; */
5788 /* } */
5789 /* else */
5790 /* { */
5791 /* times[prune->num] = new_t; */
5792 /* } */
5793
5794
5795
5796
5797
5798 if(!TIMES_Check_Node_Height_Ordering(tree))
5799 {
5800 printf("\n. prune[%d]->t:%.3f daughter[%d]->t:%.3f prune_anc[%d]->t:%.3f regraft[%d]->t:%.3f regraft_anc[%d]->t:%.3f [effective:%d] t_prior_min/max: [prune:[%.3f %.3f] regraft:[%.3f %.3f]] ",
5801 prune->num,
5802 times[prune->num],
5803 prune_daughter->num,
5804 times[prune_daughter->num],
5805 prune->anc ? prune->anc->num : -1,
5806 prune->anc ? times[prune->anc->num] : -1.,
5807 new_regraft_nd->num,
5808 times[new_regraft_nd->num],
5809 new_regraft_nd->anc ? new_regraft_nd->anc->num : -1,
5810 new_regraft_nd->anc ? times[new_regraft_nd->anc->num] : +1.,
5811 prune->num,
5812 tree->times->t_prior_min[prune->num],
5813 tree->times->t_prior_max[prune->num],
5814 tree->times->t_prior_min[new_regraft_nd->num],
5815 tree->times->t_prior_max[new_regraft_nd->num]);
5816 PhyML_Fprintf(stderr,"\n. root: %d %d %d",tree->n_root->num,tree->n_root->v[1]->num,tree->n_root->v[2]->num);
5817 assert(FALSE);
5818 }
5819
5820 RATES_Update_Cur_Bl(tree);
5821 DATE_Assign_Primary_Calibration(tree);
5822
5823 if(tree->eval_glnL == YES) new_lnL_time = TIMES_Lk_Times(NO,tree);
5824
5825 if(new_lnL_time > UNLIKELY)
5826 {
5827 Set_Both_Sides(NO,tree);
5828 if(tree->eval_alnL == YES) new_lnL_seq = Lk(NULL,tree);
5829 if(tree->eval_rlnL == YES) new_lnL_rate = RATES_Lk_Rates(tree);
5830 }
5831
5832 ratio += (new_lnL_seq - cur_lnL_seq);
5833 ratio += (new_lnL_rate - cur_lnL_rate);
5834 ratio += (new_lnL_time - cur_lnL_time);
5835
5836 ratio = exp(ratio);
5837 alpha = MIN(1.,ratio);
5838
5839 /* Always accept move */
5840 if(tree->mcmc->always_yes == YES && new_lnL_time > UNLIKELY) alpha = 1.0;
5841
5842 u = Uni();
5843
5844 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
5845
5846 if(u > alpha)
5847 {
5848 // Reject
5849 Prune_Subtree(prune_daughter->v[dir_prune],
5850 prune_daughter,
5851 &target,&residual,tree);
5852 assert(residual->left != residual->rght);
5853 assert(ori_target->left != prune_daughter->v[dir_prune]);
5854 assert(ori_target->rght != prune_daughter->v[dir_prune]);
5855 Graft_Subtree(ori_target,
5856 prune_daughter->v[dir_prune],
5857 prune_daughter,residual,prune == tree->n_root ? tree->n_root : cur_regraft_nd,tree);
5858
5859 RATES_Reset_Times(tree);
5860 RATES_Update_Cur_Bl(tree);
5861 DATE_Assign_Primary_Calibration(tree);
5862 new_lnL_time = TIMES_Lk_Times(NO,tree);
5863 if(Are_Equal(new_lnL_time,cur_lnL_time,1.E-5) == NO)
5864 {
5865 PhyML_Printf("\n. new_lnL_time: %f cur_lnL_time: %f",new_lnL_time,cur_lnL_time);
5866 assert(FALSE);
5867 }
5868
5869 /* new_lnL_seq = Lk(NULL,tree); */
5870 /* if(Are_Equal(new_lnL_seq,cur_lnL_seq,1.E-5) == NO) */
5871 /* { */
5872 /* PhyML_Printf("\n. new: %f cur: %f",new_lnL_seq,cur_lnL_seq); */
5873 /* assert(FALSE); */
5874 /* } */
5875
5876
5877 if(!(tree->times->c_lnL_times > UNLIKELY))
5878 {
5879 printf("\n. time prune: %f",times[prune->num]);
5880 printf("\n. time prune_daughter: %f",times[prune_daughter->num]);
5881 printf("\n. prune: %d prune_daughter: %d prune_daughter->v[dir_prune]: %d cur_regraft_nd: %d new_regraft_nd: %d",
5882 prune->num,
5883 prune_daughter->num,
5884 prune_daughter->v[dir_prune]->num,
5885 cur_regraft_nd->num,
5886 new_regraft_nd->num);
5887 TIMES_Lk_Times(YES,tree);
5888 fflush(NULL);
5889 }
5890 assert(tree->times->c_lnL_times > UNLIKELY);
5891
5892 tree->c_lnL = cur_lnL_seq;
5893 tree->times->c_lnL_times = cur_lnL_time;
5894 tree->rates->c_lnL_rates = cur_lnL_rate;
5895 }
5896 else
5897 {
5898 tree->mcmc->acc_move[tree->mcmc->num_move_spr_local]++;
5899 }
5900 }
5901 }
5902
5903 //////////////////////////////////////////////////////////////
5904 //////////////////////////////////////////////////////////////
5905
MCMC_Complete_MCMC(t_mcmc * mcmc,t_tree * tree)5906 void MCMC_Complete_MCMC(t_mcmc *mcmc, t_tree *tree)
5907 {
5908 int i;
5909 phydbl sum;
5910
5911 mcmc->io = tree->io;
5912 mcmc->n_moves = 0;
5913 mcmc->move_idx = -1;
5914
5915 mcmc->num_move_nd_r = mcmc->n_moves; mcmc->n_moves += 1;
5916 mcmc->num_move_br_r = mcmc->n_moves; mcmc->n_moves += 1;
5917 mcmc->num_move_times = mcmc->n_moves; mcmc->n_moves += 1;
5918 mcmc->num_move_times_and_rates = mcmc->n_moves; mcmc->n_moves += 1;
5919 mcmc->num_move_times_and_rates_root = mcmc->n_moves; mcmc->n_moves += 1;
5920 mcmc->num_move_root_time = mcmc->n_moves; mcmc->n_moves += 1;
5921 mcmc->num_move_nu = mcmc->n_moves; mcmc->n_moves += 1;
5922 mcmc->num_move_clock_r = mcmc->n_moves; mcmc->n_moves += 1;
5923 mcmc->num_move_tree_height = mcmc->n_moves; mcmc->n_moves += 1;
5924 mcmc->num_move_time_slice = mcmc->n_moves; mcmc->n_moves += 1;
5925 mcmc->num_move_subtree_height = mcmc->n_moves; mcmc->n_moves += 1;
5926 mcmc->num_move_kappa = mcmc->n_moves; mcmc->n_moves += 1;
5927 mcmc->num_move_rr = mcmc->n_moves; mcmc->n_moves += 1;
5928 mcmc->num_move_tree_rates = mcmc->n_moves; mcmc->n_moves += 1;
5929 mcmc->num_move_subtree_rates = mcmc->n_moves; mcmc->n_moves += 1;
5930 mcmc->num_move_updown_nu_cr = mcmc->n_moves; mcmc->n_moves += 1;
5931 mcmc->num_move_ras = mcmc->n_moves; mcmc->n_moves += 1;
5932 mcmc->num_move_updown_t_cr = mcmc->n_moves; mcmc->n_moves += 1;
5933 mcmc->num_move_cov_rates = mcmc->n_moves; mcmc->n_moves += 1;
5934 mcmc->num_move_cov_switch = mcmc->n_moves; mcmc->n_moves += 1;
5935 mcmc->num_move_birth_rate = mcmc->n_moves; mcmc->n_moves += 1;
5936 mcmc->num_move_death_rate = mcmc->n_moves; mcmc->n_moves += 1;
5937 mcmc->num_move_birth_death_updown = mcmc->n_moves; mcmc->n_moves += 1;
5938 mcmc->num_move_spr = mcmc->n_moves; mcmc->n_moves += 1;
5939 mcmc->num_move_spr_weighted = mcmc->n_moves; mcmc->n_moves += 1;
5940 mcmc->num_move_spr_local = mcmc->n_moves; mcmc->n_moves += 1;
5941 mcmc->num_move_spr_root = mcmc->n_moves; mcmc->n_moves += 1;
5942 mcmc->num_move_updown_t_br = mcmc->n_moves; mcmc->n_moves += 1;
5943 mcmc->num_move_jump_calibration = mcmc->n_moves; mcmc->n_moves += 1;
5944 mcmc->num_move_geo_lambda = mcmc->n_moves; mcmc->n_moves += 1;
5945 mcmc->num_move_geo_sigma = mcmc->n_moves; mcmc->n_moves += 1;
5946 mcmc->num_move_geo_tau = mcmc->n_moves; mcmc->n_moves += 1;
5947 mcmc->num_move_geo_updown_tau_lbda = mcmc->n_moves; mcmc->n_moves += 1;
5948 mcmc->num_move_geo_updown_lbda_sigma = mcmc->n_moves; mcmc->n_moves += 1;
5949 mcmc->num_move_geo_dum = mcmc->n_moves; mcmc->n_moves += 1;
5950 mcmc->num_move_phyrex_lbda = mcmc->n_moves; mcmc->n_moves += 1;
5951 mcmc->num_move_phyrex_mu = mcmc->n_moves; mcmc->n_moves += 1;
5952 mcmc->num_move_phyrex_rad = mcmc->n_moves; mcmc->n_moves += 1;
5953 mcmc->num_move_phyrex_indel_disk = mcmc->n_moves; mcmc->n_moves += 1;
5954 mcmc->num_move_phyrex_move_disk_ud = mcmc->n_moves; mcmc->n_moves += 1;
5955 mcmc->num_move_phyrex_swap_disk = mcmc->n_moves; mcmc->n_moves += 1;
5956 mcmc->num_move_phyrex_indel_hit = mcmc->n_moves; mcmc->n_moves += 1;
5957 mcmc->num_move_phyrex_spr = mcmc->n_moves; mcmc->n_moves += 1;
5958 mcmc->num_move_phyrex_spr_local = mcmc->n_moves; mcmc->n_moves += 1;
5959 mcmc->num_move_phyrex_scale_times = mcmc->n_moves; mcmc->n_moves += 1;
5960 mcmc->num_move_phyrex_ldscape_lim = mcmc->n_moves; mcmc->n_moves += 1;
5961 mcmc->num_move_phyrex_sigsq = mcmc->n_moves; mcmc->n_moves += 1;
5962 mcmc->num_move_phyrex_neff = mcmc->n_moves; mcmc->n_moves += 1;
5963 mcmc->num_move_phyrex_sim = mcmc->n_moves; mcmc->n_moves += 1;
5964 mcmc->num_move_phyrex_traj = mcmc->n_moves; mcmc->n_moves += 1;
5965 mcmc->num_move_phyrex_indel_disk_serial = mcmc->n_moves; mcmc->n_moves += 1;
5966 mcmc->num_move_phyrex_sim_plus = mcmc->n_moves; mcmc->n_moves += 1;
5967 mcmc->num_move_phyrex_indel_hit_serial = mcmc->n_moves; mcmc->n_moves += 1;
5968 mcmc->num_move_phyrex_ldsk_given_disk = mcmc->n_moves; mcmc->n_moves += 1;
5969 mcmc->num_move_phyrex_disk_given_ldsk = mcmc->n_moves; mcmc->n_moves += 1;
5970 mcmc->num_move_phyrex_ldsk_and_disk = mcmc->n_moves; mcmc->n_moves += 1;
5971 mcmc->num_move_phyrex_ldsk_multi = mcmc->n_moves; mcmc->n_moves += 1;
5972 mcmc->num_move_phyrex_disk_multi = mcmc->n_moves; mcmc->n_moves += 1;
5973 mcmc->num_move_phyrex_add_remove_jump = mcmc->n_moves; mcmc->n_moves += 1;
5974 mcmc->num_move_clade_change = mcmc->n_moves; mcmc->n_moves += 1;
5975 mcmc->num_move_phyrex_ldsk_tip_to_root = mcmc->n_moves; mcmc->n_moves += 1;
5976 mcmc->num_move_phyrex_sigsq_scale = mcmc->n_moves; mcmc->n_moves += 1;
5977
5978 mcmc->run_move = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5979 mcmc->acc_move = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5980 mcmc->prev_run_move = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5981 mcmc->prev_acc_move = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5982 mcmc->acc_rate = (phydbl *)mCalloc(mcmc->n_moves,sizeof(phydbl));
5983 mcmc->move_weight = (phydbl *)mCalloc(mcmc->n_moves,sizeof(phydbl));
5984 mcmc->move_type = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5985
5986 /* TO DO: instead of n_moves here we should have something like n_param */
5987 mcmc->ess = (phydbl *)mCalloc(mcmc->n_moves,sizeof(phydbl));
5988 mcmc->ess_run = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5989 mcmc->start_ess = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5990 mcmc->adjust_tuning = (int *)mCalloc(mcmc->n_moves,sizeof(int));
5991 mcmc->tune_move = (phydbl *)mCalloc(mcmc->n_moves,sizeof(phydbl));
5992 mcmc->sampled_val = (phydbl *)mCalloc((int)mcmc->n_moves*(mcmc->chain_len/mcmc->sample_interval + 1),sizeof(phydbl));
5993 mcmc->mode = (phydbl *)mCalloc((int)mcmc->n_moves,sizeof(phydbl));
5994 mcmc->move_name = (char **)mCalloc(mcmc->n_moves,sizeof(char *));
5995 for(i=0;i<mcmc->n_moves;i++) mcmc->move_name[i] = (char *)mCalloc(T_MAX_MCMC_MOVE_NAME,sizeof(char));
5996
5997 for(i=0;i<mcmc->n_moves;i++) mcmc->adjust_tuning[i] = YES;
5998
5999 strcpy(mcmc->move_name[mcmc->num_move_br_r],"br_rate");
6000 strcpy(mcmc->move_name[mcmc->num_move_nd_r],"nd_rate");
6001 strcpy(mcmc->move_name[mcmc->num_move_times],"times");
6002 strcpy(mcmc->move_name[mcmc->num_move_times_and_rates],"times_and_rates");
6003 strcpy(mcmc->move_name[mcmc->num_move_times_and_rates_root],"times_and_rates_root");
6004 strcpy(mcmc->move_name[mcmc->num_move_root_time],"root_time");
6005 strcpy(mcmc->move_name[mcmc->num_move_nu],"nu");
6006 strcpy(mcmc->move_name[mcmc->num_move_clock_r],"clock");
6007 strcpy(mcmc->move_name[mcmc->num_move_tree_height],"tree_height");
6008 strcpy(mcmc->move_name[mcmc->num_move_time_slice],"time_slice");
6009 strcpy(mcmc->move_name[mcmc->num_move_subtree_height],"subtree_height");
6010 strcpy(mcmc->move_name[mcmc->num_move_kappa],"kappa");
6011 strcpy(mcmc->move_name[mcmc->num_move_rr],"rr");
6012 strcpy(mcmc->move_name[mcmc->num_move_spr],"spr");
6013 strcpy(mcmc->move_name[mcmc->num_move_spr_weighted],"spr_weighted");
6014 strcpy(mcmc->move_name[mcmc->num_move_spr_local],"spr_local");
6015 strcpy(mcmc->move_name[mcmc->num_move_spr_root],"spr_root");
6016 strcpy(mcmc->move_name[mcmc->num_move_tree_rates],"tree_rates");
6017 strcpy(mcmc->move_name[mcmc->num_move_subtree_rates],"subtree_rates");
6018 strcpy(mcmc->move_name[mcmc->num_move_updown_nu_cr],"updown_nu_cr");
6019 strcpy(mcmc->move_name[mcmc->num_move_ras],"ras");
6020 strcpy(mcmc->move_name[mcmc->num_move_updown_t_cr],"updown_t_cr");
6021 strcpy(mcmc->move_name[mcmc->num_move_cov_rates],"cov_rates");
6022 strcpy(mcmc->move_name[mcmc->num_move_cov_switch],"cov_switch");
6023 strcpy(mcmc->move_name[mcmc->num_move_birth_rate],"birth_rate");
6024 strcpy(mcmc->move_name[mcmc->num_move_death_rate],"death_rate");
6025 strcpy(mcmc->move_name[mcmc->num_move_birth_death_updown],"birth_death_updown");
6026 strcpy(mcmc->move_name[mcmc->num_move_updown_t_br],"updown_t_br");
6027 strcpy(mcmc->move_name[mcmc->num_move_jump_calibration],"jump_calibration");
6028 strcpy(mcmc->move_name[mcmc->num_move_geo_lambda],"geo_lambda");
6029 strcpy(mcmc->move_name[mcmc->num_move_geo_sigma],"geo_sigma");
6030 strcpy(mcmc->move_name[mcmc->num_move_geo_tau],"geo_tau");
6031 strcpy(mcmc->move_name[mcmc->num_move_geo_updown_tau_lbda],"geo_updown_tau_lbda");
6032 strcpy(mcmc->move_name[mcmc->num_move_geo_updown_lbda_sigma],"geo_updown_lbda_sigma");
6033 strcpy(mcmc->move_name[mcmc->num_move_geo_dum],"geo_dum");
6034 strcpy(mcmc->move_name[mcmc->num_move_phyrex_lbda],"phyrex_lbda");
6035 strcpy(mcmc->move_name[mcmc->num_move_phyrex_mu],"phyrex_mu");
6036 strcpy(mcmc->move_name[mcmc->num_move_phyrex_rad],"phyrex_rad");
6037 strcpy(mcmc->move_name[mcmc->num_move_phyrex_indel_disk],"phyrex_indel_disk");
6038 strcpy(mcmc->move_name[mcmc->num_move_phyrex_move_disk_ud],"phyrex_move_disk_ud");
6039 strcpy(mcmc->move_name[mcmc->num_move_phyrex_swap_disk],"phyrex_swap_disk");
6040 strcpy(mcmc->move_name[mcmc->num_move_phyrex_indel_hit],"phyrex_indel_hit");
6041 strcpy(mcmc->move_name[mcmc->num_move_phyrex_spr],"phyrex_spr");
6042 strcpy(mcmc->move_name[mcmc->num_move_phyrex_spr_local],"phyrex_spr_local");
6043 strcpy(mcmc->move_name[mcmc->num_move_phyrex_scale_times],"phyrex_scale_times");
6044 strcpy(mcmc->move_name[mcmc->num_move_phyrex_ldscape_lim],"phyrex_ldscape_lim");
6045 strcpy(mcmc->move_name[mcmc->num_move_phyrex_sigsq],"phyrex_sigsq");
6046 strcpy(mcmc->move_name[mcmc->num_move_phyrex_neff],"phyrex_neff");
6047 strcpy(mcmc->move_name[mcmc->num_move_phyrex_sim],"phyrex_sim");
6048 strcpy(mcmc->move_name[mcmc->num_move_phyrex_traj],"phyrex_traj");
6049 strcpy(mcmc->move_name[mcmc->num_move_phyrex_indel_disk_serial],"phyrex_indel_disk_serial");
6050 strcpy(mcmc->move_name[mcmc->num_move_phyrex_sim_plus],"phyrex_sim_plus");
6051 strcpy(mcmc->move_name[mcmc->num_move_phyrex_indel_hit_serial],"phyrex_indel_hit_serial");
6052 strcpy(mcmc->move_name[mcmc->num_move_phyrex_ldsk_multi],"phyrex_ldsk_multi");
6053 strcpy(mcmc->move_name[mcmc->num_move_phyrex_disk_multi],"phyrex_disk_multi");
6054 strcpy(mcmc->move_name[mcmc->num_move_phyrex_ldsk_and_disk],"phyrex_ldsk_and_disk");
6055 strcpy(mcmc->move_name[mcmc->num_move_phyrex_disk_given_ldsk],"phyrex_disk_given_ldsk");
6056 strcpy(mcmc->move_name[mcmc->num_move_phyrex_ldsk_given_disk],"phyrex_ldsk_given_disk");
6057 strcpy(mcmc->move_name[mcmc->num_move_phyrex_add_remove_jump],"phyrex_add_remove_jump");
6058 strcpy(mcmc->move_name[mcmc->num_move_clade_change],"clade_change");
6059 strcpy(mcmc->move_name[mcmc->num_move_phyrex_ldsk_tip_to_root],"phyrex_ldsk_tip_to_root");
6060 strcpy(mcmc->move_name[mcmc->num_move_phyrex_sigsq_scale],"phyrex_sigsq_scale");
6061
6062
6063 for(i=0;i<mcmc->n_moves;i++) mcmc->move_type[i] = -1;
6064 mcmc->move_type[mcmc->num_move_nd_r] = MCMC_MOVE_SCALE_THORNE;
6065 mcmc->move_type[mcmc->num_move_br_r] = MCMC_MOVE_SCALE_THORNE;
6066 mcmc->move_type[mcmc->num_move_times] = MCMC_MOVE_SCALE_THORNE;
6067 mcmc->move_type[mcmc->num_move_root_time] = MCMC_MOVE_SCALE_THORNE;
6068 mcmc->move_type[mcmc->num_move_nu] = MCMC_MOVE_SCALE_THORNE;
6069 mcmc->move_type[mcmc->num_move_clock_r] = MCMC_MOVE_SCALE_THORNE;
6070 mcmc->move_type[mcmc->num_move_tree_height] = MCMC_MOVE_SCALE_THORNE;
6071 mcmc->move_type[mcmc->num_move_time_slice] = MCMC_MOVE_SCALE_THORNE;
6072 mcmc->move_type[mcmc->num_move_subtree_height] = MCMC_MOVE_SCALE_THORNE;
6073 mcmc->move_type[mcmc->num_move_kappa] = MCMC_MOVE_SCALE_THORNE;
6074 mcmc->move_type[mcmc->num_move_rr] = MCMC_MOVE_SCALE_THORNE;
6075 mcmc->move_type[mcmc->num_move_tree_rates] = MCMC_MOVE_SCALE_THORNE;
6076 mcmc->move_type[mcmc->num_move_subtree_rates] = MCMC_MOVE_SCALE_THORNE;
6077 mcmc->move_type[mcmc->num_move_updown_nu_cr] = MCMC_MOVE_RANDWALK_NORMAL;
6078 mcmc->move_type[mcmc->num_move_ras] = MCMC_MOVE_RANDWALK_NORMAL;
6079 mcmc->move_type[mcmc->num_move_updown_t_cr] = MCMC_MOVE_SCALE_THORNE;
6080 mcmc->move_type[mcmc->num_move_cov_rates] = MCMC_MOVE_SCALE_THORNE;
6081 mcmc->move_type[mcmc->num_move_cov_switch] = MCMC_MOVE_SCALE_THORNE;
6082 mcmc->move_type[mcmc->num_move_birth_rate] = MCMC_MOVE_SCALE_THORNE;
6083 mcmc->move_type[mcmc->num_move_death_rate] = MCMC_MOVE_SCALE_THORNE;
6084 mcmc->move_type[mcmc->num_move_birth_death_updown] = MCMC_MOVE_SCALE_THORNE;
6085 mcmc->move_type[mcmc->num_move_updown_t_br] = MCMC_MOVE_SCALE_THORNE;
6086 mcmc->move_type[mcmc->num_move_jump_calibration] = MCMC_MOVE_SCALE_THORNE;
6087 mcmc->move_type[mcmc->num_move_geo_lambda] = MCMC_MOVE_SCALE_THORNE;
6088 mcmc->move_type[mcmc->num_move_geo_sigma] = MCMC_MOVE_SCALE_THORNE;
6089 mcmc->move_type[mcmc->num_move_geo_tau] = MCMC_MOVE_SCALE_THORNE;
6090 mcmc->move_type[mcmc->num_move_geo_updown_tau_lbda] = MCMC_MOVE_SCALE_THORNE;
6091 mcmc->move_type[mcmc->num_move_geo_updown_lbda_sigma] = MCMC_MOVE_SCALE_THORNE;
6092 mcmc->move_type[mcmc->num_move_geo_dum] = MCMC_MOVE_SCALE_THORNE;
6093 mcmc->move_type[mcmc->num_move_phyrex_lbda] = MCMC_MOVE_SCALE_THORNE;
6094 mcmc->move_type[mcmc->num_move_phyrex_mu] = MCMC_MOVE_SCALE_THORNE;
6095 mcmc->move_type[mcmc->num_move_phyrex_rad] = MCMC_MOVE_SCALE_THORNE;
6096 mcmc->move_type[mcmc->num_move_phyrex_scale_times] = MCMC_MOVE_SCALE_THORNE;
6097 mcmc->move_type[mcmc->num_move_phyrex_sigsq] = MCMC_MOVE_SCALE_THORNE;
6098 mcmc->move_type[mcmc->num_move_phyrex_neff] = MCMC_MOVE_SCALE_THORNE;
6099 mcmc->move_type[mcmc->num_move_phyrex_indel_hit] = MCMC_MOVE_SCALE_THORNE;
6100 mcmc->move_type[mcmc->num_move_phyrex_indel_disk] = MCMC_MOVE_SCALE_THORNE;
6101 mcmc->move_type[mcmc->num_move_phyrex_ldsk_tip_to_root] = MCMC_MOVE_SCALE_THORNE;
6102 mcmc->move_type[mcmc->num_move_phyrex_sigsq_scale] = MCMC_MOVE_SCALE_THORNE;
6103
6104 /* We start with small tuning parameter values in order to have inflated ESS
6105 for clock_r */
6106 for(i=0;i<mcmc->n_moves;i++)
6107 {
6108 switch(mcmc->move_type[i])
6109 {
6110 case MCMC_MOVE_RANDWALK_NORMAL:
6111 {
6112 /* mcmc->tune_move[i] = 1.E-1; */
6113 mcmc->tune_move[i] = 100.;
6114 break;
6115 }
6116 case MCMC_MOVE_RANDWALK_UNIFORM:
6117 {
6118 /* mcmc->tune_move[i] = 2.0; */
6119 mcmc->tune_move[i] = 20.0;
6120 break;
6121 }
6122 case MCMC_MOVE_SCALE_GAMMA:
6123 {
6124 mcmc->tune_move[i] = 1.0;
6125 /* mcmc->tune_move[i] = 10.0; */
6126 break;
6127 }
6128 case MCMC_MOVE_SCALE_THORNE:
6129 {
6130 mcmc->tune_move[i] = 1.0;
6131 break;
6132 }
6133 default :
6134 {
6135 mcmc->tune_move[i] = 1.0;
6136 break;
6137 }
6138 }
6139 }
6140
6141 mcmc->move_weight[mcmc->num_move_br_r] = 1.0;
6142 mcmc->move_weight[mcmc->num_move_nd_r] = 0.0;
6143 mcmc->move_weight[mcmc->num_move_times] = 1.0;
6144 mcmc->move_weight[mcmc->num_move_times_and_rates] = 5.0;
6145 mcmc->move_weight[mcmc->num_move_root_time] = 0.0;
6146 mcmc->move_weight[mcmc->num_move_clock_r] = 1.0;
6147 mcmc->move_weight[mcmc->num_move_tree_height] = 1.0;
6148 mcmc->move_weight[mcmc->num_move_time_slice] = 0.0;
6149 mcmc->move_weight[mcmc->num_move_subtree_height] = 0.0;
6150 mcmc->move_weight[mcmc->num_move_nu] = 1.0;
6151 mcmc->move_weight[mcmc->num_move_kappa] = 0.5;
6152 mcmc->move_weight[mcmc->num_move_rr] = 0.5;
6153 mcmc->move_weight[mcmc->num_move_spr] = 3.0;
6154 mcmc->move_weight[mcmc->num_move_spr_weighted] = 5.0;
6155 mcmc->move_weight[mcmc->num_move_spr_local] = 3.0;
6156 mcmc->move_weight[mcmc->num_move_spr_root] = 0.0;
6157 mcmc->move_weight[mcmc->num_move_tree_rates] = 1.0;
6158 mcmc->move_weight[mcmc->num_move_subtree_rates] = 0.0;
6159 mcmc->move_weight[mcmc->num_move_updown_nu_cr] = 0.0;
6160 mcmc->move_weight[mcmc->num_move_ras] = 1.0;
6161 mcmc->move_weight[mcmc->num_move_updown_t_cr] = 1.0; /* Does not seem to work well (does not give uniform prior on root height
6162 when sampling from prior) */
6163 mcmc->move_weight[mcmc->num_move_cov_rates] = 0.0;
6164 mcmc->move_weight[mcmc->num_move_cov_switch] = 0.0;
6165 mcmc->move_weight[mcmc->num_move_birth_rate] = 1.0;
6166 mcmc->move_weight[mcmc->num_move_death_rate] = 1.0;
6167 mcmc->move_weight[mcmc->num_move_birth_death_updown] = 1.0;
6168 mcmc->move_weight[mcmc->num_move_updown_t_br] = 0.0;
6169 #if defined (INVITEE)
6170 mcmc->move_weight[mcmc->num_move_jump_calibration] = 0.1;
6171 #else
6172 mcmc->move_weight[mcmc->num_move_jump_calibration] = 0.0;
6173 #endif
6174 mcmc->move_weight[mcmc->num_move_geo_lambda] = 1.0;
6175 mcmc->move_weight[mcmc->num_move_geo_sigma] = 1.0;
6176 mcmc->move_weight[mcmc->num_move_geo_tau] = 1.0;
6177 mcmc->move_weight[mcmc->num_move_geo_updown_tau_lbda] = 1.0;
6178 mcmc->move_weight[mcmc->num_move_geo_updown_lbda_sigma] = 1.0;
6179 mcmc->move_weight[mcmc->num_move_geo_dum] = 1.0;
6180 mcmc->move_weight[mcmc->num_move_clade_change] = 1.0;
6181
6182 # if defined (PHYREX)
6183
6184 mcmc->move_weight[mcmc->num_move_phyrex_lbda] = 1.0;
6185 mcmc->move_weight[mcmc->num_move_phyrex_mu] = 1.0;
6186 mcmc->move_weight[mcmc->num_move_phyrex_rad] = 1.0;
6187 mcmc->move_weight[mcmc->num_move_phyrex_sigsq] = 1.0;
6188 mcmc->move_weight[mcmc->num_move_phyrex_neff] = 1.0;
6189 mcmc->move_weight[mcmc->num_move_phyrex_indel_disk] = 1.0;
6190 mcmc->move_weight[mcmc->num_move_phyrex_indel_hit] = 1.0;
6191 mcmc->move_weight[mcmc->num_move_phyrex_move_disk_ud] = 2.0;
6192 mcmc->move_weight[mcmc->num_move_phyrex_swap_disk] = 2.0;
6193 mcmc->move_weight[mcmc->num_move_phyrex_spr] = 0.5;
6194 mcmc->move_weight[mcmc->num_move_phyrex_spr_local] = 1.0;
6195 mcmc->move_weight[mcmc->num_move_phyrex_scale_times] = 2.0;
6196 mcmc->move_weight[mcmc->num_move_phyrex_ldscape_lim] = 0.0;
6197 mcmc->move_weight[mcmc->num_move_phyrex_sim] = 0.0;
6198 mcmc->move_weight[mcmc->num_move_phyrex_traj] = 2.0;
6199 mcmc->move_weight[mcmc->num_move_phyrex_sim_plus] = 0.0;
6200 mcmc->move_weight[mcmc->num_move_phyrex_indel_disk_serial] = 2.0;
6201 mcmc->move_weight[mcmc->num_move_phyrex_indel_hit_serial] = 2.0;
6202
6203 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_given_disk] = 1.0;
6204 mcmc->move_weight[mcmc->num_move_phyrex_disk_given_ldsk] = 1.0;
6205 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_and_disk] = 0.5;
6206 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_multi] = 1.0;
6207 mcmc->move_weight[mcmc->num_move_phyrex_disk_multi] = 1.0;
6208 mcmc->move_weight[mcmc->num_move_phyrex_add_remove_jump] = 3.0;
6209 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_tip_to_root] = 1.0;
6210 mcmc->move_weight[mcmc->num_move_phyrex_sigsq_scale] = 1.0;
6211
6212 # else
6213
6214 mcmc->move_weight[mcmc->num_move_phyrex_lbda] = 0.0;
6215 mcmc->move_weight[mcmc->num_move_phyrex_mu] = 0.0;
6216 mcmc->move_weight[mcmc->num_move_phyrex_rad] = 0.0;
6217 mcmc->move_weight[mcmc->num_move_phyrex_sigsq] = 0.0;
6218 mcmc->move_weight[mcmc->num_move_phyrex_neff] = 0.0;
6219 mcmc->move_weight[mcmc->num_move_phyrex_indel_disk] = 0.0;
6220 mcmc->move_weight[mcmc->num_move_phyrex_indel_hit] = 0.0;
6221 mcmc->move_weight[mcmc->num_move_phyrex_move_disk_ud] = 0.0;
6222 mcmc->move_weight[mcmc->num_move_phyrex_swap_disk] = 0.0;
6223 mcmc->move_weight[mcmc->num_move_phyrex_spr] = 0.0;
6224 mcmc->move_weight[mcmc->num_move_phyrex_spr_local] = 0.0;
6225 mcmc->move_weight[mcmc->num_move_phyrex_scale_times] = 0.0;
6226 mcmc->move_weight[mcmc->num_move_phyrex_ldscape_lim] = 0.0;
6227 mcmc->move_weight[mcmc->num_move_phyrex_sim] = 0.0;
6228 mcmc->move_weight[mcmc->num_move_phyrex_traj] = 0.0;
6229 mcmc->move_weight[mcmc->num_move_phyrex_sim_plus] = 0.0;
6230 mcmc->move_weight[mcmc->num_move_phyrex_indel_disk_serial] = 0.0;
6231 mcmc->move_weight[mcmc->num_move_phyrex_indel_hit_serial] = 0.0;
6232
6233 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_given_disk] = 0.0;
6234 mcmc->move_weight[mcmc->num_move_phyrex_disk_given_ldsk] = 0.0;
6235 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_and_disk] = 0.0;
6236 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_multi] = 0.0;
6237 mcmc->move_weight[mcmc->num_move_phyrex_disk_multi] = 0.0;
6238 mcmc->move_weight[mcmc->num_move_phyrex_add_remove_jump] = 0.0;
6239 mcmc->move_weight[mcmc->num_move_phyrex_ldsk_tip_to_root] = 0.0;
6240 mcmc->move_weight[mcmc->num_move_phyrex_sigsq_scale] = 0.0;
6241 #endif
6242
6243 sum = 0.0;
6244 for(i=0;i<mcmc->n_moves;i++) sum += mcmc->move_weight[i];
6245 for(i=0;i<mcmc->n_moves;i++) mcmc->move_weight[i] /= sum;
6246 for(i=1;i<mcmc->n_moves;i++) mcmc->move_weight[i] += mcmc->move_weight[i-1];
6247 }
6248
6249 //////////////////////////////////////////////////////////////
6250 //////////////////////////////////////////////////////////////
6251
MCMC_Initialize_Param_Val(t_mcmc * mcmc,t_tree * tree)6252 void MCMC_Initialize_Param_Val(t_mcmc *mcmc, t_tree *tree)
6253 {
6254 /* int i; */
6255
6256 /* mcmc->lag_param_val[mcmc->num_move_nu] = tree->rates->nu; */
6257 /* mcmc->lag_param_val[mcmc->num_move_clock_r] = tree->rates->clock_r; */
6258 /* mcmc->lag_param_val[mcmc->num_move_tree_height] = tree->times->nd_t[tree->n_root->num]; */
6259 /* mcmc->lag_param_val[mcmc->num_move_kappa] = tree->mod->kappa->v; */
6260
6261 /* For(i,2*tree->n_otu-2) */
6262 /* mcmc->lag_param_val[mcmc->num_move_br_r+i] = tree->rates->br_r[i]; */
6263
6264 /* for(i=0;i<tree->n_otu-1;i++) */
6265 /* mcmc->lag_param_val[mcmc->num_move_nd_t+i] = tree->times->nd_t[tree->n_otu+i]; */
6266
6267 /* For(i,2*tree->n_otu-1) */
6268 /* mcmc->lag_param_val[mcmc->num_move_nd_r+i] = tree->rates->nd_r[i]; */
6269
6270 /* for(i=0;i<mcmc->n_moves;i++) tree->mcmc->new_param_val[i] = tree->mcmc->lag_param_val[i]; */
6271 }
6272
6273 //////////////////////////////////////////////////////////////
6274 //////////////////////////////////////////////////////////////
6275
MCMC_Copy_To_New_Param_Val(t_mcmc * mcmc,t_tree * tree)6276 void MCMC_Copy_To_New_Param_Val(t_mcmc *mcmc, t_tree *tree)
6277 {
6278 mcmc->sampled_val[mcmc->num_move_nu*mcmc->sample_size+mcmc->sample_num] = tree->rates->nu;
6279 mcmc->sampled_val[mcmc->num_move_clock_r*mcmc->sample_size+mcmc->sample_num] = tree->rates->clock_r;
6280 mcmc->sampled_val[mcmc->num_move_tree_height*mcmc->sample_size+mcmc->sample_num] = tree->times->nd_t[tree->n_root->num];
6281 mcmc->sampled_val[mcmc->num_move_kappa*mcmc->sample_size+mcmc->sample_num] = tree->mod ? tree->mod->kappa->v : -1.;
6282 mcmc->sampled_val[mcmc->num_move_birth_rate*mcmc->sample_size+mcmc->sample_num] = tree->times->birth_rate;
6283 mcmc->sampled_val[mcmc->num_move_death_rate*mcmc->sample_size+mcmc->sample_num] = tree->times->death_rate;
6284
6285 /* For(i,2*tree->n_otu-2) */
6286 /* mcmc->sampled_val[(mcmc->num_move_br_r+i)*mcmc->sample_size+mcmc->sample_num] = tree->rates->br_r[i]; */
6287
6288
6289 /* For(i,2*tree->n_otu-1) */
6290 /* mcmc->sampled_val[(mcmc->num_move_nd_r+i)*mcmc->sample_size+mcmc->sample_num] = tree->rates->nd_r[i]; */
6291
6292 mcmc->sampled_val[mcmc->num_move_geo_tau*mcmc->sample_size+mcmc->sample_num] = tree->geo ? tree->geo->tau : -1.;
6293 mcmc->sampled_val[mcmc->num_move_geo_lambda*mcmc->sample_size+mcmc->sample_num] = tree->geo ? tree->geo->lbda : -1.;
6294 mcmc->sampled_val[mcmc->num_move_geo_sigma*mcmc->sample_size+mcmc->sample_num] = tree->geo ? tree->geo->sigma : -1.;
6295 mcmc->sampled_val[mcmc->num_move_geo_dum*mcmc->sample_size+mcmc->sample_num] = tree->geo ? tree->geo->dum : -1.;
6296 #ifdef PHYREX
6297 mcmc->sampled_val[mcmc->num_move_phyrex_lbda*mcmc->sample_size+mcmc->sample_num] = tree->mmod ? tree->mmod->lbda : -1.;
6298 mcmc->sampled_val[mcmc->num_move_phyrex_mu*mcmc->sample_size+mcmc->sample_num] = tree->mmod ? SLFV_Neighborhood_Size(tree) : -1.;
6299 mcmc->sampled_val[mcmc->num_move_phyrex_sigsq*mcmc->sample_size+mcmc->sample_num] = tree->mmod ? SLFV_Update_Sigsq(tree) : -1.;
6300 mcmc->sampled_val[mcmc->num_move_phyrex_rad*mcmc->sample_size+mcmc->sample_num] = tree->mmod ? tree->mmod->rad : -1.;
6301 #endif
6302 }
6303
6304 //////////////////////////////////////////////////////////////
6305 //////////////////////////////////////////////////////////////
6306
MCMC_Slice_One_Rate(t_node * a,t_node * d,int traversal,t_tree * tree)6307 void MCMC_Slice_One_Rate(t_node *a, t_node *d, int traversal, t_tree *tree)
6308 {
6309 phydbl L,R; /* Left and Right limits of the slice */
6310 phydbl w; /* window width */
6311 phydbl u;
6312 phydbl x0,x1;
6313 phydbl logy;
6314 t_edge *b;
6315 int i;
6316
6317
6318
6319 b = NULL;
6320 if(a == tree->n_root) b = tree->e_root;
6321 else for(i=0;i<3;i++) if(d->v[i] == a) { b = d->b[i]; break; }
6322
6323 w = 0.05;
6324 /* w = 10.; */
6325
6326 x0 = tree->rates->br_r[d->num];
6327 logy = tree->c_lnL+tree->rates->c_lnL_rates - Rexp(1.);
6328
6329 u = Uni();
6330
6331 L = x0 - w*u;
6332 R = L + w;
6333
6334 do
6335 {
6336 tree->rates->br_r[d->num] = L;
6337 tree->rates->br_do_updt[d->num] = YES;
6338 RATES_Update_Cur_Bl(tree);
6339 Lk(b,tree);
6340 RATES_Lk_Rates(tree);
6341 if(L < tree->rates->min_rate) { L = tree->rates->min_rate - w; break;}
6342 L = L - w;
6343 }
6344 while(tree->c_lnL + tree->rates->c_lnL_rates > logy);
6345 L = L + w;
6346
6347
6348 do
6349 {
6350 tree->rates->br_r[d->num] = R;
6351 tree->rates->br_do_updt[d->num] = YES;
6352 RATES_Update_Cur_Bl(tree);
6353 Lk(b,tree);
6354 RATES_Lk_Rates(tree);
6355 if(R > tree->rates->max_rate) { R = tree->rates->max_rate + w; break;}
6356 R = R + w;
6357 }
6358 while(tree->c_lnL + tree->rates->c_lnL_rates > logy);
6359 R = R - w;
6360
6361
6362 for(;;)
6363 {
6364 u = Uni();
6365 x1 = L + u*(R-L);
6366
6367 tree->rates->br_r[d->num] = x1;
6368 tree->rates->br_do_updt[d->num] = YES;
6369 RATES_Update_Cur_Bl(tree);
6370 Lk(b,tree);
6371 RATES_Lk_Rates(tree);
6372
6373 if(tree->c_lnL + tree->rates->c_lnL_rates > logy) break;
6374
6375 if(x1 < x0) L = x1;
6376 else R = x1;
6377 }
6378
6379
6380 if(traversal == YES)
6381 {
6382 if(d->tax == YES) return;
6383 else
6384 {
6385 for(i=0;i<3;i++)
6386 if(d->v[i] != a && d->b[i] != tree->e_root)
6387 {
6388 if(tree->io->lk_approx == EXACT) Update_Partial_Lk(tree,d->b[i],d);
6389 MCMC_Slice_One_Rate(d,d->v[i],YES,tree);
6390 }
6391 }
6392 if(tree->io->lk_approx == EXACT) Update_Partial_Lk(tree,b,d);
6393 }
6394 }
6395
6396 //////////////////////////////////////////////////////////////
6397 //////////////////////////////////////////////////////////////
6398
MCMC_Make_Move(phydbl * cur,phydbl * new,phydbl inf,phydbl sup,phydbl * loghr,phydbl tune,int move_type)6399 void MCMC_Make_Move(phydbl *cur, phydbl *new, phydbl inf, phydbl sup, phydbl *loghr, phydbl tune, int move_type)
6400 {
6401 phydbl u;
6402
6403
6404
6405 switch(move_type)
6406 {
6407
6408 case MCMC_MOVE_RANDWALK_NORMAL:
6409 {
6410 *new = *cur + Rnorm(0.0,tune);
6411 /* Do not use reflection here */
6412 *loghr = 0.0;
6413
6414 break;
6415 }
6416
6417 case MCMC_MOVE_RANDWALK_UNIFORM:
6418 {
6419 u = Uni();
6420 /* *new = u * (2.*tune) + (*cur) - tune; */
6421 /* *new = Reflect(*new,inf,sup); */
6422 *new = u*(sup-inf)+inf;
6423 *loghr = 0.0;
6424 break;
6425 }
6426
6427 case MCMC_MOVE_SCALE_THORNE:
6428 {
6429 u = Uni();
6430 *new = (*cur) * exp(tune*(u-.5));
6431 *loghr = log((*new)/(*cur));
6432 break;
6433 }
6434
6435 case MCMC_MOVE_SCALE_GAMMA:
6436 {
6437 phydbl r;
6438 *new = (*cur) * Rgamma(1./tune,tune);
6439 r = (*new)/(*cur);
6440 *loghr = -log(r) + log(Dgamma(1./r,1./tune,tune)/Dgamma(r,1./tune,tune));
6441 break;
6442 }
6443
6444 default :
6445 {
6446 PhyML_Printf("\n. Move not implemented");
6447 Exit("");
6448 break;
6449 }
6450 }
6451 }
6452
6453 //////////////////////////////////////////////////////////////
6454 //////////////////////////////////////////////////////////////
6455
MCMC_Read_Param_Vals(t_tree * tree)6456 void MCMC_Read_Param_Vals(t_tree *tree)
6457 {
6458 char *token;
6459 int sizemax;
6460 FILE *in_fp;
6461 phydbl val;
6462 int i,v;
6463
6464 in_fp = tree->mcmc->in_fp_par;
6465
6466 sizemax = T_MAX_LINE;
6467
6468 token = (char *)mCalloc(sizemax,sizeof(char));
6469
6470 if(fgets(token,sizemax,in_fp) == NULL) // Skip first line
6471 {
6472 PhyML_Fprintf(stderr,"\n. Wrong file format.");
6473 assert(FALSE);
6474 }
6475
6476 if(fgets(token,sizemax,in_fp) == NULL) // Skip second
6477 {
6478 PhyML_Fprintf(stderr,"\n. Wrong file format.");
6479 assert(FALSE);
6480 }
6481
6482 v=fscanf(in_fp,"%lf\t",&val); // Run
6483 /* PhyML_Printf("\n. Run = %d",(int)val); */
6484 v=fscanf(in_fp,"%lf\t",&val); // LnLike[Exact]
6485 /* PhyML_Printf("\n. LnLike = %f",val); */
6486 v=fscanf(in_fp,"%lf\t",&val); // LnLike[Approx]
6487 /* PhyML_Printf("\n. LnLike = %f",val); */
6488 v=fscanf(in_fp,"%lf\t",&val); // LnPriorRate
6489 /* PhyML_Printf("\n. LnPrior = %f",val); */
6490 v=fscanf(in_fp,"%lf\t",&val); // LnPriorTime
6491 /* PhyML_Printf("\n. LnPrior = %f",val); */
6492 v=fscanf(in_fp,"%lf\t",&val); // LnPosterior
6493 /* PhyML_Printf("\n. LnPost = %f",val); */
6494
6495 v=fscanf(in_fp,"%lf\t",&val); // ClockRate
6496 tree->rates->clock_r = val;
6497 /* PhyML_Printf("\n. Clock = %f",val); */
6498
6499 v=fscanf(in_fp,"%lf\t",&val); // EvolRate
6500
6501 v=fscanf(in_fp,"%lf\t",&val); // Nu
6502 tree->rates->nu = val;
6503 /* PhyML_Printf("\n. Nu = %f",val); */
6504
6505 v=fscanf(in_fp,"%lf\t",&val); // Birth rate
6506 tree->times->birth_rate = val;
6507
6508 v=fscanf(in_fp,"%lf\t",&val); // TsTv
6509 tree->mod->kappa->v = val;
6510 /* PhyML_Printf("\n. TsTv = %f",val); */
6511
6512 for(i=0;i<tree->n_otu-1;i++)
6513 {
6514 v=fscanf(in_fp,"%lf\t",&val); // Node heights
6515 tree->times->nd_t[i+tree->n_otu] = val;
6516 }
6517
6518 for(i=0;i<2*tree->n_otu-2;++i)
6519 {
6520 v=fscanf(in_fp,"%lf\t",&val); // Edge average rates
6521 tree->rates->br_r[i] = log(val);
6522 }
6523
6524 v++;
6525
6526 Free(token);
6527
6528 }
6529
6530 //////////////////////////////////////////////////////////////
6531 //////////////////////////////////////////////////////////////
6532
6533 #ifdef PHYREX
MCMC_PHYREX_Lbda(t_tree * tree)6534 void MCMC_PHYREX_Lbda(t_tree *tree)
6535 {
6536 if(tree->mmod->id == SLFV_GAUSSIAN || tree->mmod->id == SLFV_UNIFORM)
6537 {
6538 MCMC_Single_Param_Generic(&(tree->mmod->lbda),
6539 tree->mmod->min_lbda,
6540 tree->mmod->max_lbda,
6541 tree->mcmc->num_move_phyrex_lbda,
6542 NULL,&(tree->mmod->c_lnL),
6543 NULL,(tree->eval_glnL == YES) ? PHYREX_Wrap_Lk : NULL,
6544 tree->mcmc->move_type[tree->mcmc->num_move_phyrex_lbda],
6545 NO,NULL,tree,NULL);
6546 }
6547 }
6548 #endif
6549
6550 //////////////////////////////////////////////////////////////
6551 //////////////////////////////////////////////////////////////
6552
6553 #ifdef PHYREX
MCMC_PHYREX_Mu(t_tree * tree)6554 void MCMC_PHYREX_Mu(t_tree *tree)
6555 {
6556 if(tree->mmod->id == SLFV_GAUSSIAN || tree->mmod->id == SLFV_UNIFORM)
6557 {
6558 MCMC_Single_Param_Generic(&(tree->mmod->mu),
6559 tree->mmod->min_mu,
6560 tree->mmod->max_mu,
6561 tree->mcmc->num_move_phyrex_mu,
6562 NULL,&(tree->mmod->c_lnL),
6563 NULL,(tree->eval_glnL == YES) ? PHYREX_Wrap_Lk : NULL,
6564 tree->mcmc->move_type[tree->mcmc->num_move_phyrex_mu],
6565 NO,NULL,tree,NULL);
6566 }
6567 }
6568 #endif
6569
6570 //////////////////////////////////////////////////////////////
6571 //////////////////////////////////////////////////////////////
6572
6573 #ifdef PHYREX
MCMC_PHYREX_Radius(t_tree * tree)6574 void MCMC_PHYREX_Radius(t_tree *tree)
6575 {
6576 if(tree->mmod->id == SLFV_GAUSSIAN || tree->mmod->id == SLFV_UNIFORM)
6577 {
6578 MCMC_Single_Param_Generic(&(tree->mmod->rad),
6579 tree->mmod->min_rad,
6580 tree->mmod->max_rad,
6581 tree->mcmc->num_move_phyrex_rad,
6582 NULL,&(tree->mmod->c_lnL),
6583 NULL,(tree->eval_glnL == YES) ? PHYREX_Wrap_Lk : NULL,
6584 tree->mcmc->move_type[tree->mcmc->num_move_phyrex_rad],
6585 NO,NULL,tree,NULL);
6586 }
6587 }
6588 #endif
6589
6590 /*////////////////////////////////////////////////////////////
6591 ////////////////////////////////////////////////////////////*/
6592
6593 #ifdef PHYREX
MCMC_PHYREX_Sigsq(t_tree * tree)6594 void MCMC_PHYREX_Sigsq(t_tree *tree)
6595 {
6596 if(tree->mmod->id == RW || tree->mmod->id == RRW)
6597 {
6598 MCMC_Single_Param_Generic(&(tree->mmod->sigsq),
6599 tree->mmod->min_sigsq,
6600 tree->mmod->max_sigsq,
6601 tree->mcmc->num_move_phyrex_sigsq,
6602 NULL,&(tree->mmod->c_lnL),
6603 NULL,(tree->eval_glnL == YES) ? PHYREX_Wrap_Lk : NULL,
6604 tree->mcmc->move_type[tree->mcmc->num_move_phyrex_sigsq],
6605 NO,NULL,tree,NULL);
6606 }
6607 }
6608 #endif
6609
6610 /*////////////////////////////////////////////////////////////
6611 ////////////////////////////////////////////////////////////*/
6612
6613 #ifdef PHYREX
MCMC_PHYREX_Neff(t_tree * tree)6614 void MCMC_PHYREX_Neff(t_tree *tree)
6615 {
6616 if(tree->mmod->id == RW || tree->mmod->id == RRW)
6617 {
6618 MCMC_Single_Param_Generic(&(tree->times->scaled_pop_size),
6619 0.0,
6620 1.E+6,
6621 tree->mcmc->num_move_phyrex_neff,
6622 NULL,&(tree->times->c_lnL_times),
6623 NULL,(tree->eval_glnL == YES) ? TIMES_Wrap_Lk_Coalescent : NULL,
6624 tree->mcmc->move_type[tree->mcmc->num_move_phyrex_neff],
6625 NO,NULL,tree,NULL);
6626 }
6627 }
6628 #endif
6629
6630 /*////////////////////////////////////////////////////////////
6631 ////////////////////////////////////////////////////////////*/
6632 #ifdef PHYREX
MCMC_PHYREX_Indel_Disk(t_tree * tree)6633 void MCMC_PHYREX_Indel_Disk(t_tree *tree)
6634 {
6635 int n_disks_cur, n_disks_new;
6636 phydbl hr;
6637 phydbl cur_lbda,new_lbda;
6638 phydbl cur_mu,new_mu;
6639 phydbl cur_rad,new_rad;
6640 t_dsk *disk;
6641
6642 hr = 0.0;
6643 new_lbda = tree->mmod->lbda;
6644 cur_lbda = tree->mmod->lbda;
6645 new_mu = tree->mmod->mu;
6646 cur_mu = tree->mmod->mu;
6647 new_rad = tree->mmod->rad;
6648 cur_rad = tree->mmod->rad;
6649
6650 /* new_lbda = cur_lbda * exp(1.0*(Uni()-.5)); */
6651 /* hr += log(new_lbda/cur_lbda); */
6652
6653 /* new_mu = cur_mu * exp(0.5*(Uni()-.5)); */
6654 /* hr += log(new_mu/cur_mu); */
6655
6656 /* new_rad = cur_rad * exp(0.5*(Uni()-.5)); */
6657 /* hr += log(new_rad/cur_rad); */
6658
6659 if(new_rad > tree->mmod->max_rad || new_rad < tree->mmod->min_rad) return;
6660 if(new_mu > tree->mmod->max_mu || new_mu < tree->mmod->min_mu) return;
6661 if(new_lbda > tree->mmod->max_lbda || new_lbda < tree->mmod->min_lbda) return;
6662
6663 tree->mmod->lbda = new_lbda;
6664 tree->mmod->mu = new_mu;
6665 tree->mmod->rad = new_rad;
6666
6667 disk = tree->young_disk->prev;
6668 n_disks_cur = 0;
6669 do
6670 {
6671 if(disk->ldsk == NULL && disk->age_fixed == NO) n_disks_cur++;
6672 disk = disk->prev;
6673 }
6674 while(disk);
6675
6676 n_disks_new = (int)Rpois(n_disks_cur+SMALL);
6677 hr += Dpois(n_disks_cur,n_disks_new+SMALL,YES);
6678 hr -= Dpois(n_disks_new,n_disks_cur+SMALL,YES);
6679
6680 if(n_disks_new < n_disks_cur)
6681 {
6682 MCMC_PHYREX_Delete_Disk(hr, n_disks_cur - n_disks_new , cur_lbda, cur_mu, cur_rad, tree);
6683 }
6684 else
6685 {
6686 MCMC_PHYREX_Insert_Disk(hr, n_disks_new - n_disks_cur, cur_lbda, cur_mu, cur_rad, tree);
6687 }
6688
6689 }
6690 #endif
6691
6692 /*////////////////////////////////////////////////////////////
6693 ////////////////////////////////////////////////////////////*/
6694 /* Insert or delete a disk that does not affect any ldisk */
6695 #ifdef PHYREX
MCMC_PHYREX_Delete_Disk(phydbl hr,int n_delete_disks,phydbl cur_lbda,phydbl cur_mu,phydbl cur_rad,t_tree * tree)6696 void MCMC_PHYREX_Delete_Disk(phydbl hr, int n_delete_disks, phydbl cur_lbda, phydbl cur_mu, phydbl cur_rad, t_tree *tree)
6697 {
6698 phydbl u,alpha,ratio;
6699 phydbl cur_glnL, new_glnL;
6700 phydbl T;
6701 t_dsk *disk,**target_disk,**valid_disks;
6702 int i,j,block,n_valid_disks,*permut;
6703
6704 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_disk]++;
6705
6706 if(n_delete_disks == 0)
6707 {
6708 tree->mmod->lbda = cur_lbda;
6709 tree->mmod->rad = cur_rad;
6710 tree->mmod->mu = cur_mu;
6711 return;
6712 }
6713
6714 valid_disks = NULL;
6715 disk = NULL;
6716 new_glnL = tree->mmod->c_lnL;
6717 cur_glnL = tree->mmod->c_lnL;
6718 ratio = 0.0;
6719 block = 100;
6720
6721 disk = tree->young_disk->prev;
6722
6723 if(!disk->prev)
6724 {
6725 tree->mmod->lbda = cur_lbda;
6726 tree->mmod->rad = cur_rad;
6727 tree->mmod->mu = cur_mu;
6728 return;
6729 }
6730
6731 n_valid_disks = 0;
6732 do
6733 {
6734 if(!disk->ldsk && disk->age_fixed == NO)
6735 {
6736 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
6737 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
6738 valid_disks[n_valid_disks] = disk;
6739 n_valid_disks++;
6740 }
6741 disk = disk->prev;
6742 }
6743 while(disk && disk->prev);
6744
6745
6746 if(!n_valid_disks || (n_valid_disks - n_delete_disks < 0))
6747 {
6748 tree->mmod->lbda = cur_lbda;
6749 tree->mmod->rad = cur_rad;
6750 tree->mmod->mu = cur_mu;
6751 return;
6752 }
6753
6754 target_disk = (t_dsk **)mCalloc(n_delete_disks,sizeof(t_dsk *));
6755
6756 permut = Permutate(n_valid_disks);
6757
6758 for(j=0;j<n_delete_disks;j++)
6759 {
6760 /* Uniform selection of a disk where no coalescent nor 'hit' occurred */
6761 target_disk[j] = valid_disks[permut[j]];
6762 PHYREX_Remove_Disk(target_disk[j]);
6763 for(i=0;i<tree->mmod->n_dim;i++) hr += log(1./(tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i]));
6764 }
6765
6766 T = PHYREX_Tree_Height(tree);
6767 T = fabs(T-tree->young_disk->time);
6768
6769
6770 // Pr(n -> n-k) i.e, denominator
6771 hr += LnChoose(n_valid_disks,n_delete_disks);
6772
6773 // Pr(n-k -> n) i.e., numerator
6774 hr += n_delete_disks * log(1./T);
6775 hr += LnFact(n_delete_disks);
6776
6777 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
6778
6779 ratio += (new_glnL - cur_glnL);
6780 ratio += hr;
6781
6782 ratio = exp(ratio);
6783 alpha = MIN(1.,ratio);
6784
6785 /* Always accept move */
6786 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
6787
6788 u = Uni();
6789
6790 /* PhyML_Printf("\n- Delete new_glnL: %f [%f] hr: %f u:%f alpha: %f n_delete: %d [%d %d %f]", */
6791 /* new_glnL,cur_glnL,hr,u,alpha,n_delete_disks, */
6792 /* tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk], */
6793 /* tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_disk], */
6794 /* tree->mcmc->acc_rate[tree->mcmc->num_move_phyrex_indel_disk]); */
6795
6796
6797 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
6798
6799 if(u > alpha) /* Reject */
6800 {
6801 tree->mmod->lbda = cur_lbda;
6802 tree->mmod->rad = cur_rad;
6803 tree->mmod->mu = cur_mu;
6804
6805 for(j=0;j<n_delete_disks;j++) PHYREX_Insert_Disk(target_disk[j],tree);
6806 PHYREX_Update_Lindisk_List(tree);
6807 tree->mmod->c_lnL = cur_glnL;
6808 }
6809 else
6810 {
6811 for(j=0;j<n_delete_disks;j++) Free_Disk(target_disk[j]);
6812 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk]++;
6813 }
6814
6815 Free(valid_disks);
6816 Free(target_disk);
6817 Free(permut);
6818 }
6819 #endif
6820
6821 /*////////////////////////////////////////////////////////////
6822 ////////////////////////////////////////////////////////////*/
6823
6824 #ifdef PHYREX
MCMC_PHYREX_Insert_Disk(phydbl hr,int n_insert_disks,phydbl cur_lbda,phydbl cur_mu,phydbl cur_rad,t_tree * tree)6825 void MCMC_PHYREX_Insert_Disk(phydbl hr, int n_insert_disks, phydbl cur_lbda, phydbl cur_mu, phydbl cur_rad, t_tree *tree)
6826 {
6827 t_dsk *disk,**new_disk,**target_disk;
6828 phydbl T,t;
6829 phydbl cur_glnL, new_glnL;
6830 phydbl u,alpha,ratio;
6831 int i,j,n_valid_disks,num_prec_issue;
6832
6833
6834 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_disk]++;
6835
6836 if(n_insert_disks == 0)
6837 {
6838 tree->mmod->lbda = cur_lbda;
6839 tree->mmod->rad = cur_rad;
6840 tree->mmod->mu = cur_mu;
6841 return;
6842 }
6843
6844 disk = NULL;
6845 new_glnL = tree->mmod->c_lnL;
6846 cur_glnL = tree->mmod->c_lnL;
6847 ratio = 0.0;
6848 num_prec_issue = NO;
6849
6850 if(tree->young_disk->next) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
6851
6852 T = PHYREX_Tree_Height(tree);
6853
6854 disk = tree->young_disk->prev;
6855 n_valid_disks = 0;
6856 do
6857 {
6858 if(!disk->ldsk) n_valid_disks++;
6859 disk = disk->prev;
6860 }
6861 while(disk && disk->prev);
6862
6863 target_disk = (t_dsk **)mCalloc(n_insert_disks,sizeof(t_dsk *));
6864 new_disk = (t_dsk **)mCalloc(n_insert_disks,sizeof(t_dsk *));
6865
6866
6867 for(j=0;j<n_insert_disks;j++)
6868 {
6869 do
6870 {
6871 num_prec_issue = NO;
6872 t = Uni()*(tree->young_disk->time-T) + T;
6873 disk = tree->young_disk->prev;
6874 while(disk && disk->time > t) disk = disk->prev;
6875
6876 if(!(disk->time != t))
6877 {
6878 PhyML_Printf("\n. Numerical precision issue in insert_disk");
6879 PhyML_Printf("\n. t=%g disk->time=%g diff=%g",t,disk->time,t-disk->time);
6880 num_prec_issue = YES;
6881 }
6882 }while(num_prec_issue == YES);
6883
6884
6885
6886
6887 assert(disk->next);
6888 target_disk[j] = disk->next;
6889
6890 new_disk[j] = PHYREX_Make_Disk_Event(tree->mmod->n_dim,tree->n_otu);
6891 PHYREX_Init_Disk_Event(new_disk[j],tree->mmod->n_dim,tree->mmod);
6892 new_disk[j]->time = t;
6893
6894 PHYREX_Insert_Disk(new_disk[j],tree);
6895
6896 for(i=0;i<tree->mmod->n_dim;i++) new_disk[j]->centr->lonlat[i] = Uni()*(tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i])+tree->mmod->lim_do->lonlat[i];
6897
6898 for(i=0;i<tree->mmod->n_dim;i++) hr -= log(1./(tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i]));
6899 }
6900
6901 /* // Pr(n+k -> n) i.e, numerator */
6902 hr -= LnChoose(n_valid_disks+n_insert_disks,n_insert_disks);
6903
6904 /* // Pr(n -> n+k) i.e., denominator */
6905 T = fabs(T-tree->young_disk->time);
6906 hr -= n_insert_disks * log(1./T);
6907 hr -= LnFact(n_insert_disks);
6908
6909
6910 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
6911
6912 ratio += (new_glnL - cur_glnL);
6913 ratio += hr;
6914
6915 ratio = exp(ratio);
6916 alpha = MIN(1.,ratio);
6917
6918 /* Always accept move */
6919 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
6920
6921 u = Uni();
6922
6923 /* PhyML_Printf("\n+ Insert new_glnL: %f [%f] hr: %f u: %f alpha: %f n_insert: %d [%d %d %d %d %f]", */
6924 /* new_glnL,cur_glnL,hr,u,alpha,n_insert_disks, */
6925 /* tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk], */
6926 /* tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_disk], */
6927 /* tree->mcmc->prev_acc_move[tree->mcmc->num_move_phyrex_indel_disk], */
6928 /* tree->mcmc->prev_run_move[tree->mcmc->num_move_phyrex_indel_disk], */
6929 /* tree->mcmc->acc_rate[tree->mcmc->num_move_phyrex_indel_disk]); */
6930
6931 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
6932
6933 if(u > alpha) /* Reject */
6934 {
6935 tree->mmod->lbda = cur_lbda;
6936 tree->mmod->rad = cur_rad;
6937 tree->mmod->mu = cur_mu;
6938
6939 for(j=0;j<n_insert_disks;j++)
6940 {
6941 PHYREX_Remove_Disk(new_disk[j]);
6942 Free_Disk(new_disk[j]);
6943 }
6944
6945 PHYREX_Update_Lindisk_List(tree);
6946 tree->mmod->c_lnL = cur_glnL;
6947 }
6948 else
6949 {
6950 /* printf("\nI Accept %f",tree->mmod->lbda); */
6951 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk]++;
6952 }
6953
6954 Free(target_disk);
6955 Free(new_disk);
6956 }
6957 #endif
6958
6959 /*////////////////////////////////////////////////////////////
6960 ////////////////////////////////////////////////////////////*/
6961
6962 /* Update time of disk */
6963 #ifdef PHYREX
6964
MCMC_PHYREX_Move_Disk_Updown(t_tree * tree)6965 void MCMC_PHYREX_Move_Disk_Updown(t_tree *tree)
6966 {
6967 phydbl u,alpha,ratio;
6968 phydbl cur_glnL, new_glnL, hr;
6969 phydbl cur_alnL, new_alnL;
6970 phydbl cur_rlnL, new_rlnL;
6971 phydbl *ori_time,new_time,cur_time;
6972 phydbl max, min;
6973 t_dsk *disk,**target_disk,**all_disks;
6974 int i,block,n_all_disks,n_move_disks,*permut,update_alnL;
6975
6976 disk = NULL;
6977 block = 100;
6978 all_disks = NULL;
6979
6980 disk = tree->young_disk->prev;
6981 n_all_disks = 0;
6982 do
6983 {
6984 /* if((disk->age_fixed) == NO && !(disk->ldsk && disk->ldsk->n_next > 1 && tree->mod->s_opt->opt_bl == NO)) */
6985 if((disk->age_fixed) == NO && disk->ldsk && disk->ldsk->n_next > 1)
6986 {
6987 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
6988 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
6989 all_disks[n_all_disks] = disk;
6990 n_all_disks++;
6991 }
6992 disk = disk->prev;
6993 }
6994 while(disk);
6995
6996 if(!n_all_disks) return;
6997
6998 n_move_disks = (int)(1+0.2*n_all_disks);
6999
7000 target_disk = (t_dsk **)mCalloc(n_all_disks,sizeof(t_dsk *));
7001 ori_time = (phydbl *)mCalloc(n_all_disks,sizeof(phydbl));
7002
7003 permut = Permutate(n_all_disks);
7004
7005 for(i=0;i<n_move_disks;i++)
7006 {
7007 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_move_disk_ud]++;
7008
7009 /* cur_glnL = PHYREX_Lk(tree); */
7010 cur_glnL = tree->mmod->c_lnL;
7011 new_glnL = tree->mmod->c_lnL;
7012 new_alnL = tree->c_lnL;
7013 cur_alnL = tree->c_lnL;
7014 new_rlnL = tree->rates->c_lnL_rates;
7015 cur_rlnL = tree->rates->c_lnL_rates;
7016 hr = 0.0;
7017 ratio = 0.0;
7018
7019 target_disk[i] = all_disks[permut[i]];
7020
7021 update_alnL = NO;
7022 if(target_disk[i]->ldsk && target_disk[i]->ldsk->n_next > 1) update_alnL = YES;
7023
7024 ori_time[i] = target_disk[i]->time;
7025 cur_time = target_disk[i]->time;
7026
7027
7028 if(target_disk[i]->prev)
7029 {
7030 max = target_disk[i]->next->time;
7031 min = target_disk[i]->prev->time;
7032 new_time = Uni()*(max - min) + min;
7033 /* PhyML_Printf("\n. disk: %s max: %f min: %f curr_time: %f new_time: %f",target_disk[i]->id,max,min,target_disk[i]->time,new_time); */
7034 }
7035 else
7036 {
7037 /* phydbl new_plusmin, cur_plusmin; */
7038 phydbl K;
7039
7040 K = 0.1;
7041
7042 max = target_disk[i]->next->time;
7043
7044 /* cur_plusmin = fabs(ori_time[i] - max); */
7045 /* new_plusmin = Rexp(1./cur_plusmin); */
7046
7047 /* new_time = max - new_plusmin; */
7048 new_time = max - Rexp(K*tree->mmod->lbda);
7049
7050 /* hr += log(Dexp(cur_plusmin,1./new_plusmin)); */
7051 /* hr -= log(Dexp(new_plusmin,1./cur_plusmin)); */
7052
7053 hr -= -(max-new_time)*K*tree->mmod->lbda;
7054 hr += -(max-cur_time)*K*tree->mmod->lbda;
7055 /* PhyML_Printf("\n* disk: %s max: %f cur_time: %f new_time: %f hr: %f",target_disk[i]->id,max,target_disk[i]->time,new_time,hr); */
7056
7057
7058 /* new_glnL += -tree->mmod->lbda * fabs(new_time); */
7059 /* new_glnL -= -tree->mmod->lbda * fabs(cur_time); */
7060
7061 /* tree->mmod->c_lnL = new_glnL; */
7062
7063 }
7064
7065
7066 target_disk[i]->time = new_time;
7067
7068 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
7069 if(tree->eval_alnL == YES && update_alnL == YES) new_alnL = Lk(NULL,tree);
7070 if(tree->eval_rlnL == YES && update_alnL == YES) new_rlnL = RATES_Lk_Rates(tree);
7071
7072 ratio += (new_alnL - cur_alnL);
7073 ratio += (new_glnL - cur_glnL);
7074 ratio += (new_rlnL - cur_rlnL);
7075 ratio += hr;
7076
7077 ratio = exp(ratio);
7078 alpha = MIN(1.,ratio);
7079
7080 /* Always accept move */
7081 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
7082
7083 u = Uni();
7084
7085 /* if(target_disk[i]->prev == NULL) PhyML_Printf("\n- Move disk new_glnL: %12f [%12f] hr: %12f u:%6f alpha: %12f new_time: %12f cur_time: %12f max: %12f",new_glnL,cur_glnL,hr,u,alpha,new_time,cur_time,max); */
7086
7087 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
7088
7089 if(u > alpha) /* Reject */
7090 {
7091 /* printf("- Reject"); */
7092
7093 target_disk[i]->time = ori_time[i];
7094
7095 tree->mmod->c_lnL = cur_glnL;
7096 tree->c_lnL = cur_alnL;
7097 tree->rates->c_lnL_rates = cur_rlnL;
7098 }
7099 else
7100 {
7101 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_move_disk_ud]++;
7102 /* printf("- Accept"); */
7103 }
7104 }
7105
7106 Free(target_disk);
7107 Free(all_disks);
7108 Free(ori_time);
7109 Free(permut);
7110 }
7111 #endif
7112
7113 /*////////////////////////////////////////////////////////////
7114 ////////////////////////////////////////////////////////////*/
7115
7116 #ifdef PHYREX
MCMC_PHYREX_Scale_Times(t_tree * tree)7117 void MCMC_PHYREX_Scale_Times(t_tree *tree)
7118 {
7119 phydbl u,alpha,ratio;
7120 phydbl cur_glnL, new_glnL, hr;
7121 phydbl cur_alnL, new_alnL;
7122 phydbl cur_rlnL, new_rlnL;
7123 phydbl scale_fact_times;
7124 int n_disks;
7125 t_dsk *start_disk;
7126 phydbl K;
7127
7128 if(tree->mod->s_opt->opt_bl == NO) return;
7129
7130 cur_alnL = tree->c_lnL;
7131 new_alnL = tree->c_lnL;
7132 new_glnL = tree->mmod->c_lnL;
7133 cur_glnL = tree->mmod->c_lnL;
7134 new_rlnL = tree->rates->c_lnL_rates;
7135 cur_rlnL = tree->rates->c_lnL_rates;
7136 hr = 0.0;
7137 ratio = 0.0;
7138 K = tree->mcmc->tune_move[tree->mcmc->num_move_phyrex_scale_times];
7139
7140 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_scale_times]++;
7141
7142 u = Uni();
7143 K = 2.;
7144 scale_fact_times = exp(K*(u-.5));
7145
7146 start_disk = tree->old_samp_disk;
7147 assert(start_disk);
7148
7149 n_disks = PHYREX_Scale_All(scale_fact_times,start_disk,tree);
7150 if(!PHYREX_Check_Struct(tree))
7151 {
7152 PHYREX_Scale_All(1./scale_fact_times,start_disk,tree);
7153 return;
7154 }
7155
7156 /* The Hastings ratio involves (n_disk-2) when considering a uniform distrib
7157 for the multiplier, which is not the case here.
7158 */
7159 hr += (n_disks)*log(scale_fact_times);
7160
7161 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
7162 if(tree->eval_alnL == YES) new_alnL = Lk(NULL,tree);
7163 if(tree->eval_rlnL == YES) new_rlnL = RATES_Lk_Rates(tree);
7164
7165 ratio += (new_alnL - cur_alnL);
7166 ratio += (new_glnL - cur_glnL);
7167 ratio += (new_rlnL - cur_rlnL);
7168
7169 ratio += hr;
7170
7171 ratio = exp(ratio);
7172 alpha = MIN(1.,ratio);
7173
7174 /* Always accept move */
7175 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
7176
7177 u = Uni();
7178
7179 /* PhyML_Printf("\n. Scale times hr: %f new_glnL: %f cur_glnL: %f new_alnL: %f cur_alnL: %f new_rlnL: %f cur_rlnL: %f ratio: %f scale: %f", */
7180 /* hr, */
7181 /* new_glnL,cur_glnL, */
7182 /* new_alnL,cur_alnL, */
7183 /* new_rlnL,cur_rlnL, */
7184 /* ratio,scale_fact_times); */
7185
7186 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
7187
7188 if(u > alpha) /* Reject */
7189 {
7190 PHYREX_Scale_All(1./scale_fact_times,start_disk,tree);
7191 PHYREX_Update_Lindisk_List(tree);
7192 tree->mmod->c_lnL = cur_glnL;
7193 tree->c_lnL = cur_alnL;
7194 tree->rates->c_lnL_rates = cur_rlnL;
7195 }
7196 else
7197 {
7198 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_scale_times]++;
7199 }
7200 }
7201 #endif
7202
7203 /*////////////////////////////////////////////////////////////
7204 ////////////////////////////////////////////////////////////*/
7205
7206 #ifdef PHYREX
MCMC_PHYREX_Swap_Disk(t_tree * tree)7207 void MCMC_PHYREX_Swap_Disk(t_tree *tree)
7208 {
7209 phydbl u,alpha,ratio;
7210 phydbl cur_glnL, new_glnL;
7211 phydbl cur_alnL, new_alnL;
7212 phydbl cur_rlnL, new_rlnL;
7213 phydbl hr,t,t_min,t_max,ori_time;
7214 t_dsk *disk,*target_disk,*ori_disk_old,*ori_disk_young,**valid_disks;
7215 t_ldsk *ldsk_next;
7216 int i,j,block,n_valid_disks;
7217
7218 valid_disks = NULL;
7219 block = 100;
7220
7221 disk = tree->young_disk->prev;
7222 if(!disk->prev) return;
7223
7224 n_valid_disks = 0;
7225 do
7226 {
7227 /* Record disk with a lineage displacement or coalescent that is not the root disk and not a disk with sampled taxa on it */
7228 if(disk && disk->prev && disk->ldsk && disk->ldsk->n_next >= 1 && disk->age_fixed == NO)
7229 {
7230 if(!(disk->ldsk->n_next > 1 && tree->mod->s_opt->opt_bl == NO))
7231 {
7232 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
7233 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
7234 valid_disks[n_valid_disks] = disk;
7235 n_valid_disks++;
7236 }
7237 }
7238 disk = disk->prev;
7239 }
7240 while(disk && disk->prev);
7241 if(n_valid_disks < 2) return;
7242
7243 for(j=0;j<MIN(10,(int)(1.+0.1*tree->n_otu));++j)
7244 {
7245 disk = NULL;
7246 target_disk = NULL;
7247 ori_disk_old = NULL;
7248 ori_disk_young = NULL;
7249 ldsk_next = NULL;
7250 new_glnL = tree->mmod->c_lnL;
7251 cur_glnL = tree->mmod->c_lnL;
7252 new_alnL = tree->c_lnL;
7253 cur_alnL = tree->c_lnL;
7254 new_rlnL = tree->rates->c_lnL_rates;
7255 cur_rlnL = tree->rates->c_lnL_rates;
7256 hr = 0.0;
7257 ratio = 0.0;
7258
7259
7260 /* Uniform selection of a valid disk */
7261 i = Rand_Int(0,n_valid_disks-1);
7262 target_disk = valid_disks[i];
7263
7264 ori_disk_old = target_disk->prev;
7265 ori_disk_young = target_disk->next;
7266 ori_time = target_disk->time;
7267
7268 t_min = target_disk->ldsk->prev->disk->time;
7269 t_max = +INFINITY;
7270 for(i=0;i<target_disk->ldsk->n_next;++i)
7271 if(target_disk->ldsk->next[i]->disk->time < t_max)
7272 {
7273 t_max = target_disk->ldsk->next[i]->disk->time;
7274 ldsk_next = target_disk->ldsk->next[i];
7275 }
7276
7277 t_max = t_max - SMALL;
7278 t_min = t_min + SMALL;
7279
7280 new_glnL = cur_glnL;
7281 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(ldsk_next->disk,target_disk->ldsk->prev->disk,tree);
7282
7283 if(!(t_max > t_min))
7284 {
7285 PhyML_Printf("\n. t_max: %g t_min: %g",t_max,t_min);
7286 assert(t_max > t_min);
7287 }
7288 t = Uni()*(t_max - t_min) + t_min;
7289 target_disk->time = t;
7290
7291 PHYREX_Remove_Disk(target_disk);
7292 PHYREX_Insert_Disk(target_disk,tree);
7293
7294 if(tree->eval_glnL == YES)
7295 {
7296 new_glnL += PHYREX_Lk_Range(ldsk_next->disk,target_disk->ldsk->prev->disk,tree);
7297 tree->mmod->c_lnL = new_glnL;
7298 /* new_glnL = PHYREX_Lk(tree); */
7299 }
7300
7301 if(tree->eval_alnL == YES) new_alnL = Lk(NULL,tree);
7302 if(tree->eval_rlnL == YES) new_rlnL = RATES_Lk_Rates(tree);
7303
7304 ratio += (new_alnL - cur_alnL);
7305 ratio += (new_glnL - cur_glnL);
7306 ratio += (new_rlnL - cur_rlnL);
7307
7308 ratio += hr;
7309
7310 ratio = exp(ratio);
7311 alpha = MIN(1.,ratio);
7312
7313 /* Always accept move */
7314 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
7315
7316 u = Uni();
7317
7318 /* printf("\n- Swap new_glnL: %f [%f] new_alnL: %f [%f] hr: %f u:%f alpha: %f",new_glnL,cur_glnL,new_alnL,cur_alnL,hr,u,alpha); */
7319
7320 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
7321
7322 if(u > alpha) /* Reject */
7323 {
7324 /* printf("\n- Reject %f %f",target_disk->time,ori_time); */
7325
7326 PHYREX_Remove_Disk(target_disk);
7327
7328 target_disk->time = ori_time;
7329 target_disk->prev = ori_disk_old;
7330 target_disk->next = ori_disk_young;
7331
7332 PHYREX_Insert_Disk(target_disk,tree);
7333 PHYREX_Update_Lindisk_List_Range(ldsk_next->disk,target_disk->ldsk->prev->disk,tree);
7334
7335 tree->mmod->c_lnL = cur_glnL;
7336 tree->c_lnL = cur_alnL;
7337 tree->rates->c_lnL_rates = cur_rlnL;
7338 }
7339 else
7340 {
7341 /* printf("\n- Accept %f %f",target_disk->time,ori_time); */
7342 }
7343 }
7344
7345 Free(valid_disks);
7346
7347 }
7348 #endif
7349
7350 /*////////////////////////////////////////////////////////////
7351 ////////////////////////////////////////////////////////////*/
7352 #ifdef PHYREX
MCMC_PHYREX_Indel_Hit(t_tree * tree)7353 void MCMC_PHYREX_Indel_Hit(t_tree *tree)
7354 {
7355 int n_disks_cur, n_disks_new;
7356 t_dsk *disk;
7357 phydbl hr;
7358 phydbl cur_lbda,new_lbda;
7359 phydbl cur_mu,new_mu;
7360 phydbl cur_rad,new_rad;
7361
7362
7363 hr = 0.0;
7364 new_lbda = tree->mmod->lbda;
7365 cur_lbda = tree->mmod->lbda;
7366 new_rad = tree->mmod->rad;
7367 cur_rad = tree->mmod->rad;
7368 new_mu = tree->mmod->mu;
7369 cur_mu = tree->mmod->mu;
7370
7371 /* new_lbda = cur_lbda * exp(1.0*(Uni()-.5)); */
7372 /* hr += log(new_lbda/cur_lbda); */
7373
7374 /* new_mu = cur_mu * exp(0.5*(Uni()-.5)); */
7375 /* hr += log(new_mu/cur_mu); */
7376
7377 /* new_rad = cur_rad * exp(0.5*(Uni()-.5)); */
7378 /* hr += log(new_rad/cur_rad); */
7379
7380 if(new_rad > tree->mmod->max_rad || new_rad < tree->mmod->min_rad) return;
7381 if(new_mu > tree->mmod->max_mu || new_mu < tree->mmod->min_mu) return;
7382 if(new_lbda > tree->mmod->max_lbda || new_lbda < tree->mmod->min_lbda) return;
7383
7384 tree->mmod->lbda = new_lbda;
7385 tree->mmod->mu = new_mu;
7386 tree->mmod->rad = new_rad;
7387
7388
7389 disk = tree->young_disk->prev;
7390 n_disks_cur = 0;
7391 do
7392 {
7393 if(disk->ldsk != NULL && disk->ldsk->n_next == 1) n_disks_cur++;
7394 disk = disk->prev;
7395 }
7396 while(disk);
7397
7398 n_disks_new = (int)Rpois(n_disks_cur+SMALL);
7399 hr += Dpois(n_disks_cur,n_disks_new+SMALL,YES);
7400 hr -= Dpois(n_disks_new,n_disks_cur+SMALL,YES);
7401
7402 if(n_disks_new < n_disks_cur)
7403 {
7404 MCMC_PHYREX_Delete_Hit(hr, n_disks_cur - n_disks_new, cur_lbda, cur_rad, cur_mu, tree);
7405 }
7406 else
7407 {
7408 MCMC_PHYREX_Insert_Hit(hr, n_disks_new - n_disks_cur, cur_lbda, cur_rad, cur_mu, tree);
7409 }
7410
7411 }
7412 #endif
7413
7414
7415 /*////////////////////////////////////////////////////////////
7416 ////////////////////////////////////////////////////////////*/
7417 /* Insert a disk with a new lineage displacement */
7418 #ifdef PHYREX
MCMC_PHYREX_Insert_Hit(phydbl hr,int n_insert_disks,phydbl cur_lbda,phydbl cur_rad,phydbl cur_mu,t_tree * tree)7419 void MCMC_PHYREX_Insert_Hit(phydbl hr, int n_insert_disks, phydbl cur_lbda, phydbl cur_rad, phydbl cur_mu, t_tree *tree)
7420 {
7421 t_dsk *disk,**new_disk,*young_disk,*old_disk;
7422 t_ldsk **young_ldsk, **old_ldsk, **new_ldsk;
7423 phydbl T,t;
7424 phydbl cur_glnL, new_glnL;
7425 phydbl u,alpha,ratio;
7426 int i,j,*dir_old_young,n_valid_disks,err,num_prec_issue;
7427
7428
7429 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_hit]++;
7430
7431 if(n_insert_disks == 0)
7432 {
7433 tree->mmod->lbda = cur_lbda;
7434 tree->mmod->rad = cur_rad;
7435 tree->mmod->mu = cur_mu;
7436 return;
7437 }
7438
7439
7440 disk = NULL;
7441 new_glnL = tree->mmod->c_lnL;
7442 cur_glnL = tree->mmod->c_lnL;
7443 ratio = 0.0;
7444 num_prec_issue = NO;
7445
7446 disk = tree->young_disk->prev;
7447 n_valid_disks = 0;
7448 do
7449 {
7450 if(disk->ldsk != NULL && disk->ldsk->n_next == 1) n_valid_disks++;
7451 disk = disk->prev;
7452 }
7453 while(disk && disk->prev);
7454
7455
7456 new_disk = (t_dsk **)mCalloc(n_insert_disks,sizeof(t_dsk *));
7457 new_ldsk = (t_ldsk **)mCalloc(n_insert_disks,sizeof(t_ldsk *));
7458 old_ldsk = (t_ldsk **)mCalloc(n_insert_disks,sizeof(t_ldsk *));
7459 young_ldsk = (t_ldsk **)mCalloc(n_insert_disks,sizeof(t_ldsk *));
7460 dir_old_young = (int *)mCalloc(n_insert_disks,sizeof(int));
7461
7462 T = PHYREX_Tree_Height(tree);
7463
7464 for(j=0;j<n_insert_disks;j++)
7465 {
7466 /* Time of insertion of new disk */
7467 do
7468 {
7469 num_prec_issue = NO;
7470 t = Uni()*(tree->young_disk->time-T) + T;
7471 disk = tree->young_disk->prev;
7472 while(disk && disk->time > t) disk = disk->prev;
7473
7474 if(!(disk->time != t))
7475 {
7476 PhyML_Printf("\n. Numerical precision issue in insert_hit");
7477 PhyML_Printf("\n. t=%g disk->time=%g diff=%g",t,disk->time,t-disk->time);
7478 num_prec_issue = YES;
7479 }
7480 }while(num_prec_issue == YES);
7481
7482
7483 /* Disks located just prior and after inserted disk */
7484 young_disk = disk->next;
7485 assert(young_disk->n_ldsk_a);
7486
7487 old_disk = disk;
7488 assert(old_disk->n_ldsk_a);
7489
7490 /* Make and initialize new disk */
7491 new_disk[j] = PHYREX_Make_Disk_Event(tree->mmod->n_dim,tree->n_otu);
7492 PHYREX_Init_Disk_Event(new_disk[j],tree->mmod->n_dim,tree->mmod);
7493
7494 /* Time of the new disk */
7495 new_disk[j]->time = t;
7496
7497 /* Insert it */
7498 PHYREX_Insert_Disk(new_disk[j],tree);
7499
7500 assert(new_disk[j]->next == young_disk);
7501 assert(new_disk[j]->prev == old_disk);
7502
7503 /* Which lineage is going to be hit ? */
7504 hr -= log(1./PHYREX_Number_Of_Outgoing_Ldsks(young_disk));
7505
7506 young_ldsk[j] = PHYREX_Random_Select_Outgoing_Ldsk(young_disk);
7507 old_ldsk[j] = young_ldsk[j]->prev;
7508
7509 if(old_ldsk[j]->disk->time > young_ldsk[j]->disk->time ||
7510 young_ldsk[j]->disk->time < new_disk[j]->time ||
7511 old_ldsk[j]->disk->time > new_disk[j]->time)
7512 {
7513 PhyML_Fprintf(stderr,"\n. young_disk->time: %f",young_disk->time);
7514 PhyML_Fprintf(stderr,"\n. old_disk->time: %f",old_disk->time);
7515 PhyML_Fprintf(stderr,"\n. young_ldsk->disk->time: %f",young_ldsk[j]->disk->time);
7516 PhyML_Fprintf(stderr,"\n. old_ldsk->disk->time: %f",old_ldsk[j]->disk->time);
7517 PhyML_Fprintf(stderr,"\n. new_disk->disk->time: %f",new_disk[j]->time);
7518 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
7519 }
7520
7521
7522 /* Direction from old to young ldsk */
7523 dir_old_young[j] = PHYREX_Get_Next_Direction(young_ldsk[j],old_ldsk[j]);
7524 assert(dir_old_young[j] != -1);
7525
7526 /* Make and initialize new ldsk */
7527 new_ldsk[j] = PHYREX_Make_Lindisk_Node(tree->mmod->n_dim);
7528 PHYREX_Init_Lindisk_Node(new_ldsk[j],new_disk[j],tree->mmod->n_dim);
7529 PHYREX_Make_Lindisk_Next(new_ldsk[j]);
7530 new_disk[j]->ldsk = new_ldsk[j];
7531
7532
7533 /* Connect it */
7534 new_ldsk[j]->prev = old_ldsk[j];
7535 new_ldsk[j]->next[0] = young_ldsk[j];
7536 young_ldsk[j]->prev = new_ldsk[j];
7537 old_ldsk[j]->next[dir_old_young[j]] = new_ldsk[j];
7538
7539 /* NOT OPTIMAL */
7540 /* Update ldsk_a arrays in the time interval affected by the insertion */
7541 PHYREX_Update_Lindisk_List_Range(new_ldsk[j]->disk,old_ldsk[j]->disk,tree);
7542
7543 /* Sample position of the displaced ldsk */
7544 for(i=0;i<tree->mmod->n_dim;i++)
7545 {
7546 new_disk[j]->centr->lonlat[i] = Uni() * (tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i])+tree->mmod->lim_do->lonlat[i];
7547 hr -= log(1./(tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i]));
7548
7549 err = NO;
7550 new_ldsk[j]->coord->lonlat[i] = Rnorm_Trunc(new_disk[j]->centr->lonlat[i],
7551 1.0*tree->mmod->rad,
7552 tree->mmod->lim_do->lonlat[i],
7553 tree->mmod->lim_up->lonlat[i],&err);
7554 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
7555
7556 hr -= Log_Dnorm_Trunc(new_ldsk[j]->coord->lonlat[i],
7557 new_disk[j]->centr->lonlat[i],
7558 1.0*tree->mmod->rad,
7559 tree->mmod->lim_do->lonlat[i],
7560 tree->mmod->lim_up->lonlat[i],&err);
7561 }
7562 }
7563
7564 T = PHYREX_Tree_Height(tree);
7565 T = fabs(T-tree->young_disk->time);
7566
7567 hr -= LnChoose(n_valid_disks+n_insert_disks,n_insert_disks);
7568 hr -= n_insert_disks * log(1./T);
7569 hr -= LnFact(n_insert_disks);
7570
7571 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
7572
7573 ratio += (new_glnL - cur_glnL);
7574 ratio += hr;
7575
7576 ratio = exp(ratio);
7577 alpha = MIN(1.,ratio);
7578
7579 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
7580
7581 u = Uni();
7582
7583 /* printf("\n- Insert hit %15f %15f %5d",new_glnL - cur_glnL, alpha, n_insert_disks); */
7584
7585 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
7586
7587 if(u > alpha) /* Reject */
7588 {
7589 /* printf("+ Reject"); */
7590
7591 tree->mmod->lbda = cur_lbda;
7592 tree->mmod->rad = cur_rad;
7593 tree->mmod->mu = cur_mu;
7594
7595 for(j=n_insert_disks-1;j>=0;j--)
7596 {
7597 old_ldsk[j]->next[dir_old_young[j]] = young_ldsk[j];
7598 young_ldsk[j]->prev = old_ldsk[j];
7599
7600 PHYREX_Remove_Disk(new_disk[j]);
7601 }
7602
7603
7604 PHYREX_Update_Lindisk_List(tree);
7605 tree->mmod->c_lnL = cur_glnL;
7606
7607 for(j=n_insert_disks-1;j>=0;j--)
7608 {
7609 Free_Disk(new_disk[j]);
7610 Free_Ldisk(new_ldsk[j]);
7611 }
7612 }
7613 else
7614 {
7615 /* printf("+ Accept"); */
7616 /* if(indel > 0) printf("\n. Accept"); */
7617 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_hit]++;
7618 }
7619
7620
7621 Free(new_disk);
7622 Free(new_ldsk);
7623 Free(old_ldsk);
7624 Free(young_ldsk);
7625 Free(dir_old_young);
7626
7627
7628 }
7629 #endif
7630
7631 /*////////////////////////////////////////////////////////////
7632 ////////////////////////////////////////////////////////////*/
7633 /* Remove one or more disks with a lineage displacement */
7634 #ifdef PHYREX
MCMC_PHYREX_Delete_Hit(phydbl hr,int n_delete_disks,phydbl cur_lbda,phydbl cur_rad,phydbl cur_mu,t_tree * tree)7635 void MCMC_PHYREX_Delete_Hit(phydbl hr, int n_delete_disks, phydbl cur_lbda, phydbl cur_rad, phydbl cur_mu, t_tree *tree)
7636 {
7637 phydbl u,alpha,ratio;
7638 phydbl cur_glnL,new_glnL,T;
7639 t_dsk *disk,**target_disk,**valid_disks;
7640 t_ldsk **target_ldsk,**old_ldsk,**young_ldsk;
7641 int i,j,block,n_valid_disks,*dir_old_young,*permut,err;
7642
7643
7644 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_hit]++;
7645
7646 if(n_delete_disks == 0)
7647 {
7648 tree->mmod->rad = cur_rad;
7649 tree->mmod->mu = cur_mu;
7650 return;
7651 }
7652
7653 disk = NULL;
7654 valid_disks = NULL;
7655 new_glnL = tree->mmod->c_lnL;
7656 cur_glnL = tree->mmod->c_lnL;
7657 ratio = 0.0;
7658 block = 100;
7659
7660 if(tree->young_disk->next) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
7661
7662 disk = tree->young_disk->prev;
7663 if(!disk->prev)
7664 {
7665 tree->mmod->lbda = cur_lbda;
7666 tree->mmod->rad = cur_rad;
7667 tree->mmod->mu = cur_mu;
7668 return;
7669 }
7670
7671 n_valid_disks = 0;
7672 do
7673 {
7674 /* Include only disks with displacement that are not coalescent events */
7675 if(disk->ldsk != NULL && disk->ldsk->n_next == 1)
7676 {
7677 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
7678 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
7679 valid_disks[n_valid_disks] = disk;
7680 n_valid_disks++;
7681 }
7682 disk = disk->prev;
7683 }
7684 while(disk && disk->prev);
7685
7686 if(!n_valid_disks || (n_valid_disks - n_delete_disks < 0))
7687 {
7688 tree->mmod->rad = cur_rad;
7689 tree->mmod->mu = cur_mu;
7690 return;
7691 }
7692
7693
7694 target_disk = (t_dsk **)mCalloc(n_delete_disks,sizeof(t_dsk *));
7695 target_ldsk = (t_ldsk **)mCalloc(n_delete_disks,sizeof(t_ldsk *));
7696 old_ldsk = (t_ldsk **)mCalloc(n_delete_disks,sizeof(t_ldsk *));
7697 young_ldsk = (t_ldsk **)mCalloc(n_delete_disks,sizeof(t_ldsk *));
7698 dir_old_young = (int *)mCalloc(n_delete_disks,sizeof(int));
7699
7700 permut = Permutate(n_valid_disks);
7701
7702 for(j=0;j<n_delete_disks;j++)
7703 {
7704 target_disk[j] = valid_disks[permut[j]];
7705 target_ldsk[j] = target_disk[j]->ldsk;
7706
7707 assert(target_disk[j]->age_fixed == NO);
7708 assert(target_ldsk[j] != NULL);
7709 assert(target_ldsk[j]->n_next == 1);
7710
7711 old_ldsk[j] = target_ldsk[j]->prev;
7712 young_ldsk[j] = target_ldsk[j]->next[0];
7713
7714 dir_old_young[j] = PHYREX_Get_Next_Direction(young_ldsk[j],old_ldsk[j]);
7715 assert(dir_old_young[j] != -1);
7716
7717
7718 /* Part of the Hastings ratio corresponding to the probability of selecting */
7719 /* target_disk->ldsk->next[0] to be hit (reverse move) */
7720 hr += log(1./target_disk[j]->next->n_ldsk_a);
7721
7722 /* Density for position of the displaced ldsk */
7723 for(i=0;i<tree->mmod->n_dim;i++)
7724 {
7725 hr += log(1./(tree->mmod->lim_up->lonlat[i]-tree->mmod->lim_do->lonlat[i]));
7726
7727 hr += Log_Dnorm_Trunc(target_ldsk[j]->coord->lonlat[i],
7728 target_disk[j]->centr->lonlat[i],
7729 1.0*tree->mmod->rad,
7730 tree->mmod->lim_do->lonlat[i],
7731 tree->mmod->lim_up->lonlat[i],&err);
7732 }
7733
7734
7735 /* New connections between old_ldsk and young_ldsk */
7736 old_ldsk[j]->next[dir_old_young[j]] = young_ldsk[j];
7737 young_ldsk[j]->prev = old_ldsk[j];
7738
7739 /* Remove target disk */
7740 PHYREX_Remove_Disk(target_disk[j]);
7741
7742 /* Update ldsk_a arrays in the time interval affected by the deletion */
7743 PHYREX_Update_Lindisk_List_Range(young_ldsk[j]->disk,old_ldsk[j]->disk,tree);
7744 }
7745
7746 T = PHYREX_Tree_Height(tree);
7747 T = fabs(T-tree->young_disk->time);
7748
7749 hr += LnChoose(n_valid_disks,n_delete_disks);
7750 hr += n_delete_disks * log(1./T);
7751 hr += LnFact(n_delete_disks);
7752
7753 Free(valid_disks);
7754
7755 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
7756
7757 ratio += (new_glnL - cur_glnL);
7758 ratio += hr;
7759
7760 ratio = exp(ratio);
7761 alpha = MIN(1.,ratio);
7762
7763 /* Always accept move */
7764 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
7765
7766 u = Uni();
7767
7768 /* printf("\n- Delete hit %15f %15f %5d",new_glnL - cur_glnL, alpha, n_delete_disks); */
7769
7770 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
7771
7772 if(u > alpha) /* Reject */
7773 {
7774 tree->mmod->lbda = cur_lbda;
7775 tree->mmod->rad = cur_rad;
7776 tree->mmod->mu = cur_mu;
7777
7778 for(j=n_delete_disks-1;j>=0;j--)
7779 {
7780 PHYREX_Insert_Disk(target_disk[j],tree);
7781 old_ldsk[j]->next[dir_old_young[j]] = target_ldsk[j];
7782 young_ldsk[j]->prev = target_ldsk[j];
7783 }
7784
7785 PHYREX_Update_Lindisk_List(tree);
7786
7787 tree->mmod->c_lnL = cur_glnL;
7788 }
7789 else
7790 {
7791 for(j=0;j<n_delete_disks;j++) Free_Disk(target_disk[j]);
7792 for(j=0;j<n_delete_disks;j++) Free_Ldisk(target_ldsk[j]);
7793 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_hit]++;
7794 }
7795
7796
7797 Free(target_disk);
7798 Free(target_ldsk);
7799 Free(old_ldsk);
7800 Free(young_ldsk);
7801 Free(dir_old_young);
7802 Free(permut);
7803 }
7804 #endif
7805
7806 /*////////////////////////////////////////////////////////////
7807 ////////////////////////////////////////////////////////////*/
7808
7809 #ifdef PHYREX
MCMC_PHYREX_Prune_Regraft(t_tree * tree)7810 void MCMC_PHYREX_Prune_Regraft(t_tree *tree)
7811 {
7812 phydbl u,alpha,ratio,hr;
7813 phydbl cur_glnL, new_glnL;
7814 phydbl cur_alnL, new_alnL;
7815 phydbl cur_rlnL, new_rlnL;
7816 t_dsk *disk,*regraft_disk,**valid_disks,*oldest_disk;
7817 t_ldsk *prune_ldsk,*regraft_ldsk,*prune_ldsk_daughter,*cur_path,*new_path,*ldsk,*ldsk_dum,**valid_ldsks;
7818 phydbl *prob_disks_prune,*prob_disks_regraft;
7819 int i,block,n_valid_disks,num_regraft_disk,num_prune_disk,n_valid_ldsks;
7820 phydbl max_dist, param_exp;
7821 int cur_path_len;
7822 int n_iter;
7823 int cur_pos,new_pos;
7824 phydbl sum;
7825
7826 if(tree->mod->s_opt->opt_topo == NO) return;
7827
7828 n_iter = 1+(int)(tree->n_otu/10);
7829
7830 while(n_iter--)
7831 {
7832 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_spr]++;
7833
7834 valid_ldsks = NULL;
7835 valid_disks = NULL;
7836 disk = NULL;
7837 prob_disks_regraft = NULL;
7838 prob_disks_prune = NULL;
7839 new_glnL = tree->mmod->c_lnL;
7840 cur_glnL = tree->mmod->c_lnL;
7841 new_alnL = tree->c_lnL;
7842 cur_alnL = tree->c_lnL;
7843 new_rlnL = tree->rates->c_lnL_rates;
7844 cur_rlnL = tree->rates->c_lnL_rates;
7845 hr = 0.0;
7846 ratio = 0.0;
7847 block = 100;
7848 cur_pos = -1;
7849 new_pos = -1;
7850 param_exp = 1.0;
7851 num_regraft_disk = -1;
7852 num_prune_disk = -1;
7853
7854 if(tree->young_disk->next) assert(FALSE);
7855
7856 /* Get a ldsk from which you can prune a lineage */
7857 disk = tree->young_disk->prev;
7858 n_valid_ldsks = 0;
7859 do
7860 {
7861 /* Include only disks with displacement that are coalescent events */
7862 if(disk->ldsk != NULL && disk->ldsk->n_next > 1)
7863 {
7864 /* Root has degree 2: don't pull any lineage */
7865 if(!(disk->prev == NULL && disk->ldsk->n_next == 2))
7866 {
7867 if(!n_valid_ldsks) valid_ldsks = (t_ldsk **)mCalloc(disk->ldsk->n_next,sizeof(t_ldsk *));
7868 else valid_ldsks = (t_ldsk **)mRealloc(valid_ldsks,n_valid_ldsks+disk->ldsk->n_next,sizeof(t_ldsk *));
7869 for(i=0;i<disk->ldsk->n_next;++i)
7870 {
7871 valid_ldsks[n_valid_ldsks] = disk->ldsk->next[i];
7872 n_valid_ldsks++;
7873 }
7874 }
7875 }
7876 disk = disk->prev;
7877 }
7878 while(disk);
7879
7880 if(!n_valid_ldsks) return;
7881
7882 prune_ldsk_daughter = valid_ldsks[Rand_Int(0,n_valid_ldsks-1)];
7883 prune_ldsk = prune_ldsk_daughter->prev;
7884 hr -= log(1./(phydbl)(n_valid_ldsks));
7885 Free(valid_ldsks);
7886
7887 /* prune_ldsk_daughter is the next coalescent event or tip node */
7888 while(prune_ldsk_daughter->n_next == 1) prune_ldsk_daughter = prune_ldsk_daughter->next[0];
7889
7890
7891 /* Get a ldsk to reattach the pruned lineage to */
7892 disk = tree->young_disk->prev;
7893 n_valid_disks = 0;
7894 do
7895 {
7896 /* Include only disks with displacement that are not younger
7897 than prune_ldsk_daughter and different from prune_ldsk */
7898 if((disk->ldsk != NULL) &&
7899 (disk->ldsk->n_next >= 1) &&
7900 (disk->time < prune_ldsk_daughter->disk->time) &&
7901 (PHYREX_Is_On_Path(disk->ldsk,prune_ldsk_daughter,prune_ldsk) == NO))
7902 {
7903 if(!n_valid_disks)
7904 {
7905 valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
7906 prob_disks_prune = (phydbl *)mCalloc(block,sizeof(phydbl));
7907 }
7908 else if(!(n_valid_disks%block))
7909 {
7910 valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
7911 prob_disks_prune = (phydbl *)mRealloc(prob_disks_prune,n_valid_disks+block,sizeof(phydbl));
7912 }
7913
7914 valid_disks[n_valid_disks] = disk;
7915
7916 prob_disks_prune[n_valid_disks] = PHYREX_Dist_Between_Two_Ldsk(disk->ldsk,prune_ldsk,tree);
7917
7918 n_valid_disks++;
7919 }
7920 disk = disk->prev;
7921 }
7922 while(disk);
7923
7924 assert(n_valid_disks);
7925
7926 if(n_valid_disks > 1)
7927 {
7928 max_dist = -INFINITY;
7929 for(i=0;i<n_valid_disks;i++) if(prob_disks_prune[i] > max_dist) max_dist = prob_disks_prune[i];
7930
7931 sum = 0.0;
7932 for(i=0;i<n_valid_disks;i++) prob_disks_prune[i] /= max_dist;
7933
7934 for(i=0;i<n_valid_disks;i++) prob_disks_prune[i] = Dexp(prob_disks_prune[i],param_exp);
7935
7936 sum = 0.0;
7937 for(i=0;i<n_valid_disks;i++) sum += prob_disks_prune[i];
7938 for(i=0;i<n_valid_disks;i++) prob_disks_prune[i] /= sum;
7939
7940 num_regraft_disk = Sample_i_With_Proba_pi(prob_disks_prune,n_valid_disks);
7941 regraft_disk = valid_disks[num_regraft_disk];
7942 regraft_ldsk = regraft_disk->ldsk;
7943
7944 /* Prob of selecting this node as regraft site */
7945 hr -= log(prob_disks_prune[num_regraft_disk]);
7946 }
7947
7948 Free(prob_disks_prune);
7949 Free(valid_disks);
7950
7951 if(n_valid_disks == 1) return;
7952
7953 oldest_disk = (regraft_ldsk->disk->time < prune_ldsk->disk->time) ? (regraft_ldsk->disk) : (prune_ldsk->disk);
7954
7955 new_glnL = cur_glnL;
7956 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
7957
7958 /* Prune and regraft */
7959 cur_path_len = PHYREX_Path_Len(prune_ldsk_daughter,prune_ldsk)-2;
7960
7961 hr += PHYREX_Path_Logdensity(prune_ldsk_daughter,prune_ldsk,1.0*tree->mmod->rad,tree);
7962
7963 new_path = PHYREX_Generate_Path(prune_ldsk_daughter,regraft_ldsk,cur_path_len,1.0*tree->mmod->rad,tree);
7964 cur_path = PHYREX_Remove_Path(prune_ldsk_daughter,prune_ldsk,&cur_pos,tree);
7965 new_pos = Rand_Int(0,regraft_ldsk->n_next);
7966 hr -= log(1./(phydbl)(regraft_ldsk->n_next+1));
7967 hr += log(1./(phydbl)(prune_ldsk->n_next+1));
7968 PHYREX_Insert_Path(prune_ldsk_daughter,regraft_ldsk,new_path,new_pos,tree);
7969
7970 hr -= PHYREX_Path_Logdensity(prune_ldsk_daughter,regraft_ldsk,1.0*tree->mmod->rad,tree);
7971
7972
7973 PHYREX_Ldsk_To_Tree(tree);
7974 Update_Ancestors(tree->n_root,tree->n_root->v[2],tree);
7975 Update_Ancestors(tree->n_root,tree->n_root->v[1],tree);
7976 RATES_Update_Cur_Bl(tree);
7977
7978 assert(isinf(hr) == NO);
7979
7980 PHYREX_Update_Lindisk_List(tree);
7981
7982 /* Number of ldsk which can be pruned (reverse move) */
7983 if(regraft_disk->prev == NULL && regraft_ldsk->n_next == 3) // regraft_ldsk is root & went from 2 to 3 outgoing lineages
7984 n_valid_ldsks+=3;
7985 if(prune_ldsk->prev == NULL && prune_ldsk->n_next == 2) // prune_ldsk is root & went from 3 to 2 outgoing lineages
7986 n_valid_ldsks-=3;
7987 if(regraft_ldsk->n_next == 2) n_valid_ldsks += 2;
7988 if(prune_ldsk->n_next == 1) n_valid_ldsks -= 2;
7989
7990 assert(n_valid_ldsks > 0);
7991 hr += log(1./(phydbl)(n_valid_ldsks));
7992
7993
7994
7995
7996 /* Get a ldsk to reattach the pruned lineage to (reverse move) */
7997 disk = tree->young_disk->prev;
7998 n_valid_disks = 0;
7999 do
8000 {
8001 /* Include only disks with displacement that are not younger
8002 than prune_ldsk_daughter and different from prune_ldsk */
8003 if((disk->ldsk != NULL) &&
8004 (disk->ldsk->n_next >= 1) &&
8005 (disk->time < prune_ldsk_daughter->disk->time) &&
8006 (PHYREX_Is_On_Path(disk->ldsk,prune_ldsk_daughter,regraft_ldsk) == NO))
8007 {
8008 if(!n_valid_disks)
8009 {
8010 valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8011 prob_disks_regraft = (phydbl *)mCalloc(block,sizeof(phydbl));
8012 }
8013 else if(!(n_valid_disks%block))
8014 {
8015 valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
8016 prob_disks_regraft = (phydbl *)mRealloc(prob_disks_regraft,n_valid_disks+block,sizeof(phydbl));
8017 }
8018
8019 valid_disks[n_valid_disks] = disk;
8020
8021 prob_disks_regraft[n_valid_disks] = PHYREX_Dist_Between_Two_Ldsk(disk->ldsk,regraft_ldsk,tree);
8022
8023 if(disk == prune_ldsk->disk) num_prune_disk = n_valid_disks;
8024
8025 n_valid_disks++;
8026 }
8027 disk = disk->prev;
8028 }
8029 while(disk);
8030
8031 max_dist = -INFINITY;
8032 for(i=0;i<n_valid_disks;i++) if(prob_disks_regraft[i] > max_dist) max_dist = prob_disks_regraft[i];
8033
8034 sum = 0.0;
8035 for(i=0;i<n_valid_disks;i++) prob_disks_regraft[i] /= max_dist;
8036
8037 for(i=0;i<n_valid_disks;i++) prob_disks_regraft[i] = Dexp(prob_disks_regraft[i],param_exp);
8038
8039 sum = 0.0;
8040 for(i=0;i<n_valid_disks;i++) sum += prob_disks_regraft[i];
8041 for(i=0;i<n_valid_disks;i++) prob_disks_regraft[i] /= sum;
8042
8043
8044 assert(num_prune_disk > -1);
8045 /* Prob of selecting this node as regraft site (reverse move) */
8046 hr += log(prob_disks_regraft[num_prune_disk]);
8047
8048 Free(prob_disks_regraft);
8049 Free(valid_disks);
8050
8051 if(tree->eval_glnL == YES)
8052 {
8053 new_glnL += PHYREX_Lk_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
8054 tree->mmod->c_lnL = new_glnL;
8055 /* new_glnL = PHYREX_Lk(tree); */
8056 }
8057
8058 if(tree->eval_alnL == YES) new_alnL = Lk(NULL,tree);
8059 if(tree->eval_rlnL == YES) new_rlnL = RATES_Lk_Rates(tree);
8060
8061 ratio += (new_alnL - cur_alnL);
8062 ratio += (new_glnL - cur_glnL);
8063 ratio += (new_rlnL - cur_rlnL);
8064
8065 ratio += hr;
8066
8067 ratio = exp(ratio);
8068 alpha = MIN(1.,ratio);
8069
8070 /* PhyML_Printf("\nYYY %12f %12f %4d %4d %12f %12f", */
8071 /* prune_ldsk_daughter->disk->time - prune_ldsk->disk->time, */
8072 /* prune_ldsk_daughter->disk->time - regraft_ldsk->disk->time, */
8073 /* cur_path_len, */
8074 /* new_path_len, */
8075 /* PHYREX_Dist_Between_Two_Ldsk(regraft_ldsk,prune_ldsk,tree), */
8076 /* alpha); */
8077
8078
8079 /* Always accept move */
8080 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8081
8082 /* printf("\n. %3d %3d alpha:%12f %3d",prune_ldsk->n_next+1,regraft_ldsk->n_next-1,alpha,new_n_coal); */
8083 /* PhyML_Printf("\n. new_glnL: %f cur_glnL: %f hr: %f alpha: %f",new_glnL,cur_glnL,hr,alpha); */
8084
8085 u = Uni();
8086
8087 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
8088
8089 if(u > alpha) /* Reject */
8090 {
8091 new_path = PHYREX_Remove_Path(prune_ldsk_daughter,regraft_ldsk,&new_pos,tree);
8092 PHYREX_Insert_Path(prune_ldsk_daughter,prune_ldsk,cur_path,cur_pos,tree);
8093
8094 PHYREX_Ldsk_To_Tree(tree);
8095 Update_Ancestors(tree->n_root,tree->n_root->v[2],tree);
8096 Update_Ancestors(tree->n_root,tree->n_root->v[1],tree);
8097 RATES_Fill_Lca_Table(tree);
8098 RATES_Update_Cur_Bl(tree);
8099
8100
8101 PHYREX_Update_Lindisk_List(tree);
8102 tree->c_lnL = cur_alnL;
8103 tree->mmod->c_lnL = cur_glnL;
8104 tree->rates->c_lnL_rates = cur_rlnL;
8105
8106 ldsk = new_path;
8107 while(ldsk)
8108 {
8109 Free_Disk(ldsk->disk);
8110 ldsk_dum = ldsk;
8111 ldsk = ldsk->prev;
8112 Free_Ldisk(ldsk_dum);
8113 }
8114 }
8115 else
8116 {
8117 ldsk = cur_path;
8118 while(ldsk)
8119 {
8120 Free_Disk(ldsk->disk);
8121 ldsk_dum = ldsk;
8122 ldsk = ldsk->prev;
8123 Free_Ldisk(ldsk_dum);
8124 }
8125
8126 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_spr]++;
8127 }
8128 }
8129 }
8130 #endif
8131
8132 /*////////////////////////////////////////////////////////////
8133 ////////////////////////////////////////////////////////////*/
8134
8135 #ifdef PHYREX
MCMC_PHYREX_Prune_Regraft_Local(t_tree * tree)8136 void MCMC_PHYREX_Prune_Regraft_Local(t_tree *tree)
8137 {
8138 phydbl u,alpha,ratio,hr;
8139 phydbl cur_glnL, new_glnL;
8140 phydbl cur_alnL, new_alnL;
8141 phydbl cur_rlnL, new_rlnL;
8142 t_dsk *disk,*regraft_disk,*oldest_disk,**valid_disks;
8143 t_ldsk *prune_ldsk,*prune_ldsk_daughter,*regraft_ldsk,*cur_path,**valid_ldsks;
8144 int i,block,n_valid_disks,n_valid_ldsks;
8145 int n_iter;
8146 int cur_pos,new_pos;
8147
8148 if(tree->mod->s_opt->opt_topo == NO) return;
8149
8150 n_iter = 1+(int)(tree->n_otu/10);
8151
8152 while(n_iter--)
8153 {
8154 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_spr_local]++;
8155
8156 valid_ldsks = NULL;
8157 valid_disks = NULL;
8158 disk = NULL;
8159 new_glnL = tree->mmod->c_lnL;
8160 cur_glnL = tree->mmod->c_lnL;
8161 new_alnL = tree->c_lnL;
8162 cur_alnL = tree->c_lnL;
8163 new_rlnL = tree->rates->c_lnL_rates;
8164 cur_rlnL = tree->rates->c_lnL_rates;
8165 hr = 0.0;
8166 ratio = 0.0;
8167 block = 100;
8168 cur_pos = -1;
8169 new_pos = -1;
8170
8171 if(tree->young_disk->next) assert(FALSE);
8172
8173 /* Get a ldsk from which you can prune a lineage */
8174 disk = tree->young_disk->prev;
8175 n_valid_ldsks = 0;
8176 do
8177 {
8178 /* Include only disks with displacement that are coalescent events */
8179 if(disk->ldsk != NULL && disk->ldsk->n_next > 1)
8180 {
8181 /* Root has degree 2: don't pull any lineage */
8182 if(!(disk->prev == NULL && disk->ldsk->n_next == 2))
8183 {
8184 if(!n_valid_ldsks) valid_ldsks = (t_ldsk **)mCalloc(disk->ldsk->n_next,sizeof(t_ldsk *));
8185 else valid_ldsks = (t_ldsk **)mRealloc(valid_ldsks,n_valid_ldsks+disk->ldsk->n_next,sizeof(t_ldsk *));
8186 for(i=0;i<disk->ldsk->n_next;++i)
8187 {
8188 valid_ldsks[n_valid_ldsks] = disk->ldsk->next[i];
8189 n_valid_ldsks++;
8190 }
8191 }
8192 }
8193 disk = disk->prev;
8194 }
8195 while(disk);
8196
8197 if(!n_valid_ldsks) return;
8198
8199 prune_ldsk_daughter = valid_ldsks[Rand_Int(0,n_valid_ldsks-1)];
8200 prune_ldsk = prune_ldsk_daughter->prev;
8201 hr -= log(1./(phydbl)(n_valid_ldsks));
8202 Free(valid_ldsks);
8203
8204
8205 /* Get a ldsk to reattach the pruned lineage to */
8206 n_valid_disks = 0;
8207 if(prune_ldsk->prev != NULL)
8208 {
8209 valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8210 valid_disks[0] = prune_ldsk->prev->disk;
8211 n_valid_disks++;
8212 }
8213
8214 for(i=0;i<prune_ldsk->n_next;++i)
8215 {
8216 if(prune_ldsk->next[i] != prune_ldsk_daughter &&
8217 prune_ldsk->next[i]->n_next >= 1 &&
8218 prune_ldsk->next[i]->disk->time < prune_ldsk_daughter->disk->time)
8219 {
8220 if(n_valid_disks == 0)
8221 {
8222 valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8223 }
8224 else
8225 {
8226 if(!(n_valid_disks%block))
8227 {
8228 valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
8229 }
8230 }
8231
8232 valid_disks[n_valid_disks] = prune_ldsk->next[i]->disk;
8233
8234 n_valid_disks++;
8235 }
8236 }
8237
8238 if(!n_valid_disks) return;
8239
8240 regraft_disk = valid_disks[Rand_Int(0,n_valid_disks-1)];
8241 regraft_ldsk = regraft_disk->ldsk;
8242 hr -= log(1./(phydbl)(n_valid_disks));
8243 Free(valid_disks);
8244
8245 oldest_disk = (regraft_disk->time < prune_ldsk->disk->time) ? (regraft_disk) : (prune_ldsk->disk);
8246
8247 new_glnL = cur_glnL;
8248 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
8249
8250 /* Prune and regraft */
8251 cur_path = PHYREX_Remove_Path(prune_ldsk_daughter,prune_ldsk,&cur_pos,tree);
8252 new_pos = Rand_Int(0,regraft_ldsk->n_next);
8253 hr -= log(1./(phydbl)(regraft_ldsk->n_next+1));
8254 hr += log(1./(phydbl)(prune_ldsk->n_next+1));
8255 PHYREX_Insert_Path(prune_ldsk_daughter,regraft_ldsk,cur_path,new_pos,tree);
8256
8257 PHYREX_Ldsk_To_Tree(tree);
8258 Update_Ancestors(tree->n_root,tree->n_root->v[2],tree);
8259 Update_Ancestors(tree->n_root,tree->n_root->v[1],tree);
8260 RATES_Update_Cur_Bl(tree);
8261
8262 assert(isinf(hr) == NO);
8263
8264 PHYREX_Update_Lindisk_List_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
8265
8266
8267 /* Number of ldsk which one can be pruned (reverse move) */
8268 if(regraft_disk->prev == NULL && regraft_ldsk->n_next == 3) // regraft_ldsk is root & went from 2 to 3 outgoing lineages
8269 n_valid_ldsks+=3;
8270 if(prune_ldsk->prev == NULL && prune_ldsk->n_next == 2) // prune_ldsk is root & went from 3 to 2 outgoing lineages
8271 n_valid_ldsks-=3;
8272 if(regraft_ldsk->n_next == 2) n_valid_ldsks += 2;
8273 if(prune_ldsk->n_next == 1) n_valid_ldsks -= 2;
8274
8275
8276 assert(n_valid_ldsks > 0);
8277 hr += log(1./(phydbl)(n_valid_ldsks));
8278
8279
8280 /* Get a ldsk to reattach the pruned lineage to (reverse move) */
8281 n_valid_disks = 0;
8282 if(regraft_ldsk->prev != NULL) n_valid_disks++;
8283
8284 for(i=0;i<regraft_ldsk->n_next;++i)
8285 {
8286 if(regraft_ldsk->next[i] != prune_ldsk_daughter &&
8287 regraft_ldsk->next[i]->n_next >= 1 &&
8288 regraft_ldsk->next[i]->disk->time < prune_ldsk_daughter->disk->time)
8289 {
8290 n_valid_disks++;
8291 }
8292 }
8293
8294 assert(n_valid_disks > 0);
8295 hr += log(1./(phydbl)(n_valid_disks));
8296
8297 if(tree->eval_glnL == YES)
8298 {
8299 new_glnL += PHYREX_Lk_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
8300 tree->mmod->c_lnL = new_glnL;
8301 /* new_glnL = PHYREX_Lk(tree); */
8302 }
8303
8304 if(tree->eval_alnL == YES) new_alnL = Lk(NULL,tree);
8305 if(tree->eval_rlnL == YES) new_rlnL = RATES_Lk_Rates(tree);
8306
8307 ratio += (new_alnL - cur_alnL);
8308 ratio += (new_glnL - cur_glnL);
8309 ratio += (new_rlnL - cur_rlnL);
8310
8311 ratio += hr;
8312
8313 ratio = exp(ratio);
8314 alpha = MIN(1.,ratio);
8315
8316 /* PhyML_Printf("\nZZZ %12f %12f %12f %12f", */
8317 /* prune_ldsk_daughter->disk->time - prune_ldsk->disk->time, */
8318 /* prune_ldsk_daughter->disk->time - regraft_ldsk->disk->time, */
8319 /* PHYREX_Dist_Between_Two_Ldsk(regraft_ldsk,prune_ldsk,tree), */
8320 /* alpha); */
8321
8322
8323 /* Always accept move */
8324 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8325
8326 /* printf("\n. %3d %3d alpha:%12f %3d",prune_ldsk->n_next+1,regraft_ldsk->n_next-1,alpha,new_n_coal); */
8327 /* PhyML_Printf("\n. new_glnL: %f cur_glnL: %f hr: %f alpha: %f",new_glnL,cur_glnL,hr,alpha); */
8328
8329 u = Uni();
8330
8331 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
8332
8333 if(u > alpha) /* Reject */
8334 {
8335 cur_path = PHYREX_Remove_Path(prune_ldsk_daughter,regraft_ldsk,&new_pos,tree);
8336 PHYREX_Insert_Path(prune_ldsk_daughter,prune_ldsk,cur_path,cur_pos,tree);
8337
8338 PHYREX_Ldsk_To_Tree(tree);
8339 Update_Ancestors(tree->n_root,tree->n_root->v[2],tree);
8340 Update_Ancestors(tree->n_root,tree->n_root->v[1],tree);
8341 RATES_Fill_Lca_Table(tree);
8342 RATES_Update_Cur_Bl(tree);
8343
8344 PHYREX_Update_Lindisk_List_Range(prune_ldsk_daughter->disk,oldest_disk,tree);
8345
8346 tree->c_lnL = cur_alnL;
8347 tree->mmod->c_lnL = cur_glnL;
8348 tree->rates->c_lnL_rates = cur_rlnL;
8349 }
8350 else
8351 {
8352 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_spr_local]++;
8353 }
8354 }
8355 }
8356
8357 #endif
8358
8359 /*////////////////////////////////////////////////////////////
8360 ////////////////////////////////////////////////////////////*/
8361 #ifdef PHYREX
MCMC_PHYREX_Lineage_Traj(t_tree * tree)8362 void MCMC_PHYREX_Lineage_Traj(t_tree *tree)
8363 {
8364 phydbl u,alpha,ratio,hr;
8365 phydbl cur_glnL, new_glnL;
8366 t_dsk *disk,**valid_disks;
8367 t_ldsk *old_ldsk,*young_ldsk,*cur_path,*new_path,*ldsk,*ldsk_dum;
8368 int j,block,n_valid_disks;
8369 int n_iter,cur_path_len,new_path_len;
8370 int pos,*permut,dir_next;
8371 phydbl area;
8372
8373 n_iter = 0;
8374 valid_disks = NULL;
8375 disk = NULL;
8376 new_glnL = tree->mmod->c_lnL;
8377 cur_glnL = tree->mmod->c_lnL;
8378 hr = 0.0;
8379 ratio = 0.0;
8380 block = 100;
8381 pos = -1;
8382 dir_next = -1;
8383
8384 area = 1.0;
8385 for(j=0;j<tree->mmod->n_dim;++j) area *= (tree->mmod->lim_up->lonlat[j] - tree->mmod->lim_do->lonlat[j]);
8386
8387 disk = tree->young_disk->prev;
8388 n_valid_disks = 0;
8389 do
8390 {
8391 if(disk->ldsk && disk->ldsk->n_next >= 2)
8392 {
8393 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8394 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
8395 valid_disks[n_valid_disks] = disk;
8396 n_valid_disks++;
8397 }
8398 disk = disk->prev;
8399 }
8400 while(disk);
8401
8402 if(!n_valid_disks) return;
8403
8404 permut = Permutate(n_valid_disks);
8405 n_iter = 1+(int)(0.1*n_valid_disks);
8406
8407 for(j=0;j<n_iter;j++)
8408 {
8409 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_traj]++;
8410
8411 new_glnL = tree->mmod->c_lnL;
8412 cur_glnL = tree->mmod->c_lnL;
8413 hr = 0.0;
8414 ratio = 0.0;
8415 dir_next = -1;
8416
8417 old_ldsk = valid_disks[permut[Rand_Int(0,n_valid_disks-1)]]->ldsk;
8418 assert(old_ldsk != NULL);
8419
8420 dir_next = Rand_Int(0,old_ldsk->n_next-1);
8421 young_ldsk = old_ldsk->next[dir_next];
8422 while(young_ldsk->n_next == 1) young_ldsk = young_ldsk->next[0];
8423
8424
8425 /* dt = fabs(young_ldsk->disk->time - old_ldsk->disk->time); */
8426
8427 assert(young_ldsk != NULL);
8428
8429 new_glnL = cur_glnL;
8430 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(young_ldsk->disk,old_ldsk->disk,tree);
8431
8432 /* phydbl dist = Euclidean_Dist(young_ldsk->coord,old_ldsk->coord); */
8433 cur_path_len = PHYREX_Path_Len(young_ldsk,old_ldsk)-2;
8434
8435 /* new_path_len = Rpois(dt * 2. * PI * tree->mmod->lbda * tree->mmod->mu * pow(tree->mmod->rad,2) / area); */
8436 /* new_path_len = Rpois(cur_path_len+0.01); // To be avoided as create non-irreducible markov chain when new_path_len = 0 (since Pr(cur_path_len|new_path_len)=0) */
8437 /* new_path_len = Rpois((int)(pow(dist,2)/(4.*pow(tree->mmod->rad,2)))+0.01); */
8438
8439 /* hr -= Dpois(new_path_len,dt * 2.* PI * tree->mmod->lbda * tree->mmod->mu * pow(tree->mmod->rad,2) / area,YES); */
8440 /* hr += Dpois(cur_path_len,dt * 2.* PI * tree->mmod->lbda * tree->mmod->mu * pow(tree->mmod->rad,2) / area,YES); */
8441 /* hr -= Dpois(new_path_len,cur_path_len+0.01,YES); */
8442 /* hr += Dpois(cur_path_len,new_path_len+0.01,YES); */
8443 /* hr -= Dpois(new_path_len,(int)(pow(dist,2)/(4.*pow(tree->mmod->rad,2)))+0.01,YES); */
8444 /* hr += Dpois(cur_path_len,(int)(pow(dist,2)/(4.*pow(tree->mmod->rad,2)))+0.01,YES); */
8445
8446 /* hr -= (new_path_len) * log(1./dt); */
8447 /* hr += (cur_path_len) * log(1./dt); */
8448
8449 /* hr -= LnFact(new_path_len); */
8450 /* hr += LnFact(cur_path_len); */
8451
8452 new_path_len = cur_path_len;
8453
8454 hr += PHYREX_Path_Logdensity(young_ldsk,old_ldsk,1.0*tree->mmod->rad,tree);
8455
8456 new_path = PHYREX_Generate_Path(young_ldsk,old_ldsk,new_path_len,1.0*tree->mmod->rad,tree);
8457 cur_path = PHYREX_Remove_Path(young_ldsk,old_ldsk,&pos,tree);
8458 PHYREX_Insert_Path(young_ldsk,old_ldsk,new_path,pos,tree);
8459
8460 hr -= PHYREX_Path_Logdensity(young_ldsk,old_ldsk,1.0*tree->mmod->rad,tree);
8461
8462 if(tree->eval_glnL == YES)
8463 {
8464 new_glnL += PHYREX_Lk_Range(young_ldsk->disk,old_ldsk->disk,tree);
8465 tree->mmod->c_lnL = new_glnL;
8466 /* new_glnL = PHYREX_Lk(tree); */
8467 }
8468
8469 ratio += (new_glnL - cur_glnL);
8470 ratio += hr;
8471
8472 ratio = exp(ratio);
8473 alpha = MIN(1.,ratio);
8474
8475 /* PhyML_Printf("\nXXX %12f %4d %4d %12f %12f", */
8476 /* young_ldsk->disk->time - old_ldsk->disk->time, */
8477 /* cur_path_len,new_path_len,alpha,Euclidean_Dist(young_ldsk->coord,old_ldsk->coord)); */
8478
8479 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8480
8481
8482 u = Uni();
8483
8484 if(u > alpha) /* Reject */
8485 {
8486 new_path = PHYREX_Remove_Path(young_ldsk,old_ldsk,&pos,tree);
8487 PHYREX_Insert_Path(young_ldsk,old_ldsk,cur_path,pos,tree);
8488
8489 PHYREX_Update_Lindisk_List(tree);
8490 tree->mmod->c_lnL = cur_glnL;
8491
8492 ldsk = new_path;
8493 while(ldsk)
8494 {
8495 Free_Disk(ldsk->disk);
8496 ldsk_dum = ldsk;
8497 ldsk = ldsk->prev;
8498 Free_Ldisk(ldsk_dum);
8499 }
8500 }
8501 else
8502 {
8503 ldsk = cur_path;
8504 while(ldsk)
8505 {
8506 Free_Disk(ldsk->disk);
8507 ldsk_dum = ldsk;
8508 ldsk = ldsk->prev;
8509 Free_Ldisk(ldsk_dum);
8510 }
8511
8512 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_traj]++;
8513 }
8514
8515 }
8516
8517 Free(valid_disks);
8518 Free(permut);
8519 }
8520 #endif
8521
8522 /*////////////////////////////////////////////////////////////
8523 ////////////////////////////////////////////////////////////*/
8524
8525 #ifdef PHYREX
MCMC_PHYREX_Disk_Multi(t_tree * tree)8526 void MCMC_PHYREX_Disk_Multi(t_tree *tree)
8527 {
8528 phydbl u,alpha,ratio;
8529 phydbl cur_glnL, new_glnL, hr;
8530 t_dsk *disk,**target_disk,**all_disks;
8531 int i,j,block,n_all_disks,n_move_disks,*permut;
8532 int err;
8533
8534 /* if(tree->mmod->id == RW || tree->mmod->id == RRW) return; */
8535
8536 disk = NULL;
8537 new_glnL = tree->mmod->c_lnL;
8538 cur_glnL = tree->mmod->c_lnL;
8539 hr = 0.0;
8540 ratio = 0.0;
8541 block = 100;
8542 all_disks = NULL;
8543
8544 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_disk_multi]++;
8545
8546 disk = tree->young_disk->prev;
8547 n_all_disks = 0;
8548 do
8549 {
8550 if(disk->age_fixed == NO)
8551 {
8552 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8553 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
8554 all_disks[n_all_disks] = disk;
8555 n_all_disks++;
8556 }
8557
8558 disk = disk->prev;
8559 }
8560 while(disk);
8561
8562 if(!n_all_disks) return;
8563
8564 target_disk = (t_dsk **)mCalloc(n_all_disks,sizeof(t_dsk *));
8565
8566 /* n_move_disks = Rand_Int(1,1+(int)(n_all_disks/10)); */
8567 /* n_move_disks = MIN(10,(int)(1.+0.1*n_all_disks)); */
8568 n_move_disks = (int)(1+n_all_disks/20);
8569
8570 permut = Permutate(n_all_disks);
8571
8572 for(i=0;i<n_move_disks;i++)
8573 {
8574
8575 target_disk[i] = all_disks[permut[i]];
8576
8577 assert(target_disk[i]);
8578
8579 PHYREX_Store_Geo_Coord(target_disk[i]->centr);
8580
8581 if(target_disk[i]->ldsk != NULL)
8582 {
8583 for(j=0;j<tree->mmod->n_dim;j++)
8584 {
8585 err = NO;
8586 target_disk[i]->centr->lonlat[j] =
8587 Rnorm_Trunc(target_disk[i]->centr->lonlat[j],
8588 1.*tree->mmod->rad,
8589 tree->mmod->lim_do->lonlat[j],
8590 tree->mmod->lim_up->lonlat[j],&err);
8591
8592 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
8593
8594 hr -= Log_Dnorm_Trunc(target_disk[i]->centr->lonlat[j],
8595 target_disk[i]->centr->cpy->lonlat[j],
8596 1.*tree->mmod->rad,
8597 tree->mmod->lim_do->lonlat[j],
8598 tree->mmod->lim_up->lonlat[j],&err);
8599
8600 hr += Log_Dnorm_Trunc(target_disk[i]->centr->cpy->lonlat[j],
8601 target_disk[i]->centr->lonlat[j],
8602 1.*tree->mmod->rad,
8603 tree->mmod->lim_do->lonlat[j],
8604 tree->mmod->lim_up->lonlat[j],&err);
8605 }
8606 }
8607 else
8608 {
8609 for(j=0;j<tree->mmod->n_dim;j++)
8610 {
8611 err = NO;
8612 target_disk[i]->centr->lonlat[j] =
8613 Rnorm_Trunc(target_disk[i]->centr->lonlat[j],
8614 1.*tree->mmod->rad,
8615 tree->mmod->lim_do->lonlat[j],
8616 tree->mmod->lim_up->lonlat[j],&err);
8617
8618 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
8619
8620 hr -= Log_Dnorm_Trunc(target_disk[i]->centr->lonlat[j],
8621 target_disk[i]->centr->cpy->lonlat[j],
8622 1.*tree->mmod->rad,
8623 tree->mmod->lim_do->lonlat[j],
8624 tree->mmod->lim_up->lonlat[j],&err);
8625
8626 hr += Log_Dnorm_Trunc(target_disk[i]->centr->cpy->lonlat[j],
8627 target_disk[i]->centr->lonlat[j],
8628 1.*tree->mmod->rad,
8629 tree->mmod->lim_do->lonlat[j],
8630 tree->mmod->lim_up->lonlat[j],&err);
8631 }
8632 }
8633 }
8634
8635 Free(permut);
8636
8637 if(tree->eval_glnL == YES)
8638 {
8639 new_glnL = PHYREX_Lk(tree);
8640 }
8641
8642 ratio += (new_glnL - cur_glnL);
8643 ratio += hr;
8644
8645 ratio = exp(ratio);
8646 alpha = MIN(1.,ratio);
8647
8648 /* Always accept move */
8649 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8650
8651 u = Uni();
8652
8653 /* printf("\n- Delete new_glnL: %f [%f] hr: %f u:%f alpha: %f",new_glnL,cur_glnL,hr,u,alpha); */
8654
8655 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
8656
8657 if(u > alpha) /* Reject */
8658 {
8659 /* printf("- Reject"); */
8660
8661 for(i=0;i<n_move_disks;i++) PHYREX_Restore_Geo_Coord(target_disk[i]->centr);
8662
8663 tree->mmod->c_lnL = cur_glnL;
8664 }
8665 else
8666 {
8667 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_disk_multi]++;
8668 }
8669
8670
8671 Free(all_disks);
8672 Free(target_disk);
8673 }
8674 #endif
8675
8676 /*////////////////////////////////////////////////////////////
8677 ////////////////////////////////////////////////////////////*/
8678 /* Move ldsk on landscape */
8679 #ifdef PHYREX
MCMC_PHYREX_Ldsk_Multi(t_tree * tree)8680 void MCMC_PHYREX_Ldsk_Multi(t_tree *tree)
8681 {
8682 phydbl u,alpha,ratio;
8683 phydbl cur_glnL,new_glnL,hr,c;
8684 t_dsk *disk,**target_disk,**all_disks;
8685 int i,j,block,n_all_disks,n_move_ldsk,*permut;
8686 int err;
8687
8688 disk = NULL;
8689 new_glnL = tree->mmod->c_lnL;
8690 cur_glnL = tree->mmod->c_lnL;
8691 hr = 0.0;
8692 ratio = 0.0;
8693 block = 100;
8694 all_disks = NULL;
8695 c = -1.;
8696
8697 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_ldsk_multi]++;
8698
8699 if(tree->young_disk->next) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
8700 disk = tree->young_disk->prev;
8701 n_all_disks = 0;
8702 do
8703 {
8704 if(disk->ldsk != NULL && (disk->ldsk->nd != NULL && disk->ldsk->nd->tax == NO))
8705 {
8706 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8707 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
8708 all_disks[n_all_disks] = disk;
8709 n_all_disks++;
8710 }
8711 disk = disk->prev;
8712 }
8713 while(disk);
8714
8715 if(!n_all_disks) return;
8716
8717 n_move_ldsk = (int)(1+n_all_disks/20);
8718
8719 target_disk = (t_dsk **)mCalloc(n_all_disks,sizeof(t_dsk *));
8720
8721 permut = Permutate(n_all_disks);
8722
8723 for(i=0;i<n_move_ldsk;i++)
8724 {
8725 target_disk[i] = all_disks[permut[i]];
8726
8727 PHYREX_Store_Geo_Coord(target_disk[i]->ldsk->coord);
8728
8729 for(j=0;j<tree->mmod->n_dim;j++)
8730 {
8731 /* c: center; o: pos of direct ldsk ancestor */
8732 /* c = target_disk[i]->centr->lonlat[j]; */
8733 c = target_disk[i]->ldsk->coord->lonlat[j];
8734
8735 err = NO;
8736 target_disk[i]->ldsk->coord->lonlat[j] =
8737 Rnorm_Trunc(c,
8738 1.*tree->mmod->rad,
8739 tree->mmod->lim_do->lonlat[j],
8740 tree->mmod->lim_up->lonlat[j],&err);
8741
8742 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
8743
8744 hr -= Log_Dnorm_Trunc(target_disk[i]->ldsk->coord->lonlat[j],
8745 c,
8746 1.*tree->mmod->rad,
8747 tree->mmod->lim_do->lonlat[j],
8748 tree->mmod->lim_up->lonlat[j],&err);
8749
8750 /* hr += Log_Dnorm_Trunc(target_disk[i]->ldsk->coord->cpy->lonlat[j], */
8751 /* c, */
8752 /* 1.*tree->mmod->rad, */
8753 /* tree->mmod->lim_do->lonlat[j], */
8754 /* tree->mmod->lim_up->lonlat[j],&err); */
8755 hr += Log_Dnorm_Trunc(c,
8756 target_disk[i]->ldsk->coord->lonlat[j],
8757 1.*tree->mmod->rad,
8758 tree->mmod->lim_do->lonlat[j],
8759 tree->mmod->lim_up->lonlat[j],&err);
8760 }
8761 }
8762
8763 Free(permut);
8764
8765 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
8766
8767 ratio += (new_glnL - cur_glnL);
8768 ratio += hr;
8769
8770 ratio = exp(ratio);
8771 alpha = MIN(1.,ratio);
8772
8773 /* Always accept move */
8774 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8775
8776 u = Uni();
8777
8778 /* printf("\n- Move_ldsk %15f",new_glnL-cur_glnL); */
8779
8780 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
8781
8782 if(u > alpha) /* Reject */
8783 {
8784 /* printf("- Reject"); */
8785
8786 for(i=0;i<n_move_ldsk;i++) PHYREX_Restore_Geo_Coord(target_disk[i]->ldsk->coord);
8787
8788 tree->mmod->c_lnL = cur_glnL;
8789 }
8790 else
8791 {
8792 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_ldsk_multi]++;
8793 }
8794
8795
8796 Free(all_disks);
8797 Free(target_disk);
8798 }
8799 #endif
8800
8801 /*////////////////////////////////////////////////////////////
8802 ////////////////////////////////////////////////////////////*/
8803
8804 #ifdef PHYREX
MCMC_PHYREX_Ldsk_And_Disk(t_tree * tree)8805 void MCMC_PHYREX_Ldsk_And_Disk(t_tree *tree)
8806 {
8807 phydbl u,alpha,ratio;
8808 phydbl cur_glnL, new_glnL, hr;
8809 phydbl cur_rad,new_rad;
8810 t_dsk *disk,**target_disk,**all_disks;
8811 int i,j,block,n_all_disks,n_move_ldsk,*permut;
8812 int err;
8813
8814 /* if(tree->mmod->id == RW || tree->mmod->id == RRW) return; */
8815
8816 disk = NULL;
8817 new_glnL = tree->mmod->c_lnL;
8818 cur_glnL = tree->mmod->c_lnL;
8819 hr = 0.0;
8820 ratio = 0.0;
8821 block = 100;
8822 all_disks = NULL;
8823 cur_rad = tree->mmod->rad;
8824 new_rad = tree->mmod->rad;
8825
8826 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_ldsk_and_disk]++;
8827
8828 if(tree->young_disk->next) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
8829 disk = tree->young_disk->prev;
8830 n_all_disks = 0;
8831 do
8832 {
8833 if(disk->ldsk != NULL && disk->ldsk->n_next >= 1 && (disk->ldsk->nd != NULL && disk->ldsk->nd->tax == NO))
8834 {
8835 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8836 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
8837 all_disks[n_all_disks] = disk;
8838 n_all_disks++;
8839 }
8840 disk = disk->prev;
8841 }
8842 while(disk);
8843
8844 if(!n_all_disks) return;
8845
8846 n_move_ldsk = (int)(1+n_all_disks/20);
8847 /* n_move_ldsk = n_all_disks; */
8848
8849 target_disk = (t_dsk **)mCalloc(n_all_disks,sizeof(t_dsk *));
8850
8851 permut = Permutate(n_all_disks);
8852
8853
8854 new_rad = cur_rad * exp(0.5*(Uni()-.5));
8855 hr += log(new_rad/cur_rad);
8856
8857
8858 for(i=0;i<n_move_ldsk;i++)
8859 {
8860 target_disk[i] = all_disks[permut[i]];
8861
8862 PHYREX_Store_Geo_Coord(target_disk[i]->ldsk->coord);
8863 PHYREX_Store_Geo_Coord(target_disk[i]->centr);
8864
8865 for(j=0;j<tree->mmod->n_dim;j++)
8866 {
8867 err = NO;
8868
8869 target_disk[i]->centr->lonlat[j] =
8870 Rnorm_Trunc(target_disk[i]->ldsk->coord->lonlat[j],
8871 1.*new_rad,
8872 tree->mmod->lim_do->lonlat[j],
8873 tree->mmod->lim_up->lonlat[j],&err);
8874
8875 target_disk[i]->ldsk->coord->lonlat[j] =
8876 Rnorm_Trunc(target_disk[i]->centr->lonlat[j],
8877 1.*new_rad,
8878 tree->mmod->lim_do->lonlat[j],
8879 tree->mmod->lim_up->lonlat[j],&err);
8880
8881
8882 hr -= Log_Dnorm_Trunc(target_disk[i]->centr->lonlat[j],
8883 target_disk[i]->ldsk->coord->cpy->lonlat[j],
8884 1.*new_rad,
8885 tree->mmod->lim_do->lonlat[j],
8886 tree->mmod->lim_up->lonlat[j],&err);
8887
8888 hr += Log_Dnorm_Trunc(target_disk[i]->centr->cpy->lonlat[j],
8889 target_disk[i]->ldsk->coord->lonlat[j],
8890 1.*new_rad,
8891 tree->mmod->lim_do->lonlat[j],
8892 tree->mmod->lim_up->lonlat[j],&err);
8893
8894 hr -= Log_Dnorm_Trunc(target_disk[i]->ldsk->coord->lonlat[j],
8895 target_disk[i]->centr->lonlat[j],
8896 1.*new_rad,
8897 tree->mmod->lim_do->lonlat[j],
8898 tree->mmod->lim_up->lonlat[j],&err);
8899
8900 hr += Log_Dnorm_Trunc(target_disk[i]->ldsk->coord->cpy->lonlat[j],
8901 target_disk[i]->centr->cpy->lonlat[j],
8902 1.*new_rad,
8903 tree->mmod->lim_do->lonlat[j],
8904 tree->mmod->lim_up->lonlat[j],&err);
8905
8906 }
8907 }
8908
8909
8910 Free(permut);
8911
8912 if(tree->eval_glnL == YES) new_glnL = PHYREX_Lk(tree);
8913
8914 ratio += (new_glnL - cur_glnL);
8915 ratio += hr;
8916
8917 ratio = exp(ratio);
8918 alpha = MIN(1.,ratio);
8919
8920 /* Always accept move */
8921 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
8922
8923 u = Uni();
8924
8925 /* printf("\n- Move_ldsk %15f",new_glnL-cur_glnL); */
8926
8927 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
8928
8929 if(u > alpha) /* Reject */
8930 {
8931 /* printf("- Reject"); */
8932 for(i=0;i<n_move_ldsk;i++) PHYREX_Restore_Geo_Coord(target_disk[i]->ldsk->coord);
8933 for(i=0;i<n_move_ldsk;i++) PHYREX_Restore_Geo_Coord(target_disk[i]->centr);
8934
8935 tree->mmod->rad = cur_rad;
8936 tree->mmod->c_lnL = cur_glnL;
8937 }
8938 else
8939 {
8940 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_ldsk_and_disk]++;
8941 }
8942
8943 Free(all_disks);
8944 Free(target_disk);
8945 }
8946 #endif
8947
8948 /*////////////////////////////////////////////////////////////
8949 ////////////////////////////////////////////////////////////*/
8950
8951 #ifdef PHYREX
MCMC_PHYREX_Ldsk_Given_Disk(t_tree * tree)8952 void MCMC_PHYREX_Ldsk_Given_Disk(t_tree *tree)
8953 {
8954 phydbl u,alpha,ratio,hr,c;
8955 phydbl cur_glnL, new_glnL;
8956 phydbl K;
8957 t_dsk *disk,**all_disks;
8958 int i,j,err,n_all_disks,block,n_move_ldsk,*permut;
8959
8960 /* if(tree->mmod->id == RW || tree->mmod->id == RRW) return; */
8961
8962 block = 100;
8963 all_disks = NULL;
8964 n_all_disks = 0;
8965 c = -1.;
8966 K = tree->mcmc->tune_move[tree->mcmc->num_move_phyrex_ldsk_given_disk];
8967
8968 disk = tree->young_disk->prev;
8969 do
8970 {
8971 if(disk->ldsk != NULL)
8972 {
8973 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
8974 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
8975 all_disks[n_all_disks] = disk;
8976 n_all_disks++;
8977 }
8978 disk = disk->prev;
8979 }
8980 while(disk);
8981
8982 if(!n_all_disks) return;
8983
8984 /* n_move_ldsk = Rand_Int(1,1+(int)(n_all_disks/5)); */
8985 /* n_move_ldsk = MIN(10,(int)(1.+0.1*n_all_disks)); */
8986 /* n_move_ldsk = 1+(int)(n_all_disks/2); */
8987 n_move_ldsk = n_all_disks;
8988
8989 permut = Permutate(n_all_disks);
8990 cur_glnL = tree->mmod->c_lnL;
8991 new_glnL = tree->mmod->c_lnL;
8992 for(i=0;i<n_move_ldsk;i++)
8993 {
8994 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_ldsk_given_disk]++;
8995
8996 disk = all_disks[permut[i]];
8997
8998 hr = 0.0;
8999 ratio = 0.0;
9000
9001 PHYREX_Store_Geo_Coord(disk->ldsk->coord);
9002
9003
9004 new_glnL = cur_glnL;
9005 if(tree->eval_glnL == YES)
9006 {
9007 if(disk->ldsk->prev != NULL) new_glnL -= PHYREX_Lk_Range(disk,disk->ldsk->prev->disk,tree);
9008 else new_glnL -= PHYREX_Lk_Core(disk,tree);
9009 }
9010
9011 for(j=0;j<tree->mmod->n_dim;j++)
9012 {
9013 c = disk->centr->lonlat[j];
9014
9015 err = NO;
9016 disk->ldsk->coord->lonlat[j] =
9017 Rnorm_Trunc(c,
9018 K*tree->mmod->rad,
9019 tree->mmod->lim_do->lonlat[j],
9020 tree->mmod->lim_up->lonlat[j],&err);
9021
9022 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
9023
9024 hr -= Log_Dnorm_Trunc(disk->ldsk->coord->lonlat[j],
9025 c,
9026 K*tree->mmod->rad,
9027 tree->mmod->lim_do->lonlat[j],
9028 tree->mmod->lim_up->lonlat[j],&err);
9029
9030 hr += Log_Dnorm_Trunc(disk->ldsk->coord->cpy->lonlat[j],
9031 c,
9032 K*tree->mmod->rad,
9033 tree->mmod->lim_do->lonlat[j],
9034 tree->mmod->lim_up->lonlat[j],&err);
9035
9036 }
9037
9038 if(tree->eval_glnL == YES)
9039 {
9040 if(disk->ldsk->prev != NULL) new_glnL += PHYREX_Lk_Range(disk,disk->ldsk->prev->disk,tree);
9041 else new_glnL += PHYREX_Lk_Core(disk,tree);
9042 tree->mmod->c_lnL = new_glnL;
9043 /* new_glnL = PHYREX_Lk(tree); */
9044 }
9045
9046 ratio += (new_glnL - cur_glnL);
9047 ratio += hr;
9048
9049 ratio = exp(ratio);
9050 alpha = MIN(1.,ratio);
9051
9052 u = Uni();
9053
9054 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9055
9056 if(u > alpha) /* Reject */
9057 {
9058 PHYREX_Restore_Geo_Coord(disk->ldsk->coord);
9059 tree->mmod->c_lnL = cur_glnL;
9060 }
9061 else
9062 {
9063 cur_glnL = new_glnL;
9064 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_ldsk_given_disk]++;
9065 }
9066 }
9067
9068 Free(permut);
9069 Free(all_disks);
9070 }
9071 #endif
9072
9073 /*////////////////////////////////////////////////////////////
9074 ////////////////////////////////////////////////////////////*/
9075
9076 #ifdef PHYREX
MCMC_PHYREX_Disk_Given_Ldsk(t_tree * tree)9077 void MCMC_PHYREX_Disk_Given_Ldsk(t_tree *tree)
9078 {
9079 phydbl u,alpha,ratio,hr;
9080 phydbl cur_glnL, new_glnL;
9081 t_dsk *disk,**all_disks;
9082 int i,j,n_all_disks,block,n_move_ldsk,*permut;
9083
9084 /* if(tree->mmod->id == RW || tree->mmod->id == RRW) return; */
9085
9086 block = 100;
9087 all_disks = NULL;
9088 n_all_disks = 0;
9089
9090
9091 disk = tree->young_disk->prev;
9092 do
9093 {
9094 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
9095 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
9096 all_disks[n_all_disks] = disk;
9097 n_all_disks++;
9098 disk = disk->prev;
9099 }
9100 while(disk);
9101
9102 if(!n_all_disks) return;
9103
9104 /* n_move_ldsk = Rand_Int(1,1+(int)(n_all_disks/10)); */
9105 /* n_move_ldsk = MIN(10,(int)(1+0.1*n_all_disks)); */
9106 /* n_move_ldsk = (int)(1+n_all_disks/2); */
9107 n_move_ldsk = n_all_disks;
9108
9109 permut = Permutate(n_all_disks);
9110
9111
9112 for(i=0;i<n_move_ldsk;i++)
9113 {
9114 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_disk_given_ldsk]++;
9115
9116 disk = all_disks[permut[i]];
9117
9118 hr = 0.0;
9119 ratio = 0.0;
9120 cur_glnL = tree->mmod->c_lnL;
9121 new_glnL = tree->mmod->c_lnL;
9122
9123 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Core(disk,tree);
9124
9125 PHYREX_Store_Geo_Coord(disk->centr);
9126
9127 for(j=0;j<tree->mmod->n_dim;j++)
9128 {
9129 /* if(disk->ldsk == NULL) */
9130 disk->centr->lonlat[j] = Uni()*(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j])+tree->mmod->lim_do->lonlat[j];
9131
9132 /* else */
9133 /* { */
9134 /* /\* c: center; o: pos of direct ldsk ancestor *\/ */
9135 /* c = disk->ldsk->coord->lonlat[j]; */
9136
9137 /* err = NO; */
9138 /* disk->centr->lonlat[j] = */
9139 /* Rnorm_Trunc(c, */
9140 /* 2.*tree->mmod->rad, */
9141 /* 0.0, */
9142 /* tree->mmod->lim->lonlat[j],&err); */
9143
9144 /* if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__); */
9145
9146 /* hr -= Log_Dnorm_Trunc(disk->centr->lonlat[j], */
9147 /* c, */
9148 /* 2.*tree->mmod->rad, */
9149 /* 0.0, */
9150 /* tree->mmod->lim->lonlat[j],&err); */
9151
9152 /* hr += Log_Dnorm_Trunc(disk->centr->cpy->lonlat[j], */
9153 /* c, */
9154 /* 2.*tree->mmod->rad, */
9155 /* 0.0, */
9156 /* tree->mmod->lim->lonlat[j],&err); */
9157 /* } */
9158 }
9159
9160 if(tree->eval_glnL == YES)
9161 {
9162 new_glnL += PHYREX_Lk_Core(disk,tree);
9163 tree->mmod->c_lnL = new_glnL;
9164 }
9165
9166 ratio += (new_glnL - cur_glnL);
9167 ratio += hr;
9168
9169 ratio = exp(ratio);
9170 alpha = MIN(1.,ratio);
9171
9172 u = Uni();
9173
9174 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9175
9176 if(u > alpha) /* Reject */
9177 {
9178 PHYREX_Restore_Geo_Coord(disk->centr);
9179 tree->mmod->c_lnL = cur_glnL;
9180 }
9181 else
9182 {
9183 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_disk_given_ldsk]++;
9184 }
9185 }
9186
9187 Free(permut);
9188 Free(all_disks);
9189 }
9190 #endif
9191
9192 /*////////////////////////////////////////////////////////////
9193 ////////////////////////////////////////////////////////////*/
9194
9195 #ifdef PHYREX
9196
MCMC_PHYREX_Indel_Hit_Serial(t_tree * tree)9197 void MCMC_PHYREX_Indel_Hit_Serial(t_tree *tree)
9198 {
9199 t_dsk *disk,*new_disk,*target_disk,*young_disk,*old_disk,**valid_disks;
9200 t_ldsk *young_ldsk, *old_ldsk, *new_ldsk;
9201 int i,j,n_trials,dir_old_young,err,block;
9202 phydbl ratio, u, alpha, hr, type;
9203 phydbl cur_glnL, new_glnL;
9204 phydbl T,t,pindel;
9205 int n_valid_disks,num_prec_issue;
9206
9207 cur_glnL = tree->mmod->c_lnL;
9208 new_glnL = tree->mmod->c_lnL;
9209 hr = 0.0;
9210 ratio = 0.0;
9211 type = -1.0;
9212 n_trials = 1 + (int)(0.1*PHYREX_Total_Number_Of_Intervals(tree));
9213 T = PHYREX_Tree_Height(tree);
9214 pindel = 0.5;
9215 block = 50;
9216 num_prec_issue = NO;
9217
9218 for(i=0;i<n_trials;i++)
9219 {
9220
9221 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_hit_serial]++;
9222
9223 cur_glnL = tree->mmod->c_lnL;
9224 new_glnL = tree->mmod->c_lnL;
9225 hr = 0.0;
9226 ratio = 0.0;
9227
9228 type = Uni();
9229
9230 if(type < pindel) /* Insert */
9231 {
9232 do
9233 {
9234 num_prec_issue = NO;
9235 t = Uni()*(tree->young_disk->time-T) + T;
9236 disk = tree->young_disk->prev;
9237 while(disk && disk->time > t) disk = disk->prev;
9238
9239 if(!(disk->time != t))
9240 {
9241 PhyML_Printf("\n. Numerical precision issue in indel_hit_serial");
9242 PhyML_Printf("\n. t=%g disk->time=%g",t,disk->time);
9243 num_prec_issue = YES;
9244 }
9245 }while(num_prec_issue == YES);
9246
9247 assert(disk->next);
9248
9249 hr -= log(1./(tree->young_disk->time-T));
9250
9251 young_disk = disk->next;
9252 assert(young_disk->n_ldsk_a);
9253
9254 old_disk = disk;
9255 assert(old_disk->n_ldsk_a);
9256
9257 new_disk = PHYREX_Make_Disk_Event(tree->mmod->n_dim,tree->n_otu);
9258 PHYREX_Init_Disk_Event(new_disk,tree->mmod->n_dim,tree->mmod);
9259 new_disk->time = t;
9260
9261 /* Which lineage is going to be hit ? */
9262 hr -= log(1./PHYREX_Number_Of_Outgoing_Ldsks(young_disk));
9263
9264 /* young_ldsk = old_disk->ldsk_a[hit_ldsk_idx]; */
9265 young_ldsk = PHYREX_Random_Select_Outgoing_Ldsk(young_disk);
9266 old_ldsk = young_ldsk->prev;
9267
9268 assert(young_disk != old_ldsk->disk);
9269
9270 new_glnL = cur_glnL;
9271 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(young_ldsk->disk,old_ldsk->disk,tree);
9272
9273 /* Direction from old to young ldsk */
9274 dir_old_young = PHYREX_Get_Next_Direction(young_ldsk,old_ldsk);
9275 assert(dir_old_young != -1);
9276
9277 /* Make and initialize new ldsk */
9278 new_ldsk = PHYREX_Make_Lindisk_Node(tree->mmod->n_dim);
9279 PHYREX_Init_Lindisk_Node(new_ldsk,new_disk,tree->mmod->n_dim);
9280 PHYREX_Make_Lindisk_Next(new_ldsk);
9281 new_disk->ldsk = new_ldsk;
9282
9283 /* Connect it */
9284 new_ldsk->prev = old_ldsk;
9285 new_ldsk->next[0] = young_ldsk;
9286 young_ldsk->prev = new_ldsk;
9287 old_ldsk->next[dir_old_young] = new_ldsk;
9288
9289 /* Insert disk */
9290 PHYREX_Insert_Disk(new_disk,tree);
9291
9292 assert(new_disk->next == young_disk);
9293 assert(new_disk->prev == old_disk);
9294
9295 for(j=0;j<tree->mmod->n_dim;j++)
9296 {
9297 new_disk->centr->lonlat[j] = Uni()*(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j])+tree->mmod->lim_do->lonlat[j];
9298 hr -= log(1./(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j]));
9299
9300 err = NO;
9301 new_ldsk->coord->lonlat[j] = Rnorm_Trunc(new_disk->centr->lonlat[j],
9302 1.0*tree->mmod->rad,
9303 tree->mmod->lim_do->lonlat[j],
9304 tree->mmod->lim_up->lonlat[j],&err);
9305
9306 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
9307
9308 hr -= Log_Dnorm_Trunc(new_ldsk->coord->lonlat[j],
9309 new_disk->centr->lonlat[j],
9310 1.0*tree->mmod->rad,
9311 tree->mmod->lim_do->lonlat[j],
9312 tree->mmod->lim_up->lonlat[j],&err);
9313 }
9314
9315
9316
9317 if(tree->eval_glnL == YES)
9318 {
9319 new_glnL += PHYREX_Lk_Range(young_ldsk->disk,old_ldsk->disk,tree);
9320 tree->mmod->c_lnL = new_glnL;
9321 /* new_glnL = PHYREX_Lk(tree); */
9322 }
9323
9324
9325 n_valid_disks = 0;
9326 disk = tree->young_disk;
9327 while(disk->prev)
9328 {
9329 if(disk->ldsk && disk->ldsk->n_next == 1) n_valid_disks++;
9330 disk = disk->prev;
9331 }
9332
9333 assert(n_valid_disks);
9334 hr += log(1./n_valid_disks);
9335
9336 ratio = (new_glnL - cur_glnL);
9337 ratio += hr;
9338
9339 ratio = exp(ratio);
9340 alpha = MIN(1.,ratio);
9341
9342 /* PhyML_Printf("\n+ new_glnL: %f cur_glnL: %f hr: %f alpha: %f",new_glnL,cur_glnL,hr,alpha); */
9343
9344 /* Always accept move */
9345 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9346
9347 u = Uni();
9348
9349 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9350
9351 if(u > alpha) /* Reject */
9352 {
9353 old_ldsk->next[dir_old_young] = young_ldsk;
9354 young_ldsk->prev = old_ldsk;
9355
9356 PHYREX_Remove_Disk(new_disk);
9357
9358 assert(old_disk->next == young_disk);
9359 assert(young_disk->prev == old_disk);
9360
9361 PHYREX_Lk_Range(young_ldsk->disk,old_ldsk->disk,tree);
9362 tree->mmod->c_lnL = cur_glnL;
9363 /* PHYREX_Lk(tree); */
9364
9365 Free_Disk(new_disk);
9366 Free_Ldisk(new_ldsk);
9367
9368 }
9369 else
9370 {
9371 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_hit_serial]++;
9372 }
9373 }
9374
9375 else /* Remove hit */
9376
9377 {
9378
9379 disk = tree->young_disk->prev;
9380 valid_disks = NULL;
9381 n_valid_disks = 0;
9382 do
9383 {
9384 if(disk->ldsk && disk->ldsk->n_next == 1)
9385 {
9386 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
9387 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
9388 valid_disks[n_valid_disks] = disk;
9389 n_valid_disks++;
9390 }
9391 disk = disk->prev;
9392 }
9393 while(disk);
9394
9395 if(n_valid_disks == 0) continue;
9396 hr -= log(1./n_valid_disks);
9397
9398 target_disk = valid_disks[Rand_Int(0,n_valid_disks-1)];
9399 Free(valid_disks);
9400
9401 young_disk = target_disk->next;
9402 old_disk = target_disk->prev;
9403
9404 assert(target_disk->age_fixed == NO);
9405
9406 old_ldsk = target_disk->ldsk->prev;
9407 young_ldsk = target_disk->ldsk->next[0];
9408
9409 dir_old_young = PHYREX_Get_Next_Direction(target_disk->ldsk,old_ldsk);
9410 assert(dir_old_young != -1);
9411
9412 /* Part of the Hastings ratio corresponding to the probability of selecting */
9413 /* one of target_disk->n_ldsk_a to be hit (reverse move) */
9414 hr += log(1./target_disk->next->n_ldsk_a);
9415
9416
9417 /* Density for position of the displaced ldsk */
9418 for(j=0;j<tree->mmod->n_dim;j++)
9419 {
9420 hr += log(1./(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j]));
9421
9422 hr += Log_Dnorm_Trunc(target_disk->ldsk->coord->lonlat[j],
9423 target_disk->centr->lonlat[j],
9424 1.0*tree->mmod->rad,
9425 tree->mmod->lim_do->lonlat[j],
9426 tree->mmod->lim_up->lonlat[j],&err);
9427 }
9428
9429
9430 assert(target_disk->next);
9431
9432 new_glnL = cur_glnL;
9433 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(target_disk->next,target_disk->ldsk->prev->disk,tree);
9434
9435 /* New connections between old_ldsk and young_ldsk */
9436 old_ldsk->next[dir_old_young] = young_ldsk;
9437 young_ldsk->prev = old_ldsk;
9438
9439 hr += log(1./(tree->young_disk->time-T));
9440
9441 PHYREX_Remove_Disk(target_disk);
9442
9443 if(tree->eval_glnL == YES)
9444 {
9445 new_glnL += PHYREX_Lk_Range(target_disk->next,target_disk->ldsk->prev->disk,tree);
9446 tree->mmod->c_lnL = new_glnL;
9447 /* new_glnL = PHYREX_Lk(tree); */
9448 }
9449
9450
9451 ratio = (new_glnL - cur_glnL);
9452 ratio += hr;
9453
9454 ratio = exp(ratio);
9455 alpha = MIN(1.,ratio);
9456
9457 /* PhyML_Printf("\n- new_glnL: %f cur_glnL: %f hr: %f alpha: %f target->time: %f",new_glnL,cur_glnL,hr,alpha,target_disk->time); */
9458
9459 /* Always accept move */
9460 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9461
9462 u = Uni();
9463
9464 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9465
9466 if(u > alpha) /* Reject */
9467 {
9468 PHYREX_Insert_Disk(target_disk,tree);
9469
9470 assert(target_disk->next == young_disk);
9471 assert(target_disk->prev == old_disk);
9472
9473 old_ldsk->next[dir_old_young] = target_disk->ldsk;
9474 young_ldsk->prev = target_disk->ldsk;
9475
9476 PHYREX_Lk_Range(target_disk->next,target_disk->ldsk->prev->disk,tree);
9477 tree->mmod->c_lnL = cur_glnL;
9478 /* PHYREX_Lk(tree); */
9479 }
9480 else
9481 {
9482 Free_Ldisk(target_disk->ldsk);
9483 Free_Disk(target_disk);
9484 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_hit_serial]++;
9485 }
9486 }
9487 }
9488
9489 }
9490 #endif
9491
9492 /*////////////////////////////////////////////////////////////
9493 ////////////////////////////////////////////////////////////*/
9494
9495 #ifdef PHYREX
MCMC_PHYREX_Indel_Disk_Serial(t_tree * tree)9496 void MCMC_PHYREX_Indel_Disk_Serial(t_tree *tree)
9497 {
9498 t_dsk *disk,*new_disk,*target_disk,**valid_disks,*young_disk,*old_disk;
9499 int i,j,n_trials,n_valid_disks,block,num_prec_issue;
9500 phydbl ratio, u, alpha, hr, type;
9501 phydbl cur_glnL, new_glnL;
9502 phydbl log_lk_centr;
9503 phydbl T,t,pindel;
9504
9505 cur_glnL = tree->mmod->c_lnL;
9506 new_glnL = tree->mmod->c_lnL;
9507 hr = 0.0;
9508 ratio = 0.0;
9509 type = -1.0;
9510 n_trials = (int)(1.+0.1*PHYREX_Total_Number_Of_Intervals(tree));
9511 T = PHYREX_Tree_Height(tree);
9512 pindel = 0.5;
9513 disk = NULL;
9514 new_disk = NULL;
9515 target_disk = NULL;
9516 young_disk = NULL;
9517 old_disk = NULL;
9518 block = 50;
9519 num_prec_issue = NO;
9520 disk = NULL;
9521
9522 log_lk_centr = 0.0;
9523 for(j=0;j<tree->mmod->n_dim;j++) log_lk_centr += log(1./(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j]));
9524
9525 for(i=0;i<n_trials;i++)
9526 {
9527 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_indel_disk_serial]++;
9528
9529 cur_glnL = tree->mmod->c_lnL;
9530 new_glnL = tree->mmod->c_lnL;
9531 hr = 0.0;
9532 ratio = 0.0;
9533
9534 type = Uni();
9535
9536 if(type < pindel) /* Insert */
9537 {
9538 do
9539 {
9540 num_prec_issue = NO;
9541 t = Uni()*(tree->young_disk->time-T) + T;
9542 disk = tree->young_disk->prev;
9543 while(disk && disk->time > t) disk = disk->prev;
9544
9545 if(!(disk->time != t))
9546 {
9547 PhyML_Printf("\n. Numerical precision issue in indel_disk_serial");
9548 PhyML_Printf("\n. t=%g disk->time=%g diff=%d",t,disk->time,t-disk->time);
9549 num_prec_issue = YES;
9550 }
9551 }while(num_prec_issue == YES);
9552
9553 young_disk = disk->next;
9554 old_disk = disk;
9555
9556 assert(disk->next);
9557
9558 hr -= log(1./(tree->young_disk->time-T));
9559 hr -= log_lk_centr;
9560
9561 new_glnL = cur_glnL;
9562 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(young_disk,old_disk,tree);
9563
9564 new_disk = PHYREX_Make_Disk_Event(tree->mmod->n_dim,tree->n_otu);
9565 PHYREX_Init_Disk_Event(new_disk,tree->mmod->n_dim,tree->mmod);
9566 new_disk->time = t;
9567 PHYREX_Insert_Disk(new_disk,tree);
9568
9569 assert(new_disk->next == young_disk);
9570 assert(new_disk->prev == old_disk);
9571
9572 for(j=0;j<tree->mmod->n_dim;j++) new_disk->centr->lonlat[j] = Uni()*(tree->mmod->lim_up->lonlat[j]-tree->mmod->lim_do->lonlat[j])+tree->mmod->lim_do->lonlat[j];
9573
9574 n_valid_disks = 0;
9575 disk = tree->young_disk->prev;
9576 do
9577 {
9578 if(!disk->ldsk && disk->age_fixed == NO) n_valid_disks++;
9579 disk = disk->prev;
9580 }
9581 while(disk);
9582
9583 assert(n_valid_disks);
9584 hr += log(1./n_valid_disks);
9585
9586 if(tree->eval_glnL == YES)
9587 {
9588 new_glnL += PHYREX_Lk_Range(young_disk,old_disk,tree);
9589 tree->mmod->c_lnL = new_glnL;
9590 /* new_glnL = PHYREX_Lk(tree); */
9591 }
9592
9593 ratio = (new_glnL - cur_glnL);
9594 ratio += hr;
9595
9596 ratio = exp(ratio);
9597 alpha = MIN(1.,ratio);
9598
9599 /* Always accept move */
9600 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9601
9602 u = Uni();
9603
9604 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9605
9606 if(u > alpha) /* Reject */
9607 {
9608 PHYREX_Remove_Disk(new_disk);
9609 Free_Disk(new_disk);
9610 tree->mmod->c_lnL = cur_glnL;
9611 }
9612 else
9613 {
9614 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk_serial]++;
9615 }
9616 }
9617 else /* Remove disk */
9618 {
9619 disk = tree->young_disk->prev;
9620 valid_disks = NULL;
9621 n_valid_disks = 0;
9622 do
9623 {
9624 if(!disk->ldsk && disk->age_fixed == NO)
9625 {
9626 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
9627 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
9628 valid_disks[n_valid_disks] = disk;
9629 n_valid_disks++;
9630 }
9631 disk = disk->prev;
9632 }
9633 while(disk);
9634
9635 if(n_valid_disks == 0) continue;
9636
9637 assert(n_valid_disks);
9638 hr -= log(1./n_valid_disks);
9639
9640 target_disk = valid_disks[Rand_Int(0,n_valid_disks-1)];
9641 Free(valid_disks);
9642
9643 hr += log(1./(tree->young_disk->time-T));
9644 hr += log_lk_centr;
9645
9646 assert(target_disk->next->prev);
9647
9648 new_glnL = cur_glnL;
9649 if(tree->eval_glnL == YES) new_glnL -= PHYREX_Lk_Range(target_disk->next,target_disk->prev,tree);
9650
9651 PHYREX_Remove_Disk(target_disk);
9652 assert(target_disk->next->prev);
9653
9654 if(tree->eval_glnL == YES)
9655 {
9656 new_glnL += PHYREX_Lk_Range(target_disk->next,target_disk->prev,tree);
9657 tree->mmod->c_lnL = new_glnL;
9658 /* new_glnL = PHYREX_Lk(tree); */
9659 }
9660
9661 ratio = (new_glnL - cur_glnL);
9662 ratio += hr;
9663
9664 ratio = exp(ratio);
9665 alpha = MIN(1.,ratio);
9666
9667
9668 /* Always accept move */
9669 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9670
9671 u = Uni();
9672
9673 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9674
9675 if(u > alpha) /* Reject */
9676 {
9677 PHYREX_Insert_Disk(target_disk,tree);
9678 tree->mmod->c_lnL = cur_glnL;
9679 }
9680 else
9681 {
9682 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_indel_disk_serial]++;
9683 Free_Disk(target_disk);
9684 }
9685 }
9686 }
9687 }
9688
9689 #endif
9690
9691 /*////////////////////////////////////////////////////////////
9692 ////////////////////////////////////////////////////////////*/
9693 // Add or remove hit but leave corresponding disk unchanged
9694 #ifdef PHYREX
MCMC_PHYREX_Add_Remove_Jump(t_tree * tree)9695 void MCMC_PHYREX_Add_Remove_Jump(t_tree *tree)
9696 {
9697 t_dsk *disk,*target_disk,**valid_disks;
9698 int i,j,n_trials,n_valid_disks,block,err;
9699 phydbl ratio, u, alpha, hr;
9700 phydbl cur_glnL, new_glnL;
9701 int target_ldsk_idx,dir_next;
9702 t_ldsk *new_ldsk,*target_ldsk;
9703
9704
9705 n_trials = (int)(1.+0.1*PHYREX_Total_Number_Of_Intervals(tree));
9706 block = 50;
9707
9708 for(i=0;i<n_trials;++i)
9709 {
9710 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_add_remove_jump]++;
9711
9712 cur_glnL = tree->mmod->c_lnL;
9713 new_glnL = tree->mmod->c_lnL;
9714 hr = 0.0;
9715 ratio = 0.0;
9716 disk = NULL;
9717 target_disk = NULL;
9718 target_ldsk = NULL;
9719 new_ldsk = NULL;
9720
9721 disk = tree->young_disk->prev;
9722 valid_disks = NULL;
9723 n_valid_disks = 0;
9724 do
9725 {
9726 if(disk->prev != NULL &&
9727 disk->age_fixed == NO &&
9728 ((disk->ldsk != NULL && disk->ldsk->n_next == 1) || disk->ldsk == NULL))
9729 {
9730 if(!n_valid_disks) valid_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
9731 else if(!(n_valid_disks%block)) valid_disks = (t_dsk **)mRealloc(valid_disks,n_valid_disks+block,sizeof(t_dsk *));
9732 valid_disks[n_valid_disks] = disk;
9733 n_valid_disks++;
9734 }
9735 disk = disk->prev;
9736 }
9737 while(disk);
9738
9739 if(n_valid_disks == 0) return;
9740
9741 target_disk = valid_disks[Rand_Int(0,n_valid_disks-1)];
9742 Free(valid_disks);
9743
9744
9745 if(target_disk->ldsk == NULL) // Add jump
9746 {
9747 assert(target_disk->n_ldsk_a);
9748 target_ldsk_idx = Rand_Int(0,target_disk->n_ldsk_a-1);
9749 target_ldsk = target_disk->ldsk_a[target_ldsk_idx];
9750
9751 hr -= log(1./(phydbl)target_disk->n_ldsk_a);
9752
9753 new_glnL = cur_glnL;
9754 if(tree->eval_glnL == YES)
9755 {
9756 assert(target_disk->next);
9757 assert(target_disk->prev);
9758 new_glnL -= PHYREX_Lk_Range(target_disk,target_ldsk->prev->disk,tree);
9759 }
9760
9761
9762 dir_next = PHYREX_Get_Next_Direction(target_ldsk,target_ldsk->prev);
9763 assert(dir_next != -1);
9764
9765 /* Make and initialize new ldsk */
9766 new_ldsk = PHYREX_Make_Lindisk_Node(tree->mmod->n_dim);
9767 PHYREX_Init_Lindisk_Node(new_ldsk,target_disk,tree->mmod->n_dim);
9768 PHYREX_Make_Lindisk_Next(new_ldsk);
9769 target_disk->ldsk = new_ldsk;
9770
9771
9772 new_ldsk->prev = target_ldsk->prev;;
9773 new_ldsk->next[0] = target_ldsk;
9774 new_ldsk->prev->next[dir_next] = new_ldsk;
9775 target_ldsk->prev = new_ldsk;
9776
9777 for(j=0;j<tree->mmod->n_dim;j++)
9778 {
9779 err = NO;
9780 new_ldsk->coord->lonlat[j] = Rnorm_Trunc(target_disk->centr->lonlat[j],
9781 1.*tree->mmod->rad,
9782 tree->mmod->lim_do->lonlat[j],
9783 tree->mmod->lim_up->lonlat[j],&err);
9784
9785 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
9786
9787 hr -= Log_Dnorm_Trunc(new_ldsk->coord->lonlat[j],
9788 target_disk->centr->lonlat[j],
9789 1.*tree->mmod->rad,
9790 tree->mmod->lim_do->lonlat[j],
9791 tree->mmod->lim_up->lonlat[j],&err);
9792
9793 }
9794
9795 if(tree->eval_glnL == YES)
9796 {
9797 new_glnL += PHYREX_Lk_Range(target_disk,new_ldsk->prev->disk,tree);
9798 tree->mmod->c_lnL = new_glnL;
9799 /* new_glnL = PHYREX_Lk(tree); */
9800 }
9801
9802 ratio = (new_glnL - cur_glnL);
9803 ratio += hr;
9804
9805 ratio = exp(ratio);
9806 alpha = MIN(1.,ratio);
9807
9808 /* Always accept move */
9809 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9810
9811 u = Uni();
9812
9813 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9814
9815 if(u > alpha) /* Reject */
9816 {
9817 target_ldsk->prev = new_ldsk->prev;
9818 new_ldsk->prev->next[dir_next] = target_ldsk;
9819 target_disk->ldsk = NULL;
9820
9821 PHYREX_Lk_Range(target_disk,target_ldsk->prev->disk,tree);
9822 tree->mmod->c_lnL = cur_glnL;
9823 /* PHYREX_Lk(tree); */
9824
9825 Free_Ldisk(new_ldsk);
9826 }
9827 else
9828 {
9829 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_add_remove_jump]++;
9830 }
9831 }
9832 else // Remove jump
9833 {
9834 target_ldsk = target_disk->ldsk;
9835
9836 hr += log(1./(phydbl)target_disk->n_ldsk_a);
9837
9838 new_glnL = cur_glnL;
9839 if(tree->eval_glnL == YES)
9840 {
9841 assert(target_disk->next);
9842 assert(target_disk->prev);
9843 new_glnL -= PHYREX_Lk_Range(target_disk,target_ldsk->prev->disk,tree);
9844 }
9845
9846 for(j=0;j<tree->mmod->n_dim;j++)
9847 {
9848 hr += Log_Dnorm_Trunc(target_ldsk->coord->lonlat[j],
9849 target_disk->centr->lonlat[j],
9850 1.*tree->mmod->rad,
9851 tree->mmod->lim_do->lonlat[j],
9852 tree->mmod->lim_up->lonlat[j],&err);
9853 }
9854
9855 dir_next = PHYREX_Get_Next_Direction(target_ldsk,target_ldsk->prev);
9856 assert(dir_next != -1);
9857
9858 target_ldsk->prev->next[dir_next] = target_ldsk->next[0];
9859 target_ldsk->next[0]->prev = target_ldsk->prev;
9860 target_disk->ldsk = NULL;
9861
9862
9863 if(tree->eval_glnL == YES)
9864 {
9865 new_glnL += PHYREX_Lk_Range(target_disk,target_ldsk->prev->disk,tree);
9866 tree->mmod->c_lnL = new_glnL;
9867 /* new_glnL = PHYREX_Lk(tree); */
9868 }
9869
9870 ratio = (new_glnL - cur_glnL);
9871 ratio += hr;
9872
9873 ratio = exp(ratio);
9874 alpha = MIN(1.,ratio);
9875
9876 /* Always accept move */
9877 if(tree->mcmc->always_yes == YES && new_glnL > UNLIKELY) alpha = 1.0;
9878
9879 u = Uni();
9880
9881 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
9882
9883 if(u > alpha) /* Reject */
9884 {
9885 target_ldsk->prev->next[dir_next] = target_ldsk;
9886 target_ldsk->next[0]->prev = target_ldsk;
9887 target_disk->ldsk = target_ldsk;
9888
9889 PHYREX_Lk_Range(target_disk,target_ldsk->prev->disk,tree);
9890 tree->mmod->c_lnL = cur_glnL;
9891 /* PHYREX_Lk(tree); */
9892 }
9893 else
9894 {
9895 Free_Ldisk(target_ldsk);
9896 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_add_remove_jump]++;
9897 }
9898 }
9899 }
9900 }
9901 #endif
9902
9903 /*////////////////////////////////////////////////////////////
9904 ////////////////////////////////////////////////////////////*/
9905
9906 #ifdef PHYREX
MCMC_PHYREX_Simulate_Backward(t_tree * tree)9907 void MCMC_PHYREX_Simulate_Backward(t_tree *tree)
9908 {
9909 phydbl u,alpha,ratio,hr,t;
9910 phydbl Told,Tyoung;
9911 phydbl cur_alnL, new_alnL;
9912 phydbl cur_glnL, new_glnL;
9913 phydbl cur_rlnL, new_rlnL;
9914 t_dsk *disk,*bkp_disk,*target_disk;
9915 t_ldsk **bkp_ldsk;
9916 int i;
9917 t_node *n;
9918
9919 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_sim]++;
9920
9921 disk = NULL;
9922 bkp_disk = NULL;
9923 target_disk = NULL;
9924 bkp_ldsk = NULL;
9925
9926 new_alnL = tree->c_lnL;
9927 cur_alnL = tree->c_lnL;
9928 cur_glnL = tree->mmod->c_lnL;
9929 new_glnL = tree->mmod->c_lnL;
9930 new_rlnL = tree->rates->c_lnL_rates;
9931 cur_rlnL = tree->rates->c_lnL_rates;
9932 hr = 0.0;
9933 ratio = 0.0;
9934 t = 0.0;
9935
9936
9937 Get_Node_Ranks_From_Tip_Times(tree);
9938
9939 // Get to the oldest sampled disk
9940 n = tree->a_nodes[0];
9941 while(n->rk_prev && n->rk_prev->tax == YES) n = n->rk_prev;
9942 Tyoung = n->ldsk->disk->time;
9943
9944
9945
9946 /* Chop off the tree at time t and simulate upwards from here */
9947 Told = PHYREX_Tree_Height(tree);
9948
9949 t = Uni()*(Tyoung-Told) + Told;
9950
9951 disk = tree->young_disk->prev;
9952 while(disk && disk->time > t) disk = disk->prev;
9953 target_disk = disk->next;
9954
9955 hr -= log(fabs((target_disk->prev->time - target_disk->time)/(Tyoung-Told)));
9956
9957 bkp_disk = target_disk->prev;
9958
9959 bkp_ldsk = (t_ldsk **)mCalloc(target_disk->n_ldsk_a,sizeof(t_ldsk *));
9960 for(i=0;i<target_disk->n_ldsk_a;i++) bkp_ldsk[i] = target_disk->ldsk_a[i]->prev;
9961
9962
9963 PHYREX_Simulate_Backward_Core(target_disk,NO,tree);
9964 PHYREX_Ldsk_To_Tree(tree);
9965
9966
9967 Told = PHYREX_Tree_Height(tree);
9968 hr += log(fabs((target_disk->prev->time - target_disk->time)/(Tyoung-Told)));
9969
9970
9971 if(tree->eval_alnL == YES) new_alnL = Lk(NULL,tree);
9972 if(tree->eval_rlnL == YES) new_rlnL = RATES_Lk_Rates(tree);
9973
9974 ratio += (new_alnL - cur_alnL);
9975 ratio += (new_rlnL - cur_rlnL);
9976 ratio += hr;
9977
9978 ratio = exp(ratio);
9979 alpha = MIN(1.,ratio);
9980
9981 /* Always accept move */
9982 if(tree->mcmc->always_yes == YES) alpha = 1.0;
9983
9984 u = Uni();
9985
9986 if(u > alpha) /* Reject */
9987 {
9988 disk = target_disk->prev;
9989 while(disk->prev)
9990 {
9991 disk = disk->prev;
9992 if(disk->next->ldsk != NULL) Free_Ldisk(disk->next->ldsk);
9993 Free_Disk(disk->next);
9994 }
9995
9996 /* Root */
9997 Free_Ldisk(disk->ldsk);
9998 Free_Disk(disk);
9999
10000 target_disk->prev = bkp_disk;
10001 for(i=0;i<target_disk->n_ldsk_a;i++) target_disk->ldsk_a[i]->prev = bkp_ldsk[i];
10002
10003 if(tree->mmod->safe_phyrex == YES)
10004 {
10005 new_alnL = Lk(NULL,tree);
10006 if(Are_Equal(new_alnL,cur_alnL,1.E-3) == NO)
10007 {
10008 PhyML_Fprintf(stderr,"\n. new_alnL: %f cur_alnL: %f",new_alnL,cur_alnL);
10009 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
10010 }
10011
10012
10013 new_glnL = PHYREX_Lk(tree);
10014
10015 if(Are_Equal(new_glnL,cur_glnL,1.E-3) == NO)
10016 {
10017 PhyML_Fprintf(stderr,"\n. new_glnL: %f cur_glnL: %f",new_glnL,cur_glnL);
10018 Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
10019 }
10020 }
10021 else
10022 {
10023 PHYREX_Lk(tree);
10024 tree->c_lnL = cur_alnL;
10025 tree->mmod->c_lnL = cur_glnL;
10026 }
10027 }
10028 else
10029 {
10030 /* Accept */
10031 disk = bkp_disk;
10032 while(disk->prev)
10033 {
10034 disk = disk->prev;
10035 if(disk->next->ldsk != NULL) Free_Ldisk(disk->next->ldsk);
10036 Free_Disk(disk->next);
10037 }
10038
10039 /* Root */
10040 Free_Ldisk(disk->ldsk);
10041 Free_Disk(disk);
10042
10043 /* Likelihood needs to be updated */
10044 PHYREX_Lk(tree);
10045
10046 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_sim]++;
10047 }
10048
10049
10050 Free(bkp_ldsk);
10051 }
10052 #endif
10053
10054 /*////////////////////////////////////////////////////////////
10055 ////////////////////////////////////////////////////////////*/
10056
10057 #ifdef PHYREX
MCMC_PHYREX_Ldsk_Tip_To_Root(t_tree * tree)10058 void MCMC_PHYREX_Ldsk_Tip_To_Root(t_tree *tree)
10059 {
10060 phydbl u,alpha,ratio,hr,c;
10061 phydbl cur_glnL, new_glnL;
10062 t_dsk *disk,**all_disks;
10063 int i,j,err,n_all_disks,block,n_move_ldsk,*permut;
10064 t_geo_coord *mean;
10065 phydbl K;
10066
10067 block = 100;
10068 all_disks = NULL;
10069 n_all_disks = 0;
10070 c = -1.;
10071 mean = NULL;
10072 K = tree->mcmc->tune_move[tree->mcmc->num_move_phyrex_ldsk_tip_to_root];
10073
10074 disk = tree->young_disk->prev;
10075 do
10076 {
10077 if(disk->ldsk != NULL)
10078 {
10079 if(!n_all_disks) all_disks = (t_dsk **)mCalloc(block,sizeof(t_dsk *));
10080 else if(!(n_all_disks%block)) all_disks = (t_dsk **)mRealloc(all_disks,n_all_disks+block,sizeof(t_dsk *));
10081 all_disks[n_all_disks] = disk;
10082 n_all_disks++;
10083 }
10084 disk = disk->prev;
10085 }
10086 while(disk);
10087
10088 if(!n_all_disks) return;
10089
10090 n_move_ldsk = (int)(n_all_disks);
10091
10092 permut = Permutate(n_all_disks);
10093 cur_glnL = tree->mmod->c_lnL;
10094 new_glnL = tree->mmod->c_lnL;
10095 for(i=0;i<n_move_ldsk;i++)
10096 {
10097 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_ldsk_tip_to_root]++;
10098
10099 disk = all_disks[permut[i]];
10100
10101 hr = 0.0;
10102 ratio = 0.0;
10103
10104 PHYREX_Store_Geo_Coord(disk->ldsk->coord);
10105
10106 new_glnL = cur_glnL;
10107 if(tree->eval_glnL == YES)
10108 {
10109 if(disk->ldsk->prev != NULL) new_glnL -= PHYREX_Lk_Range(disk,disk->ldsk->prev->disk,tree);
10110 else new_glnL -= PHYREX_Lk_Core(disk,tree);
10111 }
10112
10113 mean = PHYREX_Mean_Next_Loc(disk->ldsk,tree);
10114
10115 for(j=0;j<tree->mmod->n_dim;j++)
10116 {
10117 if(disk->ldsk->prev != NULL)
10118 c = 0.5*(mean->lonlat[j] + disk->ldsk->prev->coord->lonlat[j]);
10119 else
10120 c = mean->lonlat[j];
10121
10122 err = NO;
10123 disk->ldsk->coord->lonlat[j] =
10124 Rnorm_Trunc(c,
10125 (1./K)*(tree->mmod->lim_up->lonlat[j] - tree->mmod->lim_do->lonlat[j]),
10126 tree->mmod->lim_do->lonlat[j],
10127 tree->mmod->lim_up->lonlat[j],&err);
10128
10129 if(err == YES) Generic_Exit(__FILE__,__LINE__,__FUNCTION__);
10130
10131 hr -= Log_Dnorm_Trunc(disk->ldsk->coord->lonlat[j],
10132 c,
10133 (1./K)*(tree->mmod->lim_up->lonlat[j] - tree->mmod->lim_do->lonlat[j]),
10134 tree->mmod->lim_do->lonlat[j],
10135 tree->mmod->lim_up->lonlat[j],&err);
10136
10137 hr += Log_Dnorm_Trunc(disk->ldsk->coord->cpy->lonlat[j],
10138 c,
10139 (1./K)*(tree->mmod->lim_up->lonlat[j] - tree->mmod->lim_do->lonlat[j]),
10140 tree->mmod->lim_do->lonlat[j],
10141 tree->mmod->lim_up->lonlat[j],&err);
10142
10143 }
10144
10145 Free_Geo_Coord(mean);
10146
10147 if(tree->eval_glnL == YES)
10148 {
10149 if(disk->ldsk->prev != NULL) new_glnL += PHYREX_Lk_Range(disk,disk->ldsk->prev->disk,tree);
10150 else new_glnL += PHYREX_Lk_Core(disk,tree);
10151 tree->mmod->c_lnL = new_glnL;
10152 /* new_glnL = PHYREX_Lk(tree); */
10153 }
10154
10155 ratio += (new_glnL - cur_glnL);
10156 ratio += hr;
10157
10158 ratio = exp(ratio);
10159 alpha = MIN(1.,ratio);
10160
10161 u = Uni();
10162
10163 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
10164
10165 if(u > alpha) /* Reject */
10166 {
10167 PHYREX_Restore_Geo_Coord(disk->ldsk->coord);
10168 tree->mmod->c_lnL = cur_glnL;
10169 }
10170 else
10171 {
10172 cur_glnL = new_glnL;
10173 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_ldsk_tip_to_root]++;
10174 }
10175 }
10176
10177 Free(permut);
10178 Free(all_disks);
10179
10180
10181 }
10182 #endif
10183
10184 /*////////////////////////////////////////////////////////////
10185 ////////////////////////////////////////////////////////////*/
10186
10187 #ifdef PHYREX
MCMC_PHYREX_Sigsq_Scale(t_tree * tree)10188 void MCMC_PHYREX_Sigsq_Scale(t_tree *tree)
10189 {
10190 int i,*permut;
10191 phydbl m;
10192 phydbl cur_lnL_geo,new_lnL_geo;
10193 phydbl cur_scale,new_scale;
10194 phydbl alpha,ratio,u,K,hr;
10195
10196 if(tree->mmod->id != RRW) return;
10197
10198 K = 1.0;
10199 permut = Permutate(2*tree->n_otu-2);
10200
10201 for(i=0;i<2*tree->n_otu-2;++i)
10202 {
10203 tree->mcmc->run_move[tree->mcmc->num_move_phyrex_sigsq_scale]++;
10204
10205 hr = 0.0;
10206 ratio = 0.0;
10207
10208 cur_lnL_geo = tree->mmod->c_lnL;
10209 new_lnL_geo = tree->mmod->c_lnL;
10210
10211 cur_scale = tree->mmod->sigsq_scale[permut[i]];
10212
10213 m = exp(K*(Uni()-.5));
10214 new_scale = m * cur_scale;
10215 hr += log(m);
10216
10217 tree->mmod->sigsq_scale[permut[i]] = new_scale;
10218
10219 if(tree->eval_glnL == YES) new_lnL_geo = PHYREX_Lk(tree);
10220
10221 ratio += (new_lnL_geo - cur_lnL_geo);
10222
10223 ratio += hr;
10224
10225 ratio = exp(ratio);
10226 alpha = MIN(1.,ratio);
10227
10228 /* Always accept move */
10229 if(tree->mcmc->always_yes == YES && new_lnL_geo > UNLIKELY) alpha = 1.0;
10230
10231 u = Uni();
10232
10233 assert(isnan(u) == NO && isinf(fabs(u)) == NO);
10234
10235 if(u > alpha) /* Reject */
10236 {
10237 tree->mmod->c_lnL = cur_lnL_geo;
10238 tree->mmod->sigsq_scale[permut[i]] = cur_scale;
10239 }
10240 else
10241 {
10242 tree->mcmc->acc_move[tree->mcmc->num_move_phyrex_sigsq_scale]++;
10243 }
10244 }
10245
10246 Free(permut);
10247
10248 }
10249 #endif
10250
10251 /*////////////////////////////////////////////////////////////
10252 ////////////////////////////////////////////////////////////*/
10253 /*////////////////////////////////////////////////////////////
10254 ////////////////////////////////////////////////////////////*/
10255 /*////////////////////////////////////////////////////////////
10256 ////////////////////////////////////////////////////////////*/
10257 /*////////////////////////////////////////////////////////////
10258 ////////////////////////////////////////////////////////////*/
10259 /*////////////////////////////////////////////////////////////
10260 ////////////////////////////////////////////////////////////*/
10261 /*////////////////////////////////////////////////////////////
10262 ////////////////////////////////////////////////////////////*/
10263 /*////////////////////////////////////////////////////////////
10264 ////////////////////////////////////////////////////////////*/
10265