function betas=lars(X,y, max_steps, tracep) % LARS Least Angle Regression % lars(X,y, max_steps) returns the coefficients at each step in LARS regression % INPUT: X is the design matrix of n by m, % where n is the number of examples and m is the number of features % y is the response vector of n by 1 % max_steps is max number of steps for stopping early % tracep is true/false for verbose display % OUTPUT: % betas contains the regression coefficient at each step as % columns. It is usually an m by min(m,max_steps) matrix. % % Reference: % Bradley Efron, Trevor Hastie, Iain Johnstone, and Robert Tibshirani. % Least angle regression % Source: Ann. Statist. 32 (2004), no. 2, 407--499 % http://www-stat.stanford.edu/~hastie/pub.htm [n,m]=size(X); assert(n>m, 'more observations than covariates'); y=y(:); assert(length(y)==n, 'X is n x m and Y is of length n'); eps_tol=1E-10; assert(max(abs(sum(X,1)))C_hat-eps_tol); % the set of covariates that are maximally correlated cov_added = setdiff(Acurl, Acurl_old); cov_added=cov_added(:)'; Acurl=[Acurl_old cov_added]; % so new covariates would always be appending, necessary for update Cholesky s=sign(C(Acurl)); s=s(:);% get the sign of the maximal correlations if (isempty(R)) % first time Cholesky decomposition XA=X(:,Acurl); try R=chol(XA'*XA); catch error(['LARS Variable ' sprintf(' %i', Acurl) ' are collinear ']); end else % otherwise build Cholesky incrementally Rout=updateCholXtX(R, X(:,Acurl_old), X(:,cov_added)); R=Rout; end % check for linear redundancy in data if size(R,2)==1, diagR=R(1,1); else diagR=diag(R); end; if (min(abs(diagR))eps_tol); % take only positive gammas gamma=min(gammas); % move to the soonest event end mu_hat=mu_hat+gamma*u; % update the current response beta(Acurl)=beta(Acurl)+gamma*beta_inc; % update beta betas(:,k)=beta; % record beta end % make sure the last beta is the Ordinary Least Squares solution if hit_all, assert(abs(betas(:,end)-X\y)