Thursday, March 27, 2014

Ridge Regression Model

In ridge regression model, optimal w is given by:
\$(X^{T}X+\alpha I)^{-1}X^{T}Y\$

The code:


 void CRidgeRegression::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;  
                }  
           }  
      }  
      MatrixXd tmp = mat.transpose()*mat + m_alpha*MatrixXd::Identity(n + 1, n + 1);  
      JacobiSVD<MatrixXd> svd(tmp, ComputeThinU | ComputeThinV);  
      MatrixXd res = svd.solve(mat.transpose()*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