% roc_compare.m
% Comparing the performance of two classifiers using ROC and AUC
% Dr. David Pan, UAH

clear all;
close all;
M = 100;   % number of samples generated for each class

% Generate data entries for Class 1
m1 = [-2,0];
c1 = [1,0;0,1];
rng default  % For reproducibility
r1 = mvnrnd(m1,c1,M);

data_C1 = zeros(M, 2);
data_C1 = r1;
label_C1 = ones(M, 1);

% Generate data entries for Class 2
m2 = [2,0];
c2 = [1,0;0,1];
rng default  % For reproducibility
r2 = mvnrnd(m2,c2,100);

data_C2 = zeros(M, 2);
data_C2 = r2;
label_C2 = 2*ones(M, 1);

% Combine data of two classes
data = vertcat (data_C1, data_C2);
label = vertcat (label_C1, label_C2);

% Plot the data samples of the two classes
gscatter(data(:,1),data(:,2),label,'rb','ox')

% Classifier A uses a straight line (perpendicular to the horizontal axis)
% as the decision boundary
% Continuously change the threshold T to move the decistion boundary

k = 1;
for T = 10: -0.01: -10
    % The positive class is Class 2
    S = find(data_C2(:,1) >= T);
    TP = length(S); % True Positives
    TPR_A(k) = TP/M;

    S = find(data_C1(:,1) >= T);
    FP = length(S); % False Positives
    FPR_A(k) = FP/M;
    
    k = k+1;
end

figure;
h1 = plot(FPR_A, TPR_A); grid;
title('ROC''s of Two Classifiers');
xlabel('False Positive Rate');
ylabel('True Positive Rate');

AUC_A = trapz(FPR_A, TPR_A)

% Classifier B uses a 45 deg rising line  x - y = T as the decision bounary
% Continuously change T to move the decistion boundary

k = 1;
for T = 10: -0.01: -10
    % The positive class is Class 2
    S = find(data_C2(:,1) - data_C2(:,2) >= T);
    TP = length(S); % True Positives
    TPR_B(k) = TP/M;

    S = find(data_C1(:,1) - data_C1(:,2) >= T);
    FP = length(S); % False Positives
    FPR_B(k) = FP/M;
   
    k = k+1;
end

hold on;
h2 = plot(FPR_B, TPR_B); grid;

legend([h1 h2],'Classifier A', 'Classifier B');

AUC_B = trapz(FPR_B, TPR_B)
grid


 



