forked from selective-inference/compare-selection
-
Notifications
You must be signed in to change notification settings - Fork 2
/
posi.py
219 lines (178 loc) · 7.22 KB
/
posi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os, glob, tempfile
import numpy as np
from traitlets import (HasTraits,
Integer,
Unicode,
Float,
Integer,
Instance,
Dict,
Bool,
default)
# Rpy
import rpy2.robjects as rpy
from rpy2.robjects import numpy2ri
from gaussian_methods import lasso_1se, parametric_method
# POSI selection
# since there are no p-values we just use
# marginal check, seeing if 0 in confidence interval
# in order to make speed tolerable,
# we compute POSI constant K on one instance only,
# similar to knockoffs
class POSI90(parametric_method):
method_name = Unicode("POSI")
selectiveR_method = True
selection_method = lasso_1se
model_target = Unicode("selected")
@classmethod
def setup(cls, feature_cov, data_generating_mechanism, max_model_size=6, level=0.90):
cls.feature_cov = feature_cov
cls.data_generating_mechanism = data_generating_mechanism
cls.noise = data_generating_mechanism.noise
numpy2ri.activate()
# see if we've factored this before
have_POSI_K = False
if not os.path.exists('.POSI_data'):
os.mkdir('.POSI_data')
posi_data = glob.glob('.POSI_data/*npz')
for posi_file in posi_data:
posi = np.load(posi_file)
posi_f = posi['feature_cov']
if ((posi_f.shape == feature_cov.shape) and
np.allclose(posi_f, feature_cov) and
(posi['max_model_size'] == max_model_size) and
(posi['level'] == level)):
have_POSI_K = True
print('found POSI instance: %s' % posi)
cls.POSI_K = float(posi['K'])
if not have_POSI_K:
print('simulating POSI constant')
cls.POSI_K = float(POSI_instance(feature_cov, max_model_size, n=10*feature_cov.shape[0]))
numpy2ri.deactivate()
def __init__(self, X, Y, l_theory, l_min, l_1se, sigma_reid):
parametric_method.__init__(self, X, Y, l_theory, l_min, l_1se, sigma_reid)
self._method = self.selection_method(X, Y, l_theory, l_min, l_1se, sigma_reid)
def select(self):
active_set = self._method.generate_pvalues()[0] # gives us selected variables at 1SE
if len(active_set) > 0:
numpy2ri.activate()
rpy.r.assign("X", self.X[:, active_set])
rpy.r.assign("Y", self.Y)
rpy.r.assign("K", self.POSI_K)
rpy.r('M = lm(Y ~ X - 1)')
rpy.r('L = coef(M) - K * sqrt(diag(vcov(M)))')
rpy.r('U = coef(M) + K * sqrt(diag(vcov(M)))')
L = rpy.r('L')
U = rpy.r('U')
numpy2ri.deactivate()
pre_select = np.nonzero((L > 0) + (U < 0))[0]
selected = [active_set[i] for i in pre_select]
return selected, active_set
else:
return [], []
def generate_intervals(self):
active_set = self._method.generate_pvalues()[0] # gives us selected variables at 1SE
if len(active_set) > 0:
numpy2ri.activate()
rpy.r.assign("X", self.X[:, active_set])
rpy.r.assign("Y", self.Y)
rpy.r.assign("K", self.POSI_K)
rpy.r('M = lm(Y ~ X - 1)')
rpy.r('L = coef(M) - K * sqrt(diag(vcov(M)))')
rpy.r('U = coef(M) + K * sqrt(diag(vcov(M)))')
L = rpy.r('L')
U = rpy.r('U')
numpy2ri.deactivate()
return active_set, L, U
else:
return [], [], []
def generate_pvalues(self):
raise NotImplementedError
def point_estimator(self):
raise NotImplementedError
POSI90.register()
class POSI80(POSI90):
method_name = Unicode("POSI")
selectiveR_method = True
selection_method = lasso_1se
model_target = Unicode("selected")
@classmethod
def setup(cls, feature_cov, data_generating_mechanism, max_model_size=6, level=0.80):
cls.feature_cov = feature_cov
cls.data_generating_mechanism = data_generating_mechanism
cls.noise = data_generating_mechanism.noise
numpy2ri.activate()
# see if we've factored this before
have_POSI_K = False
if not os.path.exists('.POSI_data'):
os.mkdir('.POSI_data')
posi_data = glob.glob('.POSI_data/*npz')
for posi_file in posi_data:
posi = np.load(posi_file)
posi_f = posi['feature_cov']
if ((posi_f.shape == feature_cov.shape) and
np.allclose(posi_f, feature_cov) and
(posi['max_model_size'] == max_model_size) and
(posi['level'] == level)):
have_POSI_K = True
print('found POSI instance: %s' % posi)
cls.POSI_K = float(posi['K'])
if not have_POSI_K:
print('simulating POSI constant')
cls.POSI_K = float(POSI_instance(feature_cov, max_model_size, n=10*feature_cov.shape[0]))
numpy2ri.deactivate()
POSI80.register()
class POSI95(POSI90):
method_name = Unicode("POSI")
selectiveR_method = True
selection_method = lasso_1se
model_target = Unicode("selected")
@classmethod
def setup(cls, feature_cov, data_generating_mechanism, max_model_size=6, level=0.95):
cls.feature_cov = feature_cov
cls.data_generating_mechanism = data_generating_mechanism
cls.noise = data_generating_mechanism.noise
numpy2ri.activate()
# see if we've factored this before
have_POSI_K = False
if not os.path.exists('.POSI_data'):
os.mkdir('.POSI_data')
posi_data = glob.glob('.POSI_data/*npz')
for posi_file in posi_data:
posi = np.load(posi_file)
posi_f = posi['feature_cov']
if ((posi_f.shape == feature_cov.shape) and
np.allclose(posi_f, feature_cov) and
(posi['max_model_size'] == max_model_size) and
(posi['level'] == level)):
have_POSI_K = True
print('found POSI instance: %s' % posi)
cls.POSI_K = float(posi['K'])
if not have_POSI_K:
print('simulating POSI constant')
cls.POSI_K = float(POSI_instance(feature_cov, max_model_size, n=10*feature_cov.shape[0]))
numpy2ri.deactivate()
POSI95.register()
def POSI_instance(feature_cov, max_model_size, n, level=np.array([0.8,0.9,0.95,0.99])):
numpy2ri.activate()
rpy.r.assign('Sigma', feature_cov)
chol = np.linalg.cholesky(feature_cov)
p = feature_cov.shape[0]
X = np.random.standard_normal((n, p)).dot(chol.T)
rpy.r.assign('X', X)
rpy.r.assign('max_model_size', max_model_size)
rpy.r.assign('level', level)
rpy.r('''
library(PoSI)
posi_obj = PoSI(X, modelSZ=1:max_model_size)
posi_K = summary(posi_obj, confidence=level)[,'K.PoSI']
''')
K = rpy.r("posi_K")
for i, l in enumerate(level):
print(K[i])
np.savez('.POSI_data/%s.npz' % (os.path.split(tempfile.mkstemp()[1])[1],),
K=K[i],
feature_cov=feature_cov,
level=l,
max_model_size=max_model_size)
return K