wolffd@0: function [FPrate, TPrate, AUC, thresholds] = computeROC(confidence, testClass) wolffd@0: % function [FPrate, TPrate, AUC, thresholds] = computeROC(confidence, testClass) wolffd@0: % wolffd@0: % computeROC computes the data for an ROC curve based on a classifier's confidence output. wolffd@0: % It returns the false positive rate and the true positive rate along with wolffd@0: % the area under the ROC curve, and the list of thresholds. wolffd@0: % wolffd@0: % Inputs: wolffd@0: % - confidence(i) is proportional to the probability that wolffd@0: % testClass(i) is positive wolffd@0: % wolffd@0: % testClass = 0 => target absent wolffd@0: % testClass = 1 => target present wolffd@0: % wolffd@0: % Based on algorithms 2 and 4 from Tom Fawcett's paper "ROC Graphs: Notes and wolffd@0: % Practical Considerations for Data Mining Researchers" (2003) wolffd@0: % http://www.hpl.hp.com/techreports/2003/HPL-2003-4.pdf" wolffd@0: % wolffd@0: % Vlad Magdin, 21 Feb 2005 wolffd@0: wolffd@0: % break ties in scores wolffd@0: S = rand('state'); wolffd@0: rand('state',0); wolffd@0: confidence = confidence + rand(size(confidence))*10^(-10); wolffd@0: rand('state',S) wolffd@0: [thresholds order] = sort(confidence, 'descend'); wolffd@0: testClass = testClass(order); wolffd@0: wolffd@0: %%% -- calculate TP/FP rates and totals -- %%% wolffd@0: AUC = 0; wolffd@0: faCnt = 0; wolffd@0: tpCnt = 0; wolffd@0: falseAlarms = zeros(1,size(thresholds,2)); wolffd@0: detections = zeros(1,size(thresholds,2)); wolffd@0: fPrev = -inf; wolffd@0: faPrev = 0; wolffd@0: tpPrev = 0; wolffd@0: wolffd@0: P = max(size(find(testClass==1))); wolffd@0: N = max(size(find(testClass==0))); wolffd@0: wolffd@0: for i=1:length(thresholds) wolffd@0: if thresholds(i) ~= fPrev wolffd@0: falseAlarms(i) = faCnt; wolffd@0: detections(i) = tpCnt; wolffd@0: wolffd@0: AUC = AUC + polyarea([faPrev faPrev faCnt/N faCnt/N],[0 tpPrev tpCnt/P 0]); wolffd@0: wolffd@0: fPrev = thresholds(i); wolffd@0: faPrev = faCnt/N; wolffd@0: tpPrev = tpCnt/P; wolffd@0: end wolffd@0: wolffd@0: if testClass(i) == 1 wolffd@0: tpCnt = tpCnt + 1; wolffd@0: else wolffd@0: faCnt = faCnt + 1; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: AUC = AUC + polyarea([faPrev faPrev 1 1],[0 tpPrev 1 0]); wolffd@0: wolffd@0: FPrate = falseAlarms/N; wolffd@0: TPrate = detections/P;