aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2009-05-05 16:50:58 +0000
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2009-05-05 16:50:58 +0000
commit2b2f0c0220528f021acffa8e0f0f40f3dbb0e9ac (patch)
tree436c3cc907923adb7160a1b40383c4f875b6a240 /test
parent5b364a68cb05fb8f904b237fabfd686edd955fe1 (diff)
fix linearRegression, fix doc, add unit test (it was untested since the change
making fitHyperplane no longer use it)
Diffstat (limited to 'test')
-rw-r--r--test/regression.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/regression.cpp b/test/regression.cpp
index e551bd402..534ad3d1c 100644
--- a/test/regression.cpp
+++ b/test/regression.cpp
@@ -62,6 +62,20 @@ void makeNoisyCohyperplanarPoints(int numPoints,
*(points[i]) += noiseAmplitude * VectorType::Random(size);
}
+template<typename VectorType>
+void check_linearRegression(int numPoints,
+ VectorType **points,
+ const VectorType& original,
+ typename VectorType::Scalar tolerance)
+{
+ int size = points[0]->size();
+ assert(size==2);
+ VectorType result(size);
+ linearRegression(numPoints, points, &result, 1);
+ typename VectorType::Scalar error = (result - original).norm() / original.norm();
+ VERIFY(ei_abs(error) < ei_abs(tolerance));
+}
+
template<typename VectorType,
typename HyperplaneType>
void check_fitHyperplane(int numPoints,
@@ -85,6 +99,20 @@ void test_regression()
Vector2f points2f [1000];
Vector2f *points2f_ptrs [1000];
for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
+ Vector2f coeffs2f;
+ Hyperplane<float,2> coeffs3f;
+ makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
+ coeffs2f[0] = -coeffs3f.coeffs()[0]/coeffs3f.coeffs()[1];
+ coeffs2f[1] = -coeffs3f.coeffs()[2]/coeffs3f.coeffs()[1];
+ CALL_SUBTEST(check_linearRegression(10, points2f_ptrs, coeffs2f, 0.05f));
+ CALL_SUBTEST(check_linearRegression(100, points2f_ptrs, coeffs2f, 0.01f));
+ CALL_SUBTEST(check_linearRegression(1000, points2f_ptrs, coeffs2f, 0.002f));
+ }
+
+ {
+ Vector2f points2f [1000];
+ Vector2f *points2f_ptrs [1000];
+ for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
Hyperplane<float,2> coeffs3f;
makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
CALL_SUBTEST(check_fitHyperplane(10, points2f_ptrs, coeffs3f, 0.05f));