bpp-phyl3  3.0.0
TwoParameterBinarySubstitutionModel.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 
7 // From the STL:
8 #include <cmath>
9 
10 using namespace bpp;
11 
13 
14 using namespace std;
15 
16 /******************************************************************************/
17 
19  shared_ptr<const BinaryAlphabet> alpha,
20  double mu,
21  double pi0) :
22  AbstractParameterAliasable("TwoParameterBinary."),
23  // AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "TwoParameterBinary."),
24  AbstractReversibleSubstitutionModel(alpha, std::shared_ptr<const StateMapInterface>(new CanonicalStateMap(alpha, false)), "TwoParameterBinary."),
25  mu_(mu),
26  pi0_(pi0),
27  lambda_(0),
28  exp_(0),
29  p_(size_, size_)
30 {
31  addParameter_(new Parameter(getNamespace() + "mu", mu_, std::make_shared<IntervalConstraint>(NumConstants::MILLI(), 100, false, false)));
32  addParameter_(new Parameter(getNamespace() + "pi0", pi0_, std::make_shared<IntervalConstraint>(0.05, 0.95, true, true)));
34 }
35 
36 /******************************************************************************/
37 
39 {
40  mu_ = getParameterValue("mu");
41  rate_ = mu_;
42  pi0_ = getParameterValue("pi0");
43  lambda_ = 1;
44 
45  // Frequencies:
46  freq_[0] = pi0_;
47  freq_[1] = 1 - pi0_;
48 
49  // Generator:
50  generator_(0, 0) = -1 * rate_ * freq_[1];
51  generator_(0, 1) = rate_ * freq_[1];
52  generator_(1, 0) = rate_ * freq_[0];
53  generator_(1, 1) = -1 * rate_ * freq_[0];
54 
55  // Eigen values:
56  eigenValues_[0] = 0;
57  eigenValues_[1] = -1 * mu_;
58 
59  // Eigen vectors:
60  leftEigenVectors_(0, 0) = pi0_;
61  leftEigenVectors_(0, 1) = 1 - pi0_;
62  leftEigenVectors_(1, 0) = 1;
63  leftEigenVectors_(1, 1) = -1;
64 
65  rightEigenVectors_(0, 0) = 1;
66  rightEigenVectors_(1, 0) = 1;
67  rightEigenVectors_(0, 1) = 1 - pi0_;
68  rightEigenVectors_(1, 1) = -1 * pi0_;
69 }
70 
71 /******************************************************************************/
72 
73 double TwoParameterBinarySubstitutionModel::Pij_t(size_t i, size_t j, double d) const
74 {
75  exp_ = exp(-lambda_ * rate_ * d);
76 
77  switch (i)
78  {
79  case 0:
80  switch (j)
81  {
82  case 0: return (1 - pi0_) + pi0_ * exp_;
83  case 1: return pi0_ * (1 - exp_);
84  default: return 0;
85  }
86  case 1:
87  switch (j)
88  {
89  case 0: return (1 - pi0_) * (1 - exp_);
90  case 1: return pi0_ + (1 - pi0_) * exp_;
91  default: return 0;
92  }
93  default: return 0;
94  }
95 }
96 
97 /******************************************************************************/
98 
99 double TwoParameterBinarySubstitutionModel::dPij_dt(size_t i, size_t j, double d) const
100 {
101  exp_ = rate_ * exp(-lambda_ * rate_ * d);
102 
103  switch (i)
104  {
105  case 0:
106  switch (j)
107  {
108  case 0: return -1 * pi0_ * exp_;
109  case 1: return pi0_ * exp_;
110  default: return 0;
111  }
112  case 1:
113  switch (j)
114  {
115  case 0: return (1 - pi0_) * exp_;
116  case 1: return -1 * (1 - pi0_) * exp_;
117  default: return 0;
118  }
119  default: return 0;
120  }
121 }
122 
123 /******************************************************************************/
124 
125 double TwoParameterBinarySubstitutionModel::d2Pij_dt2(size_t i, size_t j, double d) const
126 {
127  exp_ = rate_ * rate_ * exp(-lambda_ * rate_ * d);
128 
129  switch (i)
130  {
131  case 0:
132  switch (j)
133  {
134  case 0: return pi0_ * exp_;
135  case 1: return -1 * pi0_ * exp_;
136  default: return 0;
137  }
138  case 1:
139  switch (j)
140  {
141  case 0: return -1 * (1 - pi0_) * exp_;
142  case 1: return (1 - pi0_) * exp_;
143  default: return 0;
144  }
145  default: return 0;
146  }
147  return 0;
148 }
149 
150 /******************************************************************************/
151 
153 {
154  exp_ = exp(-lambda_ * rate_ * d);
155 
156  p_(0, 0) = (1 - pi0_) + pi0_ * exp_;
157  p_(0, 1) = pi0_ * (1 - exp_);
158 
159  p_(1, 0) = (1 - pi0_) * (1 - exp_);
160  p_(1, 1) = pi0_ + (1 - pi0_) * exp_;
161 
162  return p_;
163 }
164 
165 /******************************************************************************/
166 
168 {
169  exp_ = rate_ * exp(-lambda_ * rate_ * d);
170 
171  p_(0, 0) = -1 * pi0_ * exp_;
172  p_(0, 1) = pi0_ * exp_;
173 
174  p_(1, 0) = (1 - pi0_) * exp_;
175  p_(1, 1) = -1 * (1 - pi0_) * exp_;
176 
177  return p_;
178 }
179 
180 /******************************************************************************/
181 
183 {
184  exp_ = rate_ * rate_ * exp(-lambda_ * rate_ * d);
185 
186  p_(0, 0) = pi0_ * exp_;
187  p_(0, 1) = -1 * pi0_ * exp_;
188  p_(1, 0) = -1 * (1 - pi0_) * exp_;
189  p_(1, 1) = (1 - pi0_) * exp_;
190 
191  return p_;
192 }
193 
194 /******************************************************************************/
195 
197 {
198  std::shared_ptr<IntervalConstraint> bounds(new IntervalConstraint(lb, ub, true, true));
199  getParameter_("mu").setConstraint(bounds);
200 }
void addParameter_(Parameter *parameter)
std::string getNamespace() const override
double getParameterValue(const std::string &name) const override
Parameter & getParameter_(const std::string &name)
Partial implementation of the ReversibleSubstitutionModel interface.
RowMatrix< double > generator_
The generator matrix of the model.
Vdouble eigenValues_
The vector of eigen values.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
Vdouble freq_
The vector of equilibrium frequencies.
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
static double MILLI()
virtual void setConstraint(std::shared_ptr< ConstraintInterface > constraint)
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
const Matrix< double > & getd2Pij_dt2(double d) const
double dPij_dt(size_t i, size_t j, double d) const
TwoParameterBinarySubstitutionModel(std::shared_ptr< const BinaryAlphabet > alpha, double mu=1., double pi0=0.5)
double d2Pij_dt2(size_t i, size_t j, double d) const
void updateMatrices_()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
double Pij_t(size_t i, size_t j, double d) const
Defines the basic types of data flow nodes.
ExtendedFloat exp(const ExtendedFloat &ef)