bpp-phyl3 3.0.0
MultinomialFromTransitionModel.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
6
8
9using namespace bpp;
10using namespace std;
11
12/******************************************************************************/
13
14void MultinomialFromTransitionModel::compute_Multinomial_(const Eigen::VectorXd& counts) const
15{
16 Pi_.array() = 0;
17
18 for (auto i = 0; i < Eigen::Index(size_); i++)
19 {
20 double* Pi_i = &Pi_(i);
21 for (auto j = 0; j < Eigen::Index(size_); j++)
22 {
23 if (counts(j) != 0)
24 {
25 *Pi_i += counts(j) * std::log((*Pij_t)(size_t(i), size_t(j)));
26 }
27 }
28 }
29
30 Pi_.array() = (Pi_.array() + getFact_(counts)).exp();
31}
32
33void MultinomialFromTransitionModel::compute_dMultinomial_dt_(const Eigen::VectorXd& counts) const
34{
35 Pi_.array() = 0; dPi_.array() = 0;
36
37 for (size_t i = 0; i < size_; i++)
38 {
39 double* Pi_i(&Pi_(Eigen::Index(i))), *dPi_i(&dPi_(Eigen::Index(i)));
40
41 for (size_t j = 0; j < size_; j++)
42 {
43 if (counts(Eigen::Index(j)) != 0)
44 {
45 *Pi_i += counts(Eigen::Index(j)) * std::log((*Pij_t)(i, j));
46 *dPi_i += counts(Eigen::Index(j)) * (*dPij_dt)(i, j) / (*Pij_t)(i, j);
47 }
48 }
49 }
50
51 dPi_.array() *= (Pi_.array() + getFact_(counts)).exp();
52}
53
54void MultinomialFromTransitionModel::compute_d2Multinomial_dt2_(const Eigen::VectorXd& counts) const
55{
56 Pi_.array() = 0; d2Pi_.array() = 0;
57
58 for (size_t i = 0; i < size_; i++)
59 {
60 double* Pi_i(&Pi_(Eigen::Index(i))), *d2Pi_i(&d2Pi_(Eigen::Index(i)));
61
62 for (size_t j = 0; j < size_; j++)
63 {
64 if (counts(Eigen::Index(j)) != 0)
65 {
66 *Pi_i += counts(Eigen::Index(j)) * std::log((*Pij_t)(i, j));
67 *d2Pi_i += counts(Eigen::Index(j)) * (*d2Pij_dt2)(i, j) / (*Pij_t)(i, j);
68 }
69 }
70 }
71
72 d2Pi_.array() *= (Pi_.array() + getFact_(counts)).exp();
73}
74
75
76const Eigen::VectorXd& MultinomialFromTransitionModel::Lik_t(const Eigen::VectorXd& to, double t) const
77{
78 if (t != tref_)
79 {
80 tref_ = t;
82 dPij_dt = 0; d2Pij_dt2 = 0;
83 }
84
86 return Pi_;
87}
88
89const Eigen::VectorXd& MultinomialFromTransitionModel::dLik_dt(const Eigen::VectorXd& to, double t) const
90{
91 if (t != tref_)
92 {
93 tref_ = t;
96 d2Pij_dt2 = 0;
97 }
98 else if (dPij_dt == 0)
100
102
103 return dPi_;
104}
105
106const Eigen::VectorXd& MultinomialFromTransitionModel::d2Lik_dt2(const Eigen::VectorXd& to, double t) const
107{
108 if (t != tref_)
109 {
110 tref_ = t;
112 dPij_dt = 0;
114 }
115 else if (d2Pij_dt2 == 0)
117
119
120 return d2Pi_;
121}
Eigen::VectorXd Pi_
Used return vectors.
const Eigen::VectorXd & d2Lik_dt2(const Eigen::VectorXd &from, double t) const override
double getFact_(const Eigen::VectorXd &counts) const
Compute the log of the normalization term for the multinomial for any count, and keeps it in a map to...
const Eigen::VectorXd & Lik_t(const Eigen::VectorXd &from, double t) const override
void compute_Multinomial_(const Eigen::VectorXd &counts) const
Fills the res vector of the likelihoods of the counts given ancestral states & transition matrix.
const Matrix< double > * Pij_t
Transition Matrices owned by the submodel.
double tref_
Reference time to avoid recomuputation of transition matrix when time has not changed....
const Eigen::VectorXd & dLik_dt(const Eigen::VectorXd &from, double t) const override
void compute_d2Multinomial_dt2_(const Eigen::VectorXd &counts) const
void compute_dMultinomial_dt_(const Eigen::VectorXd &counts) const
virtual const Matrix< double > & getPij_t(double t) const =0
virtual const Matrix< double > & getdPij_dt(double t) const =0
virtual const Matrix< double > & getd2Pij_dt2(double t) const =0
T to(const std::string &s)
Defines the basic types of data flow nodes.
ExtendedFloat exp(const ExtendedFloat &ef)