annotate _FullBNT/KPMtools/plotROCkpm.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 [falseAlarmRate, detectionRate, area, th] = plotROC(confidence, testClass, col, varargin)
matthiasm@8 2 % You pass the scores and the classes, and the function returns the false
matthiasm@8 3 % alarm rate and the detection rate for different points across the ROC.
matthiasm@8 4 %
matthiasm@8 5 % [faR, dR] = plotROC(score, class)
matthiasm@8 6 %
matthiasm@8 7 % faR (false alarm rate) is uniformly sampled from 0 to 1
matthiasm@8 8 % dR (detection rate) is computed using the scores.
matthiasm@8 9 %
matthiasm@8 10 % class = 0 => target absent
matthiasm@8 11 % class = 1 => target present
matthiasm@8 12 %
matthiasm@8 13 % score is the output of the detector, or any other measure of detection.
matthiasm@8 14 % There is no plot unless you add a third parameter that is the color of
matthiasm@8 15 % the graph. For instance:
matthiasm@8 16 % [faR, dR] = plotROC(score, class, 'r')
matthiasm@8 17 %
matthiasm@8 18 % faR, dR are size 1x1250
matthiasm@8 19
matthiasm@8 20 if nargin < 3, col = []; end
matthiasm@8 21 [scale01] = process_options(varargin, 'scale01', 1);
matthiasm@8 22
matthiasm@8 23 S = rand('state');
matthiasm@8 24 rand('state',0);
matthiasm@8 25 confidence = confidence + rand(size(confidence))*10^(-10);
matthiasm@8 26 rand('state',S)
matthiasm@8 27
matthiasm@8 28 ndxAbs = find(testClass==0); % absent
matthiasm@8 29 ndxPres = find(testClass==1); % present
matthiasm@8 30
matthiasm@8 31 [th, j] = sort(confidence(ndxAbs));
matthiasm@8 32 th = th(fix(linspace(1, length(th), 1250)));
matthiasm@8 33
matthiasm@8 34 cAbs = confidence(ndxAbs);
matthiasm@8 35 cPres = confidence(ndxPres);
matthiasm@8 36 for t=1:length(th)
matthiasm@8 37 if length(ndxPres) == 0
matthiasm@8 38 detectionRate(t) = 0;
matthiasm@8 39 else
matthiasm@8 40 detectionRate(t) = sum(cPres>=th(t)) / length(ndxPres);
matthiasm@8 41 end
matthiasm@8 42 if length(ndxAbs) == 0
matthiasm@8 43 falseAlarmRate(t) = 0;
matthiasm@8 44 else
matthiasm@8 45 falseAlarmRate(t) = sum(cAbs>=th(t)) / length(ndxAbs);
matthiasm@8 46 end
matthiasm@8 47
matthiasm@8 48 %detectionRate(t) = sum(confidence(ndxPres)>=th(t)) / length(ndxPres);
matthiasm@8 49 %falseAlarmRate(t) = sum(confidence(ndxAbs)>=th(t)) / length(ndxAbs);
matthiasm@8 50 %detections(t) = sum(confidence(ndxPres)>=th(t));
matthiasm@8 51 %falseAlarms(t) = sum(confidence(ndxAbs)>=th(t));
matthiasm@8 52 end
matthiasm@8 53
matthiasm@8 54 area = sum(abs(falseAlarmRate(2:end) - falseAlarmRate(1:end-1)) .* detectionRate(2:end));
matthiasm@8 55
matthiasm@8 56 if ~isempty(col)
matthiasm@8 57 h=plot(falseAlarmRate, detectionRate, [col '-']);
matthiasm@8 58 %set(h, 'linewidth', 2);
matthiasm@8 59 e = 0.05;
matthiasm@8 60 if scale01
matthiasm@8 61 axis([0-e 1+e 0-e 1+e])
matthiasm@8 62 else
matthiasm@8 63 % zoom in on the top left corner
matthiasm@8 64 axis([0-e 0.5+e 0.5-e 1+e])
matthiasm@8 65 end
matthiasm@8 66 grid on
matthiasm@8 67 ylabel('detection rate')
matthiasm@8 68 xlabel('false alarm rate')
matthiasm@8 69 end