bpp-phyl  2.2.0
AbstractMixedSubstitutionModel.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractMixedSubstitutionModel
3 // Created by: Laurent Gueguen
4 // On: vendredi 19 novembre 2010, à 15h 55
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
41 
42 #include <string>
43 
44 using namespace bpp;
45 using namespace std;
46 
47 
49  const Alphabet* alpha, StateMap* stateMap, const std::string& prefix) :
50  AbstractParameterAliasable(prefix),
51  AbstractSubstitutionModel(alpha, stateMap, prefix),
52  modelsContainer_(),
53  vProbas_(),
54  vRates_()
55 {
56  for (unsigned int i = 0; i < size_; i++)
57  {
58  for (unsigned int j = 0; j < size_; j++)
59  {
60  generator_(i, j) = 0;
61  exchangeability_(i, j) = 0;
62  leftEigenVectors_(i, j) = 0;
63  rightEigenVectors_(i, j) = 0;
64  }
65  eigenValues_[i] = 0;
66  }
67  eigenDecompose_ = false;
68 }
69 
71  AbstractParameterAliasable(msm),
73  modelsContainer_(),
74  vProbas_(),
75  vRates_()
76 {
77  for (unsigned int i = 0; i < msm.modelsContainer_.size(); i++)
78  {
79  modelsContainer_.push_back(msm.modelsContainer_[i]->clone());
80  vProbas_.push_back(msm.vProbas_[i]);
81  vRates_.push_back(msm.vRates_[i]);
82  }
83 }
84 
86 {
87  AbstractParameterAliasable::operator=(model);
89 
90  // Clear existing containers:
91  modelsContainer_.clear();
92  vProbas_.clear();
93  vRates_.clear();
94 
95  for (unsigned int i = 0; i < model.modelsContainer_.size(); i++)
96  {
97  modelsContainer_.push_back(model.modelsContainer_[i]->clone());
98  vProbas_.push_back(model.vProbas_[i]);
99  vRates_.push_back(model.vRates_[i]);
100  }
101 
102  return *this;
103 }
104 
106 {
107  for (unsigned int i = 0; i < modelsContainer_.size(); i++)
108  {
109  delete modelsContainer_[i];
110  }
111 }
112 
114 {
115  return modelsContainer_[0]->getNumberOfStates();
116 }
117 
118 const Matrix<double>& AbstractMixedSubstitutionModel::getPij_t(double t) const
119 {
120  vector<const Matrix<double>* > vM;
121  double sP = 0;
122  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
123  {
124  vM.push_back(&modelsContainer_[n]->getPij_t(t));
125  sP += vProbas_[n];
126  }
127 
128  for (unsigned int i = 0; i < getNumberOfStates(); i++)
129  {
130  for (unsigned int j = 0; j < getNumberOfStates(); j++)
131  {
132  double x = 0;
133  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
134  {
135  x += (*vM[n])(i, j) * vProbas_[n];
136  }
137  pijt_(i, j) = x / sP;
138  }
139  }
140  return pijt_;
141 }
142 
143 
144 const Matrix<double>& AbstractMixedSubstitutionModel::getdPij_dt(double t) const
145 {
146  vector<const Matrix<double>* > vM;
147  double sP = 0;
148  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
149  {
150  vM.push_back(&modelsContainer_[n]->getdPij_dt(t));
151  sP += vProbas_[n];
152  }
153 
154  for (unsigned int i = 0; i < getNumberOfStates(); i++)
155  {
156  for (unsigned int j = 0; j < getNumberOfStates(); j++)
157  {
158  double x = 0;
159  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
160  {
161  x += (*vM[n])(i, j) * vProbas_[n];
162  }
163  dpijt_(i, j) = x / sP;
164  }
165  }
166  return dpijt_;
167 }
168 
169 
170 const Matrix<double>& AbstractMixedSubstitutionModel::getd2Pij_dt2(double t) const
171 {
172  vector<const Matrix<double>* > vM;
173  double sP = 0;
174  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
175  {
176  vM.push_back(&modelsContainer_[n]->getd2Pij_dt2(t));
177  sP += vProbas_[n];
178  }
179 
180  for (unsigned int i = 0; i < getNumberOfStates(); i++)
181  {
182  for (unsigned int j = 0; j < getNumberOfStates(); j++)
183  {
184  double x = 0;
185  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
186  {
187  x += (*vM[n])(i, j) * vProbas_[n];
188  }
189  d2pijt_(i, j) = x / sP;
190  }
191  }
192  return d2pijt_;
193 }
194 
195 
197 {
199 
200  double sum = 0;
201  double sP = 0;
202  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
203  {
204  sum += vRates_[n] * vProbas_[n];
205  sP += vProbas_[n];
206  }
207  sum /= sP;
208 
209  for (unsigned int n = 0; n < modelsContainer_.size(); n++)
210  {
211  vRates_[n] *= rate_ / sum;
212  modelsContainer_[n]->setRate(vRates_[n]);
213  }
214 }
215 
217 {
218  if (vd.size() != modelsContainer_.size())
219  throw Exception("AbstractMixedSubstitutionModel::setVRates bad size of Vdouble argument.");
220 
221  for (unsigned int i = 0; i < vd.size(); i++)
222  {
223  vRates_[i] = vd[i];
224  }
225 
226  normalizeVRates();
227 }
228 
230 {
231  double sum = 0;
232  double sP = 0;
233  for (unsigned int i = 0; i < vRates_.size(); i++)
234  {
235  sum += vRates_[i] * vProbas_[i];
236  sP += vProbas_[i];
237  }
238  sum /= sP;
239 
240  for (unsigned int i = 0; i < vRates_.size(); i++)
241  {
242  vRates_[i] *= rate_ / sum;
243  modelsContainer_[i]->setRate(vRates_[i]);
244  }
245 }
virtual const Matrix< double > & getd2Pij_dt2(double t) const
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
RowMatrix< double > pijt_
These ones are for bookkeeping:
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
Vdouble eigenValues_
The vector of eigen values.
STL namespace.
virtual void setVRates(const Vdouble &vd)
Sets the rates of the submodels to be proportional to a given vector, with the constraint that the me...
virtual void normalizeVRates()
Normalizes the rates of the submodels so that the mean rate of the mixture equals rate_...
AbstractSubstitutionModel & operator=(const AbstractSubstitutionModel &model)
Partial implementation of the SubstitutionModel interface.
Partial implementation for Mixed Substitution models, defined as a mixture of "simple" substitution m...
virtual const Matrix< double > & getdPij_dt(double t) const
virtual const Matrix< double > & getPij_t(double t) const
AbstractMixedSubstitutionModel & operator=(const AbstractMixedSubstitutionModel &)
bool eigenDecompose_
Tell if the eigen decomposition should be performed.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
RowMatrix< double > generator_
The generator matrix of the model.
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
virtual void setRate(double rate)
Set the rate of the model and the submodels.
virtual void setRate(double rate)
Set the rate of the model (must be positive).
std::vector< double > vRates_
vector of the rates of the models.
size_t size_
The size of the generator, i.e. the number of states.
AbstractMixedSubstitutionModel(const Alphabet *, StateMap *stateMap, const std::string &prefix)
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
virtual size_t getNumberOfStates() const
From SubstitutionModel interface.
std::vector< SubstitutionModel * > modelsContainer_
vector of pointers to SubstitutionModels.
std::vector< double > vProbas_
vector of the probabilities of the models