[mlpack-svn] r17092 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Aug 20 17:47:07 EDT 2014
Author: rcurtin
Date: Wed Aug 20 17:47:06 2014
New Revision: 17092
Log:
Resurrect Phi tests from gmm_test.cpp.
Modified:
mlpack/trunk/src/mlpack/tests/distribution_test.cpp
Modified: mlpack/trunk/src/mlpack/tests/distribution_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/distribution_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/distribution_test.cpp Wed Aug 20 17:47:06 2014
@@ -173,6 +173,123 @@
}
/**
+ * Test GaussianDistribution::Probability() in the univariate case.
+ */
+BOOST_AUTO_TEST_CASE(GaussianUnivariateProbabilityTest)
+{
+ GaussianDistribution g(arma::vec("0.0"), arma::mat("1.0"));
+
+ // Simple case.
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("0.0")), 0.398942280401433, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("1.0")), 0.241970724519143, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("-1.0")), 0.241970724519143,
+ 1e-5);
+
+ // A few more cases...
+ g.Covariance().fill(2.0);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("0.0")), 0.282094791773878, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("1.0")), 0.219695644733861, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("-1.0")), 0.219695644733861,
+ 1e-5);
+
+ g.Mean().fill(1.0);
+ g.Covariance().fill(1.0);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("1.0")), 0.398942280401433, 1e-5);
+ g.Covariance().fill(2.0);
+ BOOST_REQUIRE_CLOSE(g.Probability(arma::vec("-1.0")), 0.103776874355149,
+ 1e-5);
+}
+
+/**
+ * Test GaussianDistribution::Probability() in the multivariate case.
+ */
+BOOST_AUTO_TEST_CASE(GaussianMultivariateProbabilityTest)
+{
+ // Simple case.
+ arma::vec mean = "0 0";
+ arma::mat cov = "1 0; 0 1";
+ arma::vec x = "0 0";
+
+ GaussianDistribution g(mean, cov);
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.159154943091895, 1e-5);
+
+ g.Covariance() = "2 0; 0 2";
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.0795774715459477, 1e-5);
+
+ x = "1 1";
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.0482661763150270, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 0.0482661763150270, 1e-5);
+
+ g.Mean() = "1 1";
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.0795774715459477, 1e-5);
+ g.Mean() *= -1;
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 0.0795774715459477, 1e-5);
+
+ g.Mean() = "1 1";
+ g.Covariance() = "2 1.5; 1 4";
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.0624257046546403, 1e-5);
+ g.Mean() *= -1;
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 0.0624257046546403, 1e-5);
+
+ g.Mean() = "1 1";
+ x = "-1 4";
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 0.00144014867515135, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 0.00133352162064845, 1e-5);
+
+ // Higher-dimensional case.
+ x = "0 1 2 3 4";
+ g.Mean() = "5 6 3 3 2";
+ g.Covariance() = "6 1 1 0 2;"
+ "0 7 1 0 1;"
+ "1 1 4 1 1;"
+ "1 0 1 7 0;"
+ "2 0 1 1 6";
+
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 1.02531207499358e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 1.06784794079363e-8, 1e-5);
+
+ g.Mean() *= -1;
+ BOOST_REQUIRE_CLOSE(g.Probability(-x), 1.02531207499358e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(g.Probability(x), 1.06784794079363e-8, 1e-5);
+
+}
+
+/**
+ * Test the phi() function, for multiple points in the multivariate Gaussian
+ * case.
+ */
+BOOST_AUTO_TEST_CASE(GaussianMultipointMultivariateProbabilityTest)
+{
+ // Same case as before.
+ arma::vec mean = "5 6 3 3 2";
+ arma::mat cov = "6 1 1 0 2; 0 7 1 0 1; 1 1 4 1 1; 1 0 1 7 0; 2 0 1 1 6";
+
+ arma::mat points = "0 3 2 2 3 4;"
+ "1 2 2 1 0 0;"
+ "2 3 0 5 5 6;"
+ "3 7 8 0 1 1;"
+ "4 8 1 1 0 0;";
+
+ arma::vec phis;
+ GaussianDistribution g(mean, cov);
+ g.Probability(points, phis);
+
+ BOOST_REQUIRE_EQUAL(phis.n_elem, 6);
+
+ BOOST_REQUIRE_CLOSE(phis(0), 1.02531207499358e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(phis(1), 1.82353695848039e-7, 1e-5);
+ BOOST_REQUIRE_CLOSE(phis(2), 1.29759261892949e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(phis(3), 1.33218060268258e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(phis(4), 1.12120427975708e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(phis(5), 4.57951032485297e-7, 1e-5);
+}
+
+/**
* Make sure random observations follow the probability distribution correctly.
*/
BOOST_AUTO_TEST_CASE(GaussianDistributionRandomTest)
More information about the mlpack-svn
mailing list