%Read data
clc
[YC,txt]=xlsread('YC.xls');
Changes=YC(2:end,1:177)-YC(1:end-1,1:177);
Changes_centr=Changes-repmat(mean(Changes),size(Changes,1),1);
Dates=datenum(txt(3:end,1),'dd.mm.yy');
axis=3/12:1/12:179/12; %convert months to years

% Scatter plot: 1Y vs 3Y
scatter(Changes(:,10),Changes(:,34),'.')

%PCA based on correlation matrix
[loadings1,components1,eigenvalues1] =princomp(zscore(Changes_centr));
%PCA based on variance-covariance matrix
[loadings2,components2,eigenvalues2] =princomp(Changes_centr);
%Part of volatility explained by first three principal components
cumsum(eigenvalues1(1:3))./sum(eigenvalues1)
eigenvalues1(1:3)./sum(eigenvalues1)

%Loadings 
figure(1)
plot(axis,loadings1(:,1:3),'LineWidth',3)
legend('1st PC','2nd PC','3rd PC')
hold on
plot(axis,loadings2(:,1:3))

%First three components (integrated)
plot(Dates,-cumsum(components1(:,1:3)),'LineWidth',3)
datetick('x','mm/yy','keepticks')  
legend('1st PC','2nd PC','3rd PC')

%Scenario based on the 1st PC with probability 99 % 
scenario=loadings1(:,1)*(mean(components1(:,1))-std(components1(:,1))*norminv(0.99,0,1))+mean(Changes)';
plot(axis,scenario)







