function [ap, rec, prec] = aprecision(TY, confidence)
% compute precision and recall

[aaa, sc] = sort(confidence, 'descend');
tp = TY(sc) == 1;
fp = TY(sc) == -1;
tp = cumsum(tp);
fp = cumsum(fp);
rec = tp/sum(TY == 1);
prec = tp./(tp+fp);

% compute average precision
ap = 0;
for t = 0:0.1:1
    p = max(prec(rec >= t));
    if isempty(p)
        p = 0;
    end
    ap = ap + p/11;
end
