LinRegression.cpp 2.6 KB

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