wolffd@0
|
1 function [FPrate, TPrate, AUC, thresholds] = computeROC(confidence, testClass)
|
wolffd@0
|
2 % function [FPrate, TPrate, AUC, thresholds] = computeROC(confidence, testClass)
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % computeROC computes the data for an ROC curve based on a classifier's confidence output.
|
wolffd@0
|
5 % It returns the false positive rate and the true positive rate along with
|
wolffd@0
|
6 % the area under the ROC curve, and the list of thresholds.
|
wolffd@0
|
7 %
|
wolffd@0
|
8 % Inputs:
|
wolffd@0
|
9 % - confidence(i) is proportional to the probability that
|
wolffd@0
|
10 % testClass(i) is positive
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % testClass = 0 => target absent
|
wolffd@0
|
13 % testClass = 1 => target present
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % Based on algorithms 2 and 4 from Tom Fawcett's paper "ROC Graphs: Notes and
|
wolffd@0
|
16 % Practical Considerations for Data Mining Researchers" (2003)
|
wolffd@0
|
17 % http://www.hpl.hp.com/techreports/2003/HPL-2003-4.pdf"
|
wolffd@0
|
18 %
|
wolffd@0
|
19 % Vlad Magdin, 21 Feb 2005
|
wolffd@0
|
20
|
wolffd@0
|
21 % break ties in scores
|
wolffd@0
|
22 S = rand('state');
|
wolffd@0
|
23 rand('state',0);
|
wolffd@0
|
24 confidence = confidence + rand(size(confidence))*10^(-10);
|
wolffd@0
|
25 rand('state',S)
|
wolffd@0
|
26 [thresholds order] = sort(confidence, 'descend');
|
wolffd@0
|
27 testClass = testClass(order);
|
wolffd@0
|
28
|
wolffd@0
|
29 %%% -- calculate TP/FP rates and totals -- %%%
|
wolffd@0
|
30 AUC = 0;
|
wolffd@0
|
31 faCnt = 0;
|
wolffd@0
|
32 tpCnt = 0;
|
wolffd@0
|
33 falseAlarms = zeros(1,size(thresholds,2));
|
wolffd@0
|
34 detections = zeros(1,size(thresholds,2));
|
wolffd@0
|
35 fPrev = -inf;
|
wolffd@0
|
36 faPrev = 0;
|
wolffd@0
|
37 tpPrev = 0;
|
wolffd@0
|
38
|
wolffd@0
|
39 P = max(size(find(testClass==1)));
|
wolffd@0
|
40 N = max(size(find(testClass==0)));
|
wolffd@0
|
41
|
wolffd@0
|
42 for i=1:length(thresholds)
|
wolffd@0
|
43 if thresholds(i) ~= fPrev
|
wolffd@0
|
44 falseAlarms(i) = faCnt;
|
wolffd@0
|
45 detections(i) = tpCnt;
|
wolffd@0
|
46
|
wolffd@0
|
47 AUC = AUC + polyarea([faPrev faPrev faCnt/N faCnt/N],[0 tpPrev tpCnt/P 0]);
|
wolffd@0
|
48
|
wolffd@0
|
49 fPrev = thresholds(i);
|
wolffd@0
|
50 faPrev = faCnt/N;
|
wolffd@0
|
51 tpPrev = tpCnt/P;
|
wolffd@0
|
52 end
|
wolffd@0
|
53
|
wolffd@0
|
54 if testClass(i) == 1
|
wolffd@0
|
55 tpCnt = tpCnt + 1;
|
wolffd@0
|
56 else
|
wolffd@0
|
57 faCnt = faCnt + 1;
|
wolffd@0
|
58 end
|
wolffd@0
|
59 end
|
wolffd@0
|
60
|
wolffd@0
|
61 AUC = AUC + polyarea([faPrev faPrev 1 1],[0 tpPrev 1 0]);
|
wolffd@0
|
62
|
wolffd@0
|
63 FPrate = falseAlarms/N;
|
wolffd@0
|
64 TPrate = detections/P;
|