LinRegression.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  26. LinRegression::~LinRegression()
  27. {
  28. }
  29. LinRegression* LinRegression::clone ( void ) const
  30. {
  31. return new LinRegression(*this);
  32. }
  33. void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
  34. if (dim == 0) //dimension not specified via constructor
  35. {
  36. dim = x[0].size()+1; //use full dimension of data
  37. }
  38. cerr<<"dim: "<<dim<<endl;
  39. cerr<<"examples: "<<x.size()<<endl;
  40. for ( uint i = 0;i < dim;i++ ) //initialize vector of model parameters
  41. {
  42. modelParams.push_back(0.0);
  43. }
  44. if ( dim == 2 ) //two-dimensional least squares
  45. {
  46. double meanX;
  47. double meanY = y.Mean();
  48. double sumX = 0.0;
  49. for ( uint i = 0;i < x.size();i++ )
  50. sumX += x[i][0];
  51. meanX = sumX / (double)x.size();
  52. for ( uint i = 0; i < x.size(); i++ )
  53. modelParams[1] += x[i][0] * y[i];
  54. modelParams[1] -= x.size() * meanX * meanY;
  55. double tmp = 0.0;
  56. for ( uint i = 0; i < x.size(); i++ )
  57. tmp += x[i][0] * x[i][0];
  58. tmp -= x.size() * meanX * meanX;
  59. modelParams[1] /= tmp;
  60. modelParams[0] = meanY - modelParams[1] * meanX;
  61. }
  62. else { //N-dimensional least squares
  63. NICE::Matrix X, tmp, G;
  64. NICE::Vector params;
  65. x.toMatrix(X);
  66. NICE::Matrix Xtmp(X.rows(),X.cols()+1,1.0);
  67. // attach front column with ones
  68. for(uint row = 0;row<X.rows();row++)
  69. {
  70. for(uint col = 0;col<X.cols();col++)
  71. {
  72. Xtmp(row,col+1) = X(row,col);
  73. }
  74. }
  75. // modelParams =(X'X)^-1 * X'y
  76. NICE::Matrix tmpInv;
  77. NICE::Vector rhs;
  78. rhs.multiply(Xtmp,y,true);
  79. tmp.multiply(Xtmp,Xtmp,true);
  80. choleskyDecomp(tmp,G);
  81. choleskyInvert(G,tmpInv);
  82. params.multiply(tmpInv,rhs);
  83. modelParams = params.std_vector();
  84. }
  85. }
  86. std::vector<double> LinRegression::getModelParams(){
  87. return modelParams;
  88. }
  89. double LinRegression::predict ( const NICE::Vector & x ){
  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. }