### Blame view

matlab/stimROC.m 1.44 KB
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69``` `````` 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 ``````