hmm.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_HMM_HMM_HPP
15 #define MLPACK_METHODS_HMM_HMM_HPP
16 
17 #include <mlpack/prereqs.hpp>
19 
20 namespace mlpack {
21 namespace hmm {
22 
84 template<typename Distribution = distribution::DiscreteDistribution>
85 class HMM
86 {
87  public:
105  HMM(const size_t states = 0,
106  const Distribution emissions = Distribution(),
107  const double tolerance = 1e-5);
108 
136  HMM(const arma::vec& initial,
137  const arma::mat& transition,
138  const std::vector<Distribution>& emission,
139  const double tolerance = 1e-5);
140 
170  double Train(const std::vector<arma::mat>& dataSeq);
171 
193  void Train(const std::vector<arma::mat>& dataSeq,
194  const std::vector<arma::Row<size_t> >& stateSeq);
195 
214  double LogEstimate(const arma::mat& dataSeq,
215  arma::mat& stateLogProb,
216  arma::mat& forwardLogProb,
217  arma::mat& backwardLogProb,
218  arma::vec& logScales) const;
219 
238  double Estimate(const arma::mat& dataSeq,
239  arma::mat& stateProb,
240  arma::mat& forwardProb,
241  arma::mat& backwardProb,
242  arma::vec& scales) const;
243 
255  double Estimate(const arma::mat& dataSeq,
256  arma::mat& stateProb) const;
257 
269  void Generate(const size_t length,
270  arma::mat& dataSequence,
271  arma::Row<size_t>& stateSequence,
272  const size_t startState = 0) const;
273 
284  double Predict(const arma::mat& dataSeq,
285  arma::Row<size_t>& stateSeq) const;
286 
293  double LogLikelihood(const arma::mat& dataSeq) const;
294 
307  void Filter(const arma::mat& dataSeq,
308  arma::mat& filterSeq,
309  size_t ahead = 0) const;
310 
322  void Smooth(const arma::mat& dataSeq,
323  arma::mat& smoothSeq) const;
324 
326  const arma::vec& Initial() const { return initial; }
328  arma::vec& Initial() { return initial; }
329 
331  const arma::mat& Transition() const { return transition; }
333  arma::mat& Transition() { return transition; }
334 
336  const std::vector<Distribution>& Emission() const { return emission; }
338  std::vector<Distribution>& Emission() { return emission; }
339 
341  size_t Dimensionality() const { return dimensionality; }
343  size_t& Dimensionality() { return dimensionality; }
344 
346  double Tolerance() const { return tolerance; }
348  double& Tolerance() { return tolerance; }
349 
353  template<typename Archive>
354  void serialize(Archive& ar, const unsigned int version);
355 
356  protected:
357  // Helper functions.
368  void Forward(const arma::mat& dataSeq,
369  arma::vec& logScales,
370  arma::mat& forwardLogProb) const;
371 
383  void Backward(const arma::mat& dataSeq,
384  const arma::vec& logScales,
385  arma::mat& backwardLogProb) const;
386 
388  std::vector<Distribution> emission;
389 
391  arma::mat transition;
392 
393  private:
395  arma::vec initial;
396 
398  size_t dimensionality;
399 
401  double tolerance;
402 };
403 
404 } // namespace hmm
405 } // namespace mlpack
406 
407 // Include implementation.
408 #include "hmm_impl.hpp"
409 
410 #endif
void Filter(const arma::mat &dataSeq, arma::mat &filterSeq, size_t ahead=0) const
HMM filtering.
const arma::vec & Initial() const
Return the vector of initial state probabilities.
Definition: hmm.hpp:326
std::vector< Distribution > emission
Set of emission probability distributions; one for each state.
Definition: hmm.hpp:388
arma::vec & Initial()
Modify the vector of initial state probabilities.
Definition: hmm.hpp:328
const std::vector< Distribution > & Emission() const
Return the emission distributions.
Definition: hmm.hpp:336
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Generate(const size_t length, arma::mat &dataSequence, arma::Row< size_t > &stateSequence, const size_t startState=0) const
Generate a random data sequence of the given length.
void serialize(Archive &ar, const unsigned int version)
Serialize the object.
size_t & Dimensionality()
Set the dimensionality of observations.
Definition: hmm.hpp:343
void Forward(const arma::mat &dataSeq, arma::vec &logScales, arma::mat &forwardLogProb) const
The Forward algorithm (part of the Forward-Backward algorithm).
double Tolerance() const
Get the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:346
HMM(const size_t states=0, const Distribution emissions=Distribution(), const double tolerance=1e-5)
Create the Hidden Markov Model with the given number of hidden states and the given default distribut...
double LogEstimate(const arma::mat &dataSeq, arma::mat &stateLogProb, arma::mat &forwardLogProb, arma::mat &backwardLogProb, arma::vec &logScales) const
Estimate the probabilities of each hidden state at each time step for each given data observation...
arma::mat transition
Transition probability matrix.
Definition: hmm.hpp:391
double Estimate(const arma::mat &dataSeq, arma::mat &stateProb, arma::mat &forwardProb, arma::mat &backwardProb, arma::vec &scales) const
Estimate the probabilities of each hidden state at each time step for each given data observation...
double Predict(const arma::mat &dataSeq, arma::Row< size_t > &stateSeq) const
Compute the most probable hidden state sequence for the given data sequence, using the Viterbi algori...
arma::mat & Transition()
Return a modifiable transition matrix reference.
Definition: hmm.hpp:333
double LogLikelihood(const arma::mat &dataSeq) const
Compute the log-likelihood of the given data sequence.
size_t Dimensionality() const
Get the dimensionality of observations.
Definition: hmm.hpp:341
double & Tolerance()
Modify the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:348
const arma::mat & Transition() const
Return the transition matrix.
Definition: hmm.hpp:331
void Backward(const arma::mat &dataSeq, const arma::vec &logScales, arma::mat &backwardLogProb) const
The Backward algorithm (part of the Forward-Backward algorithm).
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:85
void Smooth(const arma::mat &dataSeq, arma::mat &smoothSeq) const
HMM smoothing.
double Train(const std::vector< arma::mat > &dataSeq)
Train the model using the Baum-Welch algorithm, with only the given unlabeled observations.
std::vector< Distribution > & Emission()
Return a modifiable emission probability matrix reference.
Definition: hmm.hpp:338