bpp-phyl3  3.0.0
AbstractMixedTransitionModel.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <string>
6 
8 
9 using namespace bpp;
10 using namespace std;
11 
12 
14  std::shared_ptr<const Alphabet> alpha,
15  std::shared_ptr<const StateMapInterface> stateMap,
16  const std::string& prefix) :
18  AbstractTransitionModel(alpha, stateMap, prefix),
19  modelsContainer_(),
20  vProbas_(),
21  vRates_()
22 {}
23 
27  modelsContainer_(),
28  vProbas_(),
29  vRates_()
30 {
31  for (size_t i = 0; i < msm.modelsContainer_.size(); ++i)
32  {
33  modelsContainer_.push_back(std::shared_ptr<TransitionModelInterface>(msm.modelsContainer_[i]->clone()));
34  vProbas_.push_back(msm.vProbas_[i]);
35  vRates_.push_back(msm.vRates_[i]);
36  }
37 }
38 
40 {
42 
43  // Clear existing containers:
44  modelsContainer_.clear();
45  vProbas_.clear();
46  vRates_.clear();
47 
48  for (size_t i = 0; i < model.modelsContainer_.size(); ++i)
49  {
50  modelsContainer_.push_back(std::shared_ptr<TransitionModelInterface>(model.modelsContainer_[i]->clone()));
51  vProbas_.push_back(model.vProbas_[i]);
52  vRates_.push_back(model.vRates_[i]);
53  }
54 
55  return *this;
56 }
57 
59 {
60  vector<const Matrix<double>* > vM;
61  double sP = 0;
62  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
63  {
64  vM.push_back(&modelsContainer_[n]->getPij_t(t));
65  sP += vProbas_[n];
66  }
67 
68  for (unsigned int i = 0; i < getNumberOfStates(); i++)
69  {
70  for (unsigned int j = 0; j < getNumberOfStates(); j++)
71  {
72  double x = 0;
73  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
74  {
75  x += (*vM[n])(i, j) * vProbas_[n];
76  }
77  pijt_(i, j) = x / sP;
78  }
79  }
80  return pijt_;
81 }
82 
83 
85 {
86  vector<const Matrix<double>* > vM;
87  double sP = 0;
88  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
89  {
90  vM.push_back(&modelsContainer_[n]->getdPij_dt(t));
91  sP += vProbas_[n];
92  }
93 
94  for (unsigned int i = 0; i < getNumberOfStates(); i++)
95  {
96  for (unsigned int j = 0; j < getNumberOfStates(); j++)
97  {
98  double x = 0;
99  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
100  {
101  x += (*vM[n])(i, j) * vProbas_[n];
102  }
103  dpijt_(i, j) = x / sP;
104  }
105  }
106  return dpijt_;
107 }
108 
109 
111 {
112  vector<const Matrix<double>* > vM;
113  double sP = 0;
114  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
115  {
116  vM.push_back(&modelsContainer_[n]->getd2Pij_dt2(t));
117  sP += vProbas_[n];
118  }
119 
120  for (unsigned int i = 0; i < getNumberOfStates(); i++)
121  {
122  for (unsigned int j = 0; j < getNumberOfStates(); j++)
123  {
124  double x = 0;
125  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
126  {
127  x += (*vM[n])(i, j) * vProbas_[n];
128  }
129  d2pijt_(i, j) = x / sP;
130  }
131  }
132  return d2pijt_;
133 }
134 
135 
137 {
139 
140  double sum = 0;
141  double sP = 0;
142  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
143  {
144  sum += vRates_[n] * vProbas_[n];
145  sP += vProbas_[n];
146  }
147  sum /= sP;
148 
149  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
150  {
151  vRates_[n] *= rate_ / sum;
152  modelsContainer_[n]->setRate(vRates_[n]);
153  }
154 }
155 
157 {
158  if (vd.size() != modelsContainer_.size())
159  throw Exception("AbstractMixedTransitionModel::setVRates bad size of Vdouble argument.");
160 
161  for (unsigned int i = 0; i < vd.size(); i++)
162  {
163  vRates_[i] = vd[i];
164  }
165 
166  normalizeVRates();
167 }
168 
170 {
171  double sum = 0;
172  double sP = 0;
173  for (unsigned int i = 0; i < vRates_.size(); i++)
174  {
175  sum += vRates_[i] * vProbas_[i];
176  sP += vProbas_[i];
177  }
178  sum /= sP;
179 
180  for (unsigned int i = 0; i < vRates_.size(); i++)
181  {
182  vRates_[i] *= rate_ / sum;
183  modelsContainer_[i]->setRate(vRates_[i]);
184  }
185 }
Partial implementation for Mixed Transition models, defined as a mixture of "simple" substitution mod...
virtual const Matrix< double > & getdPij_dt(double t) const override
AbstractMixedTransitionModel(std::shared_ptr< const Alphabet >, std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix)
std::vector< double > vProbas_
vector of the probabilities of the models
virtual void setRate(double rate) override
Set the rate of the model and the submodels.
virtual const Matrix< double > & getd2Pij_dt2(double t) const override
virtual void setVRates(const Vdouble &vd) override
Sets the rates of the submodels to be proportional to a given vector, with the constraint that the me...
virtual void normalizeVRates() override
Normalizes the rates of the submodels so that the mean rate of the mixture equals rate_.
AbstractMixedTransitionModel & operator=(const AbstractMixedTransitionModel &)
std::vector< double > vRates_
vector of the rates of the models.
virtual const Matrix< double > & getPij_t(double t) const override
From TransitionModel interface.
std::vector< std::shared_ptr< TransitionModelInterface > > modelsContainer_
vector of pointers to TransitionModels.
Partial implementation of the TransitionModel interface.
RowMatrix< double > pijt_
These ones are for bookkeeping:
virtual void setRate(double rate) override
Set the rate of the model (must be positive).
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
AbstractTransitionModel & operator=(const AbstractTransitionModel &model)=default
size_t getNumberOfStates() const override
Get the number of states.
virtual const TransitionModelInterface & model(const std::string &name) const =0
Access the submodel with the given name.
TransitionModelInterface * clone() const =0
Defines the basic types of data flow nodes.
std::vector< double > Vdouble