[mlpack-git] master: Resurrect Phi tests from gmm_test.cpp. (2e3c04b)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:59:18 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 2e3c04b428d212773b337b45d3848b04a142a6b0
Author: Ryan Curtin <ryan at ratml.org>
Date: Wed Aug 20 21:47:06 2014 +0000
Resurrect Phi tests from gmm_test.cpp.
>---------------------------------------------------------------
2e3c04b428d212773b337b45d3848b04a142a6b0
src/mlpack/tests/distribution_test.cpp | 117 +++++++++++++++++++++++++++++++++
1 file changed, 117 insertions(+)
diff --git a/src/mlpack/tests/distribution_test.cpp b/src/mlpack/tests/distribution_test.cpp
index e53a61c..fd3c76f 100644
--- a/src/mlpack/tests/distribution_test.cpp
+++ b/src/mlpack/tests/distribution_test.cpp
@@ -173,6 +173,123 @@ BOOST_AUTO_TEST_CASE(GaussianDistributionProbabilityTest)
}
/**
+ * 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-git
mailing list