[mlpack-svn] r10267 - mlpack/trunk/src/mlpack/methods/hmm
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Nov 14 01:52:47 EST 2011
Author: rcurtin
Date: 2011-11-14 01:52:47 -0500 (Mon, 14 Nov 2011)
New Revision: 10267
Modified:
mlpack/trunk/src/mlpack/methods/hmm/hmm.hpp
mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
Log:
Add function to calculate log-likelihood.
Modified: mlpack/trunk/src/mlpack/methods/hmm/hmm.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/hmm.hpp 2011-11-14 05:18:50 UTC (rev 10266)
+++ mlpack/trunk/src/mlpack/methods/hmm/hmm.hpp 2011-11-14 06:52:47 UTC (rev 10267)
@@ -98,11 +98,6 @@
const size_t startState = 0) const;
/**
- * Compute the log-likelihood of a sequence.
- */
- double LogLikelihood(const arma::vec& data_seq) 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.
@@ -112,9 +107,17 @@
* stored.
* @return Log-likelihood of most probable state sequence.
*/
- double Predict(const arma::vec& data_seq, arma::Col<size_t>& stateSeq) const;
+ double Predict(const arma::vec& dataSeq, arma::Col<size_t>& stateSeq) const;
/**
+ * Compute the log-likelihood of the given data sequence.
+ *
+ * @param dataSeq Data sequence to evaluate the likelihood of.
+ * @return Log-likelihood of the given sequence.
+ */
+ double LogLikelihood(const arma::vec& dataSeq) const;
+
+ /**
* Return the transition matrix.
*/
const arma::mat& Transition() const { return transition; }
Modified: mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp 2011-11-14 05:18:50 UTC (rev 10266)
+++ mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp 2011-11-14 06:52:47 UTC (rev 10267)
@@ -42,7 +42,6 @@
void HMM<Distribution>::Train(const std::vector<arma::vec>& dataSeq)
{
// We should allow a guess at the transition and emission matrices.
-
double loglik = 0;
double oldLoglik = 0;
@@ -317,7 +316,24 @@
return log(stateProb(stateSeq[dataSeq.n_elem - 1], dataSeq.n_elem - 1));
}
+/**
+ * Compute the log-likelihood of the given data sequence.
+ */
+template<typename Distribution>
+void HMM<Distribution>::LogLikelihood(const arma::vec& dataSeq) const
+{
+ arma::mat forward;
+ arma::vec scales;
+ Forward(dataSeq, scales, forward);
+
+ // The log-likelihood is the log of the scales for each time step.
+ return accu(log(scales));
+}
+
+/**
+ * The Forward procedure (part of the Forward-Backward algorithm).
+ */
template<typename Distribution>
void HMM<Distribution>::Forward(const arma::vec& dataSeq,
arma::vec& scales,
More information about the mlpack-svn
mailing list