MLPACK  1.0.11
hmm.hpp
Go to the documentation of this file.
1 
23 #ifndef __MLPACK_METHODS_HMM_HMM_HPP
24 #define __MLPACK_METHODS_HMM_HMM_HPP
25 
26 #include <mlpack/core.hpp>
27 
28 namespace mlpack {
29 namespace hmm {
30 
92 template<typename Distribution = distribution::DiscreteDistribution>
93 class HMM
94 {
95  public:
113  HMM(const size_t states,
114  const Distribution emissions,
115  const double tolerance = 1e-5);
116 
144  HMM(const arma::vec& initial,
145  const arma::mat& transition,
146  const std::vector<Distribution>& emission,
147  const double tolerance = 1e-5);
148 
177  void Train(const std::vector<arma::mat>& dataSeq);
178 
200  void Train(const std::vector<arma::mat>& dataSeq,
201  const std::vector<arma::Col<size_t> >& stateSeq);
202 
221  double Estimate(const arma::mat& dataSeq,
222  arma::mat& stateProb,
223  arma::mat& forwardProb,
224  arma::mat& backwardProb,
225  arma::vec& scales) const;
226 
238  double Estimate(const arma::mat& dataSeq,
239  arma::mat& stateProb) const;
240 
252  void Generate(const size_t length,
253  arma::mat& dataSequence,
254  arma::Col<size_t>& stateSequence,
255  const size_t startState = 0) const;
256 
267  double Predict(const arma::mat& dataSeq,
268  arma::Col<size_t>& stateSeq) const;
269 
276  double LogLikelihood(const arma::mat& dataSeq) const;
277 
279  const arma::vec& Initial() const { return initial; }
281  arma::vec& Initial() { return initial; }
282 
284  const arma::mat& Transition() const { return transition; }
286  arma::mat& Transition() { return transition; }
287 
289  const std::vector<Distribution>& Emission() const { return emission; }
291  std::vector<Distribution>& Emission() { return emission; }
292 
294  size_t Dimensionality() const { return dimensionality; }
296  size_t& Dimensionality() { return dimensionality; }
297 
299  double Tolerance() const { return tolerance; }
301  double& Tolerance() { return tolerance; }
302 
306  std::string ToString() const;
307 
308  private:
309  // Helper functions.
310 
321  void Forward(const arma::mat& dataSeq,
322  arma::vec& scales,
323  arma::mat& forwardProb) const;
324 
336  void Backward(const arma::mat& dataSeq,
337  const arma::vec& scales,
338  arma::mat& backwardProb) const;
339 
341  arma::vec initial;
342 
344  arma::mat transition;
345 
347  std::vector<Distribution> emission;
348 
351 
353  double tolerance;
354 };
355 
356 }; // namespace hmm
357 }; // namespace mlpack
358 
359 // Include implementation.
360 #include "hmm_impl.hpp"
361 
362 #endif
arma::vec & Initial()
Modify the vector of initial state probabilities.
Definition: hmm.hpp:281
size_t Dimensionality() const
Get the dimensionality of observations.
Definition: hmm.hpp:294
std::vector< Distribution > emission
Set of emission probability distributions; one for each state.
Definition: hmm.hpp:347
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
const arma::vec & Initial() const
Return the vector of initial state probabilities.
Definition: hmm.hpp:279
size_t & Dimensionality()
Set the dimensionality of observations.
Definition: hmm.hpp:296
const arma::mat & Transition() const
Return the transition matrix.
Definition: hmm.hpp:284
std::vector< Distribution > & Emission()
Return a modifiable emission probability matrix reference.
Definition: hmm.hpp:291
void Forward(const arma::mat &dataSeq, arma::vec &scales, arma::mat &forwardProb) const
The Forward algorithm (part of the Forward-Backward algorithm).
double tolerance
Tolerance of Baum-Welch algorithm.
Definition: hmm.hpp:353
arma::vec initial
Initial state probability vector.
Definition: hmm.hpp:341
double & Tolerance()
Modify the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:301
double LogLikelihood(const arma::mat &dataSeq) const
Compute the log-likelihood of the given data sequence.
double Tolerance() const
Get the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:299
void Generate(const size_t length, arma::mat &dataSequence, arma::Col< size_t > &stateSequence, const size_t startState=0) const
Generate a random data sequence of the given length.
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:93
std::string ToString() const
Returns a string representation of this object.
const std::vector< Distribution > & Emission() const
Return the emission distributions.
Definition: hmm.hpp:289
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...
void Backward(const arma::mat &dataSeq, const arma::vec &scales, arma::mat &backwardProb) const
The Backward algorithm (part of the Forward-Backward algorithm).
void Train(const std::vector< arma::mat > &dataSeq)
Train the model using the Baum-Welch algorithm, with only the given unlabeled observations.
size_t dimensionality
Dimensionality of observations.
Definition: hmm.hpp:350
arma::mat & Transition()
Return a modifiable transition matrix reference.
Definition: hmm.hpp:286
double Predict(const arma::mat &dataSeq, arma::Col< size_t > &stateSeq) const
Compute the most probable hidden state sequence for the given data sequence, using the Viterbi algori...
HMM(const size_t states, const Distribution emissions, const double tolerance=1e-5)
Create the Hidden Markov Model with the given number of hidden states and the given default distribut...
arma::mat transition
Transition probability matrix.
Definition: hmm.hpp:344