Thursday, March 27, 2014

Ordinary Least Squares Model

In ordinary least squares model, optimal w is given by:
\$w=X^{-1}Y\$

A draft function for this is written as

 void CLinearRegression::ComputeRegression()  
 {  
      int m = m_X.size();  
      int n = m_X[0].size();  
      MatrixXd mat(m,n+1);  
      VectorXd rhs(m);  
      for (int i = 0; i < m; i++)  
      {  
           for (int j = 0; j < n+1; j++)  
           {  
                if (j < n)  
                {  
                     mat(i, j) = m_X[i][j];  
                     rhs(i) = m_Y[i];  
                }  
                else  
                {  
                     mat(i, j) = 1;  
                }  
           }  
      }  
      JacobiSVD<MatrixXd> svd(mat, ComputeThinU | ComputeThinV);  
      MatrixXd res=svd.solve(rhs);  
      m_coef.resize(n);  
      for (int i = 0; i < n; i++)  
      {  
           m_coef[i] = res(i, 0);  
      }  
      m_intercept = res(n, 0);  
 }  

No comments:

Post a Comment