annotate _FullBNT/KPMtools/computeROC.m @ 9:4ea6619cb3f5 tip

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