GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
LinearLeastSquares.h
Go to the documentation of this file.
1 
33 #ifndef GRT_LINEAR_LEAST_SQUARES_HEADER
34 #define GRT_LINEAR_LEAST_SQUARES_HEADER
35 
36 #include "../../CoreModules/MLBase.h"
37 
38 GRT_BEGIN_NAMESPACE
39 
40 class LinearLeastSquares : public MLBase{
41 public:
42  LinearLeastSquares() : MLBase("LinearLeastSquares") {
43  m = 0;
44  b = 0;
45  r = 0;
46  }
47 
48  virtual ~LinearLeastSquares(){
49 
50  }
51 
56  virtual bool clear() override {
57  m = 0;
58  b = 0;
59  r = 0;
60  return MLBase::clear();
61  }
62 
69  bool solve( const VectorFloat &x, const VectorFloat &y ){
70 
71  //Reset any previous results
72  clear();
73 
74  if( x.getSize() == 0 && y.getSize() == 0 ){
75  warningLog << __GRT_LOG__ << " Failed to compute solution, input vectors are empty!" << std::endl;
76  return false;
77  }
78 
79  if( x.getSize() != y.getSize() ){
80  warningLog << __GRT_LOG__ << " Failed to compute solution, input vectors do not have the same size!" << std::endl;
81  return false;
82  }
83 
84  const unsigned int N = x.getSize();
85  Float sumx = 0.0; //Stores the sum of x
86  Float sumx2 = 0.0; //Stores the sum of x^2
87  Float sumxy = 0.0; //Stores the sum of x * y
88  Float sumy = 0.0; //Stores the sum of y
89  Float sumy2 = 0.0; //Stores the sum of y**2
90  Float denom = 0;
91 
92  m = 0;
93  b = 0;
94  r = 0;
95 
96  for(unsigned int i=0; i<N; i++){
97  sumx += x[i];
98  sumx2 += SQR(x[i]);
99  sumxy += x[i] * y[i];
100  sumy += y[i];
101  sumy2 += SQR(y[i]);
102  }
103 
104  denom = (N * sumx2 - SQR(sumx));
105  if (denom == 0) {
106  // singular matrix. can't solve the problem.
107  warningLog << __GRT_LOG__ << " Failed to compute solution, singular matrix detected!" << std::endl;
108  return false;
109  }
110 
111  m = (N*sumxy - sumx*sumy) / denom;
112  b = (sumy*sumx2 - sumx*sumxy) / denom;
113 
114  //compute correlation coeff
115  Float norm = 1.0/N;
116  sumx *= norm;
117  sumy *= norm;
118  sumxy *= norm;
119  sumx2 *= norm;
120  sumy2 *= norm;
121  r = (sumxy - sumx*sumy) / sqrt( (sumx2 - SQR(sumx)) * (sumy2 - SQR(sumy)) );
122 
123  trained = true;
124  converged = true;
125 
126  return true;
127  }
128 
133  Float getM() const { return m; }
134 
139  Float getB() const { return b; }
140 
145  Float getR() const { return r; }
146 
147 protected:
148 
149  Float m;
150  Float b;
151  Float r;
152 
153 };
154 
155 GRT_END_NAMESPACE
156 
157 #endif //GRT_MEAN_SHIFT_HEADER
virtual bool clear() override
MLBase(const std::string &id="", const BaseType type=BASE_TYPE_NOT_SET)
Definition: MLBase.cpp:26
UINT getSize() const
Definition: Vector.h:201
bool solve(const VectorFloat &x, const VectorFloat &y)
virtual bool clear()
Definition: MLBase.cpp:149
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72