[mlpack-svn] r15836 - mlpack/trunk/src/mlpack/tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Sep 25 15:22:32 EDT 2013


Author: rcurtin
Date: Wed Sep 25 15:22:31 2013
New Revision: 15836

Log:
Add test for ComputeError() for #298.


Modified:
   mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp

Modified: mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/linear_regression_test.cpp	Wed Sep 25 15:22:31 2013
@@ -63,4 +63,37 @@
     BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
 }
 
+/**
+ * Check the functionality of ComputeError().
+ */
+BOOST_AUTO_TEST_CASE(ComputeErrorTest)
+{
+  arma::mat predictors;
+  predictors << 0 << 1 << 2 << 4 << 8 << 16 << arma::endr
+             << 16 << 8 << 4 << 2 << 1 << 0 << arma::endr;
+  arma::vec responses = "0 2 4 3 8 8";
+
+  // http://www.mlpack.org/trac/ticket/298
+  // This dataset gives a cost of 1.189500337 (as calculated in Octave).
+  LinearRegression lr(predictors, responses);
+
+  BOOST_REQUIRE_CLOSE(lr.ComputeError(predictors, responses), 1.189500337,
+      1e-3);
+}
+
+/**
+ * Ensure that the cost is 0 when a perfectly-fitting dataset is given.
+ */
+BOOST_AUTO_TEST_CASE(ComputeErrorPerfectFitTest)
+{
+  // Linear regression should perfectly model this dataset.
+  arma::mat predictors;
+  predictors << 0 << 1 << 2 << 3 << 4 << 5 << 6 << arma::endr;
+  arma::vec responses = "1 2 3 4 5 6 7";
+
+  LinearRegression lr(predictors, responses);
+
+  BOOST_REQUIRE_SMALL(lr.ComputeError(predictors, responses), 1e-30);
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list