@@ -109,7 +109,13 @@ randomizedLasso = function(X,
109
109
L_E = t(X ) %*% X [,E ]
110
110
111
111
coef_term = L_E
112
- coef_term = coef_term %*% diag(c(rep(1 , sum(unpenalized )), sign_soln [active ])) # coefficients are non-negative
112
+ signs_ = c(rep(1 , sum(unpenalized )), sign_soln [active ])
113
+ if (length(signs_ ) == 1 ) {
114
+ coef_term = coef_term * signs_
115
+ } else {
116
+ coef_term = coef_term %*% diag(signs_ ) # scaligns are non-negative
117
+ }
118
+
113
119
coef_term [active ,] = coef_term [active ,] + ridge_term * diag(rep(1 , sum(active ))) # ridge term
114
120
115
121
subgrad_term = matrix (0 , p , sum(inactive )) # for subgrad
@@ -151,10 +157,10 @@ randomizedLasso = function(X,
151
157
152
158
# XXX only for Gaussian so far
153
159
154
- log_optimization_density = function (opt_state
155
- ) {
160
+ log_optimization_density = function (opt_state ) {
161
+
156
162
if ((sum(abs(opt_state [(inactive_start + 1 ): p ]) > inactive_lam ) > 0 ) ||
157
- (sum(opt_state [(active_start + 1 ): inactive_start ] < 0 ) > 0 )) {
163
+ (sum(opt_state [(active_start + 1 ): inactive_start ] < 0 ) > 0 )) {
158
164
return (- Inf )
159
165
}
160
166
@@ -188,7 +194,7 @@ randomizedLasso = function(X,
188
194
sample_opt_variables = function (randomizedLASSO_obj , jump_scale , nsample = 10000 ) {
189
195
return (MCMC(randomizedLASSO_obj $ log_optimization_density ,
190
196
nsample ,
191
- randomizedLASSO_obj $ observed_opt_state ,
197
+ randomizedLASSO_obj $ observed_opt_state ,
192
198
acc.rate = 0.2 ,
193
199
scale = jump_scale ))
194
200
}
@@ -232,6 +238,7 @@ importance_weight = function(noise_scale,
232
238
A = apply(A , 2 , function (x ) {return (x + target_transform $ offset_term + opt_transform $ offset_term )})
233
239
log_num = - apply(A ^ 2 , 2 , sum ) / noise_scale ^ 2
234
240
} else {
241
+
235
242
log_num = log_density_gaussian_(noise_scale ,
236
243
target_transform $ linear_term ,
237
244
as.matrix(target_sample ),
@@ -264,10 +271,11 @@ conditional_density = function(noise_scale, lasso_soln) {
264
271
observed_opt_state = lasso_soln $ observed_opt_state
265
272
266
273
nactive = length(active_set )
267
- B = opt_linear [,1 : nactive ]
274
+ B = opt_linear [,1 : nactive , drop = FALSE ]
268
275
beta_offset = opt_offset
269
- p = length(observed_opt_state )
270
- if (nactive < p ){
276
+ p = length(observed_opt_state )
277
+
278
+ if (nactive < p ) {
271
279
beta_offset = beta_offset + (opt_linear [,(nactive + 1 ): p ] %*% observed_opt_state [(nactive + 1 ): p ])
272
280
}
273
281
opt_transform = list (linear_term = B ,
@@ -313,11 +321,12 @@ randomizedLassoInf = function(X,
313
321
314
322
n = nrow(X )
315
323
p = ncol(X )
324
+
316
325
lasso_soln = randomizedLasso(X , y , lam , noise_scale = noise_scale , ridge_term = ridge_term )
317
326
active_set = lasso_soln $ active_set
318
327
inactive_set = lasso_soln $ inactive_set
319
328
nactive = length(active_set )
320
-
329
+
321
330
noise_scale = lasso_soln $ noise_scale # set to default value in randomizedLasso
322
331
323
332
if (condition_subgrad == TRUE ){
@@ -327,7 +336,7 @@ randomizedLassoInf = function(X,
327
336
ndim = length(lasso_soln $ observed_opt_state )
328
337
329
338
S = sample_opt_variables(lasso_soln , jump_scale = rep(1 / sqrt(n ), ndim ), nsample = nsample )
330
- opt_samples = S $ samples [(burnin + 1 ): nsample ,]
339
+ opt_samples = as.matrix( S $ samples [(burnin + 1 ): nsample ,, drop = FALSE ])
331
340
332
341
X_E = X [, active_set ]
333
342
X_minusE = X [, inactive_set ]
@@ -338,7 +347,7 @@ randomizedLassoInf = function(X,
338
347
lm_y = lm(y ~ X_E - 1 )
339
348
sigma = sqrt(sum(resid(lm_y )^ 2 ) / lm_y $ df.resid )
340
349
}
341
- print(c( sigma , ' sigma ' ))
350
+
342
351
target_cov = solve(t(X_E ) %*% X_E )* sigma ^ 2
343
352
cov_target_internal = rbind(target_cov , matrix (0 , nrow = p - nactive , ncol = nactive ))
344
353
observed_target = solve(t(X_E ) %*% X_E ) %*% t(X_E ) %*% y
@@ -378,13 +387,11 @@ randomizedLassoInf = function(X,
378
387
if (rootU(line_min )* rootU(line_max )< 0 ){
379
388
ci [i ,2 ] = uniroot(rootU , c(line_min , line_max ))$ root + observed_target [i ]
380
389
} else {
381
- print(" non inv u" )
382
390
ci [i ,2 ]= line_max
383
391
}
384
392
if (rootL(line_min )* rootL(line_max )< 0 ){
385
393
ci [i ,1 ] = uniroot(rootL , c(line_min , line_max ))$ root + observed_target [i ]
386
394
} else {
387
- print(" non inv u" )
388
395
ci [i ,1 ] = line_min
389
396
}
390
397
}
0 commit comments