function [betahat,ite,excess] = BoostLasso(X,y,lambda,groups,order);
% using the boosting algorithm to search for the (ordered group) Lasso solution
% at the shrinkage vector lambda. The boosting loss function is the sum of 
% a (approximate) squared loss (induced by X and y) and a Lasso penlaty.
% groups: is a cell indicating grouped variables.
% order: is a cell indicating an ordering/hierarchy
if nargin < 4 grouped = 0;
    ordered = 0;
else grouped = 1;
    if nargin < 5 ordered = 0;
    else ordered = 1;
    end
end
[n,d] = size(X);
if grouped J = length(groups); else J = size(X,2); end
epsilon = 1e-10;
T = 500;
global currentbeta;
global Ij;
    function f = target(beta);
        if (~grouped) penalty = lambda'*abs(beta); end
        if (grouped&&(~ordered))
            penalty = 0;
            for j = 1:J
                penalty = penalty + lambda(j)*norm(beta(groups{j}));
            end
        end
        if (grouped&&(ordered))
            penalty = 0;
            for j = 1:J
                b = [];
                for i = 1:length(order{j})
                    b = [b,groups{order{j}(i)}];
                end
                penalty = penalty + lambda(j)*norm(beta(b));
            end
        end
        f = (y-X*beta)'*(y-X*beta)+penalty;
    end
    function f = fun(g)
        f = target(currentbeta+g.*Ij);
    end

currentbeta = zeros(d,1);
oldloss = target(currentbeta);
for jj = 1:d
    Ij = zeros(d,1); Ij(jj) = 1;
    [a,b] = fminsearch(@fun,0);
    ghat(jj) = a;
    loss(jj) = b;
end
[newloss,minindex] = min(loss);
Ij = zeros(d,1); Ij(minindex) = 1;
currentbeta = currentbeta+ghat(minindex).*Ij;
ite = 1;
excess = oldloss - newloss;
while (excess>epsilon)&&(ite<T)
    oldloss = newloss;
    for jj = 1:d
        Ij = zeros(d,1); Ij(jj) = 1;
        [ghat(jj),loss(jj)] = fminsearch(@fun,0);
    end
    [newloss,minindex] = min(loss);
    Ij = zeros(d,1); Ij(minindex) = 1;
    currentbeta = currentbeta+ghat(minindex).*Ij;
    excess = oldloss - newloss;
    ite = ite+1;
end
betahat = currentbeta;
end
        