Skip to content

Commit 93a9602

Browse files
RF: ever_active is now 1-based indices to be more R like
1 parent 8c956f8 commit 93a9602

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

selectiveInference/R/funs.fixed.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ InverseLinftyOneRow <- function (Sigma, i, mu, maxiter=50, soln_result=NULL, kkt
348348
if (is.null(soln_result)) {
349349
soln = rep(0, p)
350350
ever_active = rep(0, p)
351-
ever_active[1] = i-1 # 0-based
351+
ever_active[1] = i # 1-based
352352
ever_active = as.integer(ever_active)
353353
nactive = as.integer(1)
354354
if (use_QP) {
@@ -373,7 +373,7 @@ InverseLinftyOneRow <- function (Sigma, i, mu, maxiter=50, soln_result=NULL, kkt
373373
if (use_QP) {
374374
result = solve_QP(Sigma, mu, maxiter, soln, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol)
375375
} else {
376-
result = find_one_row_debiasingM(Sigma, i-1, mu, maxiter, soln, gradient, ever_active, nactive, kkt_tol, objective_tol) # C function uses 0-based indexing
376+
result = find_one_row_debiasingM(Sigma, i, mu, maxiter, soln, gradient, ever_active, nactive, kkt_tol, objective_tol) # C function uses 1-based indexing for active set
377377
}
378378

379379
# Check feasibility

selectiveInference/src/debias.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
// Update one coordinate
1212

1313
double objective(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
14-
int *ever_active_ptr, /* Ever active set: 0-based */
15-
int *nactive_ptr, /* Size of ever active set */
16-
int nrow, /* how many rows in Sigma */
17-
int row, /* which row: 0-based */
18-
double bound, /* Lagrange multipler for \ell_1 */
19-
double *theta) /* current value */
14+
int *ever_active_ptr, /* Ever active set: 1-based */
15+
int *nactive_ptr, /* Size of ever active set */
16+
int nrow, /* how many rows in Sigma */
17+
int row, /* which row: 1-based */
18+
double bound, /* Lagrange multipler for \ell_1 */
19+
double *theta) /* current value */
2020
{
2121
int irow, icol;
2222
double value = 0;
@@ -32,13 +32,13 @@ double objective(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
3232
for (irow=0; irow<nactive; irow++) {
3333

3434
active_row_ptr = ((int *) ever_active_ptr + irow);
35-
active_row = *active_row_ptr;
35+
active_row = *active_row_ptr - 1; // Ever-active set is 1-based
3636
theta_row_ptr = ((double *) theta + active_row);
3737

3838
for (icol=0; icol<nactive; icol++) {
3939

4040
active_col_ptr = ((int *) ever_active_ptr + icol);
41-
active_col = *active_col_ptr;
41+
active_col = *active_col_ptr - 1; // Ever-active set is 1-based
4242
theta_col_ptr = ((double *) theta + active_col);
4343

4444
Sigma_ptr_tmp = ((double *) Sigma_ptr + nrow * active_col + active_row); // Matrices are column-major order
@@ -48,14 +48,15 @@ double objective(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
4848
value = value + bound * fabs((*theta_row_ptr)); // the \ell_1 term
4949
}
5050

51-
theta_row_ptr = ((double *) theta + row);
51+
theta_row_ptr = ((double *) theta + (row - 1)); // row is 1-based index
5252
value -= (*theta_row_ptr); // the elementary basis vector term
5353

5454
return(value);
5555
}
5656

5757
// Check if active and add it to active list if necessary
58-
58+
// Ever active set is stored as 1-based indices
59+
// coord is 0-based
5960
int update_ever_active(int coord,
6061
int *ever_active_ptr,
6162
int *nactive_ptr) {
@@ -67,7 +68,7 @@ int update_ever_active(int coord,
6768
for (iactive=0; iactive<nactive; iactive++) {
6869
ever_active_ptr_tmp = ((int *) ever_active_ptr + iactive);
6970
active_var = (*ever_active_ptr_tmp);
70-
if (active_var == coord) {
71+
if (active_var - 1 == coord) { // Indices in active set are 1-based
7172
return(1);
7273
}
7374
}
@@ -79,7 +80,7 @@ int update_ever_active(int coord,
7980
// number of active variables
8081

8182
ever_active_ptr_tmp = ((int *) ever_active_ptr + *nactive_ptr);
82-
*ever_active_ptr_tmp = coord;
83+
*ever_active_ptr_tmp = coord + 1; // Indices are 1-based
8384
*nactive_ptr += 1;
8485

8586
return(0);
@@ -88,7 +89,7 @@ int update_ever_active(int coord,
8889
int check_KKT(double *theta, /* current theta */
8990
double *gradient_ptr, /* Sigma times theta */
9091
int nrow, /* how many rows in Sigma */
91-
int row, /* which row: 0-based */
92+
int row, /* which row: 1-based */
9293
double bound, /* Lagrange multipler for \ell_1 */
9394
double tol) /* precision for checking KKT conditions */
9495
{
@@ -108,7 +109,7 @@ int check_KKT(double *theta, /* current theta */
108109

109110
// For the basis vector
110111

111-
if (row == irow) {
112+
if (row - 1 == irow) { // Row is a 1-based index
112113
gradient -= 1;
113114
}
114115

@@ -139,7 +140,7 @@ double update_one_coord(double *Sigma_ptr, /* A covariance matrix: X^T
139140
int nrow, /* How many rows in Sigma */
140141
double bound, /* feasibility parameter */
141142
double *theta, /* current value */
142-
int row, /* which row: 0-based */
143+
int row, /* which row: 1-based */
143144
int coord, /* which coordinate to update: 0-based */
144145
int is_active) /* Is this part of ever_active */
145146
{
@@ -156,8 +157,6 @@ double update_one_coord(double *Sigma_ptr, /* A covariance matrix: X^T
156157
double *quadratic_ptr = ((double *) Sigma_diag_ptr + coord);
157158
double quadratic_term = *quadratic_ptr;
158159

159-
// int *ever_active_ptr_tmp;
160-
161160
gradient_ptr_tmp = ((double *) gradient_ptr + coord);
162161
linear_term = *gradient_ptr_tmp;
163162

@@ -167,9 +166,10 @@ double update_one_coord(double *Sigma_ptr, /* A covariance matrix: X^T
167166
// The coord entry of gradient_ptr term has a diagonal term in it:
168167
// Sigma[coord, coord] * theta[coord]
169168
// This removes it.
169+
170170
linear_term -= quadratic_term * old_value;
171171

172-
if (row == coord) {
172+
if (row - 1 == coord) { // Row is 1-based
173173
linear_term -= 1;
174174
}
175175

@@ -220,13 +220,13 @@ double update_one_coord(double *Sigma_ptr, /* A covariance matrix: X^T
220220
int find_one_row_(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
221221
double *Sigma_diag_ptr, /* Diagonal entry of covariance matrix */
222222
double *gradient_ptr, /* Sigma times theta */
223-
int *ever_active_ptr, /* Ever active set: 0-based */
223+
int *ever_active_ptr, /* Ever active set: 1-based */
224224
int *nactive_ptr, /* Size of ever active set */
225225
int nrow, /* How many rows in Sigma */
226226
double bound, /* feasibility parameter */
227227
double *theta, /* current value */
228228
int maxiter, /* how many iterations */
229-
int row, /* which coordinate to update: 0-based */
229+
int row, /* which coordinate to solve: 1-based */
230230
double kkt_tol, /* precision for checking KKT conditions */
231231
double objective_tol) /* precision for checking relative decrease in objective value */
232232
{
@@ -267,7 +267,7 @@ int find_one_row_(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
267267
bound,
268268
theta,
269269
row,
270-
*active_ptr,
270+
*active_ptr-1, // Ever active set is 1-based
271271
1);
272272
active_ptr++;
273273
}

selectiveInference/src/quadratic_program.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ double objective_qp(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
3434
for (irow=0; irow<nactive; irow++) {
3535

3636
active_row_ptr = ((int *) ever_active_ptr + irow);
37-
active_row = *active_row_ptr;
37+
active_row = *active_row_ptr - 1; // Ever-active is 1-based
3838
theta_row_ptr = ((double *) theta + active_row);
3939

4040
for (icol=0; icol<nactive; icol++) {
4141

4242
active_col_ptr = ((int *) ever_active_ptr + icol);
43-
active_col = *active_col_ptr;
43+
active_col = *active_col_ptr - 1; // Ever-active is 1-based
4444
theta_col_ptr = ((double *) theta + active_col);
4545

4646
Sigma_ptr_tmp = ((double *) Sigma_ptr + nrow * active_col + active_row); // Matrices are column-major order
@@ -59,6 +59,8 @@ double objective_qp(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
5959
return(value);
6060
}
6161

62+
// Ever-active is 1-based
63+
// coord is 0-based
6264
int update_ever_active_qp(int coord,
6365
int *ever_active_ptr,
6466
int *nactive_ptr) {
@@ -70,7 +72,7 @@ int update_ever_active_qp(int coord,
7072
for (iactive=0; iactive<nactive; iactive++) {
7173
ever_active_ptr_tmp = ((int *) ever_active_ptr + iactive);
7274
active_var = *ever_active_ptr_tmp;
73-
if (active_var == coord) {
75+
if (active_var - 1 == coord) { // Ever-active is 1-based
7476
return(1);
7577
}
7678
}
@@ -82,7 +84,7 @@ int update_ever_active_qp(int coord,
8284
// number of active variables
8385

8486
ever_active_ptr_tmp = ((int *) ever_active_ptr + *nactive_ptr);
85-
*ever_active_ptr_tmp = coord;
87+
*ever_active_ptr_tmp = coord + 1; // Ever-active is 1-based
8688
*nactive_ptr += 1;
8789

8890
return(0);
@@ -130,13 +132,13 @@ double update_one_coord_qp(double *Sigma_ptr, /* A covariance matrix:
130132
double *linear_func_ptr, /* Linear term in objective */
131133
double *Sigma_diag_ptr, /* Diagonal entries of Sigma */
132134
double *gradient_ptr, /* Sigma times theta */
133-
int *ever_active_ptr, /* Ever active set: 0-based */
135+
int *ever_active_ptr, /* Ever active set: 1-based */
134136
int *nactive_ptr, /* Size of ever active set */
135137
int nrow, /* How many rows in Sigma */
136138
double bound, /* feasibility parameter */
137139
double *theta, /* current value */
138140
int coord, /* which coordinate to update: 0-based */
139-
int is_active) /* Is this part of ever_active */
141+
int is_active) /* Is this coord in ever_active */
140142
{
141143

142144
double delta;
@@ -211,7 +213,7 @@ int solve_qp(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
211213
double *linear_func_ptr, /* Linear term in objective */
212214
double *Sigma_diag_ptr, /* Diagonal entry of covariance matrix */
213215
double *gradient_ptr, /* Sigma times theta */
214-
int *ever_active_ptr, /* Ever active set: 0-based */
216+
int *ever_active_ptr, /* Ever active set: 1-based */
215217
int *nactive_ptr, /* Size of ever active set */
216218
int nrow, /* How many rows in Sigma */
217219
double bound, /* feasibility parameter */
@@ -259,7 +261,7 @@ int solve_qp(double *Sigma_ptr, /* A covariance matrix: X^TX/n */
259261
nrow,
260262
bound,
261263
theta,
262-
*active_ptr,
264+
*active_ptr - 1, // Ever-active is 1-based
263265
1);
264266
active_ptr++;
265267
}

0 commit comments

Comments
 (0)