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 initialProxy; }
328  arma::vec& Initial()
329  {
330  recalculateInitial = true;
331  return initialProxy;
332  }
333 
335  const arma::mat& Transition() const { return transitionProxy; }
337  arma::mat& Transition()
338  {
339  recalculateTransition = true;
340  return transitionProxy;
341  }
342 
344  const std::vector<Distribution>& Emission() const { return emission; }
346  std::vector<Distribution>& Emission() { return emission; }
347 
349  size_t Dimensionality() const { return dimensionality; }
351  size_t& Dimensionality() { return dimensionality; }
352 
354  double Tolerance() const { return tolerance; }
356  double& Tolerance() { return tolerance; }
357 
361  template<typename Archive>
362  void load(Archive& ar, const unsigned int version);
363 
367  template<typename Archive>
368  void save(Archive& ar, const unsigned int version) const;
369 
371 
372 
373  protected:
374  // Helper functions.
385  void Forward(const arma::mat& dataSeq,
386  arma::vec& logScales,
387  arma::mat& forwardLogProb) const;
388 
400  void Backward(const arma::mat& dataSeq,
401  const arma::vec& logScales,
402  arma::mat& backwardLogProb) const;
403 
405  std::vector<Distribution> emission;
406 
411  arma::mat transitionProxy;
412 
414  mutable arma::mat logTransition;
415 
416  private:
422  void ConvertToLogSpace() const;
423 
428  arma::vec initialProxy;
429 
431  mutable arma::vec logInitial;
432 
434  size_t dimensionality;
435 
437  double tolerance;
438 
443  mutable bool recalculateInitial;
444 
449  mutable bool recalculateTransition;
450 };
451 
452 } // namespace hmm
453 } // namespace mlpack
454 
455 // Include implementation.
456 #include "hmm_impl.hpp"
457 
458 #endif
void Filter(const arma::mat &dataSeq, arma::mat &filterSeq, size_t ahead=0) const
HMM filtering.
arma::mat transitionProxy
A proxy variable in linear space for logTransition.
Definition: hmm.hpp:411
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:405
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:344
strip_type.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.
size_t & Dimensionality()
Set the dimensionality of observations.
Definition: hmm.hpp:351
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:354
BOOST_SERIALIZATION_SPLIT_MEMBER()
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...
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:337
arma::mat logTransition
Transition probability matrix. No need to be mutable in mlpack 4.0.
Definition: hmm.hpp:414
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:349
double & Tolerance()
Modify the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:356
void save(Archive &ar, const unsigned int version) const
Save the object.
const arma::mat & Transition() const
Return the transition matrix.
Definition: hmm.hpp:335
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.
void load(Archive &ar, const unsigned int version)
Load the object.
std::vector< Distribution > & Emission()
Return a modifiable emission probability matrix reference.
Definition: hmm.hpp:346