function [fp,tp,auc,confsort] = roc(conf,gt,draw)

if nargin<4 rocplotfname=[]; end

conf=conf(:);
gt=gt(:);

% compute true and false positive rates
[so,si]=sort(-conf);
confsort=conf(si);
tporig=cumsum(gt(si)>0)/sum(gt>0);
fporig=cumsum(gt(si)<=0)/sum(gt<=0);
[uo,ui]=unique(so);
tp=[0;tporig(ui);1];
fp=[0;fporig(ui);1];

% compute lower envelope and area under curve
di=[true ; tp(2:end-1)~=tp(1:end-2) ; true];
x=fp(di);
y=tp(di);
auc=(x(2:end)-x(1:end-1))'*y(1:end-1);

if draw
    % plot lower envelope
    xp=[0 ; reshape([x x]',[],1) ; 1 ; 1];
    yp=[0 ; 0 ; reshape([y y]',[],1) ; 1];

    plot(xp,yp,'-','LineWidth',3);
    grid;
    axis([0 1 0 1]);
    xlabel('false positive rate','FontSize',16)
    ylabel('true positive rate','FontSize',16)
    title(sprintf('AUC = %.7f',auc),'FontSize',12);
end
