[mlpack] 94/149: Implementation of HMMRegression class
Barak A. Pearlmutter
barak+git at pearlmutter.net
Sat May 2 09:11:13 UTC 2015
This is an automated email from the git hooks/post-receive script.
bap pushed a commit to branch svn-trunk
in repository mlpack.
commit cb8ee31ac9fdc5a31c0910aef2e1c38822f47713
Author: michaelfox99 <michaelfox99 at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date: Sat Nov 15 19:34:22 2014 +0000
Implementation of HMMRegression class
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17371 9d5b8971-822b-0410-80eb-d18c1038ef23
---
src/mlpack/methods/hmm/hmm_regression_impl.hpp | 191 +++++++++++++++++++++++++
1 file changed, 191 insertions(+)
diff --git a/src/mlpack/methods/hmm/hmm_regression_impl.hpp b/src/mlpack/methods/hmm/hmm_regression_impl.hpp
new file mode 100644
index 0000000..0da576c
--- /dev/null
+++ b/src/mlpack/methods/hmm/hmm_regression_impl.hpp
@@ -0,0 +1,191 @@
+/**
+ * @file hmm_regression_impl.hpp
+ * @author Ryan Curtin
+ * @author Tran Quoc Long
+ * @author Michael Fox
+ *
+ * Implementation of HMMRegression class.
+ */
+#ifndef __MLPACK_METHODS_HMM_HMM_REGRESSION_IMPL_HPP
+#define __MLPACK_METHODS_HMM_HMM_REGRESSION_IMPL_HPP
+
+// Just in case...
+#include "hmm_regression.hpp"
+
+namespace mlpack {
+namespace hmm {
+
+void HMMRegression::Train(const std::vector<arma::mat>& predictors,
+ const std::vector<arma::vec>& responses)
+{
+ std::vector<arma::mat> dataSeq;
+ StackData(predictors, responses, dataSeq);
+ this->HMM::Train(dataSeq);
+}
+
+void HMMRegression::Train(const std::vector<arma::mat>& predictors,
+ const std::vector<arma::vec>& responses,
+ const std::vector<arma::Col<size_t> >& stateSeq)
+{
+ std::vector<arma::mat> dataSeq;
+ StackData(predictors, responses, dataSeq);
+ this->HMM::Train(dataSeq, stateSeq);
+}
+
+/**
+ * Estimate the probabilities of each hidden state at each time step for each
+ * given data observation.
+ */
+double HMMRegression::Estimate(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::mat& stateProb,
+ arma::mat& forwardProb,
+ arma::mat& backwardProb,
+ arma::vec& scales) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ return this->HMM::Estimate(dataSeq, stateProb, forwardProb,
+ backwardProb, scales);
+}
+
+/**
+ * Estimate the probabilities of each hidden state at each time step for each
+ * given data observation.
+ */
+double HMMRegression::Estimate(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::mat& stateProb) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ return this->HMM::Estimate(dataSeq, stateProb);
+}
+
+/**
+ * Compute the most probable hidden state sequence for the given observation
+ * using the Viterbi algorithm. Returns the log-likelihood of the most likely
+ * sequence.
+ */
+double HMMRegression::Predict(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::Col<size_t>& stateSeq) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ return this->HMM::Predict(dataSeq, stateSeq);
+}
+
+/**
+ * Compute the log-likelihood of the given data sequence.
+ */
+double HMMRegression::LogLikelihood(const arma::mat& predictors,
+ const arma::vec& responses) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ return this->HMM::LogLikelihood(dataSeq);
+}
+
+/**
+ * HMMRegression filtering.
+ */
+void HMMRegression::Filter(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::vec& filterSeq,
+ size_t ahead) const
+{
+ // First run the forward algorithm
+ arma::mat forwardProb;
+ arma::vec scales;
+ Forward(predictors, responses, scales, forwardProb);
+
+ // Propagate state, predictors ahead
+ if(ahead != 0) {
+ forwardProb = pow(transition, ahead)*forwardProb;
+ forwardProb = forwardProb.cols(0, forwardProb.n_cols-ahead-1);
+ }
+
+ // Compute expected emissions.
+ filterSeq.resize(responses.n_elem - ahead);
+ filterSeq.zeros();
+ arma::vec nextSeq;
+ for(size_t i = 0; i < emission.size(); i++)
+ {
+ emission[i].Predict(predictors.cols(ahead, predictors.n_cols-1), nextSeq);
+ filterSeq = filterSeq + nextSeq%(forwardProb.row(i).t());
+ }
+}
+
+/**
+ * HMM smoothing.
+ */
+void HMMRegression::Smooth(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::vec& smoothSeq) const
+{
+ // First run the forward algorithm
+ arma::mat stateProb;
+ Estimate(predictors, responses, stateProb);
+
+ // Compute expected emissions.
+ smoothSeq.resize(responses.n_elem);
+ smoothSeq.zeros();
+ arma::vec nextSeq;
+ for(size_t i = 0; i < emission.size(); i++)
+ {
+ emission[i].Predict(predictors, nextSeq);
+ smoothSeq = smoothSeq + nextSeq%(stateProb.row(i).t());
+ }
+
+}
+
+/**
+ * The Forward procedure (part of the Forward-Backward algorithm).
+ */
+void HMMRegression::Forward(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::vec& scales,
+ arma::mat& forwardProb) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ this->HMM::Forward(dataSeq, scales, forwardProb);
+}
+
+
+void HMMRegression::Backward(const arma::mat& predictors,
+ const arma::vec& responses,
+ const arma::vec& scales,
+ arma::mat& backwardProb) const
+{
+ arma::mat dataSeq;
+ StackData(predictors, responses, dataSeq);
+ this->HMM::Backward(dataSeq, scales, backwardProb);
+}
+
+void HMMRegression::StackData(const std::vector<arma::mat>& predictors,
+ const std::vector<arma::vec>& responses,
+ std::vector<arma::mat>& dataSeq) const
+{
+ arma::mat nextSeq;
+ for(size_t i = 0; i < predictors.size(); i++)
+ {
+ nextSeq = predictors[i];
+ nextSeq.insert_rows(0, responses[i].t());
+ dataSeq.push_back(nextSeq);
+ }
+}
+
+void HMMRegression::StackData(const arma::mat& predictors,
+ const arma::vec& responses,
+ arma::mat& dataSeq) const
+{
+ dataSeq = predictors;
+ dataSeq.insert_rows(0, responses.t());
+}
+
+}; // namespace hmm
+}; // namespace mlpack
+
+#endif
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git
More information about the debian-science-commits
mailing list