vmmlib  1.7.0
 All Classes Namespaces Functions Pages
linear_least_squares.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __VMML__LINEAR_LEAST_SQUARES__HPP__
33 #define __VMML__LINEAR_LEAST_SQUARES__HPP__
34 
35 #include <vmmlib/vector3.h>
36 #include "matrix_mxn.hpp"
37 
38 namespace vmml
39 {
40 
41 // beta_hat = ( Xt * X )^-1 * Xt * y;
42 // beta_hat -> best-fit 'solutions' to a,b = [ a,b ]transposed.
43 
44 // TODO FIXME what about the constant???
45 
47 {
48 
49 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
50 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
51 solve( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
52 {
53 
54  assert( data_points.size() >= number_of_data_points );
55 
56  matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
57  matrix_mxn< number_of_data_points, 1, float_t > y;
58 
59  for( size_t nb_index = 0; nb_index < number_of_data_points; ++nb_index )
60  {
61  float_t x = data_points[ nb_index ].x;
62 
63  for( size_t index = 0; index < number_of_unknowns; ++index )
64  {
65  X[ nb_index ][ index ] = x;
66  x *= x;
67  }
68 
69  y[ nb_index ][ 0 ] = data_points[ nb_index ].y;
70  }
71 
72  std::cout << X << std::endl;
73 
74  vmml::matrix_mxn< number_of_unknowns, number_of_data_points > Xt;
75  X.transposeTo( Xt );
76 
77  std::cout << Xt << std::endl;
78 
79  vmml::matrix_mxm< number_of_unknowns > XtX;
80  XtX.mul( Xt, X );
81 
82  vmml::matrix_mxm< number_of_unknowns > XtX_inverse;
83  XtX.computeInverse( XtX_inverse, tolerance );
84 
85  vmml::matrix_mxn< number_of_unknowns, number_of_data_points > XtX_inv_mul_Xt;
86 
87  XtX_inv_mul_Xt.mul( XtX_inverse, Xt );
88 
89  vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
90  beta_hat.mul( XtX_inv_mul_Xt, y );
91 
92  std::cout << beta_hat << std::endl;
93 
94  return beta_hat;
95 }
96 
97 
98 
99 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
100 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
101 solve_using_qr_decomposition( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
102 {
103 
104  assert( data_points.size() >= number_of_data_points );
105 
106  matrix_mxn< number_of_data_points, number_of_unknowns, float_t > X;
107  matrix_mxn< number_of_data_points, 1, float_t > y;
108 
109  // r = y - X * Beta
110  // X = Q*R
111  // Qt * r = Qt * y - ( Qt * Q ) R * Beta =
112  //
113  // | ( Qt * y )_n - R_n * Beta | | U |
114  // | ( Qt * y )_m-n | = | L |
115  //
116  // S = rt * Q * Qt * r = rt * r
117  //
118  // S = Ut * U + Lt * L
119  //
120  // => R_n * BetaHat = ( Qt * y )_n
121  //
122 
123 
124  vmml::matrix_mxn< number_of_unknowns, 1 > beta_hat;
125  return beta_hat;
126 }
127 
128 
129 template< size_t number_of_data_points, size_t number_of_unknowns, typename float_t >
130 vmml::matrix_mxn< number_of_unknowns, 1, float_t >
131 solve_using_svd( std::vector< Vector3< float_t > >& data_points, float_t tolerance = 1e-9 )
132 {
133 
134 
135 }
136 
137 };
138 
139 
140 } // namespace vmml
141 
142 #endif
143