bpp-phyl3  3.0.0
AbstractSubstitutionModel.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_MODEL_ABSTRACTSUBSTITUTIONMODEL_H
6 #define BPP_PHYL_MODEL_ABSTRACTSUBSTITUTIONMODEL_H
7 
10 #include <memory>
11 
12 #include "SubstitutionModel.h"
13 
14 namespace bpp
15 {
20  public virtual TransitionModelInterface
21 {
22 private:
23  mutable Eigen::VectorXd lik_;
24 
25 public:
28 
29 public:
30  const Eigen::VectorXd& Lik_t(const Eigen::VectorXd& values, double t) const override
31  {
32  lik_ = Eigen::VectorXd::Zero(values.size());
33 
34  const auto& pij = getPij_t(t);
35 
36  for (auto i = 0; i < values.size(); ++i)
37  {
38  for (auto j = 0; j < values.size(); ++j)
39  {
40  lik_(i) += pij(size_t(i), size_t(j)) * values(j);
41  }
42  }
43 
44  return lik_;
45  }
46 
47  const Eigen::VectorXd& dLik_dt(const Eigen::VectorXd& values, double t) const override
48  {
49  lik_ = Eigen::VectorXd::Zero(values.size());
50 
51  const auto& pij = getdPij_dt(t);
52 
53  for (auto i = 0; i < values.size(); ++i)
54  {
55  for (auto j = 0; j < values.size(); ++j)
56  {
57  lik_(i) += pij(size_t(i), size_t(j)) * values(j);
58  }
59  }
60 
61  return lik_;
62  }
63 
64  const Eigen::VectorXd& d2Lik_dt2(const Eigen::VectorXd& values, double t) const override
65  {
66  lik_ = Eigen::VectorXd::Zero(values.size());
67 
68  const auto& pij = getd2Pij_dt2(t);
69 
70  for (auto i = 0; i < values.size(); ++i)
71  {
72  for (auto j = 0; j < values.size(); ++j)
73  {
74  lik_(i) += pij(size_t(i), size_t(j)) * values(j);
75  }
76  }
77 
78  return lik_;
79  }
80 
84 };
85 
114  public virtual AbstractLkTransitionModel,
115  public virtual AbstractParameterAliasable
116 {
117 protected:
121  std::shared_ptr<const Alphabet> alphabet_;
122 
126  std::shared_ptr<const StateMapInterface> stateMap_;
127 
131  size_t size_;
132 
138  double rate_;
139 
144 
151 
153 
154 public:
156  std::shared_ptr<const Alphabet> alpha,
157  std::shared_ptr<const StateMapInterface> stateMap,
158  const std::string& prefix);
159 
161 
163 
165 
166 public:
167  const Alphabet& alphabet() const override { return *alphabet_; }
168 
169  std::shared_ptr<const Alphabet> getAlphabet() const override { return alphabet_; }
170 
171  const StateMapInterface& stateMap() const override { return *stateMap_; }
172 
173  std::shared_ptr<const StateMapInterface> getStateMap() const override { return stateMap_; }
174 
175  size_t getNumberOfStates() const override { return stateMap_->getNumberOfModelStates();}
176 
177  const std::vector<int>& getAlphabetStates() const override { return stateMap_->getAlphabetStates(); }
178 
179  std::string getAlphabetStateAsChar(size_t index) const override { return stateMap_->getAlphabetStateAsChar(index); }
180 
181  int getAlphabetStateAsInt(size_t index) const override { return stateMap_->getAlphabetStateAsInt(index); }
182 
183  std::vector<size_t> getModelStates(int code) const override { return stateMap_->getModelStates(code); }
184 
185  std::vector<size_t> getModelStates(const std::string& code) const override { return stateMap_->getModelStates(code); }
186 
187  const Vdouble& getFrequencies() const override { return freq_; }
188 
189  bool computeFrequencies() const override { return false; }
190 
191  virtual const Matrix<double>& getPij_t(double t) const override = 0;
192  virtual const Matrix<double>& getdPij_dt(double t) const override = 0;
193  virtual const Matrix<double>& getd2Pij_dt2(double t) const override = 0;
194 
195  virtual double freq(size_t i) const override { return freq_[i]; }
196 
197  virtual double Pij_t (size_t i, size_t j, double t) const override { return getPij_t(t) (i, j); }
198  virtual double dPij_dt (size_t i, size_t j, double t) const override { return getdPij_dt(t) (i, j); }
199  virtual double d2Pij_dt2(size_t i, size_t j, double t) const override { return getd2Pij_dt2(t) (i, j); }
200 
201  double getInitValue(size_t i, int state) const override;
202 
203  void setFreqFromData(const SequenceDataInterface& data, double pseudoCount = 0) override;
204 
205  virtual void setFreq(std::map<int, double>& freqs) override;
206 
207  const FrequencySetInterface& frequencySet() const override
208  {
209  throw Exception("TransitionModel::frequencySet(). No associated FrequencySet object.");
210  }
211 
217  virtual void fireParameterChanged(const ParameterList& parameters) override
218  {
220 
221  if (parameters.hasParameter(getNamespace() + "rate"))
222  {
223  rate_ = parameters.getParameterValue(getNamespace() + "rate");
224 
225  if (parameters.size() != 1)
226  updateMatrices_();
227  }
228  else
229  updateMatrices_();
230  }
231 
237  void addRateParameter() override;
238 
239  void setVerboseLevel(short level) { verboseLevel_ = level; }
240 
241  short verboseLevel() const { return verboseLevel_; }
242 
243 protected:
262  virtual void updateMatrices_() = 0;
263 
264  /*
265  * @brief : To update the eq freq
266  *
267  */
269  {
270  return freq_;
271  }
272 
273 public:
278  virtual double getRate() const override;
279 
280  virtual void setRate(double rate) override;
281 };
282 
283 
286  public virtual SubstitutionModelInterface
287 {
288 protected:
294 
299 
304 
311 
316 
321 
326 
331 
336 
341 
347 
352  std::vector< RowMatrix<double>> vPowGen_;
353 
358 
359 public:
361  std::shared_ptr<const Alphabet> alpha,
362  std::shared_ptr<const StateMapInterface> stateMap,
363  const std::string& prefix);
364 
367  isScalable_(model.isScalable_),
368  generator_(model.generator_),
369  computeFreq_(model.computeFreq_),
372  eigenValues_(model.eigenValues_),
378  vPowGen_(model.vPowGen_),
379  tmpMat_(model.tmpMat_)
380  {}
381 
383  {
385  isScalable_ = model.isScalable_;
386  generator_ = model.generator_;
387  computeFreq_ = model.computeFreq_;
390  eigenValues_ = model.eigenValues_;
396  vPowGen_ = model.vPowGen_;
397  tmpMat_ = model.tmpMat_;
398  return *this;
399  }
400 
402 
403 public:
404  bool computeFrequencies() const { return computeFreq_; }
405 
406  void computeFrequencies(bool yn) { computeFreq_ = yn; }
407 
408  const Matrix<double>& generator() const { return generator_; }
409 
411 
412  const Matrix<double>& getPij_t(double t) const;
413  const Matrix<double>& getdPij_dt(double t) const;
414  const Matrix<double>& getd2Pij_dt2(double t) const;
415 
416  double Sij(size_t i, size_t j) const { return exchangeability_(i, j); }
417 
418  const Vdouble& getEigenValues() const { return eigenValues_; }
419 
420  const Vdouble& getIEigenValues() const { return iEigenValues_; }
421 
422  bool isDiagonalizable() const { return isDiagonalizable_; }
423 
424  bool isNonSingular() const { return isNonSingular_; }
425 
427 
429 
430  virtual double Qij(size_t i, size_t j) const { return generator_(i, j); }
431 
433 
435 
436 protected:
454  virtual void updateMatrices_();
455 
456 public:
461  void setScalable(bool scalable)
462  {
463  isScalable_ = scalable;
464  }
465 
469  virtual bool isScalable() const
470  {
471  return isScalable_;
472  }
473 
477  double getScale() const;
478 
484  void setScale(double scale);
485 
489  void normalize();
490 
495  void setDiagonal();
496 
498 };
499 
500 
527 {
528 public:
530  std::shared_ptr<const Alphabet> alpha,
531  std::shared_ptr<const StateMapInterface> stateMap,
532  const std::string& prefix) :
533  AbstractSubstitutionModel(alpha, stateMap, prefix)
534  {
535  isDiagonalizable_ = true;
536  isNonSingular_ = true;
537  computeFreq_ = false;
538 
539  // to ensure non null freq_ at construction
540  for (auto& fr : freq_)
541  {
542  fr = 1.0 / static_cast<double>(size_);
543  }
544 
545  }
546 
548 
549  virtual AbstractReversibleSubstitutionModel* clone() const override = 0;
550 
551 protected:
573  virtual void updateMatrices_() override;
574 };
575 } // end of namespace bpp.
576 #endif // BPP_PHYL_MODEL_ABSTRACTSUBSTITUTIONMODEL_H
Virtual class of a Transition Model related to a given SubstitutionModel.
Partial implementation of the TransitionModel interface, with function for likelihood computations.
const Eigen::VectorXd & dLik_dt(const Eigen::VectorXd &values, double t) const override
const Eigen::VectorXd & d2Lik_dt2(const Eigen::VectorXd &values, double t) const override
const Eigen::VectorXd & Lik_t(const Eigen::VectorXd &values, double t) const override
virtual void fireParameterChanged(const ParameterList &parameters)
std::string getNamespace() const override
Partial implementation of the ReversibleSubstitutionModel interface.
AbstractReversibleSubstitutionModel(std::shared_ptr< const Alphabet > alpha, std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix)
virtual AbstractReversibleSubstitutionModel * clone() const override=0
virtual void updateMatrices_() override
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
bool computeFreq_
if the Frequencies must be computed from the generator
RowMatrix< double > generator_
The generator matrix of the model.
bool isDiagonalizable_
boolean value for diagonalizability in R of the generator_
bool isNonSingular_
boolean value for non-singularity of rightEigenVectors_
AbstractSubstitutionModel & operator=(const AbstractSubstitutionModel &model)
std::vector< RowMatrix< double > > vPowGen_
vector of the powers of generator_ for Taylor development (if rightEigenVectors_ is singular).
Vdouble eigenValues_
The vector of eigen values.
AbstractSubstitutionModel(std::shared_ptr< const Alphabet > alpha, std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix)
void normalize()
normalize the generator
const Matrix< double > & exchangeabilityMatrix() const
void setDiagonal()
set the diagonal of the generator such that sum on each line equals 0.
bool eigenDecompose_
Tell if the eigen decomposition should be performed.
virtual double Qij(size_t i, size_t j) const
A method for computing all necessary matrices.
const Matrix< double > & generator() const
void setScalable(bool scalable)
sets if model is scalable, ie scale can be changed. Default : true, set to false to avoid normalizati...
void enableEigenDecomposition(bool yn)
Set if eigenValues and Vectors must be computed.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible,...
const Matrix< double > & getdPij_dt(double t) const
Vdouble iEigenValues_
The vector of the imaginary part of the eigen values.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
bool isScalable_
If the model is scalable (ie generator can be normalized automatically).
AbstractSubstitutionModel(const AbstractSubstitutionModel &model)
const Matrix< double > & getPij_t(double t) const
const Matrix< double > & getRowLeftEigenVectors() const
virtual void updateMatrices_()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
double Sij(size_t i, size_t j) const
const Matrix< double > & getColumnRightEigenVectors() const
virtual bool isScalable() const
returns if model is scalable
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
const Matrix< double > & getd2Pij_dt2(double t) const
void setScale(double scale)
Multiplies the current generator by the given scale.
RowMatrix< double > tmpMat_
For computational issues.
Partial implementation of the TransitionModel interface.
void setFreqFromData(const SequenceDataInterface &data, double pseudoCount=0) override
Set equilibrium frequencies equal to the frequencies estimated from the data.
void addRateParameter() override
add a "rate" parameter to the model, that handles the overall rate of the process.
std::string getAlphabetStateAsChar(size_t index) const override
std::shared_ptr< const StateMapInterface > stateMap_
The map of model states with alphabet states.
std::vector< size_t > getModelStates(const std::string &code) const override
Get the state in the model corresponding to a particular state in the alphabet.
std::vector< size_t > getModelStates(int code) const override
Get the state in the model corresponding to a particular state in the alphabet.
virtual double d2Pij_dt2(size_t i, size_t j, double t) const override
AbstractTransitionModel(const AbstractTransitionModel &model)=default
virtual void fireParameterChanged(const ParameterList &parameters) override
Tells the model that a parameter value has changed.
std::shared_ptr< const Alphabet > getAlphabet() const override
RowMatrix< double > pijt_
These ones are for bookkeeping:
size_t size_
The number of states.
Vdouble freq_
The vector of equilibrium frequencies.
std::shared_ptr< const StateMapInterface > getStateMap() const override
virtual void setRate(double rate) override
Set the rate of the model (must be positive).
virtual const Matrix< double > & getPij_t(double t) const override=0
const Vdouble & getFrequencies() const override
int getAlphabetStateAsInt(size_t index) const override
virtual const Matrix< double > & getdPij_dt(double t) const override=0
virtual void updateMatrices_()=0
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
virtual double freq(size_t i) const override
const Alphabet & alphabet() const override
AbstractTransitionModel & operator=(const AbstractTransitionModel &model)=default
const FrequencySetInterface & frequencySet() const override
std::shared_ptr< const Alphabet > alphabet_
The alphabet relevant to this model.
const std::vector< int > & getAlphabetStates() const override
size_t getNumberOfStates() const override
Get the number of states.
virtual const Matrix< double > & getd2Pij_dt2(double t) const override=0
const StateMapInterface & stateMap() const override
virtual double getRate() const override
The rate of the substitution process.
double getInitValue(size_t i, int state) const override
AbstractTransitionModel(std::shared_ptr< const Alphabet > alpha, std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix)
virtual double Pij_t(size_t i, size_t j, double t) const override
virtual void setFreq(std::map< int, double > &freqs) override
Set equilibrium frequencies.
virtual double dPij_dt(size_t i, size_t j, double t) const override
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
SubModel taken from a MixedTransitionModel, kept in the context of the MixedTransitionModel (see From...
From a model, compute transition probabilities given there is at least a change of a category (ie a n...
virtual bool hasParameter(const std::string &name) const
size_t size() const
virtual double getParameterValue(const std::string &name) const
Interface for reversible substitution models.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
Interface for all substitution models.
Interface for all transition models.
virtual const Matrix< double > & getPij_t(double t) const =0
virtual const Matrix< double > & getd2Pij_dt2(double t) const =0
virtual const Matrix< double > & getdPij_dt(double t) const =0
Defines the basic types of data flow nodes.
std::vector< double > Vdouble