[mlpack-svn] r10333 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Nov 18 21:47:40 EST 2011
Author: rcurtin
Date: 2011-11-18 21:47:39 -0500 (Fri, 18 Nov 2011)
New Revision: 10333
Modified:
mlpack/trunk/src/mlpack/tests/distribution_test.cpp
Log:
Tests for GaussianDistribution class.
Modified: mlpack/trunk/src/mlpack/tests/distribution_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/distribution_test.cpp 2011-11-19 02:47:28 UTC (rev 10332)
+++ mlpack/trunk/src/mlpack/tests/distribution_test.cpp 2011-11-19 02:47:39 UTC (rev 10333)
@@ -6,6 +6,7 @@
*/
#include <mlpack/core.h>
#include <mlpack/methods/hmm/distributions/discrete_distribution.hpp>
+#include <mlpack/methods/hmm/distributions/gaussian_distribution.hpp>
#include <boost/test/unit_test.hpp>
@@ -119,4 +120,143 @@
BOOST_REQUIRE_CLOSE(d.Probability(2), 0.5, 1e-5);
}
+/**
+ * Make sure Gaussian distributions are initialized correctly.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionEmptyConstructor)
+{
+ GaussianDistribution d;
+
+ BOOST_REQUIRE_EQUAL(d.Mean().n_elem, 0);
+ BOOST_REQUIRE_EQUAL(d.Covariance().n_elem, 0);
+}
+
+/**
+ * Make sure Gaussian distributions are initialized to the correct
+ * dimensionality.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionDimensionalityConstructor)
+{
+ GaussianDistribution d(4);
+
+ BOOST_REQUIRE_EQUAL(d.Mean().n_elem, 4);
+ BOOST_REQUIRE_EQUAL(d.Covariance().n_rows, 4);
+ BOOST_REQUIRE_EQUAL(d.Covariance().n_cols, 4);
+}
+
+/**
+ * Make sure Gaussian distributions are initialized correctly when we give a
+ * mean and covariance.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionDistributionConstructor)
+{
+ arma::vec mean(3);
+ arma::mat covariance(3, 3);
+
+ mean.randu();
+ covariance.randu();
+
+ GaussianDistribution d(mean, covariance);
+
+ for (size_t i = 0; i < 3; i++)
+ BOOST_REQUIRE_CLOSE(d.Mean()[i], mean[i], 1e-5);
+
+ for (size_t i = 0; i < 3; i++)
+ for (size_t j = 0; j < 3; j++)
+ BOOST_REQUIRE_CLOSE(d.Covariance()(i, j), covariance(i, j), 1e-5);
+}
+
+/**
+ * Make sure the probability of observations is correct.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionProbabilityTest)
+{
+ 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");
+
+ GaussianDistribution d(mean, cov);
+
+ BOOST_REQUIRE_CLOSE(d.Probability("0 1 2 3 4"), 1.02531207499358e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.Probability("3 2 3 7 8"), 1.82353695848039e-7, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.Probability("2 2 0 8 1"), 1.29759261892949e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.Probability("2 1 5 0 1"), 1.33218060268258e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.Probability("3 0 5 1 0"), 1.12120427975708e-6, 1e-5);
+ BOOST_REQUIRE_CLOSE(d.Probability("4 0 6 1 0"), 4.57951032485297e-7, 1e-5);
+}
+
+/**
+ * Make sure random observations follow the probability distribution correctly.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionRandomTest)
+{
+ srand(time(NULL));
+ arma::vec mean("1.0 2.25");
+ arma::mat cov("0.85 0.60;"
+ "0.60 1.45");
+
+ GaussianDistribution d(mean, cov);
+
+ arma::mat obs(2, 5000);
+
+ for (size_t i = 0; i < 5000; i++)
+ obs.col(i) = d.Random();
+
+ // Now make sure that reflects the actual distribution.
+ arma::vec obsMean = arma::mean(obs, 1);
+ arma::mat obsCov = ccov(obs);
+
+ // 10% tolerance because this can be noisy.
+ BOOST_REQUIRE_CLOSE(obsMean[0], mean[0], 10.0);
+ BOOST_REQUIRE_CLOSE(obsMean[1], mean[1], 10.0);
+
+ BOOST_REQUIRE_CLOSE(obsCov(0, 0), cov(0, 0), 10.0);
+ BOOST_REQUIRE_CLOSE(obsCov(0, 1), cov(0, 1), 10.0);
+ BOOST_REQUIRE_CLOSE(obsCov(1, 0), cov(1, 0), 10.0);
+ BOOST_REQUIRE_CLOSE(obsCov(1, 1), cov(1, 1), 10.0);
+}
+
+/**
+ * Make sure that we can properly estimate from given observations.
+ */
+BOOST_AUTO_TEST_CASE(GaussianDistributionEstimateTest)
+{
+ arma::vec mean("1.0 3.0 0.0 2.5");
+ arma::mat cov("3.0 0.0 1.0 4.0;"
+ "0.0 2.4 0.5 0.1;"
+ "1.0 0.5 6.3 0.0;"
+ "4.0 0.1 0.0 9.1");
+
+ // Now generate the observations.
+ std::vector<arma::vec> observations;
+
+ arma::mat transChol = trans(chol(cov));
+ for (size_t i = 0; i < 10000; i++)
+ observations.push_back(transChol * arma::randn<arma::vec>(4) + mean);
+
+ // Now estimate.
+ GaussianDistribution d;
+
+ // Find actual mean and covariance of data.
+ arma::mat data(4, 10000);
+ for (size_t i = 0; i < 10000; i++)
+ data.col(i) = observations[i];
+
+ arma::vec actualMean = arma::mean(data, 1);
+ arma::mat actualCov = ccov(data);
+
+ d.Estimate(observations);
+
+ // Check that everything is estimated right.
+ for (size_t i = 0; i < 4; i++)
+ BOOST_REQUIRE_SMALL(d.Mean()[i] - actualMean[i], 1e-5);
+
+ for (size_t i = 0; i < 4; i++)
+ for (size_t j = 0; j < 4; j++)
+ BOOST_REQUIRE_SMALL(d.Covariance()(i, j) - actualCov(i, j), 1e-5);
+}
+
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-svn
mailing list