forked from ChristianGaser/BrainAGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroc.m
122 lines (104 loc) · 3.11 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function [tp, fp, AUC, THmax, tpmax, fpmax, TH85] = roc(t, y)
%
% ROC - generate a receiver operating characteristic curve
%
% [TP,FP] = ROC(T,Y) gives the true-positive rate (TP) and false positive
% rate (FP), where Y is a column vector giving the score assigned to each
% pattern and T indicates the true class (a value above zero represents
% the positive class and anything else represents the negative class). To
% plot the ROC curve,
%
% PLOT(FP,TP);
% XLABEL('FALSE POSITIVE RATE');
% YLABEL('TRUE POSITIVE RATE');
% TITLE('RECEIVER OPERATING CHARACTERISTIC (ROC)');
%
% See [1] for further information.
%
% [1] Fawcett, T., "ROC graphs : Notes and practical
% considerations for researchers", Technical report, HP
% Laboratories, MS 1143, 1501 Page Mill Road, Palo Alto
% CA 94304, USA, April 2004.
%
% See also : ROCCH, AUROC
%
% File : roc.m
%
% Date : Friday 9th June 2005
%
% Author : Dr Gavin C. Cawley
%
% Description : Generate an ROC curve for a two-class classifier.
%
% References : [1] Fawcett, T., "ROC graphs : Notes and practical
% considerations for researchers", Technical report, HP
% Laboratories, MS 1143, 1501 Page Mill Road, Palo Alto
% CA 94304, USA, April 2004.
%
% History : 10/11/2004 - v1.00
% 09/06/2005 - v1.10 - minor recoding
% 05/09/2008 - v2.00 - re-write using algorithm from [1]
%
% Copyright : (c) G. C. Cawley, September 2008.
%
% 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 2 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.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%
y0 = y;
t0 = t;
ntp = size(y,1);
% sort by classeifier output
[y,idx] = sort(y, 'descend');
t = t(idx) > 0;
% switch values if neccessary
if mean(y(t)) < mean(y(~t))
[y,idx] = sort(y0, 'ascend');
t = t0(idx) > 0;
end
% generate ROC
P = sum(t);
N = ntp - P;
fp = zeros(ntp+2,1);
tp = zeros(ntp+2,1);
FP = 0;
TP = 0;
n = 1;
yprev = -realmax;
for i=1:ntp
if y(i) ~= yprev
tp(n) = TP/P;
fp(n) = FP/N;
yprev = y(i);
n = n + 1;
end
if t(i) == 1
TP = TP + 1;
else
FP = FP + 1;
end
end
tp(n) = 1;
fp(n) = 1;
fp = fp(1:n);
tp = tp(1:n);
fp_tp = tp + 1 - fp;
ind_max = find(fp_tp==max(fp_tp));
THmax = y(ind_max);
tpmax = tp(ind_max);
fpmax = 1-fp(ind_max);
ind85 = min(find(tp>=0.85));
TH85 = y(ind85);
n = size(tp, 1);
AUC = sum((fp(2:n) - fp(1:n-1)).*(tp(2:n)+tp(1:n-1)))/2;
% bye bye...