1 | function [beta]=lasso(X,Y,lambda) |
---|
2 | % function [beta]=lasso(X,Y,lambda) |
---|
3 | % This function minimize |
---|
4 | % f(beta)=1/N*(Y-X*beta)'*(Y-X*beta)+lambda*sum(abs(beta)) |
---|
5 | % with respect to beta. |
---|
6 | % |
---|
7 | % X is a matrix of explanatory variables |
---|
8 | % Y is a vector with the response variables |
---|
9 | % lambda is the weight of the Lagrange factor |
---|
10 | % N is the number of observations |
---|
11 | |
---|
12 | [n,p]=size(X); |
---|
13 | X=X/sqrt(n); |
---|
14 | Y=Y/sqrt(n); |
---|
15 | Xtwo=2*X'*X; |
---|
16 | XY=2*X'*Y; |
---|
17 | |
---|
18 | %% Improvement suggested by Sijmen de Jong |
---|
19 | |
---|
20 | if(p>n) |
---|
21 | beta=X'*pinv( X * X') * Y; |
---|
22 | else |
---|
23 | beta=pinv(Xtwo)*XY; |
---|
24 | end |
---|
25 | %% |
---|
26 | notfound=1; |
---|
27 | |
---|
28 | while(notfound==1) |
---|
29 | notzero=find(abs(beta)>1e-5); |
---|
30 | lnz=length(notzero); |
---|
31 | therest=setdiff(1:p,notzero); |
---|
32 | if(length(therest>0)) |
---|
33 | beta(therest)=0; |
---|
34 | end; |
---|
35 | if(lnz==0) |
---|
36 | notfound=0; |
---|
37 | else |
---|
38 | g=-XY(notzero)+Xtwo(notzero,notzero)*beta(notzero)+lambda*sign(beta(notzero)); |
---|
39 | Z=sparse(1:lnz,1:lnz,0.5./abs(beta(notzero)),lnz,lnz); |
---|
40 | if(lnz<n) |
---|
41 | betastep=(Xtwo(notzero,notzero)+lambda*Z)\g; |
---|
42 | else |
---|
43 | Ainv=diag(1./diag(2*lambda*Z)); |
---|
44 | betastep=Ainv*g-Ainv*(2*X(:,notzero)'*((X(:,notzero)*Ainv*2*X(:,notzero)'+eye(n))\(X(:,notzero)*(Ainv*g)))); |
---|
45 | end |
---|
46 | tempsing=find(betastep==Inf); |
---|
47 | if((max(isinf(betastep))+max(isnan(betastep)))>0) |
---|
48 | newbeta=ones(size(tempbeta))*NaN; |
---|
49 | notfound=0; |
---|
50 | else |
---|
51 | newbeta=beta(notzero)-betastep; |
---|
52 | if(max(abs(betastep))<1E-9) |
---|
53 | notfound=0; |
---|
54 | end; |
---|
55 | end |
---|
56 | beta(notzero)=newbeta; |
---|
57 | end |
---|
58 | end |
---|
59 | |
---|