-
Notifications
You must be signed in to change notification settings - Fork 4
/
q_3b.m
111 lines (86 loc) · 2.88 KB
/
q_3b.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
clc;
% load the data
load('data\q_2.mat');
load('data\ftse.csv');
ftse = flipud(ftse);
% returns need to be changed to percentages
% for each return, covert it from
%[100 120 150 100] to [0% 20% 50% 0%]
for i=1:size(returns,2)
value = returns(1,i);
returns(:,i) = (returns(:,i) - value)/value;
end
% now remove the first opservation (it is only zeros
% as it was used to convert returns to percentages)
returns = returns(2:end,:);
% do the same for ftse
ftse = (ftse - ftse(1))/ftse(1);
ftse = ftse(2:end,:);
% for testing purpose, if we want the
% index to be the avarage of 20 assets we have
% not the real market index
[~, ftse] = portfolioAverageReturn(returns);
ftse = ftse';
nAssets = size(returns,2);
nTotal = size(returns,1);
nTrain = int16(nTotal/2);
nTest = nTotal - nTrain;
% split the returns to train and test
returnsTrain = returns(1:nTrain,:);
returnsTest = returns(nTrain+1:nTotal,:);
ftseTrain = ftse(1:nTrain);
ftseTest = ftse(nTrain+1:nTotal);
% now, with the Greedy-Forward Feature Selection (GFFS) algorithm
% how many assets we want our portfolio to contain
maxSelectedAssets = 6;
% get the weights and assets by doing lasso regression
[weights, selectedAssets] = sparseIndexTracking(returnsTrain, ftseTrain, maxSelectedAssets);
% returns of our final portfolio
avgReturnTrain = returnsTrain(:, selectedAssets)*weights;
avgReturnTest = returnsTest(:, selectedAssets)*weights;
% plot results
figure(1); clf;
subplot(1,2,1);
hold on;
grid on;
box on;
for i=1:20
if (ismember(i, selectedAssets))
w = 1;
c = [0 0.7 0.2];
plot1 = plot(returnsTrain(:,i), 'LineWidth', w, 'Color', c);
else
w = 0.1;
c = [0.7 0.7 0.7];
plot2 = plot(returnsTrain(:,i), 'LineWidth', w, 'Color', c);
end
end
plot3 = plot(ftseTrain, 'b', 'LineWidth', 4);
plot4 = plot(avgReturnTrain, 'r', 'LineWidth', 2);
xlabel('Time (Days)', 'FontSize', 18);
ylabel('Return (%)', 'FontSize', 18);
title('Index Tracking (Training)', 'FontSize', 18);
fig_legend = legend([plot4, plot3, plot1, plot2], {'Our Portfolio', 'Market Index', 'Selected Assets', 'Unselected Assets'}, 'Location', 'northwest');
set(fig_legend,'FontSize',14);
subplot(1,2,2);
hold on;
grid on;
box on;
for i=1:20
if (ismember(i, selectedAssets))
w = 1;
c = [0 0.7 0.2];
plot1 = plot(returnsTest(:,i), 'LineWidth', w, 'Color', c);
else
w = 0.1;
c = [0.7 0.7 0.7];
plot2 = plot(returnsTest(:,i), 'LineWidth', w, 'Color', c);
end
end
plot3 = plot(ftseTest, 'b', 'LineWidth', 4);
plot4 = plot(avgReturnTest, 'r', 'LineWidth', 2);
xlabel('Time (Days)', 'FontSize', 18);
ylabel('Return (%)', 'FontSize', 18);
title('Index Tracking (Testing)', 'FontSize', 18);
fig_legend = legend([plot4, plot3, plot1, plot2], {'Our Portfolio', 'Market Index', 'Selected Assets', 'Unselected Assets'}, 'Location', 'northwest');
set(fig_legend,'FontSize',14);