-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_RRR.m
81 lines (58 loc) · 2.19 KB
/
demo_RRR.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
% demo_RRR.m
%
% Examine Reduced Rank Regression (RRR), which is a special case of
% bilinear regression, and compare the closed-form RRR solution with bilinar
% optimization.
setpath; % set path
% ---------------------------------------------
% Set dimensions & rank
nx = 50; % number of input neurons
ny = 30; % number of output neurons
rnk = 3; % rank
% ----------------------------------------------
% Make true weights (random low-rank matrix)
wx = gsmooth(randn(nx,rnk),3); % column vectors
wy = gsmooth(randn(ny,rnk),3)'; % row vectors
nwtot = nx*ny; % total number of filter coefficients
wtruemat = wx*wy; % filter as a matrix
wtruevec = vec(wtruemat); % vectorized filter
% ---------------------------------------------
% Generate training data
nstim = 500; % number of trials
signse = 3; % stdev of observation noise
X = randn(nstim,nx); % input neurons
Y = X*wtruemat + randn(nstim,ny)*signse; % output neurons (observations)
% Pre-compute sufficient statistics
XX = X'*X;
XY = X'*Y;
% compute LS solution
wls = XX\XY;
%% Run reduced rank regression
[wrrr,urrr,vrrr] = compRRR(X,Y,rnk);
%% Estimate W using bilinear optimization (coordinate ascent algorithm)
% Make the necessary design matrix
XXvec = kron(speye(ny),XX); % design matrix for vectorized problem
XYvec = XY(:); % vectorized XY matrix
% set options
opts.MaxIter = 50;
opts.TolFun = 1e-8;
opts.Display = 'off';
lambda = 0; % set the ridge parameter to zero
[wbilin,ubilin,vbilin] = bilinearRegress_coordAscent_fast(XXvec,XYvec,[nx,ny],rnk,lambda,opts); % solve by bilinear optimization
%% Make plots and compute R^2
subplot(221); imagesc(wtruemat);
title(sprintf('true filter (rank=%d)',rnk));
subplot(222); imagesc(wls);
title('least squares');
subplot(223); imagesc(wrrr);
title('RRR');
subplot(224); imagesc(wbilin);
title('bilinear optim');
% Compute R^2 between true and estimated weights
msefun = @(x,y)(mean((x-y).^2));
r2fun = @(x)(1-msefun(x(:),wtruevec)./msefun(wtruevec,mean(wtruevec)));
fprintf('\nPerformance comparison (R^2):\n');
fprintf('----------------------------\n');
fprintf(' least-squares: %.3f\n',r2fun(wls));
fprintf(' RRR: %.3f\n',r2fun(wrrr));
fprintf('bilinear optim: %.3f\n',r2fun(wbilin));