00001 #ifndef __VMML__LINEAR_LEAST_SQUARES__HPP__
00002 #define __VMML__LINEAR_LEAST_SQUARES__HPP__
00003
00004 #include <vmmlib/vector3.h>
00005 #include "matrix_mxn.hpp"
00006
00007 namespace vmml
00008 {
00009
00010
00011
00012
00013
00014
00015 struct linear_least_squares
00016 {
00017
00018 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
00019 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
00020 solve( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
00021 {
00022
00023 assert( data_points.size() >= number_of_data_points );
00024
00025 matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
00026 matrix_mxn< number_of_data_points, 1, float_t > y;
00027
00028 for( size_t nb_index = 0; nb_index < number_of_data_points; ++nb_index )
00029 {
00030 float_t x = data_points[ nb_index ].x;
00031
00032 for( size_t index = 0; index < number_of_unknowns; ++index )
00033 {
00034 X[ nb_index ][ index ] = x;
00035 x *= x;
00036 }
00037
00038 y[ nb_index ][ 0 ] = data_points[ nb_index ].y;
00039 }
00040
00041 std::cout << X << std::endl;
00042
00043 vmml::matrix_mxn< number_of_unknowns, number_of_data_points > Xt;
00044 X.transposeTo( Xt );
00045
00046 std::cout << Xt << std::endl;
00047
00048 vmml::matrix_mxm< number_of_unknowns > XtX;
00049 XtX.mul( Xt, X );
00050
00051 vmml::matrix_mxm< number_of_unknowns > XtX_inverse;
00052 XtX.computeInverse( XtX_inverse, tolerance );
00053
00054 vmml::matrix_mxn< number_of_unknowns, number_of_data_points > XtX_inv_mul_Xt;
00055
00056 XtX_inv_mul_Xt.mul( XtX_inverse, Xt );
00057
00058 vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
00059 beta_hat.mul( XtX_inv_mul_Xt, y );
00060
00061 std::cout << beta_hat << std::endl;
00062
00063 return beta_hat;
00064 }
00065
00066
00067
00068 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
00069 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
00070 solve_using_qr_decomposition( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
00071 {
00072
00073 assert( data_points.size() >= number_of_data_points );
00074
00075 matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
00076 matrix_mxn< number_of_data_points, 1, float_t > y;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
00094 return beta_hat;
00095 }
00096
00097
00098 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
00099 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
00100 solve_using_svd( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
00101 {
00102
00103
00104 }
00105
00106 };
00107
00108
00109 }
00110
00111 #endif
00112