comparison toolboxes/FullBNT-1.0.7/KPMtools/computeROC.m @ 0:e9a9cd732c1e tip

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