mlpack  1.0.12
hmm.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_HMM_HMM_HPP
16 #define __MLPACK_METHODS_HMM_HMM_HPP
17 
18 #include <mlpack/core.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,
106  const Distribution emissions,
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 
169  void Train(const std::vector<arma::mat>& dataSeq);
170 
192  void Train(const std::vector<arma::mat>& dataSeq,
193  const std::vector<arma::Col<size_t> >& stateSeq);
194 
213  double Estimate(const arma::mat& dataSeq,
214  arma::mat& stateProb,
215  arma::mat& forwardProb,
216  arma::mat& backwardProb,
217  arma::vec& scales) const;
218 
230  double Estimate(const arma::mat& dataSeq,
231  arma::mat& stateProb) const;
232 
244  void Generate(const size_t length,
245  arma::mat& dataSequence,
246  arma::Col<size_t>& stateSequence,
247  const size_t startState = 0) const;
248 
259  double Predict(const arma::mat& dataSeq,
260  arma::Col<size_t>& stateSeq) const;
261 
268  double LogLikelihood(const arma::mat& dataSeq) const;
269 
271  const arma::vec& Initial() const { return initial; }
273  arma::vec& Initial() { return initial; }
274 
276  const arma::mat& Transition() const { return transition; }
278  arma::mat& Transition() { return transition; }
279 
281  const std::vector<Distribution>& Emission() const { return emission; }
283  std::vector<Distribution>& Emission() { return emission; }
284 
286  size_t Dimensionality() const { return dimensionality; }
288  size_t& Dimensionality() { return dimensionality; }
289 
291  double Tolerance() const { return tolerance; }
293  double& Tolerance() { return tolerance; }
294 
298  std::string ToString() const;
299 
300  private:
301  // Helper functions.
302 
313  void Forward(const arma::mat& dataSeq,
314  arma::vec& scales,
315  arma::mat& forwardProb) const;
316 
328  void Backward(const arma::mat& dataSeq,
329  const arma::vec& scales,
330  arma::mat& backwardProb) const;
331 
333  arma::vec initial;
334 
336  arma::mat transition;
337 
339  std::vector<Distribution> emission;
340 
343 
345  double tolerance;
346 };
347 
348 }; // namespace hmm
349 }; // namespace mlpack
350 
351 // Include implementation.
352 #include "hmm_impl.hpp"
353 
354 #endif
arma::vec & Initial()
Modify the vector of initial state probabilities.
Definition: hmm.hpp:273
size_t Dimensionality() const
Get the dimensionality of observations.
Definition: hmm.hpp:286
std::vector< Distribution > emission
Set of emission probability distributions; one for each state.
Definition: hmm.hpp:339
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
const arma::vec & Initial() const
Return the vector of initial state probabilities.
Definition: hmm.hpp:271
size_t & Dimensionality()
Set the dimensionality of observations.
Definition: hmm.hpp:288
const arma::mat & Transition() const
Return the transition matrix.
Definition: hmm.hpp:276
std::vector< Distribution > & Emission()
Return a modifiable emission probability matrix reference.
Definition: hmm.hpp:283
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:345
arma::vec initial
Initial state probability vector.
Definition: hmm.hpp:333
double & Tolerance()
Modify the tolerance of the Baum-Welch algorithm.
Definition: hmm.hpp:293
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:291
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:85
std::string ToString() const
Returns a string representation of this object.
const std::vector< Distribution > & Emission() const
Return the emission distributions.
Definition: hmm.hpp:281
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:342
arma::mat & Transition()
Return a modifiable transition matrix reference.
Definition: hmm.hpp:278
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:336