Skip to content

Commit 4d376c4

Browse files
added R code decision for wide or not; R check passing
1 parent 636483b commit 4d376c4

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

selectiveInference/R/funs.fixed.R

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,24 @@ fixedLassoInf <- function(x, y, beta,
154154

155155
# Reorder so that active set S is first
156156
Xordered = Xint[,c(S,notS,recursive=T)]
157+
hsigmaS = 1/n*(t(XS)%*%XS) # hsigma[S,S]
158+
hsigmaSinv = solve(hsigmaS) # pinv(hsigmaS)
157159

158-
hsigma <- 1/n*(t(Xordered)%*%Xordered)
159-
hsigmaS <- 1/n*(t(XS)%*%XS) # hsigma[S,S]
160-
hsigmaSinv <- solve(hsigmaS) # pinv(hsigmaS)
160+
FS = rbind(diag(length(S)),matrix(0,pp-length(S),length(S)))
161+
GS = cbind(diag(length(S)),matrix(0,length(S),pp-length(S)))
161162

162-
# Approximate inverse covariance matrix for when (n < p) from lasso_Inference.R
163+
is_wide = n < (2 * p) # somewhat arbitrary decision -- it is really for when we don't want to form with pxp matrices
163164

164-
htheta = debiasingMatrix(hsigma, n, 1:length(S), verbose=FALSE, max_try=linesearch.try, warn_kkt=TRUE)
165+
# Approximate inverse covariance matrix for when (n < p) from lasso_Inference.R
166+
if (!is_wide) {
167+
hsigma = 1/n*(t(Xordered)%*%Xordered)
168+
htheta = debiasingMatrix(hsigma, is_wide, n, 1:length(S), verbose=FALSE, max_try=linesearch.try, warn_kkt=TRUE)
169+
ithetasigma = (GS-(htheta%*%hsigma))
170+
} else {
171+
htheta = debiasingMatrix(Xordered, is_wide, n, 1:length(S), verbose=FALSE, max_try=linesearch.try, warn_kkt=TRUE)
172+
ithetasigma = (GS-((htheta%*%t(Xordered)) %*% Xordered)/n)
173+
}
165174

166-
FS = rbind(diag(length(S)),matrix(0,pp-length(S),length(S)))
167-
GS = cbind(diag(length(S)),matrix(0,length(S),pp-length(S)))
168-
ithetasigma = (GS-(htheta%*%hsigma))
169-
# ithetasigma = (diag(pp) - (htheta%*%hsigma))
170-
171175
M <- (((htheta%*%t(Xordered))+ithetasigma%*%FS%*%hsigmaSinv%*%t(XS))/n)
172176
# vector which is offset for testing debiased beta's
173177
null_value <- (((ithetasigma%*%FS%*%hsigmaSinv)%*%sign(hbetaS))*lambda/n)
@@ -264,10 +268,11 @@ fixedLassoPoly =
264268
## Approximates inverse covariance matrix theta
265269
## using coordinate descent
266270

267-
debiasingMatrix = function(Sigma,
271+
debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n depending on is_wide
272+
is_wide,
268273
nsample,
269274
rows,
270-
verbose=FALSE,
275+
verbose=FALSE,
271276
mu=NULL, # starting value of mu
272277
linesearch=TRUE, # do a linesearch?
273278
scaling_factor=1.5, # multiplicative factor for linesearch
@@ -284,7 +289,7 @@ debiasingMatrix = function(Sigma,
284289
max_active = max(50, 0.3 * nsample)
285290
}
286291

287-
p = nrow(Sigma);
292+
p = ncol(Xinfo);
288293
M = matrix(0, length(rows), p);
289294

290295
if (is.null(mu)) {
@@ -302,12 +307,13 @@ debiasingMatrix = function(Sigma,
302307
print(paste(xperc,"% done",sep="")); }
303308
}
304309

305-
output = debiasingRow(Sigma,
310+
output = debiasingRow(Xinfo, # could be X or t(X) %*% X / n depending on is_wide
311+
is_wide,
306312
row,
307313
mu,
308314
linesearch=linesearch,
309315
scaling_factor=scaling_factor,
310-
max_active=max_active,
316+
max_active=max_active,
311317
max_try=max_try,
312318
warn_kkt=FALSE,
313319
max_iter=max_iter,
@@ -329,25 +335,26 @@ debiasingMatrix = function(Sigma,
329335
return(M)
330336
}
331337

332-
# Find one row of the debiasing matrix
338+
# Find one row of the debiasing matrix -- assuming X^TX/n is not too large -- i.e. X is tall
333339

334-
debiasingRow = function (Sigma,
340+
debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n depending on is_wide
341+
is_wide,
335342
row,
336343
mu,
337-
linesearch=TRUE, # do a linesearch?
344+
linesearch=TRUE, # do a linesearch?
338345
scaling_factor=1.2, # multiplicative factor for linesearch
339-
max_active=NULL, # how big can active set get?
346+
max_active=NULL, # how big can active set get?
340347
max_try=10, # how many steps in linesearch?
341348
warn_kkt=FALSE, # warn if KKT does not seem to be satisfied?
342349
max_iter=100, # how many iterations for each optimization problem
343350
kkt_tol=1.e-4, # tolerance for the KKT conditions
344351
objective_tol=1.e-8 # tolerance for relative decrease in objective
345352
) {
346353

347-
p = nrow(Sigma)
354+
p = ncol(Xinfo)
348355

349356
if (is.null(max_active)) {
350-
max_active = nrow(Sigma)
357+
max_active = nrow(Xinfo)
351358
}
352359

353360
# Initialize variables
@@ -371,18 +378,37 @@ debiasingRow = function (Sigma,
371378

372379
while (counter_idx < max_try) {
373380

374-
result = solve_QP(Sigma,
375-
mu,
376-
max_iter,
377-
soln,
378-
linear_func,
379-
gradient,
380-
ever_active,
381-
nactive,
382-
kkt_tol,
383-
objective_tol,
384-
max_active)
381+
if (!is_wide) {
382+
Sigma = Xinfo
383+
result = solve_QP(Sigma,
384+
mu,
385+
max_iter,
386+
soln,
387+
linear_func,
388+
gradient,
389+
ever_active,
390+
nactive,
391+
kkt_tol,
392+
objective_tol,
393+
max_active)
394+
} else {
395+
X = Xinfo
396+
n = nrow(X)
397+
Xsoln = rep(0, n)
398+
result = solve_QP_wide(X,
399+
mu,
400+
max_iter,
401+
soln,
402+
linear_func,
403+
gradient,
404+
Xsoln,
405+
ever_active,
406+
nactive,
407+
kkt_tol,
408+
objective_tol,
409+
max_active)
385410

411+
}
386412
iter = result$iter
387413

388414
# Logic for whether we should continue the line search
@@ -439,6 +465,7 @@ debiasingRow = function (Sigma,
439465

440466
}
441467

468+
442469
##############################
443470

444471
print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {

selectiveInference/man/debiasingMatrix.Rd

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Newton step from some consistent estimator (such as the LASSO)
1111
to find a debiased solution.
1212
}
1313
\usage{
14-
debiasingMatrix(Sigma,
14+
debiasingMatrix(Xinfo,
15+
is_wide,
1516
nsample,
1617
rows,
1718
verbose=FALSE,
@@ -26,8 +27,14 @@ debiasingMatrix(Sigma,
2627
objective_tol=1.e-8)
2728
}
2829
\arguments{
29-
\item{Sigma}{
30-
A symmetric non-negative definite matrix, often a cross-covariance matrix.
30+
\item{Xinfo}{
31+
Either a non-negative definite matrix S=t(X) %*% X / n or X itself. If
32+
is_wide is TRUE, then Xinfo should be X, otherwise it should be S.
33+
}
34+
\item{is_wide}{
35+
Are we solving for rows of the debiasing matrix assuming it is
36+
a wide matrix so that Xinfo=X and the non-negative definite
37+
matrix of interest is t(X) %*% X / nrow(X).
3138
}
3239
\item{nsample}{
3340
Number of samples used in forming the cross-covariance matrix.
@@ -102,7 +109,7 @@ n = 50
102109
p = 100
103110
X = matrix(rnorm(n * p), n, p)
104111
S = t(X) \%*\% X / n
105-
M = debiasingMatrix(S, n, c(1,3,5))
112+
M = debiasingMatrix(S, FALSE, n, c(1,3,5))
106113

107114
}
108115

selectiveInference/src/debias.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int check_KKT_qp(double *theta, /* current theta */
2121
double *gradient_ptr, /* nndef times theta + linear_func */
2222
int nrow, /* how many rows in nndef */
2323
double bound, /* Lagrange multipler for \ell_1 */
24-
double tol) /* precision for checking KKT conditions */
24+
double tol); /* precision for checking KKT conditions */
2525

2626
int solve_wide(double *X_ptr, /* Sqrt of non-neg def matrix -- X^TX/ncase = nndef */
2727
double *X_theta_ptr, /* Fitted values */
@@ -49,7 +49,7 @@ int check_KKT_wide(double *theta_ptr, /* current theta */
4949
int nfeature, /* how many columns in X */
5050
int ncase, /* how many rows in X */
5151
double bound, /* Lagrange multipler for \ell_1 */
52-
double tol) /* precision for checking KKT conditions */
52+
double tol); /* precision for checking KKT conditions */
5353

5454
void update_gradient_wide(double *gradient_ptr, /* X^TX/ncase times theta + linear_func */
5555
double *X_theta_ptr, /* Current fitted values */

0 commit comments

Comments
 (0)