|
bpp-phyl
2.1.0
|
00001 // 00002 // File: JCnuc.cpp 00003 // Created by: Julien Dutheil 00004 // Created on: Tue May 27 16:04:36 2003 00005 // 00006 00007 /* 00008 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004) 00009 00010 This software is a computer program whose purpose is to provide classes 00011 for phylogenetic data analysis. 00012 00013 This software is governed by the CeCILL license under French law and 00014 abiding by the rules of distribution of free software. You can use, 00015 modify and/ or redistribute the software under the terms of the CeCILL 00016 license as circulated by CEA, CNRS and INRIA at the following URL 00017 "http://www.cecill.info". 00018 00019 As a counterpart to the access to the source code and rights to copy, 00020 modify and redistribute granted by the license, users are provided only 00021 with a limited warranty and the software's author, the holder of the 00022 economic rights, and the successive licensors have only limited 00023 liability. 00024 00025 In this respect, the user's attention is drawn to the risks associated 00026 with loading, using, modifying and/or developing or reproducing the 00027 software by the user in light of its specific status of free software, 00028 that may mean that it is complicated to manipulate, and that also 00029 therefore means that it is reserved for developers and experienced 00030 professionals having in-depth computer knowledge. Users are therefore 00031 encouraged to load and test the software's suitability as regards their 00032 requirements in conditions enabling the security of their systems and/or 00033 data to be ensured and, more generally, to use and operate it in the 00034 same conditions as regards security. 00035 00036 The fact that you are presently reading this means that you have had 00037 knowledge of the CeCILL license and that you accept its terms. 00038 */ 00039 00040 #include "JCnuc.h" 00041 00042 using namespace bpp; 00043 00044 #include <cmath> 00045 00046 using namespace std; 00047 00048 /******************************************************************************/ 00049 00050 JCnuc::JCnuc(const NucleicAlphabet* alpha) : 00051 AbstractParameterAliasable("JC69."), 00052 AbstractSubstitutionModel(alpha, "JC69."), 00053 AbstractReversibleSubstitutionModel(alpha, "JC69."), 00054 exp_(), 00055 p_(size_, size_) 00056 { 00057 updateMatrices(); 00058 } 00059 00060 /******************************************************************************/ 00061 00062 void JCnuc::updateMatrices() 00063 { 00064 // Frequencies: 00065 freq_[0] = freq_[1] = freq_[2] = freq_[3] = 1. / 4.; 00066 00067 // Generator and exchangeabilities: 00068 for (int i = 0; i < 4; i++) 00069 { 00070 for (int j = 0; j < 4; j++) 00071 { 00072 generator_(i, j) = (i == j) ? -1. : 1. / 3.; 00073 exchangeability_(i, j) = generator_(i, j) * 4.; 00074 } 00075 } 00076 00077 // Eigen values: 00078 eigenValues_[0] = 0; 00079 eigenValues_[1] = eigenValues_[2] = eigenValues_[3] = -4. / 3.; 00080 00081 // Eigen vectors: 00082 for (size_t i = 0; i < 4; i++) 00083 { 00084 leftEigenVectors_(0, i) = 1. / 4.; 00085 } 00086 for (size_t i = 1; i < 4; i++) 00087 { 00088 for (size_t j = 0; j < 4; j++) 00089 { 00090 leftEigenVectors_(i, j) = -1. / 4.; 00091 } 00092 } 00093 leftEigenVectors_(1, 2) = 3. / 4.; 00094 leftEigenVectors_(2, 1) = 3. / 4.; 00095 leftEigenVectors_(3, 0) = 3. / 4.; 00096 00097 for (size_t i = 0; i < 4; i++) 00098 { 00099 rightEigenVectors_(i, 0) = 1.; 00100 } 00101 for (size_t i = 1; i < 4; i++) 00102 { 00103 rightEigenVectors_(3, i) = -1.; 00104 } 00105 for (size_t i = 0; i < 3; i++) 00106 { 00107 for (size_t j = 1; j < 4; j++) 00108 { 00109 rightEigenVectors_(i, j) = 0.; 00110 } 00111 } 00112 rightEigenVectors_(2, 1) = 1.; 00113 rightEigenVectors_(1, 2) = 1.; 00114 rightEigenVectors_(0, 3) = 1.; 00115 } 00116 00117 /******************************************************************************/ 00118 00119 double JCnuc::Pij_t(size_t i, size_t j, double d) const 00120 { 00121 if (i == j) 00122 return 1. / 4. + 3. / 4. * exp(-rate_ * 4. / 3. * d); 00123 else 00124 return 1. / 4. - 1. / 4. * exp(-rate_ * 4. / 3. * d); 00125 } 00126 00127 /******************************************************************************/ 00128 00129 double JCnuc::dPij_dt(size_t i, size_t j, double d) const 00130 { 00131 if (i == j) 00132 return -exp(-rate_ * 4. / 3. * d) * rate_; 00133 else 00134 return 1. / 3. * exp(-rate_ * 4. / 3. * d) * rate_; 00135 } 00136 00137 /******************************************************************************/ 00138 00139 double JCnuc::d2Pij_dt2(size_t i, size_t j, double d) const 00140 { 00141 if (i == j) 00142 return 4. / 3. * exp(-rate_ * 4. / 3. * d) * rate_ * rate_; 00143 else 00144 return -4. / 9. * exp(-rate_ * 4. / 3. * d) * rate_ * rate_; 00145 } 00146 00147 /******************************************************************************/ 00148 00149 const Matrix<double>& JCnuc::getPij_t(double d) const 00150 { 00151 exp_ = exp(-4. / 3. * d * rate_); 00152 for (size_t i = 0; i < size_; i++) 00153 { 00154 for (size_t j = 0; j < size_; j++) 00155 { 00156 p_(i, j) = (i == j) ? 1. / 4. + 3. / 4. * exp_ : 1. / 4. - 1. / 4. * exp_; 00157 } 00158 } 00159 return p_; 00160 } 00161 00162 const Matrix<double>& JCnuc::getdPij_dt(double d) const 00163 { 00164 exp_ = exp(-4. / 3. * d * rate_); 00165 for (size_t i = 0; i < size_; i++) 00166 { 00167 for (size_t j = 0; j < size_; j++) 00168 { 00169 p_(i, j) = rate_ * ((i == j) ? -exp_ : 1. / 3. * exp_); 00170 } 00171 } 00172 return p_; 00173 } 00174 00175 const Matrix<double>& JCnuc::getd2Pij_dt2(double d) const 00176 { 00177 exp_ = exp(-4. / 3. * d * rate_); 00178 for (size_t i = 0; i < size_; i++) 00179 { 00180 for (size_t j = 0; j < size_; j++) 00181 { 00182 p_(i, j) = rate_ * rate_ * ((i == j) ? 4. / 3. * exp_ : -4. / 9. * exp_); 00183 } 00184 } 00185 return p_; 00186 } 00187 00188 /******************************************************************************/ 00189