clear;
clc;
load Data_PS3

total=[Class0Training; Class1Training];

mean_0   = mean(Class0Training);
mean_1   = mean(Class1Training);
mean_all = mean(total);

%Mean Centering the Data
x_0   = zeros(size(Class0Training));
x_1   = zeros(size(Class1Training));
x_all = zeros(size(total));
for i=1:size(Class0Training,1) 
    x_0(i,:)=Class0Training(i,:)-mean_0; 
end

for i=1:size(Class1Training,1) 
    x_1(i,:)=Class1Training(i,:)-mean_1; 
end

for i=1:size(total,1) 
    x_all(i,:)=total(i,:)-mean_all; 
end

%Compute the Outer Product
S_0   = x_0'*x_0;
S_1   = x_1'*x_1;
S_all = x_all'*x_all;

%PCA
[EV,EW]=eig(S_all);
%Eigen Vector Corresponding to the highest Eigen Value
w_pca = EV(:,3);

%Fisher
Sw=S_1+S_0;
w_fisher = inv(Sw)*((mean_0-mean_1)');
w_fisher=w_fisher/norm(w_fisher);

%Histograms and ML mu, sigma of the training data
pca_training_0 = w_pca'*Class0Training';
pca_training_1 = w_pca'*Class1Training';
mean_pca_0 = mean(pca_training_0);
var_pca_0  = cov(pca_training_0);
mean_pca_1 = mean(pca_training_1);
var_pca_1  = cov(pca_training_1);
label0=sprintf('PCA projection of Class 0 \n Mu = %f \n Var = %f\n', mean_pca_0, var_pca_0);
label1=sprintf('PCA projection of Class 1 \n Mu = %f \n Var = %f\n', mean_pca_1, var_pca_1);

figure; 
subplot(1,2,1); hist(pca_training_0); axis([-25 25 0 400]); text(-20,350,label0);
subplot(1,2,2); hist(pca_training_1); axis([-25 25 0 400]); text(-20,350,label1);

fisher_training_0 = w_fisher'*Class0Training';
fisher_training_1 = w_fisher'*Class1Training';
mean_fisher_0 = mean(fisher_training_0);
var_fisher_0  = cov(fisher_training_0);
mean_fisher_1 = mean(fisher_training_1);
var_fisher_1  = cov(fisher_training_1);
label0=sprintf('Fisher projection of Class 0 \n Mu = %f \n Var = %f\n', mean_fisher_0, var_fisher_0);
label1=sprintf('Fisher projection of Class 1 \n Mu = %f \n Var = %f\n', mean_fisher_1, var_fisher_1);

figure; 
subplot(1,2,1); hist(fisher_training_0); axis([-10 4 0 200]); text(-9,175,label0);
subplot(1,2,2); hist(fisher_training_1); axis([-10 4 0 200]); text(-9,175,label1);

%Classification using PCA
Test_0 = w_pca'*Class0Test';
Test_1 = w_pca'*Class1Test';

pca_correct_0=0;
for i = 1:size(Test_0,2);
    if normpdf(Test_0(i), mean_pca_0, sqrt(var_pca_0)) >= normpdf(Test_0(i), mean_pca_1, sqrt(var_pca_1))
        pca_correct_0=pca_correct_0+1;
    end
end

pca_correct_1=0;
for i = 1:size(Test_1,2);
    if normpdf(Test_1(i), mean_pca_1, sqrt(var_pca_1)) >= normpdf(Test_1(i), mean_pca_0, sqrt(var_pca_0))
        pca_correct_1=pca_correct_1+1;
    end
end

fprintf(1,'              Confusion Matrix for PCA\n');
fprintf(1,'              Recognized as Class 0          Recognized as Class 1\n');
fprintf(1,'Class 0           %d                                  %d\n', pca_correct_0, size(Test_0,2)-pca_correct_0);
fprintf(1,'Class 1           %d                                  %d\n', size(Test_1,2)-pca_correct_1, pca_correct_1);

%Classification using Fisher
Test_0 = w_fisher'*Class0Test';
Test_1 = w_fisher'*Class1Test';

fisher_correct_0=0;
for i = 1:size(Test_0,2);
    if normpdf(Test_0(i), mean_fisher_0, sqrt(var_fisher_0)) >= normpdf(Test_0(i), mean_fisher_1, sqrt(var_fisher_1))
        fisher_correct_0=fisher_correct_0+1;
    end
end

fisher_correct_1=0;
for i = 1:size(Test_1,2);
    if normpdf(Test_1(i), mean_fisher_1, sqrt(var_fisher_1)) >= normpdf(Test_1(i), mean_fisher_0, sqrt(var_fisher_0))
        fisher_correct_1=fisher_correct_1+1;
    end
end

fprintf(1,'              \n\n\n\nConfusion Matrix for Fisher\n');
fprintf(1,'              Recognized as Class 0          Recognized as Class 1\n');
fprintf(1,'Class 0           %d                                  %d\n', fisher_correct_0, size(Test_0,2)-fisher_correct_0);
fprintf(1,'Class 1           %d                                  %d\n', size(Test_1,2)-fisher_correct_1, fisher_correct_1);
