Skip to content

Commit 9eeaa45

Browse files
removing unused functions, fixing randomized test
1 parent ae1031b commit 9eeaa45

File tree

2 files changed

+21
-118
lines changed

2 files changed

+21
-118
lines changed

selectiveInference/R/funs.randomized.R

Lines changed: 17 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ randomizedLasso = function(X,
77
y,
88
lam,
99
family=c("gaussian","binomial"),
10-
condition_subgrad=TRUE,
1110
noise_scale=NULL,
1211
ridge_term=NULL,
1312
max_iter=100, # how many iterations for each optimization problem
@@ -41,7 +40,6 @@ randomizedLasso = function(X,
4140
}
4241
}
4342

44-
print(c("noise scale", noise_scale))
4543
if (noise_scale > 0) {
4644
perturb_ = rnorm(p) * noise_scale
4745
} else {
@@ -98,9 +96,6 @@ randomizedLasso = function(X,
9896
kkt_stop=kkt_stop,
9997
parameter_stop=parameter_stop)
10098
}
101-
#print("SOLN")
102-
#print(result$soln)
103-
print(c("nactive", length(which(result$soln!=0))))
10499

105100
sign_soln = sign(result$soln)
106101
unpenalized = lam == 0
@@ -160,20 +155,7 @@ randomizedLasso = function(X,
160155
coef_term = coef_term %*% diag(signs_) # scalings are non-negative
161156
}
162157

163-
if (sum(inactive) > 0) {
164-
if (condition_subgrad == FALSE) {
165-
subgrad_term = matrix(0, p, sum(inactive)) # for subgrad
166-
for (i in 1:sum(inactive)) {
167-
subgrad_term[inactive_set[i], i] = 1
168-
}
169-
linear_term = cbind(coef_term,
170-
subgrad_term)
171-
} else {
172-
linear_term = coef_term
173-
}
174-
} else {
175-
linear_term = coef_term
176-
}
158+
linear_term = coef_term
177159
offset_term = rep(0, p)
178160
offset_term[active] = lam[active] * sign_soln[active]
179161

@@ -192,23 +174,9 @@ randomizedLasso = function(X,
192174

193175
active_term = -L_E # for \bar{\beta}_E
194176

195-
if (sum(inactive) > 0) {
196-
if (condition_subgrad == FALSE) {
197-
inactive_term = subgrad_term
198-
linear_term = cbind(active_term,
199-
inactive_term)
200-
} else {
201-
linear_term = active_term
202-
}
203-
} else {
204-
linear_term = active_term
205-
}
206-
177+
linear_term = active_term
207178
offset_term = rep(0, p)
208179

209-
# if conditional_subgrad == FALSE, linear_term will have E columns
210-
# otherwise it will have p columns
211-
212180
internal_transform = list(linear_term=linear_term,
213181
offset_term=offset_term)
214182

@@ -239,39 +207,19 @@ randomizedLasso = function(X,
239207
return(D)
240208
}
241209

242-
# work out conditional density and save it as well
243-
244-
if (condition_subgrad == TRUE) {
245-
246-
constraints = matrix(0, length(active_set), 2)
247-
constraints[,2] = Inf
248-
249-
conditional_law = conditional_opt_transform(noise_scale,
250-
active_set,
251-
inactive_set,
252-
observed_subgrad,
253-
observed_raw,
254-
opt_transform$linear_term,
255-
opt_transform$offset_term,
256-
observed_opt_state)
257-
conditional_law$constraints = constraints
258-
law = conditional_law
259-
} else {
260-
261-
constraints = matrix(0, length(active_set), 2)
262-
constraints[,2] = Inf
263-
264-
subgrad_constraints = cbind(-lam[inactive_set], lam[inactive_set])
265-
constraints = rbind(constraints, subgrad_constraints)
210+
constraints = matrix(0, length(active_set), 2)
211+
constraints[,2] = Inf
266212

267-
full_law = list(sampling_transform=list(linear_term=opt_transform$linear_term,
268-
offset_term=opt_transform$offset_term + observed_raw),
269-
constraints=constraints,
270-
observed_opt_state=observed_opt_state,
271-
log_optimization_density=log_optimization_density,
272-
importance_transform=opt_transform)
273-
law = full_law
274-
}
213+
conditional_law = conditional_opt_transform(noise_scale,
214+
active_set,
215+
inactive_set,
216+
observed_subgrad,
217+
observed_raw,
218+
opt_transform$linear_term,
219+
opt_transform$offset_term,
220+
observed_opt_state)
221+
conditional_law$constraints = constraints
222+
law = conditional_law
275223

276224
return(list(X=X,
277225
y=y,
@@ -288,7 +236,6 @@ randomizedLasso = function(X,
288236
noise_scale=noise_scale,
289237
soln=result$soln,
290238
perturb=perturb_,
291-
condition_subgrad=condition_subgrad,
292239
ridge_term=ridge_term
293240
))
294241

@@ -302,30 +249,7 @@ sample_opt_variables = function(law, jump_scale, nsample=10000) {
302249
scale=jump_scale))
303250
}
304251

305-
# Carry out a linear decompositon of an internal
306-
# representation with respect to a target
307-
308-
# Returns an affine transform into raw coordinates (i.e. \omega or randomization coordinates)
309-
310-
linear_decomposition = function(observed_target,
311-
observed_internal,
312-
var_target,
313-
cov_target_internal,
314-
internal_transform) {
315-
var_target = as.matrix(var_target)
316-
if (nrow(var_target) == 1) {
317-
nuisance = observed_internal - cov_target_internal * observed_target / var_target
318-
target_linear = internal_transform$linear_term %*% cov_target_internal / var_target[1,1]
319-
} else {
320-
nuisance = observed_internal - cov_target_internal %*% solve(var_target) %*% observed_target
321-
target_linear = internal_transform$linear_term %*% cov_target_internal %*% solve(var_target)
322-
}
323-
target_offset = internal_transform$linear_term %*% nuisance + internal_transform$offset_term
324-
return(list(linear_term=target_linear,
325-
offset_term=target_offset))
326-
}
327-
328-
# XXX only for Gaussian so far
252+
# Gaussian importance weight
329253

330254
importance_weight = function(noise_scale,
331255
target_sample,
@@ -353,13 +277,6 @@ importance_weight = function(noise_scale,
353277
return(W)
354278
}
355279

356-
get_mean_cov = function(noise_scale, linear_term, offset_term){
357-
temp = solve(t(linear_term) %*% linear_term)
358-
cov = noise_scale^2*temp
359-
mean = temp %*% t(linear_term) %*% offset_term
360-
return(list(mean=mean, cov=cov))
361-
}
362-
363280
conditional_opt_transform = function(noise_scale,
364281
active_set,
365282
inactive_set,
@@ -413,7 +330,6 @@ conditional_opt_transform = function(noise_scale,
413330
cond_mean=cond_mean))
414331
}
415332

416-
417333
compute_target = function(rand_lasso_soln,
418334
type,
419335
sigma_est=1,
@@ -451,8 +367,7 @@ compute_target = function(rand_lasso_soln,
451367

452368
crosscov_target_internal=rbind(cov_target, matrix(0, nrow=p-nactive, ncol=nactive))
453369
}
454-
#print("signs")
455-
#print(rand_lasso_soln$sign_soln)
370+
456371
alternatives = c()
457372
for (i in 1:length(rand_lasso_soln$sign_soln)) {
458373
if (rand_lasso_soln$sign_soln[i] == 1) {
@@ -503,7 +418,7 @@ compute_target = function(rand_lasso_soln,
503418

504419
residuals = y-X%*%lasso.est
505420
scalar = 1 #sqrt(n) # JT: this is sigma?
506-
observed_target = lasso.est[active_set]+scalar*M_active %*% residuals
421+
observed_target = lasso.est[active_set] + scalar*M_active %*% residuals
507422
hat_matrix = X_active %*% solve(t(X_active) %*% X_active)
508423
crosscov_target_internal = sigma_est^2*rbind(M_active %*% hat_matrix, t(X_inactive) %*% t(M_inactive))
509424
}
@@ -649,16 +564,6 @@ randomizedLassoInf = function(rand_lasso_soln,
649564
observed_raw)
650565
log_reference_measure = log(reference_measure)
651566

652-
# alternative_measure = importance_weight(noise_scale,
653-
# t(as.matrix(target_sample) + 0.1 * sqrt(targets$cov_target[i,i])),
654-
# t(opt_samples),
655-
# importance_transform,
656-
# target_transform,
657-
# observed_raw)
658-
659-
660-
# sufficient_stat = (log(alternative_measure) - log_reference_measure) / (0.1 * sqrt(targets$cov_target[i,i]))
661-
662567
pivot = function(candidate){
663568
arg_ = candidate * sufficient_stat + log_reference_measure
664569
arg_ = arg_ - max(arg_)
@@ -676,7 +581,6 @@ randomizedLassoInf = function(rand_lasso_soln,
676581
}
677582

678583
if (construct_pvalues[i]==1){
679-
#print(alternatives[i])
680584
pvalues[i] = pivot(0)
681585
if (alternatives[i]=="two-sided"){
682586
pvalues[i] = 2*min(pvalues[i], 1-pvalues[i])

tests/randomized/test_randomized.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ test_randomized = function(seed=1, outfile=NULL, type="partial", loss="ls", lamb
4646
rand_lasso_soln = selectiveInference:::randomizedLasso(X,
4747
y,
4848
lambda*n,
49-
family=selectiveInference:::family_label(loss),
50-
condition_subgrad=TRUE)
49+
family=selectiveInference:::family_label(loss))
5150

5251
targets=selectiveInference:::compute_target(rand_lasso_soln, type=type, sigma_est=sigma_est)
5352

@@ -60,7 +59,7 @@ test_randomized = function(seed=1, outfile=NULL, type="partial", loss="ls", lamb
6059
active_vars=rand_lasso_soln$active_set
6160
cat("active_vars:",active_vars,"\n")
6261
pvalues = c(pvalues, PVS$pvalues)
63-
sel_intervals = cbind(sel_intervals, t(PVS$ci)) # matrix with two rows
62+
sel_intervals = rbind(sel_intervals, PVS$ci) # matrix with two rows
6463

6564

6665
if (length(pvalues)>0){
@@ -71,7 +70,7 @@ test_randomized = function(seed=1, outfile=NULL, type="partial", loss="ls", lamb
7170

7271
if (construct_ci && length(active_vars)>0){
7372

74-
sel_coverages=c(sel_coverages, selectiveInference:::compute_coverage(t(PVS$ci), beta[active_vars]))
73+
sel_coverages=c(sel_coverages, selectiveInference:::compute_coverage(PVS$ci, beta[active_vars]))
7574
sel_lengths=c(sel_lengths, as.vector(PVS$ci[,2]-PVS$ci[,1]))
7675
print(c("selective coverage:", mean(sel_coverages)))
7776
print(c("selective length mean:", mean(sel_lengths)))
@@ -105,6 +104,6 @@ test_randomized = function(seed=1, outfile=NULL, type="partial", loss="ls", lamb
105104
return(list(pvalues=pvalues))
106105
}
107106

108-
test_randomized()
107+
test_randomized(n=100, p=20, s=4)
109108

110109

0 commit comments

Comments
 (0)