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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Oct 1 15:21:29 EDT 2013


Author: rcurtin
Date: Tue Oct  1 15:21:29 2013
New Revision: 15891

Log:
Add a ridge regression test case (huh, I wonder how I forgot to check this in
earlier).


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	Tue Oct  1 15:21:29 2013
@@ -53,13 +53,13 @@
     responses[elem] = coeffs[0] +
         dot(coeffs.rows(1, 3), arma::ones<arma::rowvec>(3) * elem);
 
-  // Initialize and predict
+  // Initialize and predict.
   LinearRegression lr(predictors, responses);
   lr.Predict(points, predictions);
 
   // Output result and verify we have less than 5% error from "correct" value
-  // for each point
-  for(size_t i = 0; i < predictions.n_cols; ++i)
+  // for each point.
+  for (size_t i = 0; i < predictions.n_cols; ++i)
     BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
 }
 
@@ -113,7 +113,7 @@
   // invertible.  If ridge regression is not working correctly, then the matrix
   // will not be invertible and the test should segfault (or something else
   // ugly).
-  LinearRegression lr(data, responses, 0.00001);
+  LinearRegression lr(data, responses, 0.0001);
 
   // Now just make sure that it predicts some more zeros.
   arma::vec predictedResponses;
@@ -123,4 +123,54 @@
     BOOST_REQUIRE_SMALL((double) predictedResponses[i], 1e-20);
 }
 
+/**
+ * Creates two 10x3 random matrices and one 10x1 "results" matrix.
+ * Finds B in y=BX with one matrix, then predicts against the other, but uses
+ * ridge regression with an extremely small lambda value.
+ */
+BOOST_AUTO_TEST_CASE(RidgeRegressionTestCase)
+{
+  // Predictors and points are 100x3 matrices.
+  arma::mat predictors(3, 10);
+  arma::mat points(3, 10);
+
+  // Responses is the "correct" value for each point in predictors and points.
+  arma::vec responses(10);
+
+  // The values we get back when we predict for points.
+  arma::vec predictions(10);
+
+  // We'll randomly select some coefficients for the linear response.
+  arma::vec coeffs;
+  coeffs.randu(4);
+
+  // Now generate each point.
+  for (size_t row = 0; row < 3; row++)
+    predictors.row(row) = arma::linspace<arma::rowvec>(0, 9, 10);
+
+  points = predictors;
+
+  // Now add a small amount of noise to each point.
+  for (size_t elem = 0; elem < points.n_elem; elem++)
+  {
+    // Max added noise is 0.02.
+    points[elem] += math::Random() / 50.0;
+    predictors[elem] += math::Random() / 50.0;
+  }
+
+  // Generate responses.
+  for (size_t elem = 0; elem < responses.n_elem; elem++)
+    responses[elem] = coeffs[0] +
+        dot(coeffs.rows(1, 3), arma::ones<arma::rowvec>(3) * elem);
+
+  // Initialize and predict with very small lambda.
+  LinearRegression lr(predictors, responses, 0.001);
+  lr.Predict(points, predictions);
+
+  // Output result and verify we have less than 5% error from "correct" value
+  // for each point.
+  for (size_t i = 0; i < predictions.n_cols; ++i)
+    BOOST_REQUIRE_SMALL(predictions(i) - responses(i), .05);
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list