LinRegression.cpp 2.6 KB

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