clear all

num_samples = 1000;  % number of test samples from each class

sprintf('dataset1:')
dd = load('dataset1.txt');

% test samples from class 1
x1 = dd(:,1);   
% test samples from class 2
x2 = dd(:,2); 

Pd_save = [];
Pf_save = [];

% x_c is a classifier 
for x_c = -10:0.5:30

    % number of TP (True Positive) samples
    nTP = length(find(x2 > x_c));
    % approximate probability of hit, P(x > x_c | x in w=2)
    Pd = nTP / num_samples;

    % number of FP (False Positive) samples
    nFP = length(find(x1 > x_c));
    % approximate probability of false alarm, P(x > x_c | x in w=1)
    Pf = nFP / num_samples;

    Pd_save = [Pd_save; Pd];
    Pf_save = [Pf_save; Pf];
   
end


mean1 = mean(x1)
mean2 = mean(x2)
sig1 = std(x1)
sig2 = std(x2)
sig = sqrt(sig1^2 + sig2^2)

discrim = abs(mean1 - mean2)/sig % d'

figure
hold on
plot(Pf_save, Pd_save, 'r')


sprintf('dataset2:')
dd = load('dataset2.txt');

% test samples from class 1
x1 = dd(:,1);   
% test samples from class 2
x2 = dd(:,2); 


Pd_save = [];
Pf_save = [];

% x_c is a classifier 
for x_c =  -10:0.5:30

    % number of TP (True Positive) samples
    nTP = length(find(x2 > x_c));
    % approximate probability of hit, P(x > x_c | x in w=2)
    Pd = nTP / num_samples;

    % number of FP (False Positive) samples
    nFP = length(find(x1 > x_c));
    % approximate probability of false alarm, P(x > x_c | x in w=1)
    Pf = nFP / num_samples;

    Pd_save = [Pd_save; Pd];
    Pf_save = [Pf_save; Pf];
   
end

mean1 = mean(x1)
mean2 = mean(x2)
sig1 = std(x1)
sig2 = std(x2)
sig = sqrt(sig1^2 + sig2^2)

discrim = abs(mean1 - mean2)/sig % d'

plot(Pf_save, Pd_save, 'b')

sprintf('dataset3:')
dd = load('dataset3.txt');

% test samples from class 1
x1 = dd(:,1);   
% test samples from class 2
x2 = dd(:,2); 

Pd_save = [];
Pf_save = [];

% x_c is a classifier 
for x_c = -10:0.5:30

    % number of TP (True Positive) samples
    nTP = length(find(x2 > x_c));
    % approximate probability of hit, P(x > x_c | x in w=2)
    Pd = nTP / num_samples;

    % number of FP (False Positive) samples
    nFP = length(find(x1 > x_c));
    % approximate probability of false alarm, P(x > x_c | x in w=1)
    Pf = nFP / num_samples;

    Pd_save = [Pd_save; Pd];
    Pf_save = [Pf_save; Pf];
   
end

mean1 = mean(x1)
mean2 = mean(x2)
sig1 = std(x1)
sig2 = std(x2)
sig = sqrt(sig1^2 + sig2^2)

discrim = abs(mean1 - mean2)/sig % d'

plot(Pf_save, Pd_save, 'g')

sprintf('dataset4:')
dd = load('dataset4.txt');

% test samples from class 1
x1 = dd(:,1);   
% test samples from class 2
x2 = dd(:,2); 


Pd_save = [];
Pf_save = [];

% x_c is a classifier 
for x_c = -10:0.5:30

    % number of TP (True Positive) samples
    nTP = length(find(x2 > x_c));
    % approximate probability of hit, P(x > x_c | x in w=2)
    Pd = nTP / num_samples;

    % number of FP (False Positive) samples
    nFP = length(find(x1 > x_c));
    % approximate probability of false alarm, P(x > x_c | x in w=1)
    Pf = nFP / num_samples;

    Pd_save = [Pd_save; Pd];
    Pf_save = [Pf_save; Pf];
   
end

mean1 = mean(x1)
mean2 = mean(x2)
sig1 = std(x1)
sig2 = std(x2)
sig = sqrt(sig1^2 + sig2^2)

discrim = abs(mean1 - mean2)/sig % d'


plot(Pf_save, Pd_save, 'k')
xlabel('P_{FP}')
ylabel('P_{TP}')
axis equal
axis([0 1 0 1])

legend('dataset1','dataset2','dataset3','dataset4')

hold off