Skip to content

Commit

Permalink
Manually calculate ROC.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanntg committed Apr 5, 2015
1 parent f2a1c4f commit ed5c6d1
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions roc_anomally.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
function [tpr, fpr, th, auc] = roc_anomally(im_target, im_out, mn, mx)
function [tpr, fpr, th, auc] = roc_anomally(im_target, im_out)
%ROC_ANOMALLY Plot receiver operating characteristic and return values.
% Takes a target image and the output of an anomally detector.
% Shows a plot of the ROC curve.
% The anomally detector response is automatically normalized, unless mn
% and max are specified.

% remove color component
if 3 == ndims(im_target)
Expand All @@ -14,18 +12,34 @@
v_target = im_target(:);
v_out = im_out(:);

if nargin < 3
mn = min(v_out);
mx = max(v_out);
end
if abs(mn - 0) > 1e-16 || abs(mx - 1) > 1e-16
v_out = (v_out - mn) / (mx - mn);
% sort target accordingly
[th, ind] = sort(v_out, 'descend');
s_target = v_target(ind);

num = numel(s_target);
tp = 0; fp = 0;
fn = sum(s_target); tn = num - fn;
fpr = zeros(1 + num, 1); tpr = zeros(1 + num, 1);
for v = 1:num
if s_target(v)
tp = tp + 1;
fn = fn - 1;
else
fp = fp + 1;
tn = tn - 1;
end
tpr(1 + v) = tp / (tp + fn);
fpr(1 + v) = fp / (tn + fp);
end

plotroc(v_target', v_out');
[tpr, fpr, th] = roc(v_target', v_out');
% make plot
figure;
plot(fpr, tpr, 'b', 'LineWidth', 2.);
auc = trapz(fpr, tpr);
fprintf('AUC: %.5f\n', auc);
legend(sprintf('ROC (AUC: %.3f)', auc), 'Location', 'SouthEast');
hold on; plot([0; 1], [0; 1], '-', 'Color', [.8 .8 .8]); hold off;
xlim([0 1]); ylim([0 1]);
xlabel('FPR'); ylabel('TPR');

end

0 comments on commit ed5c6d1

Please sign in to comment.