Solved – Confused by MATLAB’s implementation of ridge

MATLABridge regressionsparse

I have two different implementations of ridge in MATLAB. One is simply

  1. $\mathbf x = (\mathbf{A}'\mathbf{A}+\mathbf{I}\lambda)^{-1}\mathbf{A}'\mathbf b$

    (as seen on Wikipedia's ridge regression page), with $\mathbf{I}$ being the identity matrix of size columns($\mathbf{A}$) $\times$ columns($\mathbf{A}$), and

  2. I'm simply calling Matlab's "ridge" with

    x = ridge(A, b, lambda)
    

My problem is that both return different results. (1) returns the results that I want (I know this by comparing results with other people) but why does (2) not return the same results?

My matrix $\mathbf A$ is sparse, it's filled with 1% 1's and 99% 0's. Some columns contain almost no 1's. The biggest difference seems to be that the coefficient for those columns with very few 1's are very close to 0 in (1), but can be quite far from 0 in (2)

Does anyone have any idea why it's different and how I can modify the call in (2) to give the same results as (1)?

Best Answer

This is a matlab program to validate what cardinal said, it is actually due to the centering and scaling

% Create A(10 by 3 matrix) and b(10 by 1 matrix)
A=rand(10,3);
b=rand(10,1);
lambda=0.01
% centering and scaling A 
s=std(A,0,1);
s=repmat(s,10,1);
A=(A-repmat(mean(A),10,1))./s;

%check the result
X1=inv(A'*A+eye(3)*lambda)*A'*b;
X2=ridge(b,A,lambda,1);

x1 then equal x2