-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo_matrixreg.m
516 lines (466 loc) · 11.6 KB
/
demo_matrixreg.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
%% Regularized matrix linear regression
clear;
% reset random seed
s = RandStream('mt19937ar','Seed',2);
RandStream.setGlobalStream(s);
%%
% True coefficients for regular (non-array) covariates
p0 = 5;
b0 = ones(p0,1);
%%
% 2D true signal: 64-by-64 cross
shape = imread('cross.gif');
shape = array_resize(shape,[32,32]); % 32-by-32
b = zeros(2*size(shape));
b((size(b,1)/4):(size(b,1)/4)+size(shape,1)-1, ...
(size(b,2)/4):(size(b,2)/4)+size(shape,2)-1) = shape;
[p1,p2] = size(b);
disp(size(b));
%%
% Simulate covariates
n = 500; % sample size
X = randn(n,p0); % n-by-p0 regular design matrix
M = tensor(randn(p1,p2,n)); % p1-by-p2-by-n matrix variates
disp(size(M));
%%
% Simulate responses
mu = X*b0 + double(ttt(tensor(b), M, 1:2));
sigma = 1; % noise level
y = mu + sigma*randn(n,1);
%%
% Determine max lambda to start
[~,~,stats] = matrix_sparsereg(X,M,y,inf,'normal');
maxlambda = stats.maxlambda*.95;
%%
% Fit nuclear norm regularized linear regression at grid points
gridpts = 10;
lambdas = zeros(1,gridpts);
gs = 2/(1+sqrt(5));
B = cell(1,gridpts);
AIC = zeros(1,gridpts);
BIC = zeros(1,gridpts);
tic;
for i=1:gridpts
if (i==1)
B0 = [];
else
B0 = B{i-1}; % warm start
end
lambda = maxlambda*gs^(i-1);
lambdas(i) = lambda;
[beta0,B{i},stats] = matrix_sparsereg(X,M,y,lambda,'normal','B0',B0);
AIC(i) = stats.AIC;
BIC(i) = stats.BIC;
end
toc;
%%
% disp true signal and snapshots along nuclear norm solution path
figure; hold on;
set(gca,'FontSize',20);
subplot(2,2,1);
imagesc(-b);
colormap(gray);
title('True Signal');
axis equal;
axis tight;
ploti = 1;
for i=[gridpts round(gridpts/2) 1]
ploti = ploti + 1;
subplot(2,2,ploti);
imagesc(-double(B{i}));
colormap(gray);
title({['nuclear norm,', ' \lambda=', ...
num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
axis equal;
axis tight;
end
%%
% disp AIC/BIC trace plot
figure;
set(gca,'FontSize',20);
semilogx(lambdas, AIC, '-+', lambdas, BIC, '-o');
xlabel('\lambda');
ylabel('BIC');
xlim([min(lambdas) max(lambdas)]);
title('Nuclear norm AIC/BIC');
legend('AIC', 'BIC', 'Location', 'northwest');
%% Compare to lasso penalized linear regression
%%
% Transform matrix variates to vector form
TM = tenmat(tensor(M),3,[1 2]);
Xall = [double(TM) X];
%%
% Determine max lambda to start
lambdastart = max(lsq_maxlambda(sum(Xall.^2),-y'*Xall,'enet',1));
maxlambda_lasso = lambdastart*.95;
%%
% Optimization at grid points
gridpts = 10;
B_lasso = cell(1,gridpts);
BIC_lasso = zeros(1,gridpts);
lambdas_lasso = zeros(1,gridpts);
penidx = true(size(Xall,2),1);
penidx(numel(b)+1:end) = false; % do not penalize regular covariates
tic;
for i=1:gridpts
if (i==1)
x0 = zeros(size(Xall,2),1);
else
x0 = beta;
end
lambda = maxlambda_lasso*gs^(i-1);
lambdas_lasso(i) = lambda;
[beta] = lsq_sparsereg(Xall,y,lambda,'x0',x0,'penidx',penidx);
B_lasso{i} = reshape(beta(1:numel(b)),p1,p2);
BIC_lasso(i) = .5*norm(y-Xall*beta)^2+log(n)*nnz(abs(beta)>1e-8);
end
toc;
%%
% disp true signal and snapshots along lasso solution path
figure; hold on;
set(gca,'FontSize',20);
subplot(2,2,1);
imagesc(-b);
colormap(gray);
title('True Signal');
axis equal;
axis tight;
ploti = 1;
for i=[gridpts round(gridpts/2) 1]
ploti = ploti + 1;
subplot(2,2,ploti);
imagesc(-B_lasso{i});
colormap(gray);
title({['lasso' ', \lambda=', ...
num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
axis equal;
axis tight;
end
%%
% Lasso BIC path
figure;
set(gca,'FontSize',20);
semilogx(lambdas_lasso,BIC_lasso,'-o');
title('lasso BIC');
xlabel('\lambda');
ylabel('BIC');
xlim([min(lambdas_lasso),max(lambdas_lasso)]);
%% Regularized matrix Poisson (log-linear) regression
clear;
% reset random seed
s = RandStream('mt19937ar','Seed',2);
RandStream.setGlobalStream(s);
%%
% 2D true signal: 64-by-64 cross
shape = imread('cross.gif');
shape = array_resize(shape,[32,32]); % 32-by-32
b = zeros(2*size(shape));
b((size(b,1)/4):(size(b,1)/4)+size(shape,1)-1, ...
(size(b,2)/4):(size(b,2)/4)+size(shape,2)-1) = shape;
[p1,p2] = size(b);
disp(size(b));
%%
% True coefficients for regular (non-array) covariates
p0 = 5;
b0 = ones(p0,1);
%%
% Simulate covariates
n = 750; % sample size
X = randn(n,p0); % n-by-p regular design matrix
M = tensor(randn(p1,p2,n)); % n p1-by-p2 matrix variates
disp(size(M));
% Simulate Poisson count responses from the systematic components
mu = X*b0 + double(ttt(tensor(b), M, 1:2));
mu = mu/(max(abs(mu)))*5; % scale to [-5, 5], to avoid overflow
y = poissrnd(exp(mu));
%%
% Determine max lambda to start
[~,~,stats] = matrix_sparsereg(X,M,y,inf,'poisson');
maxlambda = stats.maxlambda*.95;
%%
% Fit nuclear norm regularized Poisson regression at grid points
gridpts = 10;
lambdas = zeros(1,gridpts);
gs = 2/(1+sqrt(5));
B = cell(1,gridpts);
AIC = zeros(1,gridpts);
BIC = zeros(1,gridpts);
tic;
for i=1:gridpts
if (i==1)
B0 = [];
else
B0 = B{i-1};
end
lambda = maxlambda*gs^(i-1);
lambdas(i) = lambda;
[beta0,B{i},stats] = matrix_sparsereg(X,M,y,lambda,'poisson','B0',B0);
AIC(i) = stats.AIC;
BIC(i) = stats.BIC;
end
toc
%%
% disp true signal and snapshots along nuclear norm solution path
figure; hold on;
set(gca,'FontSize',20);
subplot(2,2,1);
imagesc(-b);
colormap(gray);
title('True Signal');
axis equal;
axis tight;
ploti = 1;
for i=[gridpts round(gridpts/2) 1]
ploti = ploti + 1;
subplot(2,2,ploti);
imagesc(-double(B{i}));
colormap(gray);
title({['nuclear norm,', ' \lambda=', ...
num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
axis equal;
axis tight;
end
%%
% disp AIC/BIC trace plot
figure;
set(gca,'FontSize',20);
semilogx(lambdas, AIC, '-+', lambdas, BIC, '-o');
xlabel('\lambda');
ylabel('BIC');
xlim([min(lambdas) max(lambdas)]);
legend('AIC', 'BIC', 'Location', 'northwest');
% %% Compare to lasso penalized Poisson (log-linear) regression
%
% %%
% % Transform matrix variates to vector form
% TM = tenmat(tensor(M),3,[1 2]);
% Xall = [double(TM) X];
%
% %%
% % Determine max lambda to start
% lambdastart = 0;
% for j=1:numel(b)
% lambdastart = max(lambdastart,glm_maxlambda(Xall(:,j),y,'loglinear'));
% end
% maxlambda_lasso = lambdastart*.95;
%
% %%
% % Optimization at grid points
% gridpts = 10;
% B_lasso = cell(1,gridpts);
% BIC_lasso = zeros(1,gridpts);
% lambdas_lasso = zeros(1,gridpts);
% penidx = true(size(Xall,2),1);
% penidx(numel(b)+1:end) = false; % do not penalize regular covariates
%
% tic;
% for i=1:gridpts
% if (i==1)
% x0 = zeros(size(Xall,2),1);
% else
% x0 = beta;
% end
% lambda = maxlambda_lasso*gs^(i-1);
% lambdas_lasso(i) = lambda;
% [beta] = glm_sparsereg(Xall,y,lambda,'loglinear', ...
% 'x0',x0,'penidx',penidx);
% B_lasso{i} = beta(1:numel(b));
% eta = Xall*beta;
% BIC_lasso(i) = - sum(y.*log(eta)-gammaln(y+1)-eta) ...
% + log(n)*nnz(abs(beta)>1e-8);
% end
% toc;
%
% %%
% % disp true sinal and snapshots along lasso solution path
% figure; hold on;
% set(gca,'FontSize',20);
%
% subplot(2,2,1);
% imagesc(-b);
% colormap(gray);
% title('True Signal');
% axis equal;
% axis tight;
%
% ploti = 1;
% for i=[gridpts round(gridpts/2) 1]
% ploti = ploti + 1;
% subplot(2,2,ploti);
% imagesc(-reshape(B_lasso{i},p1,p2));
% colormap(gray);
% title({['lasso' ', \lambda=', ...
% num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
% axis equal;
% axis tight;
% end
%
% %%
% % Lasso BIC path
% figure;
% set(gca,'FontSize',20);
% semilogx(lambdas_lasso,BIC_lasso,'-o');
% title('lasso BIC');
% xlabel('\lambda');
% ylabel('BIC');
% xlim([min(lambdas_lasso),max(lambdas_lasso)]);
%% Regularized matrix logistic regression
clear;
% reset random seed
s = RandStream('mt19937ar','Seed',2);
RandStream.setGlobalStream(s);
%%
% 2D true signal: 64-by-64 cross
shape = imread('cross.gif');
shape = array_resize(shape,[32,32]); % 32-by-32
b = zeros(2*size(shape));
b((size(b,1)/4):(size(b,1)/4)+size(shape,1)-1, ...
(size(b,2)/4):(size(b,2)/4)+size(shape,2)-1) = shape;
[p1,p2] = size(b);
disp(size(b));
%%
% True coefficients for regular (non-array) covariates
p0 = 5;
b0 = ones(p0,1);
%%
% Simulate covariates
n = 1000; % sample size
X = randn(n,p0); % n-by-p regular design matrix
M = tensor(randn(p1,p2,n)); % n p1-by-p2 matrix variates
disp(size(M));
%%
% Simulate binary responses from the systematic components
mu = X*b0 + double(ttt(tensor(b), M, 1:2));
y = binornd(1, 1./(1+exp(-mu)));
%%
% Determine max lambda to start
[~,~,stats] = matrix_sparsereg(X,M,y,inf,'binomial');
maxlambda = stats.maxlambda*0.95;
%%
% Fit nuclear norm regularized logistic regression at grid points
gridpts = 10;
lambdas = zeros(1,gridpts);
gs = 2/(1+sqrt(5));
B = cell(1,gridpts);
AIC = zeros(1,gridpts);
BIC = zeros(1,gridpts);
tic;
for i=1:gridpts
if (i==1)
B0 = [];
else
B0 = B{i-1};
end
lambda = maxlambda*gs^(i-1);
lambdas(i) = lambda;
[beta0,B{i},stats] = matrix_sparsereg(X,M,y,lambda,'binomial','B0',B0);
AIC(i) = stats.AIC;
BIC(i) = stats.BIC;
end
toc
%%
% disp true signal and and snapshots along nuclear norm solution path
figure; hold on;
set(gca,'FontSize',20);
subplot(2,2,1);
imagesc(-b);
colormap(gray);
title('True Signal');
axis equal;
axis tight;
ploti = 1;
for i=[gridpts round(gridpts/2) 1]
ploti = ploti + 1;
subplot(2,2,ploti);
imagesc(-double(B{i}));
colormap(gray);
title({['nuclear norm,', ' \lambda=', ...
num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
axis equal;
axis tight;
end
%%
% disp AIC/BIC trace plot
figure;
set(gca,'FontSize',20);
semilogx(lambdas, AIC, '-+', lambdas, BIC, '-o');
xlabel('\lambda');
ylabel('BIC');
xlim([min(lambdas) max(lambdas)]);
title('Nuclear norm AIC/BIC');
legend('AIC', 'BIC', 'Location', 'northwest');
% %% Compare to lasso penalized logistic regression
%
% %%
% % Transform matrix variates to vector form
% TM = tenmat(tensor(M),3,[1 2]);
% Xall = [double(TM) X];
%
% %%
% % Determine max lambda to start
% lambdastart = 0; % find the maximum tuning parameter to start
% for j=1:numel(b)
% lambdastart = max(lambdastart,glm_maxlambda(Xall(:,j),y,'logistic'));
% end
% maxlambda_lasso = lambdastart;
%
% %%
% % Optimization at grid points
% gridpts = 10;
% B_lasso = cell(1,gridpts);
% BIC_lasso = zeros(1,gridpts);
% lambdas_lasso = zeros(1,gridpts);
% penidx = true(size(Xall,2),1);
% penidx(numel(b)+1:end) = false; % do not penalize regular covariates
%
% tic;
% for i=1:gridpts
% if (i==1)
% x0 = zeros(size(Xall,2),1);
% else
% x0 = beta;
% end
% lambda = maxlambda_lasso*gs^(i-1);
% lambdas_lasso(i) = lambda;
% [beta] = glm_sparsereg(Xall,y,lambda,'logistic','x0',x0, ...
% 'penidx',penidx);
% B_lasso{i} = beta(1:numel(b));
% eta = Xall*beta;
% BIC_lasso(i) = - sum(y.*eta-log(1+exp(eta))) ...
% + log(n)*nnz(abs(beta)>1e-8);
% end
% toc;
%
% %%
% % disp true signal and snapshots along lasso solution path
% figure; hold on;
% set(gca,'FontSize',20);
%
% subplot(2,2,1);
% imagesc(-b);
% colormap(gray);
% title('True Signal');
% axis equal;
% axis tight;
%
% ploti = 1;
% for i=[gridpts round(gridpts/2) 1]
% ploti = ploti + 1;
% subplot(2,2,ploti);
% imagesc(-reshape(B_lasso{i},p1,p2));
% colormap(gray);
% title({['lasso' ', \lambda=', ...
% num2str(lambdas(i))]; ['BIC=', num2str(BIC(i))]});
% axis equal;
% axis tight;
% end
%
% %%
% % Lasso BIC path
% figure;
% set(gca,'FontSize',20);
% semilogx(lambdas_lasso,BIC_lasso,'-o');
% title('lasso BIC');
% xlabel('\lambda');
% ylabel('BIC');
% xlim([min(lambdas_lasso),max(lambdas_lasso)]);