22 std::shared_ptr<const Alphabet> alpha,
23 std::shared_ptr<const StateMapInterface> stateMap,
24 const string& prefix) :
28 size_(alpha->getSize()),
33 d2pijt_(size_, size_),
37 for (
auto& fr :
freq_)
39 fr = 1.0 /
static_cast<double>(
size_);
74 if (state < 0 || !alphabet_->isIntInAlphabet(state))
76 vector<int> states =
alphabet_->getAlias(state);
78 for (
size_t j = 0; j < states.size(); ++j)
90 map<int, double> freqs;
102 freq_[(size_t)i.first] = i.second;
113 std::shared_ptr<const Alphabet> alpha,
114 std::shared_ptr<const StateMapInterface> stateMap,
115 const string& prefix) :
119 generator_(size_, size_),
121 exchangeability_(size_, size_),
122 eigenDecompose_(true),
124 iEigenValues_(size_),
125 isDiagonalizable_(false),
126 rightEigenVectors_(size_, size_),
127 isNonSingular_(false),
128 leftEigenVectors_(size_, size_),
130 tmpMat_(size_, size_)
146 vector<bool> vnull(salph);
149 for (
size_t i = 0; i < salph; i++)
154 for (
size_t j = 0; j < salph; j++)
174 size_t salphok = salph - nbStop;
177 size_t gi = 0, gj = 0;
179 for (
size_t i = 0; i < salph; i++)
184 for (
size_t j = 0; j < salph; j++)
202 for (
size_t i = 0; i < nbStop; i++)
211 for (
size_t i = 0; i < salph; i++)
216 for (
size_t j = 0; j < salph; j++)
225 for (
size_t j = 0; j < salphok; j++)
230 for (
size_t j = salphok; j < salph; j++)
268 vector<size_t> vNullEv;
271 while (vNullEv.size() == 0 && fact < 1000)
275 for (
size_t i = 0; i < salph - nbStop; i++)
278 vNullEv.push_back(i);
288 if (vNullEv.size() > 1)
292 for (
auto cnull : vNullEv)
320 nulleigen = vNullEv[0];
329 for (
size_t i = 0; i < salph; i++)
357 for (
size_t i = 1; i < salph; i++)
375 for (
size_t i = 0; i < salph; i++)
409 std::vector<double> vdia(
size_);
410 std::vector<double> vup(
size_ - 1);
411 std::vector<double> vlo(
size_ - 1);
413 double l =
rate_ * t;
414 for (
size_t i = 0; i <
size_; i++)
421 vup[i] = vdia[i] * s;
424 vdia[i + 1] = vdia[i];
443 double v =
rate_ * t;
450 for (
size_t i = 1; i <
vPowGen_.size(); i++)
452 s *= v /
static_cast<double>(i);
467 for (
size_t i = 0; i <
size_; i++)
469 for (
size_t j = 0; j <
size_; j++)
471 if (
pijt_(i, j) < 0.)
496 std::vector<double> vdia(
size_);
497 std::vector<double> vup(
size_ - 1);
498 std::vector<double> vlo(
size_ - 1);
500 double l =
rate_ * t;
501 for (
size_t i = 0; i <
size_; i++)
511 vdia[i + 1] = vdia[i];
530 double v =
rate_ * t;
537 for (
size_t i = 1; i <
vPowGen_.size(); i++)
539 s *= v /
static_cast<double>(i);
567 std::vector<double> vdia(
size_);
568 std::vector<double> vup(
size_ - 1);
569 std::vector<double> vlo(
size_ - 1);
571 double l =
rate_ * t;
572 for (
size_t i = 0; i <
size_; i++)
586 vdia[i + 1] = vdia[i];
605 double v =
rate_ * t;
612 for (
size_t i = 1; i <
vPowGen_.size(); i++)
614 s *= v /
static_cast<double>(i);
636 return -VectorTools::scalar<double, double>(v,
freq_);
656 for (
size_t i = 0; i <
size_; i++)
661 for (
size_t j = 0; j <
size_; j++)
void addParameter_(Parameter *parameter)
void setParameterValue(const std::string &name, double value) override
std::string getNamespace() const override
bool hasParameter(const std::string &name) const override
virtual void updateMatrices_() override
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
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_
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
void setDiagonal()
set the diagonal of the generator such that sum on each line equals 0.
bool computeFrequencies() const
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).
const Matrix< double > & getPij_t(double t) const
virtual void updateMatrices_()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
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.
double getScale() const
return 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.
RowMatrix< double > d2pijt_
RowMatrix< double > pijt_
These ones are for bookkeeping:
size_t size_
The number of states.
Vdouble freq_
The vector of equilibrium frequencies.
virtual void setRate(double rate) override
Set the rate of the model (must be positive).
int getAlphabetStateAsInt(size_t index) const override
virtual void updateMatrices_()=0
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
RowMatrix< double > dpijt_
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
std::shared_ptr< const Alphabet > alphabet_
The alphabet relevant to this model.
size_t getNumberOfStates() const override
Get the number of states.
virtual double getRate() const override
The rate of the substitution process.
double getInitValue(size_t i, int state) const override
bool computeFrequencies() const override
AbstractTransitionModel(std::shared_ptr< const Alphabet > alpha, std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix)
virtual void setFreq(std::map< int, double > &freqs) override
Set equilibrium frequencies.
virtual std::string getName() const =0
Get the name of the model.
const RowMatrix< Real > & getV() const
const std::vector< Real > & getImagEigenValues() const
const std::vector< Real > & getRealEigenValues() const
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
Interface for reversible substitution models.
const std::vector< double > & getRow(size_t i) const
void resize(size_t nRows, size_t nCols)
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::string to_string(const NoDimension &)
ExtendedFloat abs(const ExtendedFloat &ef)