function [f,g,H] = LogisticLoss(w,X,y,lambda)
% w(feature,1)
% X(instance,feature)
% y(instance,1)

[n,p] = size(X);

Xw = X*w;
yXw = y.*Xw;

f = sum(mylogsumexp([zeros(n,1) -yXw])) + (lambda/2)*(w'*w);

if nargout > 1
    if nargout > 2
        sig = 1./(1+exp(-yXw));
        g = -(X.'*(y.*(1-sig))) + lambda*w;
    else
        g = -(X.'*(y./(1+exp(yXw)))) + lambda*w;
    end
end

if nargout > 2
    H = X.'*diag(sparse(sig.*(1-sig)))*X + lambda*eye(p);
end
