% *********************************************************
% *                                                       *
% *   LEARNING FROM DATA 1, TUTORIAL ANSWERS FOR WEEK 3   *
% *                                                       *
% *********************************************************


% QUESTION 1
% a matrix is created where the columns are the data points
% x_u=x(:,u)
x(:,1)=[ 1.3, 1.6, 2.8]'
x(:,2)=[ 4.3,-1.4, 5.8]'
x(:,3)=[-0.6, 3.7, 0.7]'
x(:,4)=[-0.4, 3.2, 5.8]'
x(:,5)=[ 3.3,-0.4, 4.3]'
x(:,6)=[-0.4, 3.1, 0.9]'

% 1.1 : calculate the mean m
m = mean(x,2)

% 1.2 : calculate the covariance matrix S
S = cov(x')

% 1.3 : eigenvalues, D, and eigenvectors, V
[V,D] = eig(S)
% Sort in descending order.
[dummy,ind] = sort(-diag(D));
V = V(:,ind);
D = D(ind,ind);

e1 = V(:,1) % corresponds to the largest eigenvalue
e2 = V(:,2)
e3 = V(:,3) % corresponds to the smallest eigenvalue

% 1.4 : two dimensional representation -> y
% repmat(m,1,6) creates a matrix M with m in 6 columns
M = repmat(m,1,6)
y = [e1, e2]'*(x-M)

% 1.5 : reconstruction -> x_r
% repmat(e,1,3) creates a matrix E with e in 6 columns
E1 = repmat(e1,1,6)
E2 = repmat(e2,1,6)
x_r = M + repmat(e1'*(x-M),3,1).*E1 + repmat(e2'*(x-M),3,1).*E2

% calculate the errors
format short e %just to avoid the idea that some err=0 ...
err = sum((x-x_r).^2,1)


% make some plots for fun ...
l1 = D(3,3); l2=D(2,2); l3=D(1,1);

plot3(x(1,:),x(2,:),x(3,:),'ro','markersize',15); hold on % original data points
plot3(x_r(1,:),x_r(2,:),x_r(3,:),'bx','markersize',15);  % original data points

line([m(1) m(1)+l1*e1(1)],[m(2) m(2)+l1*e1(2)],[m(3) m(3)+l1*e1(3)],'color','r')
line([m(1) m(1)+l2*e2(1)],[m(2) m(2)+l2*e2(2)],[m(3) m(3)+l2*e2(3)],'color','g')
line([m(1) m(1)+l3*e3(1)],[m(2) m(2)+l3*e3(2)],[m(3) m(3)+l3*e3(3)],'color','b')
axis('equal')

hold off

% QUESTION 2
% see the w2ans.pdf (maths)

