[mlpack] 275/324: Implemented Save, Load

Barak A. Pearlmutter barak+git at cs.nuim.ie
Sun Aug 17 08:22:18 UTC 2014


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 985c19d7963b242628c2a23615ad83ff1102b475
Author: michaelfox99 <michaelfox99 at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Tue Aug 5 13:29:32 2014 +0000

    Implemented Save, Load
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16959 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 src/mlpack/core/dists/gaussian_distribution.hpp | 74 +++++++++++++++++++++----
 1 file changed, 63 insertions(+), 11 deletions(-)

diff --git a/src/mlpack/core/dists/gaussian_distribution.hpp b/src/mlpack/core/dists/gaussian_distribution.hpp
index 20f2524..7b8716b 100644
--- a/src/mlpack/core/dists/gaussian_distribution.hpp
+++ b/src/mlpack/core/dists/gaussian_distribution.hpp
@@ -1,6 +1,7 @@
 /**
  * @file gaussian_distribution.hpp
  * @author Ryan Curtin
+ * @author Michael Fox
  *
  * Implementation of the Gaussian distribution.
  */
@@ -8,8 +9,6 @@
 #define __MLPACK_METHODS_HMM_DISTRIBUTIONS_GAUSSIAN_DISTRIBUTION_HPP
 
 #include <mlpack/core.hpp>
-// Should be somewhere else, maybe in core.
-#include <mlpack/methods/gmm/phi.hpp>
 
 namespace mlpack {
 namespace distribution {
@@ -52,11 +51,17 @@ class GaussianDistribution
   /**
    * Return the probability of the given observation.
    */
-  double Probability(const arma::vec& observation) const
-  {
-    return mlpack::gmm::phi(observation, mean, covariance);
-  }
-
+  double Probability(const arma::vec& observation) const;
+  
+  /**
+   * Calculates the multivariate Gaussian probability density function for each
+   * data point (column) in the given matrix
+   *
+   * @param x List of observations.
+   * @param probabilities Output probabilities for each input observation.
+   */
+  void Probability(const arma::mat& x, arma::vec& probabilities) const;
+  
   /**
    * Return a randomly generated observation according to the probability
    * distribution defined by this object.
@@ -80,22 +85,69 @@ class GaussianDistribution
   void Estimate(const arma::mat& observations,
                 const arma::vec& probabilities);
 
-  //! Return the mean.
+  /**
+   * Return the mean.
+   */
   const arma::vec& Mean() const { return mean; }
-  //! Return a modifiable copy of the mean.
+
+  /**
+   * Return a modifiable copy of the mean.
+   */
   arma::vec& Mean() { return mean; }
 
-  //! Return the covariance matrix.
+  /**
+   * Return the covariance matrix.
+   */
   const arma::mat& Covariance() const { return covariance; }
-  //! Return a modifiable copy of the covariance.
+
+  /**
+   * Return a modifiable copy of the covariance.
+   */
   arma::mat& Covariance() { return covariance; }
 
   /**
    * Returns a string representation of this object.
    */
   std::string ToString() const;
+    
+  /*
+   * Save to or Load from SaveRestoreUtility
+   */
+  void Save(util::SaveRestoreUtility& n) const;
+  void Load(const util::SaveRestoreUtility& n);
+  static std::string const Type() { return "GaussianDistribution"; }
+  
+  
+    
 };
 
+/**
+* Calculates the multivariate Gaussian probability density function for each
+* data point (column) in the given matrix
+*
+* @param x List of observations.
+* @param probabilities Output probabilities for each input observation.
+*/
+inline void GaussianDistribution::Probability(const arma::mat& x,
+                                              arma::vec& probabilities) const
+{
+  // Column i of 'diffs' is the difference between x.col(i) and the mean.
+  arma::mat diffs = x - (mean * arma::ones<arma::rowvec>(x.n_cols));
+  
+  // Now, we only want to calculate the diagonal elements of (diffs' * cov^-1 *
+  // diffs).  We just don't need any of the other elements.  We can calculate
+  // the right hand part of the equation (instead of the left side) so that
+  // later we are referencing columns, not rows -- that is faster.
+  arma::mat rhs = -0.5 * inv(covariance) * diffs;
+  arma::vec exponents(diffs.n_cols); // We will now fill this.
+  for (size_t i = 0; i < diffs.n_cols; i++)
+    exponents(i) = exp(accu(diffs.unsafe_col(i) % rhs.unsafe_col(i)));
+  
+  probabilities = pow(2 * M_PI, (double) mean.n_elem / -2.0) *
+  pow(arma::det(covariance), -0.5) * exponents;
+}
+  
+
 }; // namespace distribution
 }; // namespace mlpack
 

-- 
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