8 #include "../../PatternTools.h"
22 std::shared_ptr<SubstitutionModelSet> modelSet,
23 std::shared_ptr<DiscreteDistributionInterface> rDist,
26 bool reparametrizeRoot) :
31 if (!modelSet->isFullySetUpFor(
tree))
32 throw Exception(
"RNonHomogeneousTreeLikelihood(constructor). Model set is not fully specified.");
41 std::shared_ptr<SubstitutionModelSet> modelSet,
42 std::shared_ptr<DiscreteDistributionInterface> rDist,
45 bool reparametrizeRoot) :
50 if (!modelSet->isFullySetUpFor(
tree))
51 throw Exception(
"RNonHomogeneousTreeLikelihood(constructor). Model set is not fully specified.");
72 minusLogLik_(lik.minusLogLik_)
121 for (
size_t i = 0; i <
nbSites_; i++)
134 for (
size_t i = 0; i <
nbSites_; i++)
138 sort(la.begin(), la.end());
139 for (
size_t i =
nbSites_; i > 0; i--)
234 for (
size_t i = 0; i < tmp.size(); i++)
236 vector<int> tmpv =
modelSet_->getNodesWithParameter(tmp[i]);
240 vector<const Node*> nodes;
241 for (
size_t i = 0; i < ids.size(); i++)
245 vector<const Node*> tmpv;
247 for (
size_t i = 0; i < tmp.size(); i++)
249 if (tmp[i] ==
"BrLenRoot" || tmp[i] ==
"RootPosition")
253 tmpv.push_back(
tree_->getRootNode()->getSon(0));
254 tmpv.push_back(
tree_->getRootNode()->getSon(1));
259 tmpv.push_back(
nodes_[TextTools::to< size_t >(tmp[i].substr(5))]);
263 for (
size_t i = 0; i < nodes.size(); i++)
279 throw Exception(
"RNonHomogeneousTreeLikelihood::getValue(). Instance is not initialized.");
288 size_t rateClass)
const
326 for (
size_t i = 0; i <
nbSites_; i++)
341 throw Exception(
"Derivatives respective to rate distribution parameter are not implemented.");
345 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
356 if (variable ==
"BrLenRoot")
358 const Node* father =
tree_->getRootNode();
363 size_t nbSites = _dLikelihoods_father->size();
364 for (
size_t i = 0; i < nbSites; i++)
366 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
369 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
372 (*_dLikelihoods_father_i_c)[s] = 1.;
378 for (
size_t l = 0; l < nbNodes; l++)
396 for (
size_t i = 0; i < nbSites; i++)
398 VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[(*_patternLinks_fatherroot1_)[i]];
399 VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[(*_patternLinks_fatherroot2_)[i]];
400 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
403 Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
404 Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
405 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
406 VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
407 VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
408 VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
409 VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
412 Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
413 Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
414 Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
415 Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
416 double dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
419 dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
420 dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
421 l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
422 l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
424 double dl = pos * dl1 * l2 + (1. - pos) * dl2 * l1;
425 (*_dLikelihoods_father_i_c)[x] *= dl;
441 for (
size_t i = 0; i < nbSites; i++)
443 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
444 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
447 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
448 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
449 VVdouble* pxy__son_c = &(*pxy__son)[c];
453 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
456 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
458 (*_dLikelihoods_father_i_c)[x] *= dl;
466 else if (variable ==
"RootPosition")
468 const Node* father =
tree_->getRootNode();
473 size_t nbSites = _dLikelihoods_father->size();
474 for (
size_t i = 0; i < nbSites; i++)
476 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
479 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
482 (*_dLikelihoods_father_i_c)[s] = 1.;
488 for (
size_t l = 0; l < nbNodes; l++)
506 for (
size_t i = 0; i < nbSites; i++)
508 VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[(*_patternLinks_fatherroot1_)[i]];
509 VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[(*_patternLinks_fatherroot2_)[i]];
510 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
513 Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
514 Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
515 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
516 VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
517 VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
518 VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
519 VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
522 Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
523 Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
524 Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
525 Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
526 double dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
529 dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
530 dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
531 l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
532 l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
534 double dl = len * (dl1 * l2 - dl2 * l1);
535 (*_dLikelihoods_father_i_c)[x] *= dl;
551 for (
size_t i = 0; i < nbSites; i++)
553 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
554 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
557 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
558 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
559 VVdouble* pxy__son_c = &(*pxy__son)[c];
563 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
566 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
568 (*_dLikelihoods_father_i_c)[x] *= dl;
578 size_t brI = TextTools::to<size_t>(variable.substr(5));
585 size_t nbSites = _dLikelihoods_father->size();
586 for (
size_t i = 0; i < nbSites; i++)
588 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
591 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
594 (*_dLikelihoods_father_i_c)[s] = 1.;
600 for (
size_t l = 0; l < nbNodes; l++)
610 for (
size_t i = 0; i < nbSites; i++)
612 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
613 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
616 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
617 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
618 VVdouble* dpxy__son_c = &(*dpxy__son)[c];
622 Vdouble* dpxy__son_c_x = &(*dpxy__son_c)[x];
625 dl += (*dpxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
627 (*_dLikelihoods_father_i_c)[x] *= dl;
635 for (
size_t i = 0; i < nbSites; i++)
637 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
638 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
641 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
642 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
643 VVdouble* pxy__son_c = &(*pxy__son)[c];
647 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
650 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
652 (*_dLikelihoods_father_i_c)[x] *= dl;
676 size_t nbSites = _dLikelihoods_father->size();
677 for (
size_t i = 0; i < nbSites; i++)
679 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
682 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
685 (*_dLikelihoods_father_i_c)[s] = 1.;
691 for (
size_t l = 0; l < nbNodes; l++)
701 for (
size_t i = 0; i < nbSites; i++)
703 VVdouble* _dLikelihoods_son_i = &(*_dLikelihoods_son)[(*_patternLinks_father_son)[i]];
704 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
707 Vdouble* _dLikelihoods_son_i_c = &(*_dLikelihoods_son_i)[c];
708 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
709 VVdouble* pxy__son_c = &(*pxy__son)[c];
713 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
716 dl += (*pxy__son_c_x)[y] * (*_dLikelihoods_son_i_c)[y];
718 (*_dLikelihoods_father_i_c)[x] *= dl;
726 for (
size_t i = 0; i < nbSites; i++)
728 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
729 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
732 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
733 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
734 VVdouble* pxy__son_c = &(*pxy__son)[c];
738 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
741 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
743 (*_dLikelihoods_father_i_c)[x] *= dl;
759 size_t rateClass)
const
797 for (
size_t i = 0; i <
nbSites_; i++)
812 throw Exception(
"Derivatives respective to rate distribution parameter are not implemented.");
816 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
827 if (variable ==
"BrLenRoot")
829 const Node* father =
tree_->getRootNode();
834 size_t nbSites = _d2Likelihoods_father->size();
835 for (
size_t i = 0; i < nbSites; i++)
837 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
840 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
843 (*_d2Likelihoods_father_i_c)[s] = 1.;
849 for (
size_t l = 0; l < nbNodes; l++)
869 for (
size_t i = 0; i < nbSites; i++)
871 VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[(*_patternLinks_fatherroot1_)[i]];
872 VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[(*_patternLinks_fatherroot2_)[i]];
873 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
876 Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
877 Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
878 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
879 VVdouble* d2pxy_root1__c = &(*d2pxy_root1_)[c];
880 VVdouble* d2pxy_root2__c = &(*d2pxy_root2_)[c];
881 VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
882 VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
883 VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
884 VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
887 Vdouble* d2pxy_root1__c_x = &(*d2pxy_root1__c)[x];
888 Vdouble* d2pxy_root2__c_x = &(*d2pxy_root2__c)[x];
889 Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
890 Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
891 Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
892 Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
893 double d2l1 = 0, d2l2 = 0, dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
896 d2l1 += (*d2pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
897 d2l2 += (*d2pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
898 dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
899 dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
900 l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
901 l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
903 double d2l = pos * pos * d2l1 * l2 + (1. - pos) * (1. - pos) * d2l2 * l1 + 2 * pos * (1. - pos) * dl1 * dl2;
904 (*_d2Likelihoods_father_i_c)[x] *= d2l;
920 for (
size_t i = 0; i < nbSites; i++)
922 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
923 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
926 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
927 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
928 VVdouble* pxy__son_c = &(*pxy__son)[c];
932 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
935 d2l += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
937 (*_d2Likelihoods_father_i_c)[x] *= d2l;
945 else if (variable ==
"RootPosition")
947 const Node* father =
tree_->getRootNode();
952 size_t nbSites = _d2Likelihoods_father->size();
953 for (
size_t i = 0; i < nbSites; i++)
955 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
958 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
961 (*_d2Likelihoods_father_i_c)[s] = 1.;
967 for (
size_t l = 0; l < nbNodes; l++)
987 for (
size_t i = 0; i < nbSites; i++)
989 VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[(*_patternLinks_fatherroot1_)[i]];
990 VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[(*_patternLinks_fatherroot2_)[i]];
991 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
994 Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
995 Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
996 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
997 VVdouble* d2pxy_root1__c = &(*d2pxy_root1_)[c];
998 VVdouble* d2pxy_root2__c = &(*d2pxy_root2_)[c];
999 VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
1000 VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
1001 VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
1002 VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
1005 Vdouble* d2pxy_root1__c_x = &(*d2pxy_root1__c)[x];
1006 Vdouble* d2pxy_root2__c_x = &(*d2pxy_root2__c)[x];
1007 Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
1008 Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
1009 Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
1010 Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
1011 double d2l1 = 0, d2l2 = 0, dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
1014 d2l1 += (*d2pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
1015 d2l2 += (*d2pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
1016 dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
1017 dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
1018 l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
1019 l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
1021 double d2l = len * len * (d2l1 * l2 + d2l2 * l1 - 2 * dl1 * dl2);
1022 (*_d2Likelihoods_father_i_c)[x] *= d2l;
1038 for (
size_t i = 0; i < nbSites; i++)
1040 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
1041 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1044 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
1045 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1046 VVdouble* pxy__son_c = &(*pxy__son)[c];
1050 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
1053 d2l += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
1055 (*_d2Likelihoods_father_i_c)[x] *= d2l;
1065 size_t brI = TextTools::to<size_t>(variable.substr(5));
1072 size_t nbSites = _d2Likelihoods_father->size();
1073 for (
size_t i = 0; i < nbSites; i++)
1075 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1078 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1081 (*_d2Likelihoods_father_i_c)[s] = 1.;
1087 for (
size_t l = 0; l < nbNodes; l++)
1097 for (
size_t i = 0; i < nbSites; i++)
1099 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
1100 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1103 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
1104 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1105 VVdouble* d2pxy__son_c = &(*d2pxy__son)[c];
1109 Vdouble* d2pxy__son_c_x = &(*d2pxy__son_c)[x];
1112 d2l += (*d2pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
1114 (*_d2Likelihoods_father_i_c)[x] *= d2l;
1122 for (
size_t i = 0; i < nbSites; i++)
1124 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
1125 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1128 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
1129 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1130 VVdouble* pxy__son_c = &(*pxy__son)[c];
1134 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
1137 d2l += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
1139 (*_d2Likelihoods_father_i_c)[x] *= d2l;
1163 size_t nbSites = _d2Likelihoods_father->size();
1164 for (
size_t i = 0; i < nbSites; i++)
1166 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1169 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1172 (*_d2Likelihoods_father_i_c)[s] = 1.;
1178 for (
size_t l = 0; l < nbNodes; l++)
1188 for (
size_t i = 0; i < nbSites; i++)
1190 VVdouble* _d2Likelihoods_son_i = &(*_d2Likelihoods_son)[(*_patternLinks_father_son)[i]];
1191 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1194 Vdouble* _d2Likelihoods_son_i_c = &(*_d2Likelihoods_son_i)[c];
1195 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1196 VVdouble* pxy__son_c = &(*pxy__son)[c];
1200 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
1203 d2l += (*pxy__son_c_x)[y] * (*_d2Likelihoods_son_i_c)[y];
1205 (*_d2Likelihoods_father_i_c)[x] *= d2l;
1213 for (
size_t i = 0; i < nbSites; i++)
1215 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
1216 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
1219 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
1220 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
1221 VVdouble* pxy__son_c = &(*pxy__son)[c];
1225 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
1228 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
1230 (*_d2Likelihoods_father_i_c)[x] *= dl;
1260 for (
size_t i = 0; i < nbSites; i++)
1263 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
1267 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
1271 (*_likelihoods_node_i_c)[x] = 1.;
1276 for (
size_t l = 0; l < nbNodes; l++)
1288 for (
size_t i = 0; i < nbSites; i++)
1291 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_node_son)[i]];
1292 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
1296 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
1297 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
1298 VVdouble* pxy__son_c = &(*pxy__son)[c];
1303 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
1304 double likelihood = 0;
1307 likelihood += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
1309 (*_likelihoods_node_i_c)[x] *= likelihood;
1321 cout <<
"Likelihoods at node " << node->
getName() <<
": " << endl;
1323 cout <<
" ***" << endl;
std::shared_ptr< DiscreteDistributionInterface > rateDistribution_
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
Partial implementation for branch non-homogeneous models of the TreeLikelihood interface.
std::map< int, VVVdouble > d2pxy_
std::shared_ptr< SubstitutionModelSet > modelSet_
AbstractNonHomogeneousTreeLikelihood & operator=(const AbstractNonHomogeneousTreeLikelihood &lik)
Assignation operator.
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
std::map< int, VVVdouble > dpxy_
ParameterList getSubstitutionModelParameters() const override
Get the parameters associated to substitution model(s).
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
std::map< int, const Node * > idToNode_
An index linking nodes to their id, for faster access than the getNode() method.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
ParameterList brLenParameters_
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
ParameterList getRateDistributionParameters() const override
Get the parameters associated to the rate distribution.
std::map< int, VVVdouble > pxy_
std::vector< double > rootFreqs_
void setParametersValues(const ParameterList ¶meters) override
bool hasParameter(const std::string &name) const override
double getParameterValue(const std::string &name) const override
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
const Tree & tree() const
Get the tree (topology and branch lengths).
std::unique_ptr< const AlignmentDataInterface > data_
std::shared_ptr< TreeTemplate< Node > > tree_
bool isInitialized() const
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
virtual int getId() const
Get the node's id.
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool isLeaf() const
virtual size_t getNumberOfSons() const
virtual ParameterList getCommonParametersWith(const ParameterList ¶ms) const
virtual std::vector< std::string > getParameterNames() const
This class implement the 'traditional' way of computing likelihood for a tree, allowing for non-homog...
double getFirstOrderDerivative(const std::string &variable) const
void init_(bool usePatterns)
Method called by constructors.
virtual void computeDownSubtreeD2Likelihood(const Node *)
virtual double getD2LikelihoodForASite(size_t site) const
void setParameters(const ParameterList ¶meters)
Implements the Function interface.
double getLikelihood() const
Get the likelihood for the whole dataset.
virtual void computeTreeDLikelihood(const std::string &variable)
RNonHomogeneousTreeLikelihood & operator=(const RNonHomogeneousTreeLikelihood &lik)
virtual double getD2LogLikelihood() const
double getLikelihoodForASite(size_t site) const
Get the likelihood for a site.
RNonHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< SubstitutionModelSet > modelSet, std::shared_ptr< DiscreteDistributionInterface > rDist, bool verbose=true, bool usePatterns=true, bool reparametrizeRoot=false)
Build a new NonHomogeneousTreeLikelihood object without data.
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
void fireParameterChanged(const ParameterList ¶ms)
virtual double getDLikelihoodForASite(size_t site) const
double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the likelihood for a site knowing its rate class and its ancestral state.
virtual ~RNonHomogeneousTreeLikelihood()
virtual double getD2LogLikelihoodForASite(size_t site) const
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
virtual double getDLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
std::shared_ptr< DRASRTreeLikelihoodData > likelihoodData_
virtual double getDLogLikelihoodForASite(size_t site) const
virtual void displayLikelihood(const Node *node)
This method is mainly for debugging purpose.
virtual void computeDownSubtreeDLikelihood(const Node *)
virtual double getD2LikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
virtual void computeTreeD2Likelihood(const std::string &variable)
double getLogLikelihoodForASite(size_t site) const
Get the logarithm of the likelihood for a site.
virtual double getDLogLikelihood() const
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the logarithm of the likelihood for a site knowing its rate class.
double getSecondOrderDerivative(const std::string &variable) const
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
virtual void computeTreeLikelihood()
Interface for phylogenetic tree objects.
std::string toString(T t)
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< double > Vdouble
ExtendedFloat pow(const ExtendedFloat &ef, double exp)
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble