close all
clear

%  Example of using PCA


%first create our 2D data
x=[2.5,0.5,2.2,1.9,3.1,2.3,2,1,1.5,1.1];

y=[2.4,0.7,2.9,2.2,3.0,2.7,1.6,1.1,1.6,0.9];

plot(x,y,'o','LineWidth',2,'MarkerEdgeColor','k');

%  compute mean for x

[ax,bx] =size(x);

mean_x= sum(x)/bx;

%  compute mean for y

[ay,by] =size(y);

mean_y = sum(y)/by;


%  compute  adjusted data

adjust_x = x - mean_x;

adjust_y = y - mean_y;


% compute covariance matrix which will be of the size 2 by 2 or
%  cov(x,x) cov(x,y)
%  cov(y,x)  cov(y,y)

  %compute covariance matrix
  
  C = cov(adjust_x,adjust_y);


% compute eigenvalues and eigenvectors ov the covariance matrix

[eigvec, eigval] = eig(C);
  
[U,S,V] = svd(C);

% plot adjusted data compared with eigenvectors
figure(1)

     plot(adjust_x,adjust_y,'o','LineWidth',2,'MarkerEdgeColor','k');
     grid('on');

hold  on

%**********  plot using computation of eigenvalues of covariance matrix*******
% plot on the same figure eigenvectors

% first eigenvector


%vec1x=[eigvec(1,1),-eigvec(1,1)];
%vec1y=[eigvec(2,1),-eigvec(2,1)];

%plot(vec1x,vec1y,'b', 'LineWidth',2);

%second eigenvector

%vec2x=[2*eigvec(2,1),-2*eigvec(2,1)];
%vec2y=[2*eigvec(2,2),-2*eigvec(2,2)];

%plot(vec2x,vec2y,'r', 'LineWidth',2);
%******************************************************

% plotting using svd of the covariance matrix ********************************
% first eigenvector via svd: this is the principle component of the data set
% since it's corresponds to the largest eigenvalue.
% We can call it also as feature vector.

u1x=[2*U(1,1),-2*U(1,1)];
u1y=[2*U(2,1),-2*U(2,1)];

plot(u1x,u1y,'-- b', 'LineWidth',2);



% plotting the second eigenvalue vector 
u2x=[2*U(2,1),-2*U(2,1)];
u2y=[2*U(2,2),-2*U(2,2)];

plot(u2x,u2y,'-- r', 'LineWidth',2);


% Now form final rotated data

Data =[adjust_x; adjust_y];

PCA = U*Data;

plot(PCA(1,:),PCA(2,:),'o','LineWidth',2, 'MarkerEdgeColor','r');

axis([-2,2,-2,2])
legend('adjusted data','first eigenvector of covariance matrix','second eigenvector of covariance matrix','rotated adjusted data after PCA analysis');
title('PCA analysis of transformed data using 2 eigenvectors')

figure
% now we plot out vector with the largest eigenvalue
PCAnew1 = U(1,:)*Data;

plot(PCAnew1,PCAnew1,'o','LineWidth',2, 'MarkerEdgeColor','r');

hold on
PCAnew2 = U(2,:)*Data;

plot(PCAnew2,PCAnew2,'o','LineWidth',2, 'MarkerEdgeColor','b');
  grid('on');
legend('data points for the first eigenvector in U','data points for the second eigenvector in U')
title('PCA analysis of transformed data using 1 eigenvector ')



figure

    plot(adjust_x,adjust_y,'o','LineWidth',2,'MarkerEdgeColor','k');
     grid('on');
hold on

plot(PCA(1,:),PCA(2,:),'o','LineWidth',2, 'MarkerEdgeColor','g');

plot(PCAnew1,PCAnew1,'o','LineWidth',2, 'MarkerEdgeColor','r');

u1x=[5*U(1,1),-5*U(1,1)];
u1y=[5*U(2,1),-5*U(2,1)];

plot(u1x,u1y,'-- b', 'LineWidth',2);
axis([-3,3,-3,3])

legend('adjusted data','rotated adjusted data after PCA analysis','PCA analysis using the first eigenvector of the covariance matrix','first eigenvector of the covariance matrix');
