function f=alasso(x,y);
% Returns alasso solution via 5-fold cross-validation with weights based on the Lasso estimate  
% Assume that x and y are centered.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n,p]=size(x);

%if n>=p 
%    wei=x'*x\x'*y; % inverse of weight vector
%else
    [XX,mm,s]=normalize(x);
    [s_opt, b] = crossvalidate(@lars, 5, 1000, XX, y, 'lasso', 0, 0, [], 0);
    wei=b./(sqrt(n)*s);
    wei=wei';
%end

wei = abs(wei);
X=x.*(ones(n,1)*wei');
flag = (wei ~= 0)';
X = X(:,flag);

%[XX,mm,s]=normalize(X);
%[s_opt, b] = crossvalidate(@lars, 5, 1000, XX, y, 'lasso', 0, 0, [], 0);
%b=b./(sqrt(n)*s);

[s_opt, b] = crossvalidate(@lars, 5, 1000, X, y, 'lasso', 0, 0, [], 0);


b=b'.*wei(flag);
betahat = zeros(p,1);
betahat(flag) = b;
f = betahat;
end %main function
