wolffd@0: function [falseAlarmRate, detectionRate, area, th] = plotROC(confidence, testClass, col, varargin) wolffd@0: % You pass the scores and the classes, and the function returns the false wolffd@0: % alarm rate and the detection rate for different points across the ROC. wolffd@0: % wolffd@0: % [faR, dR] = plotROC(score, class) wolffd@0: % wolffd@0: % faR (false alarm rate) is uniformly sampled from 0 to 1 wolffd@0: % dR (detection rate) is computed using the scores. wolffd@0: % wolffd@0: % class = 0 => target absent wolffd@0: % class = 1 => target present wolffd@0: % wolffd@0: % score is the output of the detector, or any other measure of detection. wolffd@0: % There is no plot unless you add a third parameter that is the color of wolffd@0: % the graph. For instance: wolffd@0: % [faR, dR] = plotROC(score, class, 'r') wolffd@0: % wolffd@0: % faR, dR are size 1x1250 wolffd@0: wolffd@0: if nargin < 3, col = []; end wolffd@0: [scale01] = process_options(varargin, 'scale01', 1); wolffd@0: 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: wolffd@0: ndxAbs = find(testClass==0); % absent wolffd@0: ndxPres = find(testClass==1); % present wolffd@0: wolffd@0: [th, j] = sort(confidence(ndxAbs)); wolffd@0: th = th(fix(linspace(1, length(th), 1250))); wolffd@0: wolffd@0: cAbs = confidence(ndxAbs); wolffd@0: cPres = confidence(ndxPres); wolffd@0: for t=1:length(th) wolffd@0: if length(ndxPres) == 0 wolffd@0: detectionRate(t) = 0; wolffd@0: else wolffd@0: detectionRate(t) = sum(cPres>=th(t)) / length(ndxPres); wolffd@0: end wolffd@0: if length(ndxAbs) == 0 wolffd@0: falseAlarmRate(t) = 0; wolffd@0: else wolffd@0: falseAlarmRate(t) = sum(cAbs>=th(t)) / length(ndxAbs); wolffd@0: end wolffd@0: wolffd@0: %detectionRate(t) = sum(confidence(ndxPres)>=th(t)) / length(ndxPres); wolffd@0: %falseAlarmRate(t) = sum(confidence(ndxAbs)>=th(t)) / length(ndxAbs); wolffd@0: %detections(t) = sum(confidence(ndxPres)>=th(t)); wolffd@0: %falseAlarms(t) = sum(confidence(ndxAbs)>=th(t)); wolffd@0: end wolffd@0: wolffd@0: area = sum(abs(falseAlarmRate(2:end) - falseAlarmRate(1:end-1)) .* detectionRate(2:end)); wolffd@0: wolffd@0: if ~isempty(col) wolffd@0: h=plot(falseAlarmRate, detectionRate, [col '-']); wolffd@0: %set(h, 'linewidth', 2); wolffd@0: e = 0.05; wolffd@0: if scale01 wolffd@0: axis([0-e 1+e 0-e 1+e]) wolffd@0: else wolffd@0: % zoom in on the top left corner wolffd@0: axis([0-e 0.5+e 0.5-e 1+e]) wolffd@0: end wolffd@0: grid on wolffd@0: ylabel('detection rate') wolffd@0: xlabel('false alarm rate') wolffd@0: end