-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathroc.m
73 lines (64 loc) · 2.19 KB
/
roc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
% ROC Receiver operating characteristic curve
%
% [tp,fp] = roc(data);
%
% INPUTS
% data - Nx2 matrix [t , y], where
% t - a vector indicating class value (>0 positive class, <=0 negative)
% y - score value for each instance
%
% OUTPUTS
% tp - true positive rate
% fp - false positive rate
%
% EXAMPLES
% % Classic binormal ROC. 100 samples from each class, with a unit mean separation
% % between the classes.
% >> mu = 1;
% >> y = [randn(100,1)+mu ; randn(100,1)];
% >> t = [ones(100,1) ; zeros(100,1)];
% >> [tp,fp] = roc([t,y])
% >> plot(fp,tp);
%
% REFERENCE
% Tom Fawcett. ROC Graphs: Notes and practical considerations (Algorithm 3)
% Hewlett Packard Technical report 2003-4
% $ Copyright (C) 2014 Brian Lau http://www.subcortex.net/ $
% The full license and most recent version of the code can be found on GitHub:
% https://github.com/brian-lau/MatlabAUC
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% REVISION HISTORY:
% brian 03.08.08 written based on http://theoval.sys.uea.ac.uk/matlab/roc/roc.m
% Copyright G.C. Cawley under GPLv2
% with the ability to handle ties
function [tp,fp] = roc(data)
if size(data,2) ~= 2
error('Incorrect input size in ROC!');
end
t = data(:,1);
y = data(:,2);
% process targets
t = t > 0;
% sort by classifier output
[Y,idx] = sort(-y);
t = t(idx);
% compute true positive and false positive rates
tp = cumsum(t)/sum(t);
fp = cumsum(~t)/sum(~t);
% handle equally scored instances (BL 030708, see pg. 10 of Fawcett)
[uY,idx] = unique(Y);
tp = tp(idx);
fp = fp(idx);
% add trivial end-points
tp = [0 ; tp ; 1];
fp = [0 ; fp ; 1];