forked from Xiaolu-Zhu/LongitudinalClustering
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmmmcp_code.cpp
More file actions
319 lines (232 loc) · 7.93 KB
/
admmmcp_code.cpp
File metadata and controls
319 lines (232 loc) · 7.93 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
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
#include <RcppArmadillo.h>
#include <string.h>
#include <stdio.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// calculate prox_L2
// [[Rcpp::export]]
arma::vec prox_L2(arma::vec x, double sigma){
int n = x.n_elem;
double lv;
arma::vec px(n);
lv = norm(x, 2);
if (lv == 0.)
px = x;
else
px = fmax(0., 1. - (sigma/lv))*x;
return px;
}
// Update B
// [[Rcpp::export]]
arma::mat update_B(arma::mat X, arma::mat diagD, arma::vec y, arma::mat V,
arma::mat Lambda, int n, double gamma1, arma::mat index, double theta) {
int m = index.n_rows;
int p = V.n_rows;
arma::mat B_new(p, n);
arma::mat V_tilde(p, m);
int i, j;
V_tilde = V + Lambda/theta;
arma::mat eyemat_n = arma::eye<arma::mat>(n,n);
arma::mat eyemat_p = arma::eye<arma::mat>(p,p);
arma::mat onemat_n = arma::ones<arma::mat>(n,n);
arma::mat AtA = arma::kron(n*eyemat_n - onemat_n,eyemat_p);
arma::mat XtX = arma::trans(X)*X;
arma::mat invmat = arma::inv(XtX + gamma1*diagD + theta*AtA) ;
arma::vec lv = arma::zeros<arma::vec>(n*p);
for (int l=0; l<m; l++) {
i = index(l, 0);
j = index(l, 1);
arma::vec e1 = arma::zeros<arma::vec>(n);
arma::vec e2 = arma::zeros<arma::vec>(n);
e1(i-1) = 1;
e2(j-1) = 1;
arma::mat Alt = arma::kron(e1 - e2, eyemat_p);
lv = lv + Alt*V_tilde.col(l);
}
arma::vec rhs = arma::trans(X)*y + theta*lv;
arma::vec b_new = invmat*rhs;
B_new = arma::reshape(b_new, p, n);
return B_new;
}
// Update B initial
// [[Rcpp::export]]
arma::mat update_B_ini(arma::mat X, arma::mat diagD, arma::vec y,
int n, double gamma1, arma::mat index, double lambda0) {
int p = X.n_cols/n;
arma::mat B_new(p, n);
arma::mat eyemat_n = arma::eye<arma::mat>(n,n);
arma::mat eyemat_p = arma::eye<arma::mat>(p,p);
arma::mat onemat_n = arma::ones<arma::mat>(n,n);
arma::mat AtA = arma::kron(n*eyemat_n - onemat_n,eyemat_p);
arma::mat XtX = arma::trans(X)*X;
arma::mat invmat = arma::inv(XtX + gamma1*diagD + lambda0*AtA) ;
arma::vec rhs = arma::trans(X)*y;
arma::vec b_new = invmat*rhs;
B_new = arma::reshape(b_new, p, n);
return B_new;
}
// Update V
// [[Rcpp::export]]
arma::mat update_V(arma::mat B, arma::mat Lambda,
double gamma2, double theta, double tau, arma::mat index) {
int m = index.n_rows;
int p = B.n_rows;
arma::mat V(p, m);
double sigma = gamma2/theta;
int i, j;
arma::vec x(p);
double mcp = tau*theta/(tau*theta -1);
for (int l=0; l<m; l++) {
i = index(l, 0);
j = index(l, 1);
x = B.col(i-1) - B.col(j-1) - (1.0/(theta))*Lambda.col(l);
if (arma::norm(x,2) >= tau*gamma2)
V.col(l) = x;
else
V.col(l) = prox_L2(x, sigma)*mcp;
}
return V;
}
// Update Lambda
// [[Rcpp::export]]
arma::mat update_Lambda(arma::mat Lambda, arma::mat B, arma::mat V,
double theta, arma::mat index) {
int m = index.n_rows;
int p = B.n_rows;
arma::mat Lambda_new(p, m);
int i, j;
arma::vec x(p);
for (int l=0; l<m; l++) {
i = index(l, 0);
j = index(l, 1);
x = V.col(l) - B.col(i-1) + B.col(j-1);
Lambda_new.col(l) = Lambda.col(l) + theta*x;
}
return Lambda_new;
}
// Stopping criterion related
// Stopping criterion related
// [[Rcpp::export]]
double tolerance_primal(arma::mat B, arma::mat V, double eps_abs, double eps_rel, arma::mat index) {
int m = index.n_rows;
int p = V.n_rows;
int i,j;
arma::vec dbl(p);
arma::vec dvl(p);
double db = 0.0;
double dv = 0.0;
double output = sqrt(p*m)*(eps_abs);
for (int l=0; l<m; l++) {
i = index(l, 0);
j = index(l, 1);
dbl = pow(B.col(i-1) - B.col(j-1),2);
db = db + sum(dbl);
dvl = pow(V.col(l),2);
dv = dv + sum(dvl);
}
db = sqrt(db);
dv = sqrt(dv);
output = output + (eps_rel)*fmax(db,dv);
return output;
}
// [[Rcpp::export]]
double tolerance_dual(arma::mat Lambda, arma::mat index, int n, double eps_abs, double eps_rel) {
int m = index.n_rows;
int p = Lambda.n_rows;
double output = sqrt(p*n)*(eps_abs);
double s;
arma::vec AtLambda = arma::zeros<arma::vec>(p*n);
arma::mat eyemat_p = arma::eye<arma::mat>(p,p);
for (int l=0; l<m; l++) {
int i = index(l, 0);
int j = index(l, 1);
arma::vec e1 = arma::zeros<arma::vec>(n);
arma::vec e2 = arma::zeros<arma::vec>(n);
e1(i-1) = 1;
e2(j-1) = 1;
arma::mat Alt = arma::kron(e1 - e2, eyemat_p);
AtLambda = AtLambda + Alt*Lambda.col(l);
}
s = arma::norm(AtLambda,2);
output = output + s*(eps_rel);
return output;
}
// This function computes the L2 norm of the primal residual.
// [[Rcpp::export]]
double residual_primal(arma::mat B, arma::mat V, arma::mat index) {
int m = index.n_rows;
int p = B.n_rows;
arma::vec x(p);
double r = 0.0;
for (int l=0; l<m; l++) {
int i = index(l, 0);
int j = index(l, 1);
x = pow(B.col(i-1) - B.col(j-1) - V.col(l),2);
r = r + sum(x);
}
double residual = sqrt(r);
return residual;
}
// This function computes the L2 norm of the dual residual.
// [[Rcpp::export]]
double residual_dual(arma::mat V, arma::mat V_old, arma::mat index, int n, double theta) {
int m = index.n_rows;
int p = V.n_rows;
double s = 0.0;
arma::mat diff = V - V_old;
arma::vec v(p);
for(int i=0;i<n;i++){
arma::vec v1 = arma::zeros<arma::vec>(p);
arma::vec v2 = arma::zeros<arma::vec>(p);
for(int ix=0;ix<m;ix++){
int l1 = index(ix,0);
if(l1 == i + 1)
v1 = v1 + diff.col(ix);
else
v1 = v1 + arma::zeros<arma::vec>(p);
int l2 = index(ix,1);
if(l2 == i + 1)
v2 = v2 + diff.col(ix);
else
v2 = v2 + arma::zeros<arma::vec>(p);
}
v = pow(v1-v2,2);
s = s + sum(v);
}
double residual = theta*sqrt(s);
return residual;
}
// [[Rcpp::export]]
List prclust_admm(arma::mat X, arma::vec y, arma::mat diagD, arma::mat B_0, arma::mat index,
double gamma1, double gamma2, double theta, double tau, int n, int p, int max_iter,
double eps_abs, double eps_rel) {
int m = index.n_rows;
arma::mat V = arma::zeros<arma::mat>(p,m);
arma::mat Lambda = arma::zeros<arma::mat>(p,m);
double rp, rd;
double tp, td;
arma::mat B_new(p,n);
arma::mat V_old(p,m);
arma::mat Lambda_old(p,m);
V = update_V(B_0, Lambda, gamma2, theta, tau, index);
int iter = 1;
for (int its=0; its< max_iter; its++) {
V_old = V;
Lambda_old = Lambda;
B_new = update_B(X, diagD, y, V_old, Lambda_old, n, gamma1, index, theta);
V = update_V(B_new, Lambda_old, gamma2, theta, tau, index);
Lambda = update_Lambda(Lambda_old, B_new, V, theta, index);
rp = residual_primal(B_new, V, index);
tp = tolerance_primal(B_new, V, eps_abs, eps_rel, index);
rd = residual_dual(V, V_old, index, n, theta);
td = tolerance_dual(Lambda, index, n, eps_abs, eps_rel);
if ((rp <= tp) & (rd <= td)) break;
iter = iter + 1;
}
if (iter > max_iter)
iter = max_iter;
return List::create(Named("B") = B_new,
Named("Lambda") = Lambda,
Named("V") = V,
Named("iterations") = iter);
}