41 #include <Bpp/Numeric/NumConstants.h> 42 #include <Bpp/Text/TextTools.h> 50 const Alphabet* alpha,
51 vector<SubstitutionModel*> vpModel) :
52 AbstractParameterAliasable(
"Mixture."),
55 size_t i, nbmod = vpModel.size();
57 for (i = 0; i < nbmod; i++)
60 throw Exception(
"Empty model number " + TextTools::toString(i) +
" in MixtureOfSubstitutionModels constructor");
61 for (
size_t j = i + 1; j < nbmod; j++)
63 if (vpModel[i] == vpModel[j])
64 throw Exception(
"Same model at positions " + TextTools::toString(i) +
" and " +
65 TextTools::toString(j) +
" in MixtureOfSubstitutionModels constructor");
71 for (i = 0; i < nbmod; i++)
74 vProbas_.push_back(1.0 / static_cast<double>(nbmod));
81 for (i = 0; i < nbmod - 1; i++)
83 addParameter_(
new Parameter(
"Mixture.relproba" + TextTools::toString(i + 1), 1.0 / static_cast<double>(nbmod - i), &Parameter::PROP_CONSTRAINT_EX));
84 addParameter_(
new Parameter(
"Mixture.relrate" + TextTools::toString(i + 1), 1.0 / static_cast<double>(nbmod - i), &Parameter::PROP_CONSTRAINT_EX));
89 for (i = 0; i < nbmod; i++)
91 modelsContainer_[i]->setNamespace(
"Mixture." + TextTools::toString(i + 1) +
"_" + vpModel[i]->getNamespace());
92 addParameters_(vpModel[i]->getParameters());
95 for (i = 0; i < nbmod; i++)
97 vpModel[i]->addRateParameter();
104 const Alphabet* alpha,
105 vector<SubstitutionModel*> vpModel,
108 AbstractParameterAliasable(
"Mixture."),
111 size_t i, nbmod = vpModel.size();
113 for (i = 0; i < nbmod; i++)
116 throw Exception(
"Empty model number " + TextTools::toString(i) +
" in MixtureOfSubstitutionModels constructor");
117 for (
size_t j = i + 1; j < nbmod; j++)
119 if (vpModel[i] == vpModel[j])
120 throw Exception(
"Same model at positions " + TextTools::toString(i) +
" and " +
121 TextTools::toString(j) +
" in MixtureOfSubstitutionModels constructor");
128 for (i = 0; i < nbmod; i++)
131 throw Exception(
"Non positive rate: " + TextTools::toString(vrate[i]) +
" in MixtureOfSubstitutionModels constructor.");
133 throw Exception(
"Non positive probability: " + TextTools::toString(vproba[i]) +
" in MixtureOfSubstitutionModels constructor.");
135 y += vproba[i] * vrate[i];
138 if (fabs(1. - x) > NumConstants::SMALL())
139 throw Exception(
"Probabilities must equal 1 (sum = " + TextTools::toString(x) +
").");
140 if (fabs(1. - y) > NumConstants::SMALL())
141 throw Exception(
"Expectation on rates must equal 1 (E =" + TextTools::toString(y) +
").");
146 for (i = 0; i < nbmod; i++)
153 for (i = 0; i < nbmod; i++)
155 vProbas_.push_back(1.0 / static_cast<double>(nbmod));
165 for (i = 0; i < nbmod - 1; i++)
167 addParameter_(
new Parameter(
"Mixture.relproba" + TextTools::toString(i + 1), vproba[i] / (1 - x), &Parameter::PROP_CONSTRAINT_EX));
169 addParameter_(
new Parameter(
"Mixture.relrate" + TextTools::toString(i + 1), vproba[i] * vrate[i] / (1 - y), &Parameter::PROP_CONSTRAINT_EX));
170 y += vproba[i] * vrate[i];
175 for (i = 0; i < nbmod; i++)
177 modelsContainer_[i]->setNamespace(
"Mixture." + TextTools::toString(i + 1) +
"_" + vpModel[i]->getNamespace());
178 addParameters_(vpModel[i]->getParameters());
181 for (i = 0; i < nbmod; i++)
183 vpModel[i]->addRateParameter();
190 AbstractParameterAliasable(msm),
213 for (i = 0; i < nbmod - 1; i++)
215 y = getParameterValue(
"relproba" + TextTools::toString(i + 1));
224 for (i = 0; i < nbmod - 1; i++)
226 y = getParameterValue(
"relrate" + TextTools::toString(i + 1));
227 if (
vProbas_[i] < NumConstants::SMALL())
229 vRates_[i] = NumConstants::SMALL();
240 if (
vProbas_[nbmod - 1] < NumConstants::SMALL())
242 vRates_[nbmod - 1] = NumConstants::SMALL();
252 for (i = 0; i < nbmod; i++)
259 for (i = 0; i < nbmod; i++)
285 matchParametersValues(pl);
294 for (i = 0; i < nbmod - 1; i++)
300 for (i = 0; i < nbmod - 1; i++)
302 setParameterValue(
"relrate" + TextTools::toString(i + 1),
vProbas_[i] / sP *
vRates_[i] / (1 - y));
316 throw Exception(
"MixtureOfSubstitutionModels::getSubmodelNumbers model description do not match " + desc);
319 submodnb.push_back(static_cast<int>(i));
void setFreq(std::map< int, double > &)
applies setFreq to all the models of the mixture and recovers the parameters values.
virtual size_t getNumberOfModels() const
returns the number of models in the mixture
MixtureOfSubstitutionModels & operator=(const MixtureOfSubstitutionModels &)
virtual void setVRates(const Vdouble &vd)
Sets the rates of the submodels to be proportional to a given vector, with the constraint that the me...
Vdouble freq_
The vector of equilibrium frequencies.
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
virtual ~MixtureOfSubstitutionModels()
Partial implementation for Mixed Substitution models, defined as a mixture of "simple" substitution m...
virtual std::string getName() const =0
Get the name of the model.
AbstractMixedSubstitutionModel & operator=(const AbstractMixedSubstitutionModel &)
MixtureOfSubstitutionModels(const Alphabet *alpha, std::vector< SubstitutionModel *> vpModel)
Constructor of a MixtureOfSubstitutionModels, where all the models have rate 1 and equal probability...
Vint getSubmodelNumbers(std::string &desc) const
Returns the vector of numbers of the submodels in the mixture that match a description of the paramet...
std::vector< double > vRates_
vector of the rates of the models.
virtual void setVRates(const Vdouble &vd)
Sets the rates of the submodels to follow the constraint that the mean rate of the mixture equals rat...
virtual const SubstitutionModel * getNModel(size_t i) const
Returns a specific model from the mixture.
virtual size_t getNumberOfStates() const
From SubstitutionModel interface.
std::vector< SubstitutionModel * > modelsContainer_
vector of pointers to SubstitutionModels.
Substitution models defined as a mixture of several substitution models.
std::vector< double > vProbas_
vector of the probabilities of the models