[mlpack] 37/58: Resurrect Phi tests from gmm_test.cpp.
Barak A. Pearlmutter
barak+git at cs.nuim.ie
Tue Sep 9 13:19:41 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 58802b0af73088897bbcaca7bac53884fed2479d
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date: Wed Aug 20 21:47:06 2014 +0000
Resurrect Phi tests from gmm_test.cpp.
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17092 9d5b8971-822b-0410-80eb-d18c1038ef23
---
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)
--
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