function [TPR, FPR, AUC] = stimROC(C, T) %build an ROC curve % C - class labels as an array of binary values (1 = true positive) % T - threshold used for classification %sort the thresholds in descending order and get the indices [~, I] = sort(T, 'descend'); %sort the class labels in the same order as the thresholds Cs = C(I); %calculate the number of measurements M = size(C, 2); %calculate the number of positives P = nnz(C); %calculate the number of negatives N = M - P; %if all examples are positives or negatives, return a perfect score? if P == M error('ERROR: no positive observations'); end if P == 0 error('ERROR: no negative observations'); end %allocate space for the ROC curve TPR = zeros(1, M); FPR = zeros(1, M); %calculate the number of inflection points ip = 0; for i = 2:M if Cs(i) ~= Cs(i-1) ip = ip + 1; end end %initialize the true and false positive rates to zero TP = 0; FP = 0; for i = 1:M if Cs(i) == 1 TP = TP + 1; else FP = FP + 1; end TPR(i) = TP / P; FPR(i) = FP / N; end %calculate the area under the ROC curve AUC = 0; for i = 2:M w = FPR(i) - FPR(i-1); h = TPR(i); AUC = AUC + w * h; end