-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluationLSExpApprox.m
48 lines (43 loc) · 1.69 KB
/
evaluationLSExpApprox.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
% Evaluation of OIS Forward Rate Curves with LSExpSpline
reloadData = true;
dataLength = 20; % longest maturity in years
var = 5; % set of spline curves
pvar = 1000000; % penalty induced
dataFile = ['Splines' num2str(dataLength) '_' num2str(var) '_' num2str(pvar) '.mat'];
forwardData = ['forwardCurve' dataFile];
pcaData = ['EVD_' dataFile];
if ~exist('forwardCurvesSplines','var') || reloadData
load(forwardData);
end
% settings
dt = dataLength/size(forwardCurvesSplines,2);
lastPC = 4; %must be <= 9
% from this point and forward, the curve contains data for all maturities
% up to 30 years.
% The code block in "if reloadData" takes a long time to run. the outputs
% have been saved in EVD...mat
if ~exist('totEigVal','var') && ~reloadData
load(pcaData);
end
if reloadData
% contains all the forward rate curves
forwardDiff = forwardCurvesSplines(2:end,:) - forwardCurvesSplines(1:end-1,:);
forwardCov = cov(forwardDiff);
[forwardEigVecSplines, forwardEigValSplines] = eig(forwardCov);
forwardEigVecSplines = fliplr(forwardEigVecSplines);
forwardEigValSplines = rot90(forwardEigValSplines,2);
totEigValSplines = sum(sum(forwardEigValSplines));
loadingsPCSplines = forwardEigVecSplines(:,1:lastPC);
weightsPCSplines = diag(forwardEigValSplines(1:lastPC,1:lastPC))/totEigValSplines;
totPCWeightSplines = sum(weightsPCSplines);
save(pcaData,'loadingsPCSplines','weightsPCSplines','totPCWeightSplines','totEigValSplines');
end
plotPC = 4; % must be <= lastPC
legendPC = repmat(char(0),plotPC,3);
for i = 1:plotPC
legendPC(i,:) = strcat('PC ',num2str(i));
end
figure(1);
hold on;
plot(dt:dt:size(loadingsPCSplines,1)*dt,loadingsPCSplines(:,1:plotPC));
legend(legendPC);