[mlpack] 66/207: Added TestCase for GaussianDistribution::Train(observation, probabilities)

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:41 UTC 2017


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

bap pushed a commit to branch master
in repository mlpack.

commit 5a47b8532c713e56762d842bc7cc1828b0861614
Author: Lakshya Agrawal <zeeshan.lakshya at gmail.com>
Date:   Fri Jan 27 11:37:52 2017 +0530

    Added TestCase for GaussianDistribution::Train(observation, probabilities)
---
 src/mlpack/tests/distribution_test.cpp | 80 +++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 34 deletions(-)

diff --git a/src/mlpack/tests/distribution_test.cpp b/src/mlpack/tests/distribution_test.cpp
index a3bf51d..7de0776 100644
--- a/src/mlpack/tests/distribution_test.cpp
+++ b/src/mlpack/tests/distribution_test.cpp
@@ -397,22 +397,28 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionTrainTest)
 **/
 BOOST_AUTO_TEST_CASE(GaussianDistributionTrainWithProbabilitiesTest)
 {
-  arma::vec mean = ("5.0");
-  arma::vec cov = ("2.0");
+  double mean = 5.0;
+  double stddeviation = 2.0;
 
-  GaussianDistribution dist(mean, cov);
-  size_t N = 5000;
-  size_t d = 1;
+  //Creates a normal distribution generator.
+  std::default_random_engine generator;
+  generator.seed(std::time(NULL));
+  std::normal_distribution<double> dist(mean, stddeviation);
+  
+  size_t N = 50000;
+  size_t d =1;
   
   arma::mat rdata(d, N);
   
-  for (size_t i = 0; i < d; i++)
-    for (size_t j = 0; j < N; j++)
+  for(size_t i = 0;i < d; i++)
+    for(size_t j = 0;j < N;j++)
       rdata(i,j) = dist(generator);
 
+  //Creates a uniform distribution generator
+  std::uniform_real_distribution<double> prob(0, 1);
   arma::vec probabilities(N);
   
-  for (size_t i = 0; i < N; i++)
+  for(size_t i = 0;i < N;i++)
     probabilities(i) = prob(generator);
   
   //Fits result with probabilities and data.
@@ -426,8 +432,8 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionTrainWithProbabilitiesTest)
   BOOST_REQUIRE_CLOSE(guDist.Mean()[0], guDist2.Mean()[0], 5);
   BOOST_REQUIRE_CLOSE(guDist.Covariance()[0], guDist2.Covariance()[0], 5);
   
-  BOOST_REQUIRE_CLOSE(guDist.Mean()[0], mean[0], 5);
-  BOOST_REQUIRE_CLOSE(guDist.Covariance()[0], cov[0], 5);
+  BOOST_REQUIRE_CLOSE(guDist.Mean()[0], mean, 5);
+  BOOST_REQUIRE_CLOSE(guDist.Covariance()[0], stddeviation*stddeviation, 5);
 }
 /**
   *This test ensures that the same result is obtained when trained
@@ -435,26 +441,30 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionTrainWithProbabilitiesTest)
 **/
 BOOST_AUTO_TEST_CASE(GaussianDistributionWithProbabilties1Test)
 {
-  arma::vec mean = ("5.0");
-  arma::vec cov  = ("4.0");
+  double mean = 5.0;
+  double stddeviation = 4.0;
+
+  //Create a normal distribution random generator
+  std::default_random_engine generator;
+  generator.seed(std::time(NULL));
+  std::normal_distribution<double> dist(mean, stddeviation);
 
-  GaussianDistribution dist(mean, cov);
   size_t N = 50000;
   size_t d = 1;
 
   arma::mat rdata(d, N);
 
-  for (size_t i = 0; i < d; i++)
-    for (size_t j = 0; j < N ; j++)
+  for(size_t i = 0; i < d; i++)
+    for(size_t j = 0; j < N ; j++)
       rdata(i,j) = dist(generator);
 
   arma::vec probabilities(N, arma::fill::ones);
 
-  //Fits data with only data
+  //fits data with only data
   GaussianDistribution guDist;
   guDist.Train(rdata);
 
-  //Fits result with data and each probability as 1
+  //fits result with data and each probability as 1
   GaussianDistribution guDist2;
   guDist2.Train(rdata, probabilities);
 
@@ -470,16 +480,20 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionWithProbabilties1Test)
 **/
 BOOST_AUTO_TEST_CASE(GaussianDistributionTrainWithTwoDistProbabilitiesTest)
 {
-  arma::vec mean1 = ("5.0");
-  arma::vec cov1 = ("4.0");
+  double mean1 = 5.0;
+  double stddeviation1 = 4.0;
 
-  arma::vec mean2 = ("3.0");
-  arma::vec cov2 = ("1.0");
+  double mean2 = 3.0;
+  double stddeviation2 = 1.0;
 
-  //Creates two GaussianDistribution 
-  GaussianDistribution dist1(mean1, cov1);
-  GaussianDistribution dist2(mean2, cov2);
+  //Create two gaussian distribution random generator
+  std::default_random_engine generator;
+  generator.seed(std::time(NULL));
+  std::normal_distribution<double> dist1(mean1, stddeviation1);
+  std::normal_distribution<double> dist2(mean2, stddeviation2);
 
+  std::uniform_real_distribution<double> lowProb(0, 0.02);
+  std::uniform_real_distribution<double> highProb(0.98, 1);
 
   size_t N = 50000;
   size_t d = 1;
@@ -488,36 +502,34 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionTrainWithTwoDistProbabilitiesTest)
   arma::vec probabilities(N);
 
   //draws point alternatily from the two different distributions.
-  for (size_t i = 0 ; i < d; i++)
+  for(size_t i = 0 ; i < d; i++)
   {
-    for (size_t j = 0; j < N; j++)
+    for(size_t j = 0; j < N; j++)
     {
-      if (j%2 == 0)
+      if(j%2 == 0)
         rdata(i,j) = dist1(generator);
       else
         rdata(i,j) = dist2(generator);
     }
   }
 
-  for (size_t i = 0 ; i < N ; i++)
+  for(size_t i = 0 ; i < N ; i++)
   {
-    if (i%2 == 0)
+    if(i%2 == 0)
       probabilities(i) = highProb(generator);
     else
-      probabilities(i) = Random(0, 0.02);
+      probabilities(i) = lowProb(generator);
   }
 
   GaussianDistribution guDist;
   guDist.Train(rdata, probabilities);
 
-  BOOST_REQUIRE_CLOSE(guDist.Mean()[0], mean1[0], 5);
-  BOOST_REQUIRE_CLOSE(guDist.Covariance()[0], cov1[0], 5);
+  BOOST_REQUIRE_CLOSE(guDist.Mean()[0], mean1, 5);
+  BOOST_REQUIRE_CLOSE(guDist.Covariance()[0], stddeviation1*stddeviation1, 5);
 }
-
 /******************************/
 /** Gamma Distribution Tests **/
 /******************************/
-
 /**
  * Make sure that using an object to fit one reference set and then asking
  * to fit another works properly.

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