%
% An example to show PCA as an unsupervised learning algorithm 
% that learns a representation of data. 
%
% PCA learns a linear projection that aligns the direction of
% greatest variance with the axes of the new space.
%  

% Get on screen/load the data 
%figure('position',get(0,'screensize'));
%axes('position',[0 0 1 1]); 
%[x1 x2] = ginput; 
%X = [x1 x2]; 
load data2D; 
[m,n] = size(X); 

% make E(x) = 0
%
X1 = X - ones(m,1)*mean(X); 

% SVD
[U,S,W] = svd(X1); 

%PCA 
Z = X1*W; 

% covariance of the new variable z 
var_z = Z'*Z 

%---------------------

% First plot: the original data consists of samples of x. 
% In this space, the variance might occur along directions 
% that are not axis-aligned. 
%  

figure(1);
plot(X1(:,1),X1(:,2),'ro','MarkerSize',10,'MarkerFaceColor','r'); 
axis([-0.5 0.5 -0.5 0.5]) 
axis('square') 
hold on 
plot([-0.5 0.5],[0 0],'b-','LineWidth',1)
plot([0 0],[-0.5 0.5],'b-','LineWidth',1)
plot([-0.5 0.5],[-0.5 0.5],'g-','LineWidth',2)
title('Original data')
xlabel('x1') 
ylabel('x2') 
hold off 

% Second plot: the transformed data z = x'*W now varies most along 
% the axis z1. The direction of second most variance is
% now along z2. 

figure(2); 
plot(Z(:,1),Z(:,2),'bo','MarkerSize',10,'MarkerFaceColor','b')
axis([-0.5 0.5 -0.5 0.5]) 
axis('square') 
hold on 
plot([-0.5 0.5],[0 0],'b-','LineWidth',1)
plot([0 0],[-0.5 0.5],'b-','LineWidth',1)
plot([-0.5 0.5],[0 0],'g-','LineWidth',2)
title('PCA-transformed data')
xlabel('z1') 
ylabel('z2') 
hold off 
