3
3
#
4
4
# min 1/2 || y - \beta_0 - X \beta ||_2^2 + \lambda || \beta ||_1 - \omega^T\beta + \frac{\epsilon}{2} \|\beta\|^2_2
5
5
6
- randomizedLASSO = function (X ,
6
+ randomizedLasso = function (X ,
7
7
y ,
8
8
lam ,
9
- noise_scale ,
10
- ridge_term ,
9
+ noise_scale = NULL ,
10
+ ridge_term = NULL ,
11
11
noise_type = c(' gaussian' , ' laplace' ),
12
12
max_iter = 100 , # how many iterations for each optimization problem
13
13
kkt_tol = 1.e-4 , # tolerance for the KKT conditions
@@ -20,6 +20,21 @@ randomizedLASSO = function(X,
20
20
21
21
n = nrow(X ); p = ncol(X )
22
22
23
+ mean_diag = mean(apply(X ^ 2 , 2 , sum ))
24
+
25
+ # default ridge term
26
+
27
+ if (is.null(ridge_term )) {
28
+ ridge_term = sqrt(mean_diag ) * sd(y ) / sqrt(n )
29
+ }
30
+
31
+ # default noise level
32
+
33
+ if (is.null(noise_scale )) {
34
+ noise_scale = 0.5 * sd(y ) * sqrt(mean_diag )
35
+ }
36
+
37
+ print(c(noise_scale , ridge_term ))
23
38
noise_type = match.arg(noise_type )
24
39
25
40
if (noise_scale > 0 ) {
@@ -246,10 +261,10 @@ conditional_density = function(noise_scale, lasso_soln) {
246
261
if (sum(opt_state < 0 ) > 0 ) {
247
262
return (- Inf )
248
263
}
249
- D = selectiveInference ::: log_density_gaussian_conditional_(noise_scale ,
250
- reduced_B ,
251
- as.matrix(opt_state ),
252
- reduced_beta_offset )
264
+ D = log_density_gaussian_conditional_(noise_scale ,
265
+ reduced_B ,
266
+ as.matrix(opt_state ),
267
+ reduced_beta_offset )
253
268
return (D )
254
269
}
255
270
lasso_soln $ log_optimization_density = log_condl_optimization_density
@@ -258,23 +273,42 @@ conditional_density = function(noise_scale, lasso_soln) {
258
273
return (lasso_soln )
259
274
}
260
275
261
- randomized_inference = function (X , y , sigma , lam , noise_scale , ridge_term ){
276
+ randomizedLassoInf = function (X ,
277
+ y ,
278
+ lam ,
279
+ sigma = NULL ,
280
+ noise_scale = NULL ,
281
+ ridge_term = NULL ,
282
+ condition_subgrad = TRUE ,
283
+ level = 0.9 ) {
262
284
263
285
n = nrow(X )
264
286
p = ncol(X )
265
- lasso_soln = selectiveInference ::: randomizedLASSO (X , y , lam , noise_scale , ridge_term )
287
+ lasso_soln = randomizedLasso (X , y , lam , noise_scale , ridge_term )
266
288
active_set = lasso_soln $ active_set
267
289
inactive_set = lasso_soln $ inactive_set
268
290
nactive = length(active_set )
269
-
291
+
292
+ if (condition_subgrad == TRUE ){
293
+ lasso_soln = conditional_density(noise_scale ,lasso_soln )
294
+ }
295
+
270
296
dim = length(lasso_soln $ observed_opt_state )
271
297
print(paste(" chain dim" , dim ))
272
- S = selectiveInference ::: sample_opt_variables(lasso_soln , jump_scale = rep(1 / sqrt(n ), dim ), nsample = 10000 )
298
+ S = sample_opt_variables(lasso_soln , jump_scale = rep(1 / sqrt(n ), dim ), nsample = 10000 )
273
299
opt_samples = S $ samples [2001 : 10000 ,]
274
300
print(paste(" dim opt samples" , toString(dim(opt_samples ))))
275
301
276
302
X_E = X [, active_set ]
277
303
X_minusE = X [, inactive_set ]
304
+
305
+ # if no sigma given, use OLS estimate
306
+
307
+ if (is.null(sigma )) {
308
+ lm_y = lm(y ~ X [,E ] - 1 )
309
+ sigma = sum(resid(lm_y )^ 2 / lm_y $ df.resid )
310
+ }
311
+ print(c(sigma , ' sigma' ))
278
312
target_cov = solve(t(X_E ) %*% X_E )* sigma ^ 2
279
313
cov_target_internal = rbind(target_cov , matrix (0 , nrow = p - nactive , ncol = nactive ))
280
314
observed_target = solve(t(X_E ) %*% X_E ) %*% t(X_E ) %*% y
@@ -283,37 +317,46 @@ randomized_inference = function(X, y, sigma, lam, noise_scale, ridge_term){
283
317
opt_transform = lasso_soln $ optimization_transform
284
318
observed_raw = lasso_soln $ observed_raw
285
319
286
- pvalus = rep(0 , nactive )
320
+ pvalues = rep(0 , nactive )
287
321
ci = matrix (0 , nactive , 2 )
288
322
for (i in 1 : nactive ){
289
- target_transform = selectiveInference ::: linear_decomposition(observed_target [i ],
290
- observed_internal ,
291
- target_cov [i ,i ],
292
- cov_target_internal [,i ],
293
- internal_transform )
323
+ target_transform = linear_decomposition(observed_target [i ],
324
+ observed_internal ,
325
+ target_cov [i ,i ],
326
+ cov_target_internal [,i ],
327
+ internal_transform )
294
328
target_sample = rnorm(nrow(opt_samples )) * sqrt(target_cov [i ,i ])
295
329
296
330
pivot = function (candidate ){
297
- weights = selectiveInference ::: importance_weight(noise_scale ,
298
- t(as.matrix(target_sample )) + candidate ,
299
- t(opt_samples ),
300
- opt_transform ,
301
- target_transform ,
302
- observed_raw )
303
- return (mean((target_sample < observed_target [i ])* weights )/ mean(weights ))
331
+ weights = importance_weight(noise_scale ,
332
+ t(as.matrix(target_sample )) + candidate ,
333
+ t(opt_samples ),
334
+ opt_transform ,
335
+ target_transform ,
336
+ observed_raw )
337
+ return (mean((target_sample + candidate < observed_target [i ])* weights )/ mean(weights ))
304
338
}
305
- level = 0.9
306
339
rootU = function (candidate ){
307
340
return (pivot(observed_target [i ]+ candidate )- (1 - level )/ 2 )
308
341
}
309
342
rootL = function (candidate ){
310
343
return (pivot(observed_target [i ]+ candidate )- (1 + level )/ 2 )
311
344
}
312
345
pvalues [i ] = pivot(0 )
313
- line_min = - 10 * sd(target_sample )
314
- line_max = 10 * sd(target_sample )
315
- ci [i ,1 ] = uniroot(rootU , c(line_min , line_max ))$ root + observed_target [i ]
316
- ci [i ,2 ] = uniroot(rootL , c(line_min , line_max ))$ root + observed_target [i ]
346
+ line_min = - 20 * sd(target_sample )
347
+ line_max = 20 * sd(target_sample )
348
+ if (rootU(line_min )* rootU(line_max )< 0 ){
349
+ ci [i ,2 ] = uniroot(rootU , c(line_min , line_max ))$ root + observed_target [i ]
350
+ } else {
351
+ print(" non inv u" )
352
+ ci [i ,2 ]= line_max
353
+ }
354
+ if (rootL(line_min )* rootL(line_max )< 0 ){
355
+ ci [i ,1 ] = uniroot(rootL , c(line_min , line_max ))$ root + observed_target [i ]
356
+ } else {
357
+ print(" non inv u" )
358
+ ci [i ,1 ] = line_min
359
+ }
317
360
}
318
- return (list (pvalues = pvalues , ci = ci ))
361
+ return (list (active_set = active_set , pvalues = pvalues , ci = ci ))
319
362
}
0 commit comments