14 #ifndef MLPACK_METHODS_HMM_HMM_HPP 15 #define MLPACK_METHODS_HMM_HMM_HPP 84 template<
typename Distribution = distribution::DiscreteDistribution>
105 HMM(
const size_t states = 0,
106 const Distribution emissions = Distribution(),
107 const double tolerance = 1e-5);
136 HMM(
const arma::vec& initial,
137 const arma::mat& transition,
138 const std::vector<Distribution>&
emission,
139 const double tolerance = 1e-5);
169 double Train(
const std::vector<arma::mat>& dataSeq);
191 void Train(
const std::vector<arma::mat>& dataSeq,
192 const std::vector<arma::Row<size_t> >& stateSeq);
213 arma::mat& stateLogProb,
214 arma::mat& forwardLogProb,
215 arma::mat& backwardLogProb,
216 arma::vec& logScales)
const;
236 double Estimate(
const arma::mat& dataSeq,
237 arma::mat& stateProb,
238 arma::mat& forwardProb,
239 arma::mat& backwardProb,
240 arma::vec& scales)
const;
253 double Estimate(
const arma::mat& dataSeq,
254 arma::mat& stateProb)
const;
268 arma::mat& dataSequence,
269 arma::Row<size_t>& stateSequence,
270 const size_t startState = 0)
const;
282 double Predict(
const arma::mat& dataSeq,
283 arma::Row<size_t>& stateSeq)
const;
308 arma::vec& forwardLogProb)
const;
327 double &logLikelihood,
328 arma::vec& forwardLogProb)
const;
346 arma::vec& forwardLogProb)
const;
364 double &logLikelihood,
365 arma::vec& forwardLogProb)
const;
378 void Filter(
const arma::mat& dataSeq,
379 arma::mat& filterSeq,
380 size_t ahead = 0)
const;
393 void Smooth(
const arma::mat& dataSeq,
394 arma::mat& smoothSeq)
const;
397 const arma::vec&
Initial()
const {
return initialProxy; }
401 recalculateInitial =
true;
410 recalculateTransition =
true;
432 template<
typename Archive>
433 void load(Archive& ar,
const uint32_t version);
438 template<
typename Archive>
439 void save(Archive& ar,
const uint32_t version)
const;
449 arma::vec
ForwardAtT0(
const arma::vec& emissionLogProb,
450 double& logScales)
const;
460 arma::vec
ForwardAtTn(
const arma::vec& emissionLogProb,
462 const arma::vec& prevForwardLogProb)
const;
475 void Forward(
const arma::mat& dataSeq,
476 arma::vec& logScales,
477 arma::mat& forwardLogProb,
478 arma::mat& logProbs)
const;
491 void Backward(
const arma::mat& dataSeq,
492 const arma::vec& logScales,
493 arma::mat& backwardLogProb,
494 arma::mat& logProbs)
const;
514 void ConvertToLogSpace()
const;
520 arma::vec initialProxy;
523 mutable arma::vec logInitial;
526 size_t dimensionality;
535 mutable bool recalculateInitial;
541 mutable bool recalculateTransition;
548 #include "hmm_impl.hpp" 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.
const arma::vec & Initial() const
Return the vector of initial state probabilities.
std::vector< Distribution > emission
Set of emission probability distributions; one for each state.
arma::vec & Initial()
Modify the vector of initial state probabilities.
arma::vec ForwardAtTn(const arma::vec &emissionLogProb, double &logScales, const arma::vec &prevForwardLogProb) const
Given emission probabilities, computes forward probabilities for time t>0.
const std::vector< Distribution > & Emission() const
Return the emission distributions.
Linear algebra utility functions, generally performed on matrices or vectors.
void save(Archive &ar, const uint32_t version) const
Save the object.
double EmissionLogScaleFactor(const arma::vec &emissionLogProb, arma::vec &forwardLogProb) const
Compute the log of the scaling factor of the given emission probability at time t.
void Backward(const arma::mat &dataSeq, const arma::vec &logScales, arma::mat &backwardLogProb, arma::mat &logProbs) const
The Backward algorithm (part of the Forward-Backward algorithm).
double LogScaleFactor(const arma::vec &data, arma::vec &forwardLogProb) const
Compute the log of the scaling factor of the given data at time t.
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.
double Tolerance() const
Get the tolerance of the Baum-Welch algorithm.
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::vec ForwardAtT0(const arma::vec &emissionLogProb, double &logScales) const
Given emission probabilities, computes forward probabilities at time t=0.
arma::mat & Transition()
Return a modifiable transition matrix reference.
arma::mat logTransition
Transition probability matrix. No need to be mutable in mlpack 4.0.
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.
double & Tolerance()
Modify the tolerance of the Baum-Welch algorithm.
const arma::mat & Transition() const
Return the transition matrix.
void load(Archive &ar, const uint32_t version)
Load the object.
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
double EmissionLogLikelihood(const arma::vec &emissionLogProb, double &logLikelihood, arma::vec &forwardLogProb) const
Compute the log-likelihood of the given emission probability up to time t, storing the result in logL...
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 Forward(const arma::mat &dataSeq, arma::vec &logScales, arma::mat &forwardLogProb, arma::mat &logProbs) const
The Forward algorithm (part of the Forward-Backward algorithm).
std::vector< Distribution > & Emission()
Return a modifiable emission probability matrix reference.