-
Notifications
You must be signed in to change notification settings - Fork 0
/
rac_mbadmm_nnls.m
200 lines (176 loc) · 4.18 KB
/
rac_mbadmm_nnls.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
%% RAC - MBADMM - non negative least squares
% MSE 310 Linear Programming
% project 3
% Stephen Palmieri
%% RAC
clear all;close all;clc;
n = 10;
p = 20;
% n = 1000;
% p = 2000;
% n = 100;
% p = 200;
blocks = 5;
rng(5)
y = sprandn(n,1,.1);
X = sprandn(n,p,.1);
beta_true = pos(sprandn(p,1,.1));
y = X*beta_true;
beta01 = pos(sprandn(p,1,.1));
z01 = pos(sprandn(p,1,.1));
mu01 = pos(sprandn(p,1,.1));
k = 1;
err(k) = norm(beta_true-beta01,2);
err_bz(k) = norm(beta01-z01,2);
toler = 1e-4;
maxIter = 1000;
gammas = [.01, .1, 1, 10];
for jj = 1:length(gammas)
beta0 = beta01;z0 = z01;mu0 = mu01;
tic
for ii = 1:maxIter
[beta_out,z_out,mu_out] = rac_nnls(y,X,beta0,z0, mu0, blocks,gammas(jj));
beta0 = beta_out; z0 = z_out;mu0 = mu_out;
obj(k,jj) = 1/(2*n) * (y-X*beta_out)'*(y-X*beta_out);
obj_al(k,jj) = obj(k,jj) + gammas(jj)/2*norm(beta_out-z_out,2)^2 - mu_out'*(beta_out-z_out);
k = k+1;
err(k,jj) = norm(beta_true-beta_out,2);
err_bz(k,jj) = norm(beta_out-z_out,2);
% if abs(err(k)-err(k-1)) < toler
if abs(err(k,jj)) < toler
disp('below tolerance')
break
else
end
end
t(jj) = toc;
k = 1;
end
%verifying non-negativity constraints
% beta_out >= 0
% z_out >= 0
figure
plot(gammas,t, 'b*')
xlabel('\gamma values')
ylabel('time (s)')
title('Time elapsed for RAC')
mean(t)
%% plotting RAC
figure
for iii = 1:length(gammas)
plot(err(2:200,iii))
hold on
end
xlabel('iterations')
ylabel('error')
title('l-2 norm \beta error from true value')
legend('.01', '.1','1','10')
hold off
figure
for iii = 1:length(gammas)
plot(err_bz(2:200,iii))
hold on
end
xlabel('iterations')
ylabel('error')
title('l-2 norm (\beta - z) error')
legend('.01', '.1','1','10')
hold off
figure
for iii = 1:length(gammas)
plot(obj(2:200,iii))
hold on
% plot(obj_al)
% legend('original obj','augmented Lagrangian')
end
xlabel('iterations')
ylabel('objective loss')
title('Non-negative Least Squares Objective Loss vs Iterations using RAC-MBADMM')
legend('.01', '.1','1','10')
hold off
%% Randomly Permute Comparison - Section IV
clear all;close all;clc;
n = 100;
p = 200;
% n = 1000;
% p = 2000;
% n = 10;
% p = 20;
blocks = p;
rng(5)
y = sprandn(n,1,.1);
X = sprandn(n,p,.1);
beta_true = pos(sprandn(p,1,.1));
y = X*beta_true;
beta01 = pos(sprandn(p,1,.1));
z01 = pos(sprandn(p,1,.1));
mu01 = pos(sprandn(p,1,.1));
k = 1;
err(k) = norm(beta_true-beta01,2);
err_bz(k) = norm(beta01-z01,2);
toler = 1e-3;
maxIter = 1000;
gammas = [.01, .1, 1, 10];
for jj = 1:length(gammas)
beta0 = beta01;z0 = z01;mu0 = mu01;
tic;
for ii = 1:maxIter
[beta_out,z_out,mu_out] = rp_nnls(y,X,beta0,z0, mu0, blocks,gammas(jj));
beta0 = beta_out; z0 = z_out;mu0 = mu_out;
obj(k,jj) = 1/(2*n) * (y-X*beta_out)'*(y-X*beta_out);
obj_al(k,jj) = obj(k,jj) + gammas(jj)/2*norm(beta_out-z_out,2)^2 - mu_out'*(beta_out-z_out);
k = k+1;
err(k,jj) = norm(beta_true-beta_out,2);
err_bz(k,jj) = norm(beta_out-z_out,2);
% if abs(err(k)-err(k-1)) < toler
if abs(err(k,jj)) < toler
disp('below tolerance')
break
else
end
end
t(jj) = toc;
k = 1;
end
%verifying non-negativity constraints
% beta_out >= 0
% z_out >= 0
figure
plot(gammas,t, 'b*')
xlabel('\gamma values')
ylabel('time (s)')
title('Time elapsed for RP')
mean(t)
%% plotting RP
figure
for iii = 1:length(gammas)
plot(err(2:200,iii))
hold on
end
xlabel('iterations')
ylabel('error')
title('l-2 norm \beta error from true value')
legend('.01', '.1','1','10')
hold off
figure
for iii = 1:length(gammas)
plot(err_bz(2:200,iii))
hold on
end
xlabel('iterations')
ylabel('error')
title('l-2 norm (\beta - z) error')
legend('.01', '.1','1','10')
hold off
figure
for iii = 1:length(gammas)
plot(obj(2:200,iii))
hold on
% plot(obj_al)
% legend('original obj','augmented Lagrangian')
end
xlabel('iterations')
ylabel('objective loss')
title('Non-negative Least Squares Objective Loss vs Iterations using RP-MBADMM')
legend('.01', '.1','1','10')
hold off