Skip to content

Commit 9c7bfcc

Browse files
without linesearch we know agree with Adel's code at fixed mu
1 parent 9f26844 commit 9c7bfcc

File tree

5 files changed

+75
-69
lines changed

5 files changed

+75
-69
lines changed

selectiveInference/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Maintainer: Rob Tibshirani <[email protected]>
99
Depends:
1010
glmnet,
1111
intervals,
12-
survival
12+
survival,
1313
Suggests:
1414
Rmpfr
1515
Description: New tools for post-selection inference, for use with forward

selectiveInference/NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ importFrom("stats", dnorm, lsfit, pexp, pnorm, predict,
4444
importFrom("stats", "coef", "df", "lm", "pf")
4545
importFrom("stats", "glm", "residuals", "vcov")
4646
importFrom("Rcpp", "sourceCpp")
47-
importFrom("distr", "Norm", "DExp")
47+

selectiveInference/R/funs.fixed.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n d
319319
nsample,
320320
rows,
321321
verbose=FALSE,
322-
mu=NULL, # starting value of mu
322+
bound=NULL, # starting value of bound
323323
linesearch=TRUE, # do a linesearch?
324324
scaling_factor=1.5, # multiplicative factor for linesearch
325325
max_active=NULL, # how big can active set get?
@@ -342,8 +342,8 @@ debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n d
342342
p = ncol(Xinfo);
343343
M = matrix(0, length(rows), p);
344344

345-
if (is.null(mu)) {
346-
mu = (1/sqrt(nsample)) * qnorm(1-(0.1/(p^2)))
345+
if (is.null(bound)) {
346+
bound = (1/sqrt(nsample)) * qnorm(1-(0.1/(p^2)))
347347
}
348348

349349
xperc = 0;
@@ -359,7 +359,7 @@ debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n d
359359
output = debiasingRow(Xinfo, # could be X or t(X) %*% X / n depending on is_wide
360360
is_wide,
361361
row,
362-
mu,
362+
bound,
363363
linesearch=linesearch,
364364
scaling_factor=scaling_factor,
365365
max_active=max_active,
@@ -393,7 +393,7 @@ debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n d
393393
debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n depending on is_wide
394394
is_wide,
395395
row,
396-
mu,
396+
bound,
397397
linesearch=TRUE, # do a linesearch?
398398
scaling_factor=1.5, # multiplicative factor for linesearch
399399
max_active=NULL, # how big can active set get?
@@ -414,9 +414,11 @@ debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n dep
414414
max_active = min(nrow(Xinfo), ncol(Xinfo))
415415
}
416416

417+
417418
# Initialize variables
418419

419420
soln = rep(0, p)
421+
soln = as.numeric(soln)
420422
ever_active = rep(0, p)
421423
ever_active[1] = row # 1-based
422424
ever_active = as.integer(ever_active)
@@ -432,13 +434,16 @@ debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n dep
432434

433435
last_output = NULL
434436

435-
Xsoln = rep(0, n)
437+
if (is_wide) {
438+
n = nrow(Xinfo)
439+
Xsoln = as.numeric(rep(0, n))
440+
}
436441

437442
while (counter_idx < max_try) {
438443

439444
if (!is_wide) {
440445
result = solve_QP(Xinfo, # this is non-neg-def matrix
441-
mu,
446+
bound,
442447
max_iter,
443448
soln,
444449
linear_func,
@@ -453,9 +458,9 @@ debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n dep
453458
kkt_stop,
454459
parameter_stop)
455460
} else {
456-
result = solve_QP_wide(Xinfo, # this is a design matrix
457-
rep(mu, p), # vector of Lagrange multipliers
458-
0, # ridge_term
461+
result = solve_QP_wide(Xinfo, # this is a design matrix
462+
as.numeric(rep(bound, p)), # vector of Lagrange multipliers
463+
0, # ridge_term
459464
max_iter,
460465
soln,
461466
linear_func,
@@ -493,13 +498,13 @@ debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n dep
493498
if ((iter < (max_iter+1)) && (counter_idx > 1)) {
494499
break; # we've found a feasible point and solved the problem
495500
}
496-
mu = mu * scaling_factor;
501+
bound = bound * scaling_factor;
497502
} else { # trying to drop the bound parameter further
498503
if ((iter == (max_iter + 1)) && (counter_idx > 1)) {
499504
result = last_output; # problem seems infeasible because we didn't solve it
500505
break; # so we revert to previously found solution
501506
}
502-
mu = mu / scaling_factor;
507+
bound = bound / scaling_factor;
503508
}
504509

505510
# If the active set has grown to a certain size

selectiveInference/R/funs.randomized.R

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
#
44
# min 1/2 || y - \beta_0 - X \beta ||_2^2 + \lambda || \beta ||_1 - \omega^T\beta + \frac{\epsilon}{2} \|\beta\|^2_2
55

6-
fit_randomized_lasso = function(X,
7-
y,
8-
lam,
9-
noise_scale,
10-
ridge_term,
11-
noise_type=c('gaussian', 'laplace'),
12-
max_iter=100, # how many iterations for each optimization problem
13-
kkt_tol=1.e-4, # tolerance for the KKT conditions
14-
parameter_tol=1.e-8, # tolerance for relative convergence of parameter
15-
objective_tol=1.e-8, # tolerance for relative decrease in objective
16-
objective_stop=FALSE,
17-
kkt_stop=TRUE,
18-
param_stop=TRUE)
6+
randomizedLASSO = function(X,
7+
y,
8+
lam,
9+
noise_scale,
10+
ridge_term,
11+
noise_type=c('gaussian', 'laplace'),
12+
max_iter=100, # how many iterations for each optimization problem
13+
kkt_tol=1.e-4, # tolerance for the KKT conditions
14+
parameter_tol=1.e-8, # tolerance for relative convergence of parameter
15+
objective_tol=1.e-8, # tolerance for relative decrease in objective
16+
objective_stop=FALSE,
17+
kkt_stop=TRUE,
18+
param_stop=TRUE)
1919
{
2020

2121
n = nrow(X); p = ncol(X)
@@ -24,12 +24,11 @@ fit_randomized_lasso = function(X,
2424

2525
if (noise_scale > 0) {
2626
if (noise_type == 'gaussian') {
27-
D = Norm(mean=0, sd=noise_scale)
27+
perturb_ = rnorm(p) * noise_scale
2828
}
2929
else if (noise_type == 'laplace') {
30-
D = DExp(rate = 1 / noise_scale) # D is a Laplace distribution with rate = 1.
30+
perturb_ = rexp(p) * (2 * rbinom(p, 1, 0.5) - 1) * noise_scale
3131
}
32-
perturb_ = distr::r(D)(p)
3332
} else {
3433
perturb_ = rep(0, p)
3534
}

tests/test_debiasing.R

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ library(selectiveInference)
22

33

44
## Approximates inverse covariance matrix theta
5-
InverseLinfty <- function(sigma, n, resol=1.5, mu=NULL, maxiter=50, threshold=1e-10, verbose = TRUE) {
5+
InverseLinfty <- function(sigma, n, resol=1.5, bound=NULL, maxiter=50, threshold=1e-10, verbose = TRUE) {
66
isgiven <- 1;
7-
if (is.null(mu)){
7+
if (is.null(bound)){
88
isgiven <- 0;
99
}
1010

@@ -19,43 +19,43 @@ InverseLinfty <- function(sigma, n, resol=1.5, mu=NULL, maxiter=50, threshold=1e
1919
print(paste(xperc,"% done",sep="")); }
2020
}
2121
if (isgiven==0){
22-
mu <- (1/sqrt(n)) * qnorm(1-(0.1/(p^2)));
22+
bound <- (1/sqrt(n)) * qnorm(1-(0.1/(p^2)));
2323
}
24-
mu.stop <- 0;
24+
bound.stop <- 0;
2525
try.no <- 1;
2626
incr <- 0;
27-
while ((mu.stop != 1)&&(try.no<10)){
27+
while ((bound.stop != 1)&&(try.no<10)){
2828
last.beta <- beta
29-
output <- InverseLinftyOneRow(sigma, i, mu, maxiter=maxiter, threshold=threshold)
29+
output <- InverseLinftyOneRow(sigma, i, bound, maxiter=maxiter, threshold=threshold)
3030
beta <- output$optsol
3131
iter <- output$iter
3232
if (isgiven==1){
33-
mu.stop <- 1
33+
bound.stop <- 1
3434
}
3535
else{
3636
if (try.no==1){
3737
if (iter == (maxiter+1)){
3838
incr <- 1;
39-
mu <- mu*resol;
39+
bound <- bound*resol;
4040
} else {
4141
incr <- 0;
42-
mu <- mu/resol;
42+
bound <- bound/resol;
4343
}
4444
}
4545
if (try.no > 1){
4646
if ((incr == 1)&&(iter == (maxiter+1))){
47-
mu <- mu*resol;
47+
bound <- bound*resol;
4848
}
4949
if ((incr == 1)&&(iter < (maxiter+1))){
50-
mu.stop <- 1;
50+
bound.stop <- 1;
5151
}
5252
if ((incr == 0)&&(iter < (maxiter+1))){
53-
mu <- mu/resol;
53+
bound <- bound/resol;
5454
}
5555
if ((incr == 0)&&(iter == (maxiter+1))){
56-
mu <- mu*resol;
56+
bound <- bound*resol;
5757
beta <- last.beta;
58-
mu.stop <- 1;
58+
bound.stop <- 1;
5959
}
6060
}
6161
}
@@ -66,14 +66,14 @@ InverseLinfty <- function(sigma, n, resol=1.5, mu=NULL, maxiter=50, threshold=1e
6666
return(M)
6767
}
6868

69-
InverseLinftyOneRow <- function ( sigma, i, mu, maxiter=50, threshold=1e-10) {
69+
InverseLinftyOneRow <- function ( sigma, i, bound, maxiter=50, threshold=1e-10) {
7070
p <- nrow(sigma);
7171
rho <- max(abs(sigma[i,-i])) / sigma[i,i];
72-
mu0 <- rho/(1+rho);
72+
bound0 <- rho/(1+rho);
7373
beta <- rep(0,p);
7474

75-
#if (mu >= mu0){
76-
# beta[i] <- (1-mu0)/sigma[i,i];
75+
#if (bound >= bound0){
76+
# beta[i] <- (1-bound0)/sigma[i,i];
7777
# returnlist <- list("optsol" = beta, "iter" = 0);
7878
# return(returnlist);
7979
#}
@@ -82,7 +82,7 @@ InverseLinftyOneRow <- function ( sigma, i, mu, maxiter=50, threshold=1e-10) {
8282
last.norm2 <- 1;
8383
iter <- 1;
8484
iter.old <- 1;
85-
beta[i] <- (1-mu0)/sigma[i,i];
85+
beta[i] <- (1-bound0)/sigma[i,i];
8686
beta.old <- beta;
8787
sigma.tilde <- sigma;
8888
diag(sigma.tilde) <- 0;
@@ -95,7 +95,7 @@ InverseLinftyOneRow <- function ( sigma, i, mu, maxiter=50, threshold=1e-10) {
9595
v <- vs[j];
9696
if (j==i)
9797
v <- v+1;
98-
beta[j] <- SoftThreshold(v,mu)/sigma[j,j];
98+
beta[j] <- SoftThreshold(v,bound)/sigma[j,j];
9999
if (oldval != beta[j]){
100100
vs <- vs + (oldval-beta[j])*sigma.tilde[,j];
101101
}
@@ -112,7 +112,7 @@ InverseLinftyOneRow <- function ( sigma, i, mu, maxiter=50, threshold=1e-10) {
112112
# vs <- -sigma.tilde%*%beta;
113113
}
114114

115-
# print(c(iter, maxiter, diff.norm2, threshold * last.norm2, threshold, mu))
115+
# print(c(iter, maxiter, diff.norm2, threshold * last.norm2, threshold, bound))
116116

117117
}
118118

@@ -142,19 +142,21 @@ n = 100; p = 50
142142
X = matrix(rnorm(n * p), n, p)
143143
S = t(X) %*% X / n
144144

145-
mu = 7.791408e-02
145+
debiasing_bound = 7.791408e-02
146146

147147
tol = 1.e-12
148148

149-
rows = c(1:2)
150-
A1 = debiasingMatrix(S, FALSE, n, rows, mu=mu, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol)
151-
A2 = debiasingMatrix(S / n, FALSE, n, rows, mu=mu, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol)
149+
rows = as.integer(c(1:2))
150+
print('here')
151+
print(rows)
152+
A1 = debiasingMatrix(S, FALSE, n, rows, bound=debiasing_bound, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol, linesearch=FALSE)
152153

153-
B1 = debiasingMatrix(X, TRUE, n, rows, mu=mu, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol)
154-
B2 = debiasingMatrix(X / sqrt(n), TRUE, n, rows, mu=mu, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol)
154+
A2 = debiasingMatrix(S / n, FALSE, n, rows, bound=debiasing_bound, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol, linesearch=FALSE)
155+
B1 = debiasingMatrix(X, TRUE, n, rows, bound=debiasing_bound, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol, linesearch=FALSE)
156+
B2 = debiasingMatrix(X / sqrt(n), TRUE, n, rows, bound=debiasing_bound, max_iter=1000, kkt_tol=tol, objective_tol=tol, parameter_tol=tol, linesearch=FALSE)
155157

156-
C1 = InverseLinfty(S, n, mu=mu, maxiter=1000)[rows,]
157-
C2 = InverseLinfty(S / n, n, mu=mu, maxiter=1000)[rows,]
158+
C1 = InverseLinfty(S, n, bound=debiasing_bound, maxiter=1000)[rows,]
159+
C2 = InverseLinfty(S / n, n, bound=debiasing_bound, maxiter=1000)[rows,]
158160

159161
par(mfrow=c(2,3))
160162

@@ -172,30 +174,30 @@ print(c('C', sum(C1[1,] == 0)))
172174

173175
## Are our points feasible
174176

175-
feasibility = function(S, soln, j, mu) {
177+
feasibility = function(S, soln, j, debiasing_bound) {
176178
p = nrow(S)
177179
E = rep(0, p)
178180
E[j] = 1
179181
G = S %*% soln - E
180-
return(c(max(abs(G)), mu))
182+
return(c(max(abs(G)), debiasing_bound))
181183
}
182184

183-
print(c('feasibility A', feasibility(S, A1[1,], 1, mu)))
184-
print(c('feasibility B', feasibility(S, B1[1,], 1, mu)))
185-
print(c('feasibility C', feasibility(S, C1[1,], 1, mu)))
185+
print(c('feasibility A', feasibility(S, A1[1,], 1, debiasing_bound)))
186+
print(c('feasibility B', feasibility(S, B1[1,], 1, debiasing_bound)))
187+
print(c('feasibility C', feasibility(S, C1[1,], 1, debiasing_bound)))
186188

187-
active_KKT = function(S, soln, j, mu) {
189+
active_KKT = function(S, soln, j, debiasing_bound) {
188190
p = nrow(S)
189191
E = rep(0, p)
190192
E[j] = 1
191193
G = S %*% soln - E
192194
print(which(soln != 0))
193195
print(G[j])
194-
return(c(G[soln != 0] * sign(soln)[soln != 0], mu))
196+
return(c(G[soln != 0] * sign(soln)[soln != 0], debiasing_bound))
195197
}
196198

197-
print(c('active_KKT A', active_KKT(S, A1[1,], 1, mu)))
198-
print(c('active_KKT B', active_KKT(S, B1[1,], 1, mu)))
199-
print(c('active_KKT C', active_KKT(S, C1[1,], 1, mu)))
199+
print(c('active_KKT A', active_KKT(S, A1[1,], 1, debiasing_bound)))
200+
print(c('active_KKT B', active_KKT(S, B1[1,], 1, debiasing_bound)))
201+
print(c('active_KKT C', active_KKT(S, C1[1,], 1, debiasing_bound)))
200202

201203

0 commit comments

Comments
 (0)