clear all
close all

% Creating data
load flu.mat
data=double(flu(:,2:end));

MU1=[1 1 1];
MU2=[0 0 0];
SIGMA=[1 0 0; 0 1 0; 0 0 0];
cases=20;
M1=mvnrnd(MU1,SIGMA,cases);
M2=mvnrnd(MU2,SIGMA,cases);
M=[M1;M2];
%data=M;

% n=3;
% m=3;
% A=randn(n,m) %input
X=cov(data) %always square, always the variance, data doesn't get lost
[V,D]=eig(X); %shows us where it's the most of variance, eig only calculate square matrices

evalue=[];
pca_matrix=[];
X_mod=[];

% You can use Kaiser Method, Scree Test or "Percentage-Test". We chose
% "Percentage-Test". If eigenvalue is 0, we can remove it (its eigenvector)
% without loosing any information/data.

V=fliplr(V);
D=flipud(fliplr(D));

% For data=A
% for i=1:m
%     evalue=[evalue D(i,i)];
%     if evalue(i)>=.1*max(evalue) && ceil(abs(evalue(i)))~=0
%         X_mod=[X_mod X(:,i)];
%         pca_matrix=[pca_matrix V(:,i)];
%     end
% end

% For data=flu
for i=1:size(data,2)
    evalue=[evalue D(i,i)];
    if evalue(i)>=.0*max(evalue) && ceil(abs(evalue(i)))~=0
        X_mod=[X_mod X(:,i)];
        pca_matrix=[pca_matrix V(:,i)];
    end
end

% For data=M
% for i=1:size(data,2)
%     evalue=[evalue D(i,i)];
%     if evalue(i)>=.1*max(evalue) && ceil(abs(evalue(i)))~=0
%         X_mod=[X_mod X(:,i)];
%         pca_matrix=[pca_matrix V(:,i)];
%     end
% end

evalue
evalue_mod=[];

for i=1:length(evalue)
    evalue_mod=[evalue_mod sum(evalue(1:i))];
end

plot(evalue_mod*100/sum(evalue))
title('Lost Data Depending on Number of Eigenvectors')
ylabel('%')
xlabel('Number of Eigenvectors')
set(gca,'xtick',1:length(evalue));

pca_matrix
% real_pca=pca(A)
real_pca=pca(data)

new_data=data*pca_matrix;

figure
biplot(real_pca)
title('PCA - MATLAB')

figure
biplot(pca_matrix')
title('PCA - New Axis')

figure
biplot(data)
title('Original Data')

figure
biplot(new_data)
title('New Data')







   