14 std::shared_ptr<HmmStateAlphabet> hiddenAlphabet,
15 std::shared_ptr<HmmTransitionMatrix> transitionMatrix,
16 std::shared_ptr<HmmEmissionProbabilities> emissionProbabilities,
17 const std::string& prefix) :
20 hiddenAlphabet_(hiddenAlphabet),
21 transitionMatrix_(transitionMatrix),
22 emissionProbabilities_(emissionProbabilities),
24 partialLogLikelihoods_(),
27 partialDLogLikelihoods_(),
29 partialD2LogLikelihoods_(),
31 backLogLikelihoodUpToDate_(false),
37 throw Exception(
"LogsumHmmLikelihood: null pointer passed for HmmStateAlphabet.");
38 if (!transitionMatrix)
39 throw Exception(
"LogsumHmmLikelihood: null pointer passed for HmmTransitionMatrix.");
40 if (!emissionProbabilities)
41 throw Exception(
"LogsumHmmLikelihood: null pointer passed for HmmEmissionProbabilities.");
44 throw Exception(
"LogsumHmmLikelihood: HmmTransitionMatrix and HmmEmissionProbabilities should point toward the same HmmStateAlphabet object.");
46 throw Exception(
"LogsumHmmLikelihood: HmmTransitionMatrix and HmmEmissionProbabilities should point toward the same HmmStateAlphabet object.");
82 if (alphabetChanged && !transitionsChanged)
84 if (alphabetChanged && !emissionChanged)
114 const vector<double>* emissions = &(*emissionProbabilities_)(0);
132 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
137 for (
size_t i = 1; i <
nbSites_; i++)
140 size_t iip = (i - 1) * nbStates_;
141 emissions = &(*emissionProbabilities_)(i);
150 a = logTrans[jj + k] + logLikelihood_[iip + k];
153 logLikelihood_[ii + j] = log((*emissions)[j]) + x;
196 sort(copy.begin(), copy.end());
197 for (
size_t i = copy.size(); i > 0; --i)
210 for (
size_t i = 0; i <
nbSites_; i++)
231 const vector<double>* emissions = 0;
232 size_t nextBrkPt = 0;
233 vector<size_t>::const_reverse_iterator bpIt =
breakPoints_.rbegin();
243 for (
size_t i =
nbSites_ - 1; i > 0; i--)
245 emissions = &(*emissionProbabilities_)(i);
254 x =
NumTools::logsum(x, log((*emissions)[k]) + logTrans[jj + k] + backLogLikelihood_[i][k]);
256 backLogLikelihood_[i - 1][j] = x;
261 for (
unsigned int j = 0; j <
nbStates_; j++)
285 x += probs[i] * (*emissionProbabilities_)(site, i);
293 std::vector< std::vector<double>> vv;
297 for (
size_t i = 0; i <
nbSites_; i++)
302 ret[i] += vv[i][j] * (*emissionProbabilities_)(i, j);
319 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
340 size_t offset = append ? probs.size() : 0;
342 for (
size_t i = 0; i <
nbSites_; i++)
351 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
356 for (
size_t i = 0; i <
nbSites_; i++)
384 for (
size_t i = 0; i <
nbSites_; i++)
398 const vector<double>* emissions = &(*emissionProbabilities_)(0);
408 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
413 for (
size_t i = 1; i <
nbSites_; i++)
415 size_t iip = (i - 1) * nbStates_;
417 emissions = &(*emissionProbabilities_)(i);
420 for (
size_t kp = 0; kp <
nbStates_; kp++)
456 for (
size_t kp = 0; kp <
nbStates_; kp++)
469 sort(copy.begin(), copy.end());
470 for (
size_t i = copy.size(); i > 0; --i)
492 for (
size_t i = 0; i <
nbSites_; i++)
506 const vector<double>* emissions = &(*emissionProbabilities_)(0);
512 d2LogLikelihood_[0][j] = (*d2Emissions)[j] / (*emissions)[j] - pow((*dEmissions)[j] / (*emissions)[j], 2);
517 vector<size_t>::const_iterator bpIt =
breakPoints_.begin();
522 for (
size_t i = 1; i <
nbSites_; i++)
524 size_t iip = (i - 1) * nbStates_;
526 emissions = &(*emissionProbabilities_)(i);
530 for (
size_t kp = 0; kp <
nbStates_; kp++)
562 d2LogLikelihood_[i][j] = (*d2Emissions)[j] / (*emissions)[j] - pow((*dEmissions)[j] / (*emissions)[j], 2);
575 for (
size_t kp = 0; kp <
nbStates_; kp++)
593 sort(copy.begin(), copy.end());
594 for (
size_t i = copy.size(); i > 0; --i)
std::shared_ptr< HmmEmissionProbabilities > emissionProbabilities_
void setNamespace(const std::string &nameSpace) override
Set the namespace for the parameter names.
std::vector< double > partialDLogLikelihoods_
void computeBackward_() const
std::vector< double > logLikelihood_
The likelihood array.
A partial implementation of the Parametrizable interface.
void setNamespace(const std::string &prefix) override
Set the namespace for the parameter names.
double getD2LogLikelihoodForASite(size_t site) const override
std::vector< std::vector< double > > d2LogLikelihood_
void fireParameterChanged(const ParameterList &pl) override
Notify the class when one or several parameters have changed.
std::shared_ptr< HmmTransitionMatrix > transitionMatrix_
The parameter list object.
Vdouble getLikelihoodForEachSite() const override
Get the likelihood for each site.
Matrix storage by column.
Vdouble getHiddenStatesPosteriorProbabilitiesForASite(size_t site) const override
void computeD2Forward_() const
std::vector< std::vector< double > > backLogLikelihood_
backward logLikelihood
std::vector< double > partialLogLikelihoods_
std::vector< double > Vdouble
std::vector< double > partialD2LogLikelihoods_
void getHiddenStatesPosteriorProbabilities(std::vector< std::vector< double >> &probs, bool append=false) const override
partial impmementation of Hmm Likelihoods.
std::shared_ptr< HmmStateAlphabet > hiddenAlphabet_
The alphabet describing the hidden states.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
bool backLogLikelihoodUpToDate_
double getLikelihoodForASite(size_t site) const override
Get the likelihood for a site, and its derivatives.
std::vector< std::vector< double > > dLogLikelihood_
The DLogLikelihood arrays.
std::vector< size_t > breakPoints_
LogsumHmmLikelihood(std::shared_ptr< HmmStateAlphabet > hiddenAlphabet, std::shared_ptr< HmmTransitionMatrix > transitionMatrix, std::shared_ptr< HmmEmissionProbabilities > emissionProbabilities, const std::string &prefix="")
Build a new LogsumHmmLikelihood object.
void computeDForward_() const
double getDLogLikelihoodForASite(size_t site) const override
virtual void addParameters_(const ParameterList ¶meters)