bpp-phyl3  3.0.0
EquiprobableSubstitutionModel.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 // From bpp-seq:
9 
10 using namespace bpp;
11 
12 #include <cmath>
13 #include <map>
14 
15 using namespace std;
16 
17 /******************************************************************************/
18 
20  std::shared_ptr<const Alphabet> alpha) :
22  AbstractReversibleSubstitutionModel(alpha, make_shared<CanonicalStateMap>(alpha, false), "Equi."),
23  exp_(), p_(size_, size_), freqSet_(nullptr)
24 {
25  freqSet_ = make_unique<FixedFrequencySet>(getStateMap(), freq_);
27 }
28 
30  std::shared_ptr<const Alphabet> alpha,
31  std::unique_ptr<FrequencySetInterface> freqSet,
32  bool initFreqs) :
33  AbstractParameterAliasable("Equi+F."),
34  AbstractReversibleSubstitutionModel(alpha, make_shared<CanonicalStateMap>(alpha, false), "Equi+F."),
35  exp_(), p_(size_, size_), freqSet_(std::move(freqSet))
36 {
37  freqSet_->setNamespace("Equi+F." + freqSet_->getNamespace());
38  if (initFreqs)
39  freqSet_->setFrequencies(freq_);
40  else
41  freq_ = freqSet_->getFrequencies();
42  addParameters_(freqSet_->getParameters());
44 }
45 
46 /******************************************************************************/
47 
49 {
50  // Frequencies:
51  for (unsigned int i = 0; i < size_; ++i)
52  {
53  freq_[i] = 1. / (float)size_;
54  }
55 
56  // Generator:
57  for (unsigned int i = 0; i < size_; ++i)
58  {
59  for (unsigned int j = 0; j < size_; ++j)
60  {
61  generator_(i, j) = (i == j) ? -1. : 1. / (float)(size_ - 1);
62  exchangeability_(i, j) = generator_(i, j) * (float)size_;
63  }
64  }
65 
66  // Eigen values:
67  eigenValues_[0] = 0;
68  for (unsigned int i = 1; i < size_; ++i)
69  {
70  eigenValues_[i] = -(float)size_ / (float)(size_ - 1);
71  }
72 
73  // Eigen vectors:
74  for (unsigned int i = 0; i < size_; ++i)
75  {
76  leftEigenVectors_(0, i) = 1. / (float)size_;
77  }
78  for (unsigned int i = 1; i < size_; ++i)
79  {
80  for (unsigned int j = 0; j < size_; ++j)
81  {
82  leftEigenVectors_(i, j) = -1. / (float)size_;
83  }
84  }
85  for (unsigned int i = 0; i < (size_ - 1); ++i)
86  {
87  leftEigenVectors_((size_ - 1) - i, i) = (float)(size_ - 1) / (float)size_;
88  }
89 
90  for (unsigned int i = 0; i < size_; ++i)
91  {
92  rightEigenVectors_(i, 0) = 1.;
93  }
94  for (unsigned int i = 1; i < size_; ++i)
95  {
96  rightEigenVectors_((size_ - 1), i) = -1.;
97  }
98  for (unsigned int i = 0; i < (size_ - 1); ++i)
99  {
100  for (unsigned int j = 1; j < size_; ++j)
101  {
102  rightEigenVectors_(i, j) = 0.;
103  }
104  }
105  for (unsigned int i = 1; i < size_; ++i)
106  {
107  rightEigenVectors_((size_ - 1) - i, i) = 1.;
108  }
109 }
110 
111 /******************************************************************************/
112 
113 double EquiprobableSubstitutionModel::Pij_t(size_t i, size_t j, double d) const
114 {
115  if (i == j)
116  return 1. / (float)size_ + (float)(size_ - 1) / (float)size_ * exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
117  else
118  return 1. / (float)size_ - 1. / (float)size_ * exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
119 }
120 
121 /******************************************************************************/
122 
123 double EquiprobableSubstitutionModel::dPij_dt(size_t i, size_t j, double d) const
124 {
125  if (i == j)
126  return -rate_* exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
127  else
128  return rate_ * 1. / (float)(size_ - 1) * exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
129 }
130 
131 /******************************************************************************/
132 
133 double EquiprobableSubstitutionModel::d2Pij_dt2(size_t i, size_t j, double d) const
134 {
135  if (i == j)
136  return rate_ * rate_ * (float)size_ / (float)(size_ - 1) * exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
137  else
138  return -rate_ * rate_ * (float)size_ / pow((float)(size_ - 1), 2) * exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
139 }
140 
141 /******************************************************************************/
142 
144 {
145  exp_ = exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
146  for (unsigned int i = 0; i < size_; i++)
147  {
148  for (unsigned int j = 0; j < size_; j++)
149  {
150  p_(i, j) = (i == j) ? 1. / (float)size_ + (float)(size_ - 1) / (float)size_ * exp_ : 1. / (float)size_ - 1. / (float)size_ * exp_;
151  }
152  }
153  return p_;
154 }
155 
157 {
158  exp_ = exp(-rate_ * (float)size_ / (float)(size_ - 1) * d);
159  for (unsigned int i = 0; i < size_; i++)
160  {
161  for (unsigned int j = 0; j < size_; j++)
162  {
163  p_(i, j) = rate_ * ((i == j) ? -exp_ : 1. / (float)(size_ - 1) * exp_);
164  }
165  }
166  return p_;
167 }
168 
170 {
171  exp_ = exp( rate_ * -(float)size_ / (float)(size_ - 1) * d);
172  for (unsigned int i = 0; i < size_; i++)
173  {
174  for (unsigned int j = 0; j < size_; j++)
175  {
176  p_(i, j) = rate_ * rate_ * ((i == j) ? (float)size_ / (float)(size_ - 1) * exp_ : -(float)size_ / pow((float)(size_ - 1), 2) * exp_);
177  }
178  }
179  return p_;
180 }
181 
182 /******************************************************************************/
183 
184 void EquiprobableSubstitutionModel::setFreq(std::map<int, double>& freqs)
185 {
186  for (auto i : freqs)
187  {
188  freq_[(size_t)i.first] = i.second;
189  }
190 
191  freqSet_->setFrequencies(freq_);
192  // Update parameters and re-compute generator and eigen values:
193  matchParametersValues(freqSet_->getParameters());
194 }
195 
196 /******************************************************************************/
void addParameters_(const ParameterList &parameters)
bool matchParametersValues(const ParameterList &parameters) override
Partial implementation of the ReversibleSubstitutionModel interface.
RowMatrix< double > generator_
The generator matrix of the model.
Vdouble eigenValues_
The vector of eigen values.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible,...
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
size_t size_
The number of states.
Vdouble freq_
The vector of equilibrium frequencies.
std::shared_ptr< const StateMapInterface > getStateMap() const override
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
double Pij_t(size_t i, size_t j, double d) const override
std::unique_ptr< FrequencySetInterface > freqSet_
const Matrix< double > & getd2Pij_dt2(double d) const override
void setFreq(std::map< int, double > &freq) override
Set equilibrium frequencies.
double d2Pij_dt2(size_t i, size_t j, double d) const override
EquiprobableSubstitutionModel(std::shared_ptr< const Alphabet > alpha)
Build a simple equiprobable model, with original equilibrium frequencies.
double dPij_dt(size_t i, size_t j, double d) const override
const Matrix< double > & getdPij_dt(double d) const override
const Matrix< double > & getPij_t(double d) const override
Defines the basic types of data flow nodes.
ExtendedFloat exp(const ExtendedFloat &ef)
ExtendedFloat pow(const ExtendedFloat &ef, double exp)