function a = auc_roc(f,y,verbose)

if nargin < 3
    verbose = 0;
end

% sort by score
[f,i] = sort(f);
y = y(i);
n = length(f);

% postive examples
np = max(sum(y >  0), 1);
% negative examples
nn = max(sum(y <= 0), 1);

% true positives
tp = np*ones(n+1, 1);
% false positives
fp = nn*ones(n+1, 1);

for i = 1:n
    if y(i) > 0
        % positive example
        tp(i+1) = tp(i) - 1;
        fp(i+1) = fp(i);
    else
        % negative example
        tp(i+1) = tp(i);
        fp(i+1) = fp(i) - 1;
    end
end
fp_rate = fp(end:-1:1)/nn;
tp_rate = tp(end:-1:1)/np;

a = auc(fp_rate, tp_rate);
if verbose
    plot(fp_rate, tp_rate);
    title(['AUC (ROC): ' num2str(a, '%5.3f%%')]);	
end
