annotate toolboxes/FullBNT-1.0.7/KPMtools/computeROC.m @ 0:cc4b1211e677 tip

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