bpp-core3  3.0.0
LogsumHmmLikelihood.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_NUMERIC_HMM_LOGSUMHMMLIKELIHOOD_H
6 #define BPP_NUMERIC_HMM_LOGSUMHMMLIKELIHOOD_H
7 
8 
9 #include "../AbstractParametrizable.h"
10 #include "../Matrix/Matrix.h"
11 #include "../NumTools.h"
12 #include "HmmLikelihood.h"
13 
14 // From the STL:
15 #include <vector>
16 #include <memory>
17 
18 namespace bpp
19 {
32  public virtual AbstractHmmLikelihood,
34 {
35 protected:
39  std::shared_ptr<HmmStateAlphabet> hiddenAlphabet_;
40  std::shared_ptr<HmmTransitionMatrix> transitionMatrix_;
41  std::shared_ptr<HmmEmissionProbabilities> emissionProbabilities_;
42 
49  std::vector<double> logLikelihood_;
50  std::vector<double> partialLogLikelihoods_;
51  double logLik_;
52 
63  mutable std::vector< std::vector<double>> dLogLikelihood_;
64  mutable std::vector<double> partialDLogLikelihoods_;
65 
66  mutable std::vector< std::vector<double>> d2LogLikelihood_;
67  mutable std::vector<double> partialD2LogLikelihoods_;
68 
75  mutable std::vector<std::vector<double>> backLogLikelihood_;
77 
78  std::vector<size_t> breakPoints_;
79 
81 
82 public:
91  std::shared_ptr<HmmStateAlphabet> hiddenAlphabet,
92  std::shared_ptr<HmmTransitionMatrix> transitionMatrix,
93  std::shared_ptr<HmmEmissionProbabilities> emissionProbabilities,
94  const std::string& prefix = "");
95 
99  hiddenAlphabet_(),
100  transitionMatrix_(),
101  emissionProbabilities_(),
102  logLikelihood_(lik.logLikelihood_),
103  partialLogLikelihoods_(lik.partialLogLikelihoods_),
104  logLik_(lik.logLik_),
105  dLogLikelihood_(lik.dLogLikelihood_),
106  partialDLogLikelihoods_(lik.partialDLogLikelihoods_),
107  d2LogLikelihood_(lik.d2LogLikelihood_),
108  partialD2LogLikelihoods_(lik.partialD2LogLikelihoods_),
109  backLogLikelihood_(lik.backLogLikelihood_),
110  backLogLikelihoodUpToDate_(lik.backLogLikelihoodUpToDate_),
111  breakPoints_(lik.breakPoints_),
112  nbStates_(lik.nbStates_),
113  nbSites_(lik.nbSites_)
114  {
115  hiddenAlphabet_ = std::shared_ptr<HmmStateAlphabet>(lik.hiddenAlphabet_->clone());
116  transitionMatrix_ = std::shared_ptr<HmmTransitionMatrix>(lik.transitionMatrix_->clone());
117  emissionProbabilities_ = std::shared_ptr<HmmEmissionProbabilities>(lik.emissionProbabilities_->clone());
118 
119  // Now adjust pointers:
120  transitionMatrix_->setHmmStateAlphabet(hiddenAlphabet_);
121  emissionProbabilities_->setHmmStateAlphabet(hiddenAlphabet_);
122  }
123 
125  {
127  AbstractParametrizable::operator=(lik);
128 
129  hiddenAlphabet_ = std::shared_ptr<HmmStateAlphabet>(lik.hiddenAlphabet_->clone());
130  transitionMatrix_ = std::shared_ptr<HmmTransitionMatrix>(lik.transitionMatrix_->clone());
131  emissionProbabilities_ = std::shared_ptr<HmmEmissionProbabilities>(lik.emissionProbabilities_->clone());
132 
133  logLikelihood_ = lik.logLikelihood_;
134  partialLogLikelihoods_ = lik.partialLogLikelihoods_;
135  dLogLikelihood_ = lik.dLogLikelihood_;
136  partialDLogLikelihoods_ = lik.partialDLogLikelihoods_;
137  d2LogLikelihood_ = lik.d2LogLikelihood_;
138  partialD2LogLikelihoods_ = lik.partialD2LogLikelihoods_;
139  backLogLikelihood_ = lik.backLogLikelihood_;
140  backLogLikelihoodUpToDate_ = lik.backLogLikelihoodUpToDate_;
141  logLik_ = lik.logLik_;
142  breakPoints_ = lik.breakPoints_;
143  nbStates_ = lik.nbStates_;
144  nbSites_ = lik.nbSites_;
145 
146  // Now adjust pointers:
147  transitionMatrix_->setHmmStateAlphabet(hiddenAlphabet_);
148  emissionProbabilities_->setHmmStateAlphabet(hiddenAlphabet_);
149  return *this;
150  }
151 
152  virtual ~LogsumHmmLikelihood() {}
153 
154 
155  LogsumHmmLikelihood* clone() const override { return new LogsumHmmLikelihood(*this); }
156 
157 public:
158  const HmmStateAlphabet& hmmStateAlphabet() const override { return *hiddenAlphabet_; }
159  std::shared_ptr<const HmmStateAlphabet> getHmmStateAlphabet() const override { return hiddenAlphabet_; }
160 
162  std::shared_ptr<HmmStateAlphabet> getHmmStateAlphabet() override { return hiddenAlphabet_; }
163 
164  const HmmTransitionMatrix& hmmTransitionMatrix() const override { return *transitionMatrix_; }
165  std::shared_ptr<const HmmTransitionMatrix> getHmmTransitionMatrix() const override { return transitionMatrix_; }
166 
168  std::shared_ptr<HmmTransitionMatrix> getHmmTransitionMatrix() override { return transitionMatrix_; }
169 
171  std::shared_ptr<const HmmEmissionProbabilities> getHmmEmissionProbabilities() const override { return emissionProbabilities_; }
172 
174  std::shared_ptr<HmmEmissionProbabilities> getHmmEmissionProbabilities() override { return emissionProbabilities_; }
175 
176  void setBreakPoints(const std::vector<size_t>& breakPoints) override
177  {
178  breakPoints_ = breakPoints;
179  computeForward_();
180  backLogLikelihoodUpToDate_ = false;
181  }
182 
183  const std::vector<size_t>& getBreakPoints() const override { return breakPoints_; }
184 
185  void setParameters(const ParameterList& pl) override
186  {
188  }
189 
190  double getValue() const override { return -logLik_; }
191 
192  double getLogLikelihood() const override { return logLik_; }
193 
194  double getLikelihoodForASite(size_t site) const override;
195 
196  double getDLogLikelihoodForASite(size_t site) const override;
197 
198  double getD2LogLikelihoodForASite(size_t site) const override;
199 
200  Vdouble getLikelihoodForEachSite() const override;
201 
202  void setNamespace(const std::string& nameSpace) override;
203 
204  void fireParameterChanged(const ParameterList& pl) override;
205 
206  Vdouble getHiddenStatesPosteriorProbabilitiesForASite(size_t site) const override;
207 
208  void getHiddenStatesPosteriorProbabilities(std::vector< std::vector<double>>& probs, bool append = false) const override;
209 
210  void computeLikelihood();
211 
212 protected:
213  void computeForward_();
214 
215  void computeBackward_() const;
216 
217  void computeDLikelihood_() const override
218  {
220  }
221 
222  void computeD2Likelihood_() const override
223  {
225  }
226 
227  void computeDForward_() const;
228 
229  void computeD2Forward_() const;
230 };
231 }
232 #endif // BPP_NUMERIC_HMM_LOGSUMHMMLIKELIHOOD_H
Hidden states alphabet.
HmmTransitionMatrix & hmmTransitionMatrix() override
std::shared_ptr< HmmEmissionProbabilities > emissionProbabilities_
void setNamespace(const std::string &nameSpace) override
Set the namespace for the parameter names.
std::vector< double > partialDLogLikelihoods_
std::vector< double > logLikelihood_
The likelihood array.
std::shared_ptr< const HmmStateAlphabet > getHmmStateAlphabet() const override
A simple implementation of hidden Markov models recursion.
A partial implementation of the Parametrizable interface.
const HmmStateAlphabet & hmmStateAlphabet() const override
Interface for computing emission probabilities in a Hidden Markov Model.
double getD2LogLikelihoodForASite(size_t site) const override
std::vector< std::vector< double > > d2LogLikelihood_
std::shared_ptr< HmmTransitionMatrix > getHmmTransitionMatrix() override
void fireParameterChanged(const ParameterList &pl) override
Notify the class when one or several parameters have changed.
std::shared_ptr< HmmTransitionMatrix > transitionMatrix_
const HmmTransitionMatrix & hmmTransitionMatrix() const override
std::shared_ptr< HmmEmissionProbabilities > getHmmEmissionProbabilities() override
The parameter list object.
Definition: ParameterList.h:27
HmmStateAlphabet & hmmStateAlphabet() override
Vdouble getLikelihoodForEachSite() const override
Get the likelihood for each site.
void setParameters(const ParameterList &pl) override
Set the point where the function must be computed.
std::shared_ptr< const HmmTransitionMatrix > getHmmTransitionMatrix() const override
void setParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
const std::vector< size_t > & getBreakPoints() const override
Vdouble getHiddenStatesPosteriorProbabilitiesForASite(size_t site) const override
std::vector< std::vector< double > > backLogLikelihood_
backward logLikelihood
std::vector< double > partialLogLikelihoods_
std::vector< double > Vdouble
Definition: VectorTools.h:34
std::vector< double > partialD2LogLikelihoods_
void computeDLikelihood_() const override
void getHiddenStatesPosteriorProbabilities(std::vector< std::vector< double >> &probs, bool append=false) const override
partial impmementation of Hmm Likelihoods.
HmmEmissionProbabilities & hmmEmissionProbabilities() override
std::shared_ptr< HmmStateAlphabet > hiddenAlphabet_
The alphabet describing the hidden states.
double getLogLikelihood() const override
AbstractHmmLikelihood & operator=(const AbstractHmmLikelihood &adhlik)
std::shared_ptr< const HmmEmissionProbabilities > getHmmEmissionProbabilities() const override
void setBreakPoints(const std::vector< size_t > &breakPoints) override
std::shared_ptr< HmmStateAlphabet > getHmmStateAlphabet() override
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.
LogsumHmmLikelihood & operator=(const LogsumHmmLikelihood &lik)
void computeD2Likelihood_() const override
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.
double getDLogLikelihoodForASite(size_t site) const override
Describe the transition probabilities between hidden states of a Hidden Markov Model.
LogsumHmmLikelihood * clone() const override
Create a copy of this object and send a pointer to it.
const HmmEmissionProbabilities & hmmEmissionProbabilities() const override
LogsumHmmLikelihood(const LogsumHmmLikelihood &lik)
double getValue() const override
Get the value of the function at the current point.