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