-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPort_Opt_example.m
287 lines (264 loc) · 11.3 KB
/
Port_Opt_example.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
% Solve the portfolio optimization problem:
%
% min f(x) = -sum(log < w_i ,x > )
% s.t. x in Simplex
% where x in R^p, w_i in R^p, i = 1,...,n.
%
% Define W = [w_1T;...;w_nT] which is a n*p matrix, we can rewrite the
% problem as:
%
% min f(x) = -eTlog(Wx)
% s.t. x in Simplex.
%
% gradient_f(x) = -WT(1./(Wx)).
% hessian_f(x) = WTdiag(1/(Wx).^2)W.
%
% Will compare our algorithm FWPN with Proximal Newton (PN),
% Frank Wolfe (FW), Frank Wolfe with Line Search (FW-LS) and
% Proximal Gradient with Barzilai-Brorwein's step-size (PG-BB)
%
% References
% [1] Frank,M.andWolfe,P. Analgorithmforquadraticprogramming.
% Naval Research Logistics Quarterly, 3:95–110, 1956.
% [2] Jaggi, M. Revisiting Frank-Wolfe: Projection-Free Sparse
% Convex Optimization. JMLR W&CP, 28(1):427–435, 2013.
%% Set Path
clear;
addpath(genpath(pwd));
%% Choosing the data
use_real_data = 0;
if use_real_data == 1
id = 3;
plist = {'473500_wk.mat','625723_wk.mat','625889_wk.mat'};
pname = plist{id};
load(pname);
[n,p] = size(W);
fprintf(' We are solving real problem of size n is %5d and p is %5d.\n', n,p);
fprintf('\n');
else
n = 1e+5;
p = 1e+3;
W = PortGenData(n, p, 0.1);
fprintf(' We are solving synthetic problem of size n is %5d and p is %5d.\n', n,p);
fprintf('\n');
end
%% Preprocessing the data
if (min(min(W)) >= 0)
fprintf('Data is valid.\n');
fprintf('\n');
else
fprintf('Data is adjusted by taking exp.\n');
fprintf('\n');
W = exp(W);
end
x0 = ones(p,1)/p;
%% Solving the problem by using inexact variable metric (IVM) method
fprintf('************************************************************************\n')
fprintf('********** Solving portfolio problem by using IVM method *********\n')
fprintf('************************************************************************\n')
Options.M = 10;
Options.tau = 0.1;
Options.theta = 0.01;
Options.maxiters = 100;
Options.sub_max_iter = size(W,2)/10;
Options.lambda_tol = 1e-3;
get_obj = @(x) -sum(log(W*x));
get_grad = @(x) - ( (1./(W*x))' * W)';
SubSolver = @(x, y, theta, max_iter) PortIVMSubSolver(x, y, W, theta, max_iter);
hist_IVM = IVMSolver(x0, Options, SubSolver, get_obj, get_grad);
%% Solving the problem by using standard Proximal Newton method
fprintf('************************************************************************\n')
fprintf('*************** Solving portfolio problem by using PN ******************\n')
fprintf('************************************************************************\n')
options.Miter = 10;
options.printst = 1;
tols.main = 1e-8;
options.Lest = 1;
hist_PN = PortPNSolver(W, x0, options, tols);
%% Solving the problem by using proximal-gradient method with B-B stepsize
options.cpr = 0;
options.Miter = 1000;
options.printst = 100;
tols.main = 1e-6;
fprintf('************************************************************************\n')
fprintf('************** Solving portfolio problem by using PG-BB ****************\n')
fprintf('************************************************************************\n')
hist_PGBB = PortPGBBSolver(W, x0, options, tols);
%% Solving the problem by using nonmonotone spectral proximal gradient method
options.gamma = 1e-6;
options.alpha = 1e-1;
options.Alpha = [1e-7, 1];
options.M = 5;
options.sigma = 0.5;
options.maxiters = 1000;
options.printdist = 10;
options.tol = 1e-4;
fprintf('************************************************************************\n')
fprintf('************** Solving portfolio problem by using nSPG ****************\n')
fprintf('************************************************************************\n')
hist_nSPG = PortnSPG(W, x0, options);
%% Solving the problem by using Frank-Wolfe method
options.cpr = 0;
options.linesearch = 0;
options.Miter = 2000;
options.printst = 1000;
tols.main = 1e-4;
fprintf('************************************************************************\n')
fprintf('*************** Solving portfolio problem by using FW ******************\n')
fprintf('************************************************************************\n')
hist_FW = PortFWSolver(W, x0, options, tols);
%% Solving the problem by using Frank-Wolfe method with line search
options.cpr = 0;
options.linesearch = 1;
options.Miter = 2000;
options.printst = 1000;
tols.main = 1e-4;
fprintf('************************************************************************\n')
fprintf('************** Solving portfolio problem by using FW-LS ****************\n')
fprintf('************************************************************************\n')
hist_FWLS = PortFWSolver(W, x0, options, tols);
%% Solving the problem by using our method
Options.lambda0 = 1;
Options.lambda_tol = 1e-6;
Options.sub_tol = 0.1;
Options.short2long = 10;
Options.max_iter = 100;
Options.sub_max_iter = size(W,2)/5;
get_obj = @(x) -sum(log(W*x));
SubSolver = @(x, y, tol, max_iter) PortFWPNSubSolver(x, y, W, tol, max_iter);
fprintf('************************************************************************\n')
fprintf('********** Solving portfolio problem by using our method(FWPN) *********\n')
fprintf('************************************************************************\n')
hist_FWPN = ProxNSolver(x0, Options, SubSolver, get_obj);
%% Plot the result
% figure;
%
% f_star = min([hist_FWPN.obj, hist_FW.f(end), hist_PN.f(end), hist_FWLS.f(end), hist_PGBB.f(end), hist_IVM.f(end)]);
% legend_fig1 = {};
% MarkSize = 7;
%
% semilogy([0, hist_FWPN.cumul_time], abs([hist_FWPN.f, hist_FWPN.obj] - f_star),...
% 'o--', 'Color', [0.8500 0.3250 0.0980],...
% 'MarkerEdgeColor',[0.8500 0.3250 0.0980], 'MarkerFaceColor',[0.8500 0.3250 0.0980], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,1} = 'FWPN';
%
% semilogy(hist_PN.cumul_time(1:end-1), abs(hist_PN.f - f_star),...
% '^-', 'Color', [0.9290 0.6940 0.1250],...
% 'MarkerEdgeColor',[0.9290 0.6940 0.1250], 'MarkerFaceColor', [0.9290 0.6940 0.1250], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,2} = 'PN';
%
% totallength = length(hist_FW.f);
% dist = floor(totallength/10);
% index = 1:dist:totallength;
% semilogy(hist_FW.cumul_time(index), abs(hist_FW.f(index) - f_star),...
% 'd-','color', [0.4660 0.6740 0.1880],...
% 'MarkerEdgeColor',[0.4660 0.6740 0.1880], 'MarkerFaceColor', [0.4660 0.6740 0.1880], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,3} = 'FW';
%
% totallength = length(hist_FWLS.f);
% dist = floor(totallength/10);
% index = 1:dist:totallength;
% semilogy(hist_FWLS.cumul_time(index), abs(hist_FWLS.f(index) - f_star),...
% 'v-','color', [0 0.4470 0.7410],...
% 'MarkerEdgeColor',[0 0.4470 0.7410], 'MarkerFaceColor', [0 0.4470 0.7410], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,4} = 'FWLS';
%
% totallength = length(hist_PGBB.f);
% dist = floor(totallength/10);
% index = 1:dist:totallength;
% semilogy(hist_PGBB.cumul_time(index), abs(hist_PGBB.f(index) - f_star),...
% 's-', 'Color',[0.3010 0.7450 0.9330],...
% 'MarkerEdgeColor', [0.3010 0.7450 0.9330], 'MarkerFaceColor',[0.3010 0.7450 0.9330], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,5} = 'PGBB';
%
% totallength = length(hist_nSPG.f);
% dist = floor(totallength/10);
% index = 1:dist:totallength;
% semilogy(hist_nSPG.cumul_time(index), abs(hist_nSPG.f(index) - f_star),...
% 's-', 'Color',[0 0 1],...
% 'MarkerEdgeColor', [0 0 1], 'MarkerFaceColor',[0 0 1], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,6} = 'nSPG';
%
% semilogy(hist_IVM.cumul_time, abs(hist_IVM.f - f_star),...
% 'o--', 'Color', [1 0 0],...
% 'MarkerEdgeColor',[1 0 0], 'MarkerFaceColor',[1 0 0], 'MarkerSize', MarkSize); hold on
% legend_fig1{1,7} = 'IVM';
%
%
% xlabel('Time($s$)', 'Interpreter', 'latex', 'FontSize', 20);
%
% ylabel('$f(X) - f^\star$','Interpreter', 'latex', 'FontSize', 20);
%
% if use_real_data == 1
% title('real: $n = $' + string(n) + ', $p = $' + string(p),'Interpreter', 'latex', 'FontSize', 20)
% else
% title('syn: $n = $' + string(n) + ', $p = $' + string(p),'Interpreter', 'latex', 'FontSize', 20)
% end
% h1 = legend(legend_fig1);
% xlim([-0.3,inf]);
% set(h1, 'Interpreter', 'latex', 'FontSize', 12);
%% Plot the result
figure;
f_star = min([hist_FWPN.obj, hist_FW.f(end), hist_PN.f(end), hist_FWLS.f(end), hist_PGBB.f(end), hist_IVM.f(end)]);
f0 = -sum(log(W*x0));
EPS = 0.5;
legend_fig1 = {};
MarkSize = 7;
loglog([EPS, hist_FWPN.cumul_time], abs([hist_FWPN.f, hist_FWPN.obj] - f_star),...
'o--', 'Color', [0.8500 0.3250 0.0980],...
'MarkerEdgeColor',[0.8500 0.3250 0.0980], 'MarkerFaceColor',[0.8500 0.3250 0.0980], 'MarkerSize', MarkSize); hold on
legend_fig1{1,1} = 'FWPN';
totallength = length(hist_PN.f);
dist = floor(totallength/10);
index = 1:dist:totallength;
index = floor(totallength.^((1:10)/10));
loglog([EPS, hist_PN.cumul_time(index)], abs([f0, hist_PN.f(index)] - f_star),...
'^-', 'Color', [0.9290 0.6940 0.1250],...
'MarkerEdgeColor',[0.9290 0.6940 0.1250], 'MarkerFaceColor', [0.9290 0.6940 0.1250], 'MarkerSize', MarkSize); hold on
legend_fig1{1,2} = 'PN';
totallength = length(hist_FW.f);
dist = floor(totallength/10);
index = 1:dist:totallength;
index = floor(totallength.^((1:10)/10));
semilogy([EPS, hist_FW.cumul_time(index)], abs([f0, hist_FW.f(index)] - f_star),...
'd-','color', [0.4660 0.6740 0.1880],...
'MarkerEdgeColor',[0.4660 0.6740 0.1880], 'MarkerFaceColor', [0.4660 0.6740 0.1880], 'MarkerSize', MarkSize); hold on
legend_fig1{1,3} = 'FW';
totallength = length(hist_FWLS.f);
dist = floor(totallength/10);
index = 1:dist:totallength;
index = floor(totallength.^((1:10)/10));
loglog([EPS, hist_FWLS.cumul_time(index)], abs([f0, hist_FWLS.f(index)] - f_star),...
'v-','color', [0 0.4470 0.7410],...
'MarkerEdgeColor',[0 0.4470 0.7410], 'MarkerFaceColor', [0 0.4470 0.7410], 'MarkerSize', MarkSize); hold on
legend_fig1{1,4} = 'FWLS';
totallength = length(hist_PGBB.f);
dist = floor(totallength/10);
index = 1:dist:totallength;
index = floor(totallength.^((1:10)/10));
loglog([EPS, hist_PGBB.cumul_time(index)], abs([f0, hist_PGBB.f(index)] - f_star),...
's-', 'Color',[0.3010 0.7450 0.9330],...
'MarkerEdgeColor', [0.3010 0.7450 0.9330], 'MarkerFaceColor',[0.3010 0.7450 0.9330], 'MarkerSize', MarkSize); hold on
legend_fig1{1,5} = 'PGBB';
totallength = length(hist_nSPG.f);
dist = floor(totallength/10);
index = 1:dist:totallength;
index = floor(totallength.^((1:10)/10));
loglog([EPS, hist_nSPG.cumul_time(index)], abs([f0, hist_nSPG.f(index)] - f_star),...
'p-', 'Color',[0 0 1],...
'MarkerEdgeColor', [0 0 1], 'MarkerFaceColor',[0 0 1], 'MarkerSize', MarkSize); hold on
legend_fig1{1,6} = 'nSPG';
loglog([EPS, hist_IVM.cumul_time], abs([f0, hist_IVM.f] - f_star),...
'*--', 'Color', [1 0 0],...
'MarkerEdgeColor',[1 0 0], 'MarkerFaceColor',[1 0 0], 'MarkerSize', MarkSize); hold on
legend_fig1{1,7} = 'IVM';
xlabel('Time($s$)', 'Interpreter', 'latex', 'FontSize', 20);
ylabel('$f(X) - f^\star$','Interpreter', 'latex', 'FontSize', 20);
if use_real_data == 1
title('real: $n = $' + string(n) + ', $p = $' + string(p),'Interpreter', 'latex', 'FontSize', 20)
else
title('syn: $n = $' + string(n) + ', $p = $' + string(p),'Interpreter', 'latex', 'FontSize', 20)
end
h1 = legend(legend_fig1, 'Location', 'southwest');
xlim([0,inf]);
set(h1, 'Interpreter', 'latex', 'FontSize', 12);