bpp-phyl  2.2.0
JCnuc.cpp
Go to the documentation of this file.
1 //
2 // File: JCnuc.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue May 27 16:04:36 2003
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 
40 #include "JCnuc.h"
41 
42 using namespace bpp;
43 
44 #include <cmath>
45 
46 using namespace std;
47 
48 /******************************************************************************/
49 
50 JCnuc::JCnuc(const NucleicAlphabet* alpha) :
51  AbstractParameterAliasable("JC69."),
52  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "JC69."),
53  exp_(),
54  p_(size_, size_)
55 {
57 }
58 
59 /******************************************************************************/
60 
62 {
63  // Frequencies:
64  freq_[0] = freq_[1] = freq_[2] = freq_[3] = 1. / 4.;
65 
66  // Generator and exchangeabilities:
67  for (size_t i = 0; i < 4; ++i)
68  {
69  for (size_t j = 0; j < 4; ++j)
70  {
71  generator_(i, j) = (i == j) ? -1. : 1. / 3.;
72  exchangeability_(i, j) = generator_(i, j) * 4.;
73  }
74  }
75 
76  // Eigen values:
77  eigenValues_[0] = 0;
78  eigenValues_[1] = eigenValues_[2] = eigenValues_[3] = -4. / 3.;
79 
80  // Eigen vectors:
81  for (size_t i = 0; i < 4; i++)
82  {
83  leftEigenVectors_(0, i) = 1. / 4.;
84  }
85  for (size_t i = 1; i < 4; i++)
86  {
87  for (size_t j = 0; j < 4; j++)
88  {
89  leftEigenVectors_(i, j) = -1. / 4.;
90  }
91  }
92  leftEigenVectors_(1, 2) = 3. / 4.;
93  leftEigenVectors_(2, 1) = 3. / 4.;
94  leftEigenVectors_(3, 0) = 3. / 4.;
95 
96  for (size_t i = 0; i < 4; i++)
97  {
98  rightEigenVectors_(i, 0) = 1.;
99  }
100  for (size_t i = 1; i < 4; i++)
101  {
102  rightEigenVectors_(3, i) = -1.;
103  }
104  for (size_t i = 0; i < 3; i++)
105  {
106  for (size_t j = 1; j < 4; j++)
107  {
108  rightEigenVectors_(i, j) = 0.;
109  }
110  }
111  rightEigenVectors_(2, 1) = 1.;
112  rightEigenVectors_(1, 2) = 1.;
113  rightEigenVectors_(0, 3) = 1.;
114 }
115 
116 /******************************************************************************/
117 
118 double JCnuc::Pij_t(size_t i, size_t j, double d) const
119 {
120  if (i == j)
121  return 1. / 4. + 3. / 4. * exp(-rate_ * 4. / 3. * d);
122  else
123  return 1. / 4. - 1. / 4. * exp(-rate_ * 4. / 3. * d);
124 }
125 
126 /******************************************************************************/
127 
128 double JCnuc::dPij_dt(size_t i, size_t j, double d) const
129 {
130  if (i == j)
131  return -exp(-rate_ * 4. / 3. * d) * rate_;
132  else
133  return 1. / 3. * exp(-rate_ * 4. / 3. * d) * rate_;
134 }
135 
136 /******************************************************************************/
137 
138 double JCnuc::d2Pij_dt2(size_t i, size_t j, double d) const
139 {
140  if (i == j)
141  return 4. / 3. * exp(-rate_ * 4. / 3. * d) * rate_ * rate_;
142  else
143  return -4. / 9. * exp(-rate_ * 4. / 3. * d) * rate_ * rate_;
144 }
145 
146 /******************************************************************************/
147 
148 const Matrix<double>& JCnuc::getPij_t(double d) const
149 {
150  exp_ = exp(-4. / 3. * d * rate_);
151  for (size_t i = 0; i < size_; i++)
152  {
153  for (size_t j = 0; j < size_; j++)
154  {
155  p_(i, j) = (i == j) ? 1. / 4. + 3. / 4. * exp_ : 1. / 4. - 1. / 4. * exp_;
156  }
157  }
158  return p_;
159 }
160 
161 const Matrix<double>& JCnuc::getdPij_dt(double d) const
162 {
163  exp_ = exp(-4. / 3. * d * rate_);
164  for (size_t i = 0; i < size_; i++)
165  {
166  for (size_t j = 0; j < size_; j++)
167  {
168  p_(i, j) = rate_ * ((i == j) ? -exp_ : 1. / 3. * exp_);
169  }
170  }
171  return p_;
172 }
173 
174 const Matrix<double>& JCnuc::getd2Pij_dt2(double d) const
175 {
176  exp_ = exp(-4. / 3. * d * rate_);
177  for (size_t i = 0; i < size_; i++)
178  {
179  for (size_t j = 0; j < size_; j++)
180  {
181  p_(i, j) = rate_ * rate_ * ((i == j) ? 4. / 3. * exp_ : -4. / 9. * exp_);
182  }
183  }
184  return p_;
185 }
186 
187 /******************************************************************************/
188 
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
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).
void updateMatrices()
Definition: JCnuc.cpp:61
Vdouble eigenValues_
The vector of eigen values.
STL namespace.
Vdouble freq_
The vector of equilibrium frequencies.
const Matrix< double > & getd2Pij_dt2(double d) const
Definition: JCnuc.cpp:174
Partial implementation of the ReversibleSubstitutionModel interface.
RowMatrix< double > p_
Definition: JCnuc.h:135
double exp_
Definition: JCnuc.h:134
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
Definition: JCnuc.cpp:138
size_t size_
The size of the generator, i.e. the number of states.
double dPij_dt(size_t i, size_t j, double d) const
Definition: JCnuc.cpp:128
JCnuc(const NucleicAlphabet *alpha)
Definition: JCnuc.cpp:50
const Matrix< double > & getdPij_dt(double d) const
Definition: JCnuc.cpp:161
const Matrix< double > & getPij_t(double d) const
Definition: JCnuc.cpp:148
double Pij_t(size_t i, size_t j, double d) const
Definition: JCnuc.cpp:118