A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
More...
|
| 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 distribution for emissions. More...
|
|
| HMM (const arma::vec &initial, const arma::mat &transition, const std::vector< Distribution > &emission, const double tolerance=1e-5) |
| Create the Hidden Markov Model with the given initial probability vector, the given transition matrix, and the given emission distributions. More...
|
|
size_t | Dimensionality () const |
| Get the dimensionality of observations. More...
|
|
size_t & | Dimensionality () |
| Set the dimensionality of observations. More...
|
|
const std::vector< Distribution > & | Emission () const |
| Return the emission distributions. More...
|
|
std::vector< Distribution > & | Emission () |
| Return a modifiable emission probability matrix reference. More...
|
|
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, using the Forward-Backward algorithm. More...
|
|
double | Estimate (const arma::mat &dataSeq, arma::mat &stateProb) const |
| Estimate the probabilities of each hidden state at each time step of each given data observation, using the Forward-Backward algorithm. More...
|
|
void | Filter (const arma::mat &dataSeq, arma::mat &filterSeq, size_t ahead=0) const |
| HMM filtering. More...
|
|
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. More...
|
|
const arma::vec & | Initial () const |
| Return the vector of initial state probabilities. More...
|
|
arma::vec & | Initial () |
| Modify the vector of initial state probabilities. More...
|
|
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, using the Forward-Backward algorithm. More...
|
|
double | LogLikelihood (const arma::mat &dataSeq) const |
| Compute the log-likelihood of the given data sequence. More...
|
|
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 algorithm, returning the log-likelihood of the most likely state sequence. More...
|
|
|
void | serialize (Archive &ar, const unsigned int version) |
| Serialize the object. More...
|
|
void | Smooth (const arma::mat &dataSeq, arma::mat &smoothSeq) const |
| HMM smoothing. More...
|
|
double | Tolerance () const |
| Get the tolerance of the Baum-Welch algorithm. More...
|
|
double & | Tolerance () |
| Modify the tolerance of the Baum-Welch algorithm. More...
|
|
double | Train (const std::vector< arma::mat > &dataSeq) |
| Train the model using the Baum-Welch algorithm, with only the given unlabeled observations. More...
|
|
void | Train (const std::vector< arma::mat > &dataSeq, const std::vector< arma::Row< size_t > > &stateSeq) |
| Train the model using the given labeled observations; the transition and emission matrices are directly estimated. More...
|
|
const arma::mat & | Transition () const |
| Return the transition matrix. More...
|
|
arma::mat & | Transition () |
| Return a modifiable transition matrix reference. More...
|
|
template
<
typename
Distribution
=
distribution::DiscreteDistribution
>
class mlpack::hmm::HMM< Distribution >
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
This HMM class supports training (supervised and unsupervised), prediction of state sequences via the Viterbi algorithm, estimation of state probabilities, generation of random sequences, and calculation of the log-likelihood of a given sequence.
The template parameter, Distribution, specifies the distribution which the emissions follow. The class should implement the following functions:
class Distribution
{
public:
typedef something DataType;
double Probability(const DataType& observation) const;
double Train(
const std::vector<DataType>& observations);
double Train(
const std::vector<DataType>& observations,
const std::vector<double>& probabilities);
};
See the mlpack::distribution::DiscreteDistribution class for an example. One would use the DiscreteDistribution class when the observations are non-negative integers. Other distributions could be Gaussians, a mixture of Gaussians (GMM), or any other probability distribution implementing the four Distribution functions.
Usage of the HMM class generally involves either training an HMM or loading an already-known HMM and taking probability measurements of sequences. Example code for supervised training of a Gaussian HMM (that is, where the emission output distribution is a single Gaussian for each hidden state) is given below.
extern arma::mat observations;
extern arma::Row<size_t> states;
HMM<GaussianDistribution> hmm(5, GaussianDistribution(observations.n_rows));
hmm.Train(observations, states);
Once initialized, the HMM can evaluate the probability of a certain sequence (with LogLikelihood()), predict the most likely sequence of hidden states (with Predict()), generate a sequence (with Generate()), or estimate the probabilities of each state for a sequence of observations (with Train()).
- Template Parameters
-
Distribution | Type of emission distribution for this HMM. |
Definition at line 85 of file hmm.hpp.
HMM |
( |
const arma::vec & |
initial, |
|
|
const arma::mat & |
transition, |
|
|
const std::vector< Distribution > & |
emission, |
|
|
const double |
tolerance = 1e-5 |
|
) |
| |
Create the Hidden Markov Model with the given initial probability vector, the given transition matrix, and the given emission distributions.
The dimensionality of the observations of the HMM are taken from the given emission distributions. Alternately, the dimensionality can be set with Dimensionality().
The initial state probability vector should have length equal to the number of states, and each entry represents the probability of being in the given state at time T = 0 (the beginning of a sequence).
The transition matrix should be such that T(i, j) is the probability of transition to state i from state j. The columns of the matrix should sum to 1.
The emission matrix should be such that E(i, j) is the probability of emission i while in state j. The columns of the matrix should sum to 1.
Optionally, the tolerance for convergence of the Baum-Welch algorithm can be set.
- Parameters
-
initial | Initial state probabilities. |
transition | Transition matrix. |
emission | Emission distributions. |
tolerance | Tolerance for convergence of training algorithm (Baum-Welch). |
double Train |
( |
const std::vector< arma::mat > & |
dataSeq | ) |
|
Train the model using the Baum-Welch algorithm, with only the given unlabeled observations.
Instead of giving a guess transition and emission matrix here, do that in the constructor. Each matrix in the vector of data sequences holds an individual data sequence; each point in each individual data sequence should be a column in the matrix. The number of rows in each matrix should be equal to the dimensionality of the HMM (which is set in the constructor).
It is preferable to use the other overload of Train(), with labeled data. That will produce much better results. However, if labeled data is unavailable, this will work. In addition, it is possible to use Train() with labeled data first, and then continue to train the model using this overload of Train() with unlabeled data.
The tolerance of the Baum-Welch algorithm can be set either in the constructor or with the Tolerance() method. When the change in log-likelihood of the model between iterations is less than the tolerance, the Baum-Welch algorithm terminates.
- Note
- Train() can be called multiple times with different sequences; each time it is called, it uses the current parameters of the HMM as a starting point for training.
- Parameters
-
dataSeq | Vector of observation sequences. |
- Returns
- Log-likelihood of state sequence.