[mlpack-git] master: Implemented Save, Load (efc43b0)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:56:23 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit efc43b0edb8e2d427b6810e397e8adb3630af3a4
Author: michaelfox99 <michaelfox99 at gmail.com>
Date:   Tue Aug 5 13:29:32 2014 +0000

    Implemented Save, Load


>---------------------------------------------------------------

efc43b0edb8e2d427b6810e397e8adb3630af3a4
 src/mlpack/core/dists/gaussian_distribution.hpp | 72 +++++++++++++++++++++----
 1 file changed, 62 insertions(+), 10 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,10 +51,16 @@ 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
@@ -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
 



More information about the mlpack-git mailing list