bpp-phyl  2.2.0
BinarySubstitutionModel.cpp
Go to the documentation of this file.
1 //
2 // File: BinarySubstitutionModel.cpp
3 // Created by: Laurent Gueguen
4 // Created on: 2009
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 // From the STL:
43 #include <cmath>
44 
45 using namespace bpp;
46 
47 #include <Bpp/Numeric/Matrix/MatrixTools.h>
48 
49 using namespace std;
50 
51 /******************************************************************************/
52 
53 BinarySubstitutionModel::BinarySubstitutionModel(const BinaryAlphabet* alpha, double kappa) :
54  AbstractParameterAliasable("Binary."),
55  //AbstractSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "Binary."),
56  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "Binary."),
57  kappa_(kappa),
58  lambda_(0),
59  exp_(0),
60  p_(size_,size_)
61 {
62  addParameter_(new Parameter(getNamespace() + "kappa", kappa_, &Parameter::R_PLUS_STAR));
64 }
65 
66 /******************************************************************************/
67 
69 {
70  kappa_ = getParameterValue("kappa"); // alpha/beta
71  lambda_ = (kappa_ + 1) * (kappa_ + 1) / (2 * kappa_);
72 
73  // Frequences:
74  freq_[0] = 1 / (kappa_ + 1);
75  freq_[1] = kappa_ / (kappa_ + 1);
76 
77  // Generator:
78  generator_(0, 0) = rate_ * -(kappa_ + 1) / 2;
79  generator_(0, 1) = rate_ * (kappa_ + 1) / 2;
80  generator_(1, 0) = rate_ * (kappa_ + 1) / (2 * kappa_);
81  generator_(1, 1) = rate_ * -(kappa_ + 1) / (2 * kappa_);
82 
83  // Eigen values:
84  eigenValues_[0] = 0;
85  eigenValues_[1] = -rate_ * lambda_;
86 
87  // Eigen vectors:
88  leftEigenVectors_(0,0) = 1 / (kappa_ + 1);
89  leftEigenVectors_(0,1) = kappa_ / (kappa_ + 1);
90  if (kappa_ != 1.0)
91  {
92  leftEigenVectors_(1,0) = (kappa_ - 1) / (kappa_ + 1);
93  leftEigenVectors_(1,1) = -(kappa_ - 1) / (kappa_ + 1);
94  }
95  else
96  {
97  leftEigenVectors_(1,0) = 1;
98  leftEigenVectors_(1,1) = -1;
99  }
100 
101  rightEigenVectors_(0,0) = 1;
102  rightEigenVectors_(1,0) = 1;
103 
104  if (kappa_ != 1.0)
105  {
106  rightEigenVectors_(0,1) = kappa_ / (kappa_ - 1);
107  rightEigenVectors_(1,1) = -1 / (kappa_ - 1);
108  }
109  else
110  {
111  rightEigenVectors_(0,1) = 1 / 2;
112  rightEigenVectors_(1,1) = -1 / 2;
113  }
114 }
115 
116 /******************************************************************************/
117 
118 double BinarySubstitutionModel::Pij_t(size_t i, size_t j, double d) const
119 {
120  exp_ = exp(-lambda_ * d);
121 
122  switch (i)
123  {
124  case 0: {
125  switch (j)
126  {
127  case 0: return (1 + kappa_ * exp_) / (kappa_ + 1);
128  case 1: return kappa_ / (kappa_ + 1) * (1 - exp_);
129  }
130  }
131  case 1: {
132  switch (j)
133  {
134  case 0: return (1 - exp_) / (kappa_ + 1);
135  case 1: return (kappa_ + exp_) / (kappa_ + 1);
136  }
137  }
138  }
139  return 0;
140 }
141 
142 /******************************************************************************/
143 
144 double BinarySubstitutionModel::dPij_dt(size_t i, size_t j, double d) const
145 {
146  exp_ = exp(-lambda_ * d);
147 
148  switch (i)
149  {
150  case 0: {
151  switch (j)
152  {
153  case 0: return -(kappa_ + 1) / 2 * exp_;
154  case 1: return (kappa_ + 1) / 2 * exp_;
155  }
156  }
157  case 1: {
158  switch (j)
159  {
160  case 0: return (kappa_ + 1) / (2 * kappa_) * exp_;
161  case 1: return -(kappa_ + 1) / (2 * kappa_) * exp_;
162  }
163  }
164  }
165  return 0;
166 }
167 
168 /******************************************************************************/
169 
170 double BinarySubstitutionModel::d2Pij_dt2(size_t i, size_t j, double d) const
171 {
172  exp_ = exp(-lambda_ * d);
173 
174  switch (i)
175  {
176  case 0: {
177  switch (j)
178  {
179  case 0: return lambda_ * (kappa_ + 1) / 2 * exp_;
180  case 1: return -lambda_ * (kappa_ + 1) / 2 * exp_;
181  }
182  }
183  case 1: {
184  switch (j)
185  {
186  case 0: return -lambda_ * (kappa_ + 1) / (2 * kappa_) * exp_;
187  case 1: return lambda_ * (kappa_ + 1) / (2 * kappa_) * exp_;
188  }
189  }
190  }
191  return 0;
192 }
193 
194 /******************************************************************************/
195 
196 const Matrix<double>& BinarySubstitutionModel::getPij_t(double d) const
197 {
198  exp_ = exp(-lambda_ * d);
199 
200  p_(0,0) = (1 + kappa_ * exp_) / (kappa_ + 1);
201  p_(0,1) = kappa_ / (kappa_ + 1) * (1 - exp_);
202 
203  p_(1,0) = (1 - exp_) / (kappa_ + 1);
204  p_(1,1) = (kappa_ + exp_) / (kappa_ + 1);
205 
206  return p_;
207 }
208 
209 const Matrix<double>& BinarySubstitutionModel::getdPij_dt(double d) const
210 {
211  exp_ = exp(-lambda_ * d);
212 
213  p_(0,0) = -(kappa_ + 1) / 2 * exp_;
214  p_(0,1) = (kappa_ + 1) / 2 * exp_;
215 
216  p_(1,0) = (kappa_ + 1) / (2 * kappa_) * exp_;
217  p_(1,1) = -(kappa_ + 1) / (2 * kappa_) * exp_;
218 
219  return p_;
220 }
221 
222 const Matrix<double>& BinarySubstitutionModel::getd2Pij_dt2(double d) const
223 {
224  exp_ = exp(-lambda_ * d);
225 
226  p_(0,0) = lambda_ * (kappa_ + 1) / 2 * exp_;
227  p_(0,1) = -lambda_ * (kappa_ + 1) / 2 * exp_;
228  p_(1,0) = -lambda_ * (kappa_ + 1) / (2 * kappa_) * exp_;
229  p_(1,1) = lambda_ * (kappa_ + 1) / (2 * kappa_) * exp_;
230 
231  return p_;
232 }
233 
234 /******************************************************************************/
235 
236 void BinarySubstitutionModel::setFreq(std::map<int, double>& freqs)
237 {
238  kappa_ = freqs[1] / freqs[0];
239  setParameterValue("kappa",kappa_);
240  updateMatrices();
241 }
const Matrix< double > & getdPij_dt(double d) const
const Matrix< double > & getd2Pij_dt2(double d) const
double dPij_dt(size_t i, size_t j, double d) const
const Matrix< double > & getPij_t(double d) const
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
Vdouble eigenValues_
The vector of eigen values.
BinarySubstitutionModel(const BinaryAlphabet *alpha, double kappa=1.)
STL namespace.
Vdouble freq_
The vector of equilibrium frequencies.
Partial implementation of the ReversibleSubstitutionModel interface.
double Pij_t(size_t i, size_t j, double d) const
void setFreq(std::map< int, double > &freqs)
Set equilibrium frequencies.
void updateMatrices()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
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...
double d2Pij_dt2(size_t i, size_t j, double d) const