LinRegression.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file LinRegression.cpp
  3. * @brief Algorithm for linear regression
  4. * @author Frank Prüfer
  5. * @date 08/13/2013
  6. */
  7. #include "vislearning/regression/linregression/LinRegression.h"
  8. #include "core/vector/Algorithms.h"
  9. #include "LinRegression.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. LinRegression::LinRegression()
  14. {
  15. dim = 0;
  16. }
  17. LinRegression::LinRegression(uint dimension)
  18. {
  19. dim = dimension;
  20. }
  21. LinRegression::LinRegression ( const LinRegression & src ) :
  22. RegressionAlgorithm ( src )
  23. {
  24. dim = src.dim;
  25. modelParams = src.modelParams;
  26. }
  27. LinRegression::~LinRegression()
  28. {
  29. }
  30. LinRegression* LinRegression::clone ( void ) const
  31. {
  32. return new LinRegression(*this);
  33. }
  34. void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y )
  35. {
  36. if (dim == 0){ //dimension not specified via constructor
  37. dim = x[0].size()+1; //use full dimension of data
  38. }
  39. for ( uint i = 0;i < dim;i++ ){ //initialize vector of model parameters
  40. modelParams.push_back(0.0);
  41. }
  42. if ( dim == 2 ) //two-dimensional least squares
  43. {
  44. double meanX;
  45. double meanY = y.Mean();
  46. double sumX = 0.0;
  47. for ( uint i = 0;i < x.size();i++ )
  48. sumX += x[i][0];
  49. meanX = sumX / (double)x.size();
  50. for ( uint i = 0; i < x.size(); i++ )
  51. modelParams[1] += x[i][0] * y[i];
  52. modelParams[1] -= x.size() * meanX * meanY;
  53. double tmp = 0.0;
  54. for ( uint i = 0; i < x.size(); i++ )
  55. tmp += x[i][0] * x[i][0];
  56. tmp -= x.size() * meanX * meanX;
  57. modelParams[1] /= tmp;
  58. modelParams[0] = meanY - modelParams[1] * meanX;
  59. }
  60. else { //N-dimensional least squares
  61. NICE::Matrix X, tmp, G;
  62. NICE::Vector params;
  63. x.toMatrix(X);
  64. NICE::Matrix Xtmp(X.rows(),X.cols()+1,1.0);
  65. // attach front column with ones
  66. for(uint row = 0;row<X.rows();row++)
  67. {
  68. for(uint col = 0;col<X.cols();col++)
  69. {
  70. Xtmp(row,col+1) = X(row,col);
  71. }
  72. }
  73. // modelParams =(X'X)^-1 * X'y
  74. NICE::Matrix tmpInv;
  75. NICE::Vector rhs;
  76. rhs.multiply(Xtmp,y,true);
  77. tmp.multiply(Xtmp,Xtmp,true);
  78. choleskyDecomp(tmp,G);
  79. choleskyInvert(G,tmpInv);
  80. params.multiply(tmpInv,rhs);
  81. modelParams = params.std_vector();
  82. }
  83. }
  84. std::vector<double> LinRegression::getModelParams()
  85. {
  86. return modelParams;
  87. }
  88. double LinRegression::predict ( const NICE::Vector & x )
  89. {
  90. double y;
  91. if ( dim == 2 ) //two-dimensional least squares
  92. {
  93. y = modelParams[0] + modelParams[1] * x[0];
  94. }
  95. else {
  96. // y = x * modelParams
  97. NICE::Vector nModel(modelParams);
  98. NICE:: Vector xTmp(1,1.0);
  99. xTmp.append(x);
  100. y = xTmp.scalarProduct(nModel);
  101. }
  102. return y;
  103. }