5 #include "../../App/ApplicationTools.h" 15 std::shared_ptr<HmmStateAlphabet> hiddenAlphabet,
16 std::shared_ptr<HmmTransitionMatrix> transitionMatrix,
17 std::shared_ptr<HmmEmissionProbabilities> emissionProbabilities,
18 const std::string& prefix) :
21 hiddenAlphabet_(hiddenAlphabet),
22 transitionMatrix_(transitionMatrix),
23 emissionProbabilities_(emissionProbabilities),
28 backLikelihoodUpToDate_(false),
38 throw Exception(
"RescaledHmmLikelihood: null pointer passed for HmmStateAlphabet.");
39 if (!transitionMatrix)
40 throw Exception(
"RescaledHmmLikelihood: null pointer passed for HmmTransitionMatrix.");
41 if (!emissionProbabilities)
42 throw Exception(
"RescaledHmmLikelihood: null pointer passed for HmmEmissionProbabilities.");
44 throw Exception(
"RescaledHmmLikelihood: HmmTransitionMatrix and HmmEmissionProbabilities should point toward the same HmmStateAlphabet object.");
45 if (!
hiddenAlphabet_->worksWith(emissionProbabilities->hmmStateAlphabet()))
46 throw Exception(
"RescaledHmmLikelihood: HmmTransitionMatrix and HmmEmissionProbabilities should point toward the same HmmStateAlphabet object.");
81 if (alphabetChanged && !transitionsChanged)
83 if (alphabetChanged && !emissionChanged)
106 if (std::isnan(trans[ii + j]))
107 throw Exception(
"RescaledHmmLikelihood::computeForward_. NaN transition probability");
108 if (trans[ii + j] < 0)
115 const vector<double>* emissions = &(*emissionProbabilities_)(0);
125 tmp[j] = (*emissions)[j] * x;
137 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
142 for (
size_t i = 1; i <
nbSites_; i++)
145 size_t iip = (i - 1) * nbStates_;
147 emissions = &(*emissionProbabilities_)(i);
164 tmp[j] = (*emissions)[j] * x;
167 (*
ApplicationTools::warning <<
"Negative probability at " << i <<
", state " << j <<
": " << (*emissions)[j] <<
"\t" << x).endLine();
189 tmp[j] = (*emissions)[j] * x;
214 sort(lScales.begin(), lScales.end(), cmp);
216 for (
size_t i = 0; i <
nbSites_; ++i)
218 logLik_ += lScales[i];
229 for (
size_t i = 0; i <
nbSites_; i++)
250 const vector<double>* emissions = 0;
251 size_t nextBrkPt = 0;
252 vector<size_t>::const_reverse_iterator bpIt =
breakPoints_.rbegin();
263 for (
size_t i =
nbSites_ - 1; i > 0; i--)
265 emissions = &(*emissionProbabilities_)(i);
304 x += probs[i] * (*emissionProbabilities_)(site, i);
312 std::vector< std::vector<double>> vv;
316 for (
size_t i = 0; i <
nbSites_; i++)
321 ret[i] += vv[i][j] * (*emissionProbabilities_)(i, j);
348 size_t offset = append ? probs.size() : 0;
350 for (
size_t i = 0; i <
nbSites_; i++)
358 for (
size_t i = 0; i <
nbSites_; i++)
376 for (
size_t i = 0; i <
nbSites_; i++)
393 const vector<double>* emissions = &(*emissionProbabilities_)(0);
401 dScales_[0] += dTmp[j];
404 dLScales[0] = dScales_[0] /
scales_[0];
415 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
419 for (
size_t i = 1; i <
nbSites_; i++)
421 size_t iip = (i - 1) * nbStates_;
425 emissions = &(*emissionProbabilities_)(i);
438 tmp[j] = (*emissions)[j] * x;
441 dScales_[i] += dTmp[j];
451 dScales_[i] += dTmp[j];
461 dLScales[i] = dScales_[i] /
scales_[i];
470 sort(dLScales.begin(), dLScales.end(), cmp);
472 for (
size_t i = 0; i <
nbSites_; ++i)
474 dLogLik_ += dLScales[i];
492 for (
size_t i = 0; i <
nbSites_; i++)
509 const vector<double>* emissions = &(*emissionProbabilities_)(0);
517 d2Tmp[j] = (*d2Emissions)[j] *
transitionMatrix_->getEquilibriumFrequencies()[j];
519 d2Scales_[0] += d2Tmp[j];
533 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
537 for (
size_t i = 1; i <
nbSites_; i++)
541 emissions = &(*emissionProbabilities_)(i);
547 size_t iip = (i - 1) * nbStates_;
557 tmp[j] = (*emissions)[j] * x;
562 d2Scales_[i] += d2Tmp[j];
571 d2Tmp[j] = (*d2Emissions)[j] *
transitionMatrix_->getEquilibriumFrequencies()[j];
573 d2Scales_[i] += d2Tmp[j];
593 sort(d2LScales.begin(), d2LScales.end(), cmp);
595 for (
size_t i = 0; i <
nbSites_; ++i)
std::vector< size_t > breakPoints_
void computeDForward_() const
std::vector< double > likelihood_
The likelihood arrays.
std::vector< double > dScales_
double getLikelihoodForASite(size_t site) const override
Get the likelihood for a site, and its derivatives.
A partial implementation of the Parametrizable interface.
void computeD2Forward_() const
void setNamespace(const std::string &prefix) override
Set the namespace for the parameter names.
bool backLikelihoodUpToDate_
void setNamespace(const std::string &nameSpace) override
Set the namespace for the parameter names.
void getHiddenStatesPosteriorProbabilities(std::vector< std::vector< double >> &probs, bool append=false) const override
std::vector< std::vector< double > > backLikelihood_
backward likelihood
The parameter list object.
std::shared_ptr< HmmEmissionProbabilities > emissionProbabilities_
RescaledHmmLikelihood(std::shared_ptr< HmmStateAlphabet > hiddenAlphabet, std::shared_ptr< HmmTransitionMatrix > transitionMatrix, std::shared_ptr< HmmEmissionProbabilities > emissionProbabilities, const std::string &prefix)
Build a new RescaledHmmLikelihood object.
std::vector< double > d2Scales_
Matrix storage by column.
Vdouble getLikelihoodForEachSite() const override
Get the likelihood for each site.
void fireParameterChanged(const ParameterList &pl) override
Notify the class when one or several parameters have changed.
std::vector< double > Vdouble
std::vector< double > scales_
scales for likelihood computing
std::vector< std::vector< double > > d2Likelihood_
partial impmementation of Hmm Likelihoods.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
std::shared_ptr< HmmStateAlphabet > hiddenAlphabet_
The alphabet describing the hidden states.
std::shared_ptr< HmmTransitionMatrix > transitionMatrix_
double getDLogLikelihoodForASite(size_t site) const override
void computeBackward_() const
std::string toString(T t)
General template method to convert to a string.
Vdouble getHiddenStatesPosteriorProbabilitiesForASite(size_t site) const override
virtual void addParameters_(const ParameterList ¶meters)
double getD2LogLikelihoodForASite(size_t site) const override
std::vector< std::vector< double > > dLikelihood_
derivatec of forward likelihood