Skip to content

Commit c26a06b

Browse files
WIP: running but not agreeing with other solver
1 parent ce0dc07 commit c26a06b

File tree

4 files changed

+71
-36
lines changed

4 files changed

+71
-36
lines changed

selectiveInference/R/RcppExports.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# This file was generated by Rcpp::compileAttributes
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

44
solve_QP <- function(Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active) {
5-
.Call('selectiveInference_solve_QP', PACKAGE = 'selectiveInference', Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active)
5+
.Call('_selectiveInference_solve_QP', PACKAGE = 'selectiveInference', Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active)
66
}
77

88
solve_QP_wide <- function(X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active) {
9-
.Call('selectiveInference_solve_QP_wide', PACKAGE = 'selectiveInference', X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active)
9+
.Call('_selectiveInference_solve_QP_wide', PACKAGE = 'selectiveInference', X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active)
1010
}
1111

1212
update1_ <- function(Q2, w, m, k) {
13-
.Call('selectiveInference_update1_', PACKAGE = 'selectiveInference', Q2, w, m, k)
13+
.Call('_selectiveInference_update1_', PACKAGE = 'selectiveInference', Q2, w, m, k)
1414
}
1515

1616
downdate1_ <- function(Q1, R, j0, m, n) {
17-
.Call('selectiveInference_downdate1_', PACKAGE = 'selectiveInference', Q1, R, j0, m, n)
17+
.Call('_selectiveInference_downdate1_', PACKAGE = 'selectiveInference', Q1, R, j0, m, n)
1818
}
1919

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by Rcpp::compileAttributes
1+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

44
#include <Rcpp.h>
@@ -7,10 +7,10 @@ using namespace Rcpp;
77

88
// solve_QP
99
Rcpp::List solve_QP(Rcpp::NumericMatrix Sigma, double bound, int maxiter, Rcpp::NumericVector theta, Rcpp::NumericVector linear_func, Rcpp::NumericVector gradient, Rcpp::IntegerVector ever_active, Rcpp::IntegerVector nactive, double kkt_tol, double objective_tol, int max_active);
10-
RcppExport SEXP selectiveInference_solve_QP(SEXP SigmaSEXP, SEXP boundSEXP, SEXP maxiterSEXP, SEXP thetaSEXP, SEXP linear_funcSEXP, SEXP gradientSEXP, SEXP ever_activeSEXP, SEXP nactiveSEXP, SEXP kkt_tolSEXP, SEXP objective_tolSEXP, SEXP max_activeSEXP) {
10+
RcppExport SEXP _selectiveInference_solve_QP(SEXP SigmaSEXP, SEXP boundSEXP, SEXP maxiterSEXP, SEXP thetaSEXP, SEXP linear_funcSEXP, SEXP gradientSEXP, SEXP ever_activeSEXP, SEXP nactiveSEXP, SEXP kkt_tolSEXP, SEXP objective_tolSEXP, SEXP max_activeSEXP) {
1111
BEGIN_RCPP
12-
Rcpp::RObject __result;
13-
Rcpp::RNGScope __rngScope;
12+
Rcpp::RObject rcpp_result_gen;
13+
Rcpp::RNGScope rcpp_rngScope_gen;
1414
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type Sigma(SigmaSEXP);
1515
Rcpp::traits::input_parameter< double >::type bound(boundSEXP);
1616
Rcpp::traits::input_parameter< int >::type maxiter(maxiterSEXP);
@@ -22,16 +22,16 @@ BEGIN_RCPP
2222
Rcpp::traits::input_parameter< double >::type kkt_tol(kkt_tolSEXP);
2323
Rcpp::traits::input_parameter< double >::type objective_tol(objective_tolSEXP);
2424
Rcpp::traits::input_parameter< int >::type max_active(max_activeSEXP);
25-
__result = Rcpp::wrap(solve_QP(Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active));
26-
return __result;
25+
rcpp_result_gen = Rcpp::wrap(solve_QP(Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active));
26+
return rcpp_result_gen;
2727
END_RCPP
2828
}
2929
// solve_QP_wide
3030
Rcpp::List solve_QP_wide(Rcpp::NumericMatrix X, double bound, int maxiter, Rcpp::NumericVector theta, Rcpp::NumericVector linear_func, Rcpp::NumericVector gradient, Rcpp::NumericVector X_theta, Rcpp::IntegerVector ever_active, Rcpp::IntegerVector nactive, double kkt_tol, double objective_tol, int max_active);
31-
RcppExport SEXP selectiveInference_solve_QP_wide(SEXP XSEXP, SEXP boundSEXP, SEXP maxiterSEXP, SEXP thetaSEXP, SEXP linear_funcSEXP, SEXP gradientSEXP, SEXP X_thetaSEXP, SEXP ever_activeSEXP, SEXP nactiveSEXP, SEXP kkt_tolSEXP, SEXP objective_tolSEXP, SEXP max_activeSEXP) {
31+
RcppExport SEXP _selectiveInference_solve_QP_wide(SEXP XSEXP, SEXP boundSEXP, SEXP maxiterSEXP, SEXP thetaSEXP, SEXP linear_funcSEXP, SEXP gradientSEXP, SEXP X_thetaSEXP, SEXP ever_activeSEXP, SEXP nactiveSEXP, SEXP kkt_tolSEXP, SEXP objective_tolSEXP, SEXP max_activeSEXP) {
3232
BEGIN_RCPP
33-
Rcpp::RObject __result;
34-
Rcpp::RNGScope __rngScope;
33+
Rcpp::RObject rcpp_result_gen;
34+
Rcpp::RNGScope rcpp_rngScope_gen;
3535
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type X(XSEXP);
3636
Rcpp::traits::input_parameter< double >::type bound(boundSEXP);
3737
Rcpp::traits::input_parameter< int >::type maxiter(maxiterSEXP);
@@ -44,36 +44,49 @@ BEGIN_RCPP
4444
Rcpp::traits::input_parameter< double >::type kkt_tol(kkt_tolSEXP);
4545
Rcpp::traits::input_parameter< double >::type objective_tol(objective_tolSEXP);
4646
Rcpp::traits::input_parameter< int >::type max_active(max_activeSEXP);
47-
__result = Rcpp::wrap(solve_QP_wide(X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active));
48-
return __result;
47+
rcpp_result_gen = Rcpp::wrap(solve_QP_wide(X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active));
48+
return rcpp_result_gen;
4949
END_RCPP
5050
}
5151
// update1_
5252
Rcpp::List update1_(Rcpp::NumericMatrix Q2, Rcpp::NumericVector w, int m, int k);
53-
RcppExport SEXP selectiveInference_update1_(SEXP Q2SEXP, SEXP wSEXP, SEXP mSEXP, SEXP kSEXP) {
53+
RcppExport SEXP _selectiveInference_update1_(SEXP Q2SEXP, SEXP wSEXP, SEXP mSEXP, SEXP kSEXP) {
5454
BEGIN_RCPP
55-
Rcpp::RObject __result;
56-
Rcpp::RNGScope __rngScope;
55+
Rcpp::RObject rcpp_result_gen;
56+
Rcpp::RNGScope rcpp_rngScope_gen;
5757
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type Q2(Q2SEXP);
5858
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type w(wSEXP);
5959
Rcpp::traits::input_parameter< int >::type m(mSEXP);
6060
Rcpp::traits::input_parameter< int >::type k(kSEXP);
61-
__result = Rcpp::wrap(update1_(Q2, w, m, k));
62-
return __result;
61+
rcpp_result_gen = Rcpp::wrap(update1_(Q2, w, m, k));
62+
return rcpp_result_gen;
6363
END_RCPP
6464
}
6565
// downdate1_
6666
Rcpp::List downdate1_(Rcpp::NumericMatrix Q1, Rcpp::NumericMatrix R, int j0, int m, int n);
67-
RcppExport SEXP selectiveInference_downdate1_(SEXP Q1SEXP, SEXP RSEXP, SEXP j0SEXP, SEXP mSEXP, SEXP nSEXP) {
67+
RcppExport SEXP _selectiveInference_downdate1_(SEXP Q1SEXP, SEXP RSEXP, SEXP j0SEXP, SEXP mSEXP, SEXP nSEXP) {
6868
BEGIN_RCPP
69-
Rcpp::RObject __result;
70-
Rcpp::RNGScope __rngScope;
69+
Rcpp::RObject rcpp_result_gen;
70+
Rcpp::RNGScope rcpp_rngScope_gen;
7171
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type Q1(Q1SEXP);
7272
Rcpp::traits::input_parameter< Rcpp::NumericMatrix >::type R(RSEXP);
7373
Rcpp::traits::input_parameter< int >::type j0(j0SEXP);
7474
Rcpp::traits::input_parameter< int >::type m(mSEXP);
7575
Rcpp::traits::input_parameter< int >::type n(nSEXP);
76-
__result = Rcpp::wrap(downdate1_(Q1, R, j0, m, n));
77-
return __result;
76+
rcpp_result_gen = Rcpp::wrap(downdate1_(Q1, R, j0, m, n));
77+
return rcpp_result_gen;
7878
END_RCPP
7979
}
80+
81+
static const R_CallMethodDef CallEntries[] = {
82+
{"_selectiveInference_solve_QP", (DL_FUNC) &_selectiveInference_solve_QP, 11},
83+
{"_selectiveInference_solve_QP_wide", (DL_FUNC) &_selectiveInference_solve_QP_wide, 12},
84+
{"_selectiveInference_update1_", (DL_FUNC) &_selectiveInference_update1_, 4},
85+
{"_selectiveInference_downdate1_", (DL_FUNC) &_selectiveInference_downdate1_, 5},
86+
{NULL, NULL, 0}
87+
};
88+
89+
RcppExport void R_init_selectiveInference(DllInfo *dll) {
90+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
91+
R_useDynamicSymbols(dll, FALSE);
92+
}

selectiveInference/src/quadratic_program_wide.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ int solve_wide(double *X_ptr, /* A design matrix */
388388
ncase,
389389
bound,
390390
kkt_tol) == 1) {
391+
fprintf(stderr, "break1\n");
391392
break;
392393
}
393394

@@ -426,6 +427,7 @@ int solve_wide(double *X_ptr, /* A design matrix */
426427
ncase,
427428
bound,
428429
kkt_tol) == 1) {
430+
fprintf(stderr, "break2\n");
429431
break;
430432
}
431433

@@ -434,6 +436,7 @@ int solve_wide(double *X_ptr, /* A design matrix */
434436
// fprintf(stderr, "here18 %d\n", *nactive_ptr);
435437

436438
if (*nactive_ptr >= max_active) {
439+
fprintf(stderr, "break3\n");
437440
break;
438441
}
439442

@@ -454,6 +457,7 @@ int solve_wide(double *X_ptr, /* A design matrix */
454457
// fprintf(stderr, "here8\n");
455458

456459
if ((fabs(old_value - new_value) < objective_tol * fabs(new_value)) && (iter > 0)) {
460+
fprintf(stderr, "break5 %f %f %f %d\n", old_value, new_value, objective_tol, iter);
457461
break;
458462
}
459463
old_value = new_value;

test.R

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,46 @@ set.seed(43)
22

33
n = 100
44
p = 200
5-
lam = 0.1
5+
lam = 0.2
66
X = matrix(rnorm(n*p), n, p)
77
Y = rnorm(n)
88
library(selectiveInference)
99
p = ncol(X)
1010
soln_R = rep(0, p)
1111
grad = -t(X) %*% Y / n
12-
ever_active = c(1, rep(0, p-1))
12+
ever_active = as.integer(c(1, rep(0, p-1)))
1313
nactive = as.integer(1)
1414
kkt_tol = 1.e-12
15-
objective_tol = 1.e-12
15+
objective_tol = 1.e-16
1616
maxiter = 500
17-
soln_R = selectiveInference:::solve_QP(t(X) %*% X / n, lam, maxiter, soln_R, -t(X) %*% Y / n, grad, ever_active, nactive, kkt_tol, objective_tol, p)$soln
17+
soln_R = selectiveInference:::solve_QP(t(X) %*% X / n, lam, maxiter, soln_R, -t(X) %*% Y / n, grad, ever_active, nactive, kkt_tol, objective_tol, p)
18+
print('active')
19+
print(nactive)
20+
print(ever_active)
21+
print(soln_R$ever_active)
22+
soln_R = soln_R$soln
23+
soln_R_old = soln_R
1824
print(soln_R)
1925
Xtheta = rep(0, n)
2026
nactive = as.integer(1)
21-
ever_active = c(1, rep(0, p-1))
27+
ever_active = as.integer(c(1, rep(0, p-1)))
2228
soln_R = rep(0, p)
2329
grad = - t(X) %*% Y / n
2430
# test wide solver
25-
soln_R_wide = selectiveInference:::solve_QP_wide(X, lam, maxiter, soln_R, -t(X) %*% Y / n, grad, Xtheta, ever_active, nactive, kkt_tol, objective_tol, p)
26-
print(soln_R_wide)
27-
print(soln_R)
28-
print(soln_R_wide$soln)
29-
print(soln_R_wide$soln - soln_R)
31+
soln_R_wide = selectiveInference:::solve_QP_wide(X, lam, maxiter, soln_R*1., -t(X) %*% Y / n, grad, Xtheta, ever_active, nactive, kkt_tol, objective_tol, p)
32+
print(nactive)
33+
print(soln_R_wide$ever_active)
34+
35+
print('diff')
36+
print(soln_R_wide$soln - soln_R_old)
37+
print(soln_R_wide$gradient[soln_R_wide$ever_active])
38+
print(max(abs(soln_R_wide$gradient[-soln_R_wide$ever_active])))
39+
print(soln_R_wide$kkt_check)
40+
print(soln_R_wide$iter)
41+
# print(Xtheta - X %*% soln_R_wide$soln)
42+
# print(Xtheta)
43+
44+
soln_R_wide = selectiveInference:::solve_QP_wide(X, 0.7 * lam, maxiter, soln_R_wide$soln, -t(X) %*% Y / n, grad, Xtheta, ever_active, nactive, kkt_tol, objective_tol, p)
45+
# print(Xtheta - X %*% soln_R_wide$soln)
46+
# print(soln_R_wide$soln)
47+
soln_R_wide = selectiveInference:::solve_QP_wide(X, 0.5 * lam, maxiter, soln_R_wide$soln, -t(X) %*% Y / n, grad, Xtheta, ever_active, nactive, kkt_tol, objective_tol, p)

0 commit comments

Comments
 (0)