Skip to content

Commit e90a8f1

Browse files
BF: opt_transform linear_term was not cast as matrix in conditional_density
1 parent 8187b53 commit e90a8f1

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

selectiveInference/R/funs.randomized.R

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ randomizedLasso = function(X,
109109
L_E = t(X) %*% X[,E]
110110

111111
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+
113119
coef_term[active,] = coef_term[active,] + ridge_term * diag(rep(1, sum(active))) # ridge term
114120

115121
subgrad_term = matrix(0, p, sum(inactive)) # for subgrad
@@ -151,10 +157,10 @@ randomizedLasso = function(X,
151157

152158
# XXX only for Gaussian so far
153159

154-
log_optimization_density = function(opt_state
155-
) {
160+
log_optimization_density = function(opt_state) {
161+
156162
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)) {
158164
return(-Inf)
159165
}
160166

@@ -188,7 +194,7 @@ randomizedLasso = function(X,
188194
sample_opt_variables = function(randomizedLASSO_obj, jump_scale, nsample=10000) {
189195
return(MCMC(randomizedLASSO_obj$log_optimization_density,
190196
nsample,
191-
randomizedLASSO_obj$observed_opt_state,
197+
randomizedLASSO_obj$observed_opt_state,
192198
acc.rate=0.2,
193199
scale=jump_scale))
194200
}
@@ -232,6 +238,7 @@ importance_weight = function(noise_scale,
232238
A = apply(A, 2, function(x) {return(x + target_transform$offset_term + opt_transform$offset_term)})
233239
log_num = -apply(A^2, 2, sum) / noise_scale^2
234240
} else {
241+
235242
log_num = log_density_gaussian_(noise_scale,
236243
target_transform$linear_term,
237244
as.matrix(target_sample),
@@ -264,10 +271,11 @@ conditional_density = function(noise_scale, lasso_soln) {
264271
observed_opt_state = lasso_soln$observed_opt_state
265272

266273
nactive = length(active_set)
267-
B = opt_linear[,1:nactive]
274+
B = opt_linear[,1:nactive,drop=FALSE]
268275
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) {
271279
beta_offset = beta_offset+(opt_linear[,(nactive+1):p] %*% observed_opt_state[(nactive+1):p])
272280
}
273281
opt_transform = list(linear_term=B,
@@ -313,11 +321,12 @@ randomizedLassoInf = function(X,
313321

314322
n = nrow(X)
315323
p = ncol(X)
324+
316325
lasso_soln = randomizedLasso(X, y, lam, noise_scale=noise_scale, ridge_term=ridge_term)
317326
active_set = lasso_soln$active_set
318327
inactive_set = lasso_soln$inactive_set
319328
nactive = length(active_set)
320-
329+
321330
noise_scale = lasso_soln$noise_scale # set to default value in randomizedLasso
322331

323332
if (condition_subgrad==TRUE){
@@ -327,7 +336,7 @@ randomizedLassoInf = function(X,
327336
ndim = length(lasso_soln$observed_opt_state)
328337

329338
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])
331340

332341
X_E = X[, active_set]
333342
X_minusE = X[, inactive_set]
@@ -338,7 +347,7 @@ randomizedLassoInf = function(X,
338347
lm_y = lm(y ~ X_E - 1)
339348
sigma = sqrt(sum(resid(lm_y)^2) / lm_y$df.resid)
340349
}
341-
print(c(sigma, 'sigma'))
350+
342351
target_cov = solve(t(X_E) %*% X_E)*sigma^2
343352
cov_target_internal = rbind(target_cov, matrix(0, nrow=p-nactive, ncol=nactive))
344353
observed_target = solve(t(X_E) %*% X_E) %*% t(X_E) %*% y
@@ -378,13 +387,11 @@ randomizedLassoInf = function(X,
378387
if (rootU(line_min)*rootU(line_max)<0){
379388
ci[i,2] = uniroot(rootU, c(line_min, line_max))$root+observed_target[i]
380389
} else{
381-
print("non inv u")
382390
ci[i,2]=line_max
383391
}
384392
if (rootL(line_min)*rootL(line_max)<0){
385393
ci[i,1] = uniroot(rootL, c(line_min, line_max))$root+observed_target[i]
386394
} else{
387-
print("non inv u")
388395
ci[i,1] = line_min
389396
}
390397
}

tests/randomized/test_instances.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ gaussian_instance = function(n, p, s, sigma=1, rho=0, signal=6, X=NA,
2525
}
2626

2727

28-
collect_results = function(n,p,s, nsim=100, level=0.9){
28+
collect_results = function(n,p,s, nsim=100, level=0.9, condition_subgrad=TRUE){
2929
rho=0.3
30-
lam=1.
30+
lam=1.2
3131
sigma=1
3232
sample_pvalues = c()
3333
sample_coverage = c()
@@ -36,7 +36,7 @@ collect_results = function(n,p,s, nsim=100, level=0.9){
3636
X=data$X
3737
y=data$y
3838
beta=data$beta
39-
result = selectiveInference:::randomizedLassoInf(X, y, lam, level=level, burnin=2000, nsample=4000)
39+
result = selectiveInference:::randomizedLassoInf(X, y, lam, level=level, burnin=2000, nsample=4000, condition_subgrad=condition_subgrad)
4040
true_beta = beta[result$active_set]
4141
coverage = rep(0, nrow(result$ci))
4242
for (i in 1:nrow(result$ci)){

0 commit comments

Comments
 (0)