eigen2_regression.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 #include <Eigen/LeastSquares>
00027 
00028 template<typename VectorType,
00029          typename HyperplaneType>
00030 void makeNoisyCohyperplanarPoints(int numPoints,
00031                                   VectorType **points,
00032                                   HyperplaneType *hyperplane,
00033                                   typename VectorType::Scalar noiseAmplitude)
00034 {
00035   typedef typename VectorType::Scalar Scalar;
00036   const int size = points[0]->size();
00037   // pick a random hyperplane, store the coefficients of its equation
00038   hyperplane->coeffs().resize(size + 1);
00039   for(int j = 0; j < size + 1; j++)
00040   {
00041     do {
00042       hyperplane->coeffs().coeffRef(j) = ei_random<Scalar>();
00043     } while(ei_abs(hyperplane->coeffs().coeff(j)) < 0.5);
00044   }
00045 
00046   // now pick numPoints random points on this hyperplane
00047   for(int i = 0; i < numPoints; i++)
00048   {
00049     VectorType& cur_point = *(points[i]);
00050     do
00051     {
00052       cur_point = VectorType::Random(size)/*.normalized()*/;
00053       // project cur_point onto the hyperplane
00054       Scalar x = - (hyperplane->coeffs().start(size).cwise()*cur_point).sum();
00055       cur_point *= hyperplane->coeffs().coeff(size) / x;
00056     } while( cur_point.norm() < 0.5
00057           || cur_point.norm() > 2.0 );
00058   }
00059 
00060   // add some noise to these points
00061   for(int i = 0; i < numPoints; i++ )
00062     *(points[i]) += noiseAmplitude * VectorType::Random(size);
00063 }
00064 
00065 template<typename VectorType>
00066 void check_linearRegression(int numPoints,
00067                             VectorType **points,
00068                             const VectorType& original,
00069                             typename VectorType::Scalar tolerance)
00070 {
00071   int size = points[0]->size();
00072   assert(size==2);
00073   VectorType result(size);
00074   linearRegression(numPoints, points, &result, 1);
00075   typename VectorType::Scalar error = (result - original).norm() / original.norm();
00076   VERIFY(ei_abs(error) < ei_abs(tolerance));
00077 }
00078 
00079 template<typename VectorType,
00080          typename HyperplaneType>
00081 void check_fitHyperplane(int numPoints,
00082                          VectorType **points,
00083                          const HyperplaneType& original,
00084                          typename VectorType::Scalar tolerance)
00085 {
00086   int size = points[0]->size();
00087   HyperplaneType result(size);
00088   fitHyperplane(numPoints, points, &result);
00089   result.coeffs() *= original.coeffs().coeff(size)/result.coeffs().coeff(size);
00090   typename VectorType::Scalar error = (result.coeffs() - original.coeffs()).norm() / original.coeffs().norm();
00091   std::cout << ei_abs(error) << "  xxx   " << ei_abs(tolerance) << std::endl;
00092   VERIFY(ei_abs(error) < ei_abs(tolerance));
00093 }
00094 
00095 void test_eigen2_regression()
00096 {
00097   for(int i = 0; i < g_repeat; i++)
00098   {
00099 #ifdef EIGEN_TEST_PART_1
00100     {
00101       Vector2f points2f [1000];
00102       Vector2f *points2f_ptrs [1000];
00103       for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
00104       Vector2f coeffs2f;
00105       Hyperplane<float,2> coeffs3f;
00106       makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
00107       coeffs2f[0] = -coeffs3f.coeffs()[0]/coeffs3f.coeffs()[1];
00108       coeffs2f[1] = -coeffs3f.coeffs()[2]/coeffs3f.coeffs()[1];
00109       CALL_SUBTEST(check_linearRegression(10, points2f_ptrs, coeffs2f, 0.05f));
00110       CALL_SUBTEST(check_linearRegression(100, points2f_ptrs, coeffs2f, 0.01f));
00111       CALL_SUBTEST(check_linearRegression(1000, points2f_ptrs, coeffs2f, 0.002f));
00112     }
00113 #endif
00114 #ifdef EIGEN_TEST_PART_2
00115     {
00116       Vector2f points2f [1000];
00117       Vector2f *points2f_ptrs [1000];
00118       for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
00119       Hyperplane<float,2> coeffs3f;
00120       makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
00121       CALL_SUBTEST(check_fitHyperplane(10, points2f_ptrs, coeffs3f, 0.05f));
00122       CALL_SUBTEST(check_fitHyperplane(100, points2f_ptrs, coeffs3f, 0.01f));
00123       CALL_SUBTEST(check_fitHyperplane(1000, points2f_ptrs, coeffs3f, 0.002f));
00124     }
00125 #endif
00126 #ifdef EIGEN_TEST_PART_3
00127     {
00128       Vector4d points4d [1000];
00129       Vector4d *points4d_ptrs [1000];
00130       for(int i = 0; i < 1000; i++) points4d_ptrs[i] = &(points4d[i]);
00131       Hyperplane<double,4> coeffs5d;
00132       makeNoisyCohyperplanarPoints(1000, points4d_ptrs, &coeffs5d, 0.01);
00133       CALL_SUBTEST(check_fitHyperplane(10, points4d_ptrs, coeffs5d, 0.05));
00134       CALL_SUBTEST(check_fitHyperplane(100, points4d_ptrs, coeffs5d, 0.01));
00135       CALL_SUBTEST(check_fitHyperplane(1000, points4d_ptrs, coeffs5d, 0.002));
00136     }
00137 #endif
00138 #ifdef EIGEN_TEST_PART_4
00139     {
00140       VectorXcd *points11cd_ptrs[1000];
00141       for(int i = 0; i < 1000; i++) points11cd_ptrs[i] = new VectorXcd(11);
00142       Hyperplane<std::complex<double>,Dynamic> *coeffs12cd = new Hyperplane<std::complex<double>,Dynamic>(11);
00143       makeNoisyCohyperplanarPoints(1000, points11cd_ptrs, coeffs12cd, 0.01);
00144       CALL_SUBTEST(check_fitHyperplane(100, points11cd_ptrs, *coeffs12cd, 0.025));
00145       CALL_SUBTEST(check_fitHyperplane(1000, points11cd_ptrs, *coeffs12cd, 0.006));
00146       delete coeffs12cd;
00147       for(int i = 0; i < 1000; i++) delete points11cd_ptrs[i];
00148     }
00149 #endif
00150   }
00151 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:38