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