Skip to content

Commit 6dfdd0d

Browse files
forming internal affine transform
1 parent b2de9d9 commit 6dfdd0d

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

selectiveInference/R/funs.randomized.R

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,61 @@ randomizedLASSO = function(X,
7171
sign_soln = sign(result$soln)
7272

7373
unpenalized = lam == 0
74-
active = !unpenalized * (sign_soln != 0)
75-
inactive = !unpenzlied * (sign_soln == 0)
74+
active = (!unpenalized) & (sign_soln != 0)
75+
inactive = (!unpenalized) & (sign_soln == 0)
7676

7777
unpenalized_set = which(unpenalized)
7878
active_set = which(active)
7979
inactive_set = which(inactive)
8080

81-
coef_term = t(X) %*% X[,c(unpenalized_set, # the coefficients
82-
active_set)]
81+
# affine transform for optimization variables
82+
83+
E = c(unpenalized_set, active_set)
84+
I = inactive_set
85+
X_E = X[,E]
86+
X_I = X[,I]
87+
L_E = t(X) %*% X[,E]
88+
89+
coef_term = L_E
8390
coef_term = coef_term %*% diag(c(rep(1, sum(unpenalized)), sign_soln[active])) # coefficients are non-negative
8491
coef_term[active,] = coef_term[active,] + ridge_term * diag(rep(1, sum(active))) # ridge term
8592

86-
subgrad_term = cbind(matrix(0, sum(inactive), sum(active) + sum(unpenalized)),
87-
diag(rep(1, sum(inactive))))
88-
linear_term = rbind(coef_term,
93+
subgrad_term = matrix(0, p, sum(inactive)) # for subgrad
94+
for (i in 1:sum(inactive)) {
95+
subgrad_term[inactive_set[i], i] = 1
96+
}
97+
98+
linear_term = cbind(coef_term,
8999
subgrad_term)
90100

91101
offset_term = rep(0, p)
92102
offset_term[active] = lam[active] * sign_soln[active]
93103

94-
104+
opt_transform = list(linear_term=linear_term,
105+
offset_term=offset_term)
106+
107+
# affine transform for internal (data) variables
108+
# for now just use parametric in terms of
109+
# (\bar{\beta}_E, X_{-E}^T(y-X_E\bar{\beta}_E)
110+
#
111+
# we have to reconstruct -X^TY from this pair
112+
#
113+
114+
active_term = -L_E # for \bar{\beta}_E
115+
116+
inactive_term = -subgrad_term
117+
linear_term = cbind(active_term,
118+
inactive_term)
119+
offset_term = rep(0, p)
120+
internal_transform = list(linear_term = linear_term,
121+
offset_term = offset_term)
122+
123+
return(list(active_set = active_set,
124+
inactive_set = inactive_set,
125+
unpenalized_set = unpenalized_set,
126+
sign_soln = sign_soln,
127+
opt_transform = opt_transform,
128+
internal_transform = internal_transform
129+
))
95130

96-
list(active_set = active_set,
97-
inactive_set = inactive_set,
98-
unpenalized_set = unpenalized_set,
99-
sign_soln = sign_soln)
100-
101-
102-
return(result)
103131
}

tests/randomized/test_randomized.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ test = function() {
88
lam = 20 / sqrt(n)
99
noise_scale = 0.01 * sqrt(n)
1010
ridge_term = .1 / sqrt(n)
11-
fit_randomized_lasso(X, y, lam, noise_scale, ridge_term)
11+
selectiveInference:::randomizedLASSO(X, y, lam, noise_scale, ridge_term)
1212
}
1313

14-
print(test())
14+
A=test()
15+
#print(test())

0 commit comments

Comments
 (0)