-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.m
More file actions
137 lines (115 loc) · 4.08 KB
/
test.m
File metadata and controls
137 lines (115 loc) · 4.08 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
addpath('functions/')
addpath('data/')
addpath('helperfuncs/')
addpath('tests/')
disp(sprintf('Loading FTSE100 stock data\n'));
% The anonymous function ds.ftse100() returns a matrix of FTSE100
% returns R, the Market Index of these returns I = mean(R,2), the number of
% assets n, the time window T and the groupings contained in the variable
% groups
ds = datasets;
[ I, R, n, T, groups ] = ds.ftse_yahoo();
% randgroups = ceil(linspace(0.1,9,99));
% groups = randgroups(randperm(length(randgroups)))';
% Lambdas (Inputs)
l_l = 0.01;
l_gl = 0.7;
l_sgl1 = 0.5;
l_sgl2 = 0.05;
omega1 = 500;
omega2 = 5000;
% Get all anonymousregression functions
rs=reg_funcs;
% Selecting only a subset 100 days to run this test:
T = 100;
R = R(:,1:T);
I = I(1:T);
wsize = 60;
% Lasso
tes_l = [];
pimats_l = [];
zeros_l = [];
gsums_l = [];
% Group Lasso
tes_gl = [];
pimats_gl = [];
zeros_gl = [];
gsums_gl = [];
% Sparse Group Lasso Omega=300
tes_sgl300 = [];
pimats_sgl300 = [];
zeros_sgl300 = [];
gsums_sgl300 = [];
% Sparse Group Lasso Omega=3000
tes_sgl3000 = [];
pimats_sgl3000 = [];
zeros_sgl3000 = [];
gsums_sgl3000 = [];
for i=1:T-wsize
fprintf('Running #%d of %d...\n',i,T-wsize);
% Setting up test-train data for window
R_train = R(:,i:i+wsize-1);
R_test = R(:,i+wsize);
I_train = I(i:i+wsize-1);
I_test = I(i+wsize);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Lasso
fprintf('\t> Starting Lasso.\n');
pimat_l = rs.sglasso(I_train,R_train,groups,0,l_l);
gsum = sum(pimat_l);
pimat = pimat_l/gsum;
error = norm(I_test - R_test'*pimat,1);
nzeros = sum(pimat<0.0001);
tes_l = [ tes_l, error ];
pimats_l = [ pimats_l, pimat ];
zeros_l = [ zeros_l, nzeros ];
gsums_l = [ gsums_l, gsum ];
fprintf('\t\t* Done Lasso. Err: %f, Zeros: %f.\n',error,nzeros);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % Group Lasso
% fprintf('\t> Starting Group Lasso.\n');
% pimat_gl = rs.sglasso(I_train,R_train,groups,l_gl,0);
%
% gsum = sum(pimat_gl);
% pimat = pimat_gl/gsum;
% error = norm(I_test - R_test'*pimat,1);
% nzeros = sum(pimat<0.0005);
%
% tes_gl = [ tes_gl, error ];
% pimats_gl = [ pimats_gl, pimat ];
% zeros_gl = [ zeros_gl, nzeros ];
% gsums_gl = [ gsums_gl, gsum ];
% fprintf('\t\t* Done Group Lasso. Err: %f, Zeros: %f.\n',error,nzeros);
%
%
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % Spares Group Lasso, Omega=300
% fprintf('\t> Starting Sparse Group Lasso. Omega=500\n');
% pimat_sgl300 = rs.sglasso(I_train,R_train,groups,l_sgl1/omega1,l_sgl1);
%
% gsum = sum(pimat_sgl300);
% pimat = pimat_sgl300/gsum;
% error = norm(I_test - R_test'*pimat,1);
% nzeros = sum(pimat<0.0005);
%
% tes_sgl300 = [ tes_sgl300, error ];
% pimats_sgl300 = [ pimats_sgl300, pimat ];
% zeros_sgl300 = [ zeros_sgl300, nzeros ];
% gsums_sgl300 = [ gsums_sgl300, gsum ];
% fprintf('\t\t* Done Sparse Group Lasso. Err: %f, Zeros: %f.\n',error,nzeros);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Sparse Group Lasso, Omega=3000
fprintf('\t> Starting Sparse Group Lasso. Omega=3000\n');
pimat_sgl3000 = rs.sglasso(I_train,R_train,groups,l_sgl2/omega2,l_sgl2);
gsum = sum(pimat_sgl3000);
pimat = pimat_sgl3000/gsum;
error = norm(I_test - R_test'*pimat,1);
nzeros = sum(pimat<0.0005);
tes_sgl3000 = [ tes_sgl3000, error ];
pimats_sgl3000 = [ pimats_sgl3000, pimat ];
zeros_sgl3000 = [ zeros_sgl3000, nzeros ];
gsums_sgl3000 = [ gsums_sgl3000, gsum ];
fprintf('\t\t* Done Sparse Group Lasso. Err: %f, Zeros: %f.\n',error,nzeros);
end
plot([tes_l', tes_gl', tes_sgl300', tes_sgl3000'])
sum([zeros_l'<=zeros_sgl3000' tes_sgl3000' <= tes_l' ])