-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenlasso.cpp
212 lines (179 loc) · 6.33 KB
/
genlasso.cpp
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
#include <Rcpp.h>
using namespace Rcpp;
//' Solving Generalized LASSO with fixed \eqn{\lambda = 1}
//'
//' Solves efficiently the generalized LASSO problem of the form
//' \deqn{
//' \hat{\beta} = \operatorname{argmin} \frac{1}{2} || y - \beta ||_2^2 + ||D\beta||_1
//' }
//' where \eqn{\beta} and \eqn{y} are \eqn{m}-dimensional vectors and
//' \eqn{D} is a \eqn{(c \times m)}-matrix where \eqn{c \geq m}.
//' We solve this optimization problem using an adaption of the ADMM
//' algorithm presented in Zhu (2017).
//'
//' @param Y The \eqn{y} vector of length \eqn{m}
//' @param W The weight matrix \eqn{W} of dimensions \eqn{m \times m}
//' @param m The number of graphs
//' @param eta1 Equals \eqn{\lambda_1 / rho}
//' @param eta2 Equals \eqn{\lambda_2 / rho}
//' @param a Value added to the diagonal of \eqn{-D'D} so that
//' the matrix is positive definite, see `matrix_A_inner_ADMM` in package `CVN`
//' @param rho The ADMM's parameter
//' @param max_iter Maximum number of iterations
//' @param eps Stopping criterion. If differences
//' are smaller than \eqn{\epsilon}, algorithm
//' is halted
//' @param truncate Values below \code{truncate} are
//' set to \code{0}
//'
//' @return The estimated vector \eqn{\hat{\beta}}
//'
//' @references
//' Zhu, Y. (2017). An Augmented ADMM Algorithm With Application to the
//' Generalized Lasso Problem. Journal of Computational and Graphical Statistics,
//' 26(1), 195–204. https://doi.org/10.1080/10618600.2015.1114491
//'
// [[Rcpp::export]]
NumericVector genlassoRcpp(const NumericVector Y,
const NumericMatrix W,
const int m,
const double eta1,
const double eta2,
double a,
const double rho,
const int max_iter,
const double eps,
const double truncate) {
// some frequently used constants
a = rho*a ;
const double C = 1 / (1 + a) ;
// number of rows in matrix D
const int c = (m*m + m) / 2 ;
/* Compute y to a double array. I did this to make sure that the C compilation
* environment does not have an effect */
double* y = new double[m + 1] ;
for (int i = 0; i < m; i ++) {
*(y + i) = (double)(Y[i]) ;
}
/* initialize vectors for beta-update step in the ADMM */
double *beta_new = new double[m+1];
double *beta_old = new double[m+1] ;
double *delta = new double[m+1] ;
for (int i = 0; i < m; i ++) {
*(beta_new + i) = 0;
*(beta_old + i) = 0;
*(delta + i) = 0;
}
/* initialize vectors for alpha-update step in the ADMM */
double *alpha_new = new double[c+1] ;
double *alpha_old1 = new double[c+1] ;
double *alpha_old2 = new double[c+1] ;
double *alpha = new double[c+1];
for (int i = 0; i < c; i ++) {
*(alpha_new + i) = 0 ;
*(alpha_old1 + i) = 0 ;
*(alpha_old2 + i) = 0 ;
*(alpha + i) = 0 ;
}
/* Indices used for the alpha-update step */
int* steps = new int[m - 1] ;
*steps = 0 ;
for (int i = 1; i < (m-1); i ++) {
*(steps + i) = m - i + *(steps + i - 1) ;
}
int iter = 0 ; // number of iterations
double diff = 0 ; // absolute difference between beta^(k+1) and beta^k
/* loop until either the max. no. of iterations are reached
* or the difference (diff) is smaller then eps
*/
while (iter < max_iter) {
/* ------- beta-update step ---------*/
// alpha = 2*alpha_old1 - alpha_old2 ;
for (int i = 0; i < c; i++) {
*(alpha + i) = 2*(*(alpha_old1 + i)) - *(alpha_old2 + i) ;
}
// go over all possible pairs (i,j), same as D^T %*% (2 alpha^(k) - alpha^(k-1))
for (int i = 0; i < m; i++) {
*(delta + i) = eta1 * (*(alpha + i)) ;
for (int j = i+1; j < m; j++) {
*(delta + i) += eta2* W(i,j)*(*(alpha + m + steps[i] + (j - i) - 1)) ;
}
for (int j = 0; j < i; j++) {
*(delta + i) -= eta2*W(i,j)*(*(alpha + m + steps[j] - (j - i) - 1)) ;
}
}
diff = 0 ;
for (int i = 0; i < m; i ++) {
*(beta_new + i) = C*(a*(*(beta_old + i)) + y[i] - *(delta + i)) ;
diff += fabs(*(beta_new + i) - *(beta_old + i)) ; //abs(beta_new[i] - beta_old[i]) ;
}
// determine whether converged or not
if (diff < eps) {
/* Turn to zero when really close */
for (int i = 0; i < m; i ++) {
if (fabs(*(beta_new + i)) < truncate) {
*(beta_new + i) = 0 ;
}
}
// convert results to NumericVector for R
NumericVector res = NumericVector(beta_new, beta_new + m) ;
// Clean-up ------------
delete[] beta_old;
delete[] alpha_old1;
delete[] alpha_old2;
delete[] alpha_new;
delete[] alpha;
delete[] y;
delete[] beta_new ;
return(res);
}
/* --------- alpha update step ----------- */
for (int i = 0; i < m; i++) {
*(alpha_new + i) = *(alpha_old1 + i) + rho * eta1 * *(beta_new + i) ;
}
int k = m;
// go over all unique pairs (i,j)
for (int i = 0; i < m-1; i++) {
for (int j = i+1; j < m; j++) {
*(alpha_new + k) = *(alpha_old1 + k) + rho * eta2 * W(i,j) * (*(beta_new + i) - *(beta_new + j)) ;
k ++;
}
}
/* Threshold alpha. Must lie in [-1, 1] range */
for (int i = 0; i < c; i ++) {
if (alpha_new[i] > 1) {
alpha_new[i] = 1 ;
}
if (alpha_new[i] < -1) {
alpha_new[i] = -1 ;
}
}
/* update beta and alpha for the next iteration step */
for (int i = 0; i < m; i ++) {
*(beta_old + i) = *(beta_new + i) ;
}
for (int i = 0; i < c; i ++) {
*(alpha_old2 + i) = *(alpha_old1 + i) ;
*(alpha_old1 + i) = *(alpha_new + i) ;
}
iter ++;
}
/* Turn to zero when really close */
for (int i = 0; i < m; i ++) {
if (fabs(*(beta_new + i)) < truncate) {
*(beta_new + i) = 0 ;
}
}
// convert results to NumericVector for R
NumericVector res = NumericVector(beta_new, beta_new + m) ;
// Clean-up ------------
delete[] beta_old;
delete[] alpha_old1;
delete[] alpha_old2;
delete[] alpha_new;
delete[] alpha;
delete[] y;
delete[] beta_new ;
delete[] steps;
return(res);
}