[mlpack-svn] r10331 - mlpack/trunk/src/mlpack/methods/hmm/distributions

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Nov 18 21:47:20 EST 2011


Author: rcurtin
Date: 2011-11-18 21:47:19 -0500 (Fri, 18 Nov 2011)
New Revision: 10331

Added:
   mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.cpp
   mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.hpp
Log:
Add Gaussian distribution class.


Added: mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.cpp	2011-11-19 02:47:19 UTC (rev 10331)
@@ -0,0 +1,98 @@
+/**
+ * @file gaussian_distribution.cpp
+ * @author Ryan Curtin
+ *
+ * Implementation of Gaussian distribution class.
+ */
+#include "gaussian_distribution.hpp"
+
+using namespace mlpack;
+using namespace mlpack::distribution;
+
+arma::vec GaussianDistribution::Random() const
+{
+  // Should we store chol(covariance) for easier calculation later?
+  return trans(chol(covariance)) * arma::randn<arma::vec>(mean.n_elem) + mean;
+}
+
+/**
+ * Estimate the Gaussian distribution directly from the given observations.
+ *
+ * @param observations List of observations.
+ */
+void GaussianDistribution::Estimate(const std::vector<arma::vec> observations)
+{
+  // Calculate the mean and covariance with each point.  Because this is a
+  // std::vector and not a matrix, this is a little more difficult.
+  if (observations.size() > 0)
+  {
+    mean.zeros(observations[0].n_elem);
+    covariance.zeros(observations[0].n_elem, observations[0].n_elem);
+  }
+  else // This will end up just being empty.
+  {
+    mean.zeros(0);
+    covariance.zeros(0);
+  }
+
+  // Calculate the mean.
+  for (size_t i = 0; i < observations.size(); i++)
+    mean += observations[i];
+
+  // Normalize the mean.
+  mean /= observations.size();
+
+  // Now calculate the covariance.
+  for (size_t i = 0; i < observations.size(); i++)
+  {
+    arma::vec obsNoMean = observations[i] - mean;
+    covariance += obsNoMean * trans(obsNoMean);
+  }
+
+  // Finish estimating the covariance by normalizing, with the (1 / (n - 1)) so
+  // that it is the unbiased estimator.
+  covariance /= (observations.size() - 1);
+}
+
+/**
+ * Estimate the Gaussian distribution from the given observations, taking into
+ * account the probability of each observation actually being from this
+ * distribution.
+ */
+void GaussianDistribution::Estimate(const std::vector<arma::vec> observations,
+                                    const std::vector<double> probabilities)
+{
+  if (observations.size() > 0)
+  {
+    mean.zeros(observations[0].n_elem);
+    covariance.zeros(observations[0].n_elem, observations[0].n_elem);
+  }
+  else // This will end up just being empty.
+  {
+    mean.zeros(0);
+    covariance.zeros(0);
+  }
+
+  double sumProb = 0;
+
+  // First calculate the mean, and save the sum of all the probabilities for
+  // later normalization.
+  for (size_t i = 0; i < observations.size(); i++)
+  {
+    mean += probabilities[i] * observations[i];
+    sumProb += probabilities[i];
+  }
+
+  // Normalize.
+  mean /= sumProb;
+
+  // Now find the covariance.
+  for (size_t i = 0; i < observations.size(); i++)
+  {
+    arma::vec obsNoMean = observations[i] - mean;
+    covariance += probabilities[i] * (obsNoMean * trans(obsNoMean));
+  }
+
+  // This is probably biased, but I don't know how to unbias it.
+  covariance /= sumProb;
+}

Added: mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/hmm/distributions/gaussian_distribution.hpp	2011-11-19 02:47:19 UTC (rev 10331)
@@ -0,0 +1,104 @@
+/**
+ * @file gaussian_distribution.hpp
+ * @author Ryan Curtin
+ *
+ * Implementation of the Gaussian distribution.
+ */
+#ifndef __MLPACK_METHODS_HMM_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP
+#define __MLPACK_METHODS_HMM_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP
+
+#include <mlpack/core.h>
+// Should be somewhere else, maybe in core.
+#include <mlpack/methods/gmm/phi.hpp>
+
+namespace mlpack {
+namespace distribution {
+
+class GaussianDistribution
+{
+ private:
+  //! Mean of the distribution.
+  arma::vec mean;
+  //! Covariance of the distribution.
+  arma::mat covariance;
+
+ public:
+  //! The type of data which this distribution uses.
+  typedef arma::vec DataType;
+
+  /**
+   * Default constructor, which creates a Gaussian with zero dimension.
+   */
+  GaussianDistribution() { /* nothing to do */ }
+
+  /**
+   * Create a Gaussian distribution with zero mean and identity covariance with
+   * the given dimensionality.
+   */
+  GaussianDistribution(const size_t dimension) :
+      mean(dimension), covariance(arma::eye<arma::mat>(dimension, dimension))
+  { /* nothing to do */ }
+
+  /**
+   * Create a Gaussian distribution with the given mean and covariance.
+   */
+  GaussianDistribution(const arma::vec& mean, const arma::mat& covariance) :
+      mean(mean), covariance(covariance) { /* nothing to do */ }
+
+  /**
+   * Return the probability of the given observation.
+   */
+  double Probability(const arma::vec& observation) const
+  {
+    return mlpack::gmm::phi(observation, mean, covariance);
+  }
+
+  /**
+   * Return a randomly generated observation according to the probability
+   * distribution defined by this object.
+   *
+   * @return Random observation from this Gaussian distribution.
+   */
+  arma::vec Random() const;
+
+  /**
+   * Estimate the Gaussian distribution directly from the given observations.
+   *
+   * @param observations List of observations.
+   */
+  void Estimate(const std::vector<arma::vec> observations);
+
+  /**
+   * Estimate the Gaussian distribution from the given observations, taking into
+   * account the probability of each observation actually being from this
+   * distribution.
+   */
+  void Estimate(const std::vector<arma::vec> observations,
+                const std::vector<double> probabilities);
+
+  /**
+   * Return the mean.
+   */
+  const arma::vec& Mean() const { return mean; }
+
+  /**
+   * Return a modifiable copy of the mean.
+   */
+  arma::vec& Mean() { return mean; }
+
+  /**
+   * Return the covariance matrix.
+   */
+  const arma::mat& Covariance() const { return covariance; }
+
+  /**
+   * Return a modifiable copy of the covariance.
+   */
+  arma::mat& Covariance() { return covariance; }
+
+};
+
+}; // namespace distribution
+}; // namespace mlpack
+
+#endif




More information about the mlpack-svn mailing list