-
Notifications
You must be signed in to change notification settings - Fork 1
/
conformal_predictors.py
296 lines (238 loc) · 9.9 KB
/
conformal_predictors.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import math
import numpy as np
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from quantile_forest import RandomForestQuantileRegressor
from base_conformal_predictor import BaseConformalPredictor
class LPRegressionConformalPredictor(BaseConformalPredictor):
def __init__(self, y, p=1., model='randomforest'):
super().__init__('continuous', 'continuous')
# Set the order of the norm
self.p = p
# Set the minimum and the maximum non-conformity scores
self.r_min = 0.
self.r_max = (y.max() - y.min()) ** p
# The machine learning model
if model == 'randomforest':
self.model = RandomForestRegressor()
# The multiplicative factor is known
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_known_multiplicative_factor
def get_model_prediction(self, X):
# Compute the machine learning model prediction
yh = self.model.predict(X)
return yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
yh = self.get_model_prediction(X)
r = np.abs(yh - y) ** self.p
return r
def get_prediction(self, X_test):
# Compute the conformal prediction set
yh_test = self.get_model_prediction(X_test)
C = [
yh_test - (self.threshold ** (1. / self.p)),
yh_test + (self.threshold ** (1. / self.p))
]
return C
def integrate_multiplicative_factor(self, r):
# Compute the integral of the multiplicative factor over each
# (r[i], r[i + 1]] region
int_mf = 2. * ((r[1 :] ** (1. / self.p)) - (r[: -1] ** (1. / self.p)))
return int_mf
class ZeroOneClassificationConformalPredictor(BaseConformalPredictor):
def __init__(self, num_classes, model='randomforest'):
super().__init__('discrete', 'discrete')
# Set the number of classes
self.num_classes = num_classes
# Set the space of non-conformity scores
self.r_space = np.array([0., 1.])
# The machine learning model
if model == 'randomforest':
self.model = RandomForestClassifier()
# The multiplicative factor is known
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_known_multiplicative_factor
def get_model_prediction(self, X):
# Compute the machine learning model prediction
yh = self.model.predict(X)
return yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
yh = self.get_model_prediction(X)
r = (yh != y).astype(float)
return r
def get_prediction(self, X_test):
# Compute the conformal prediction set
n_test = X_test.shape[0]
if self.threshold == 0.:
yh_test = self.get_model_prediction(X_test)
C = np.zeros((n_test, self.num_classes), bool)
C[np.arange(n_test), yh_test] = 1.
else:
C = np.ones((n_test, self.num_classes), bool)
return C
def multiplicative_factor(self, r):
# Compute the multiplicative factor for each non-conformity score
mf = (r == 0.) + ((self.num_classes - 1) * (r == 1.))
return mf
class CQRRegressionConformalPredictor(BaseConformalPredictor):
def __init__(self, y, y_num=100, model='randomforest'):
super().__init__('continuous', 'continuous')
# Set the minimum and the maximum labels
self.y_min = y.min()
self.y_max = y.max()
# Set the number of labels to use for the expected prediction set size
# estimates
self.y_num = y_num
# The machine learning model
if model == 'randomforest':
self.model = RandomForestQuantileRegressor()
# The multiplicative factor is unknown
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_unknown_multiplicative_factor
def set_calibration(self, X_cal, y_cal, alpha):
# Set the significance level
self.alpha = alpha
super().set_calibration(X_cal, y_cal, alpha)
def get_model_prediction(self, X):
# Compute the machine learning model prediction
quantiles = [self.alpha / 2., 1. - (self.alpha / 2.)]
Yh = self.model.predict(X, quantiles=quantiles)
return Yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
Yh = self.get_model_prediction(X)
r = np.maximum(Yh[:, 0] - y, y - Yh[:, 1])
return r
def get_nonconformity_scores(self, X):
# Compute all labels to use
ys, self.y_width = np.linspace(
self.y_min, self.y_max, self.y_num + 1, retstep=True
)
ys += (self.y_width / 2.)
ys = ys[: -1]
# Compute the non-conformity scores for all labels
Yh = self.get_model_prediction(X)
R = []
for y in ys:
r = np.maximum(Yh[:, 0] - y, y - Yh[:, 1])
R.append(r[:, np.newaxis])
R = np.hstack(R)
return R
def get_prediction(self, X_test):
# Compute the conformal prediction set
Yh_test = self.get_model_prediction(X_test)
C = [Yh_test[:, 0] - self.threshold, Yh_test[:, 1] + self.threshold]
return C
class LACClassificationConformalPredictor(BaseConformalPredictor):
def __init__(self, num_classes, model='randomforest'):
super().__init__('discrete', 'continuous')
# Set the number of classes
self.num_classes = num_classes
# The machine learning model
if model == 'randomforest':
self.model = RandomForestClassifier()
# The multiplicative factor is unknown
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_unknown_multiplicative_factor
def get_model_prediction(self, X):
# Compute the machine learning model prediction
Yh = self.model.predict_proba(X)
return Yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
n = X.shape[0]
Yh = self.get_model_prediction(X)
r = 1. - Yh[np.arange(n), y]
return r
def get_nonconformity_scores(self, X):
# Compute the non-conformity scores for all labels
Yh = self.get_model_prediction(X)
R = 1. - Yh
return R
def get_prediction(self, X_test):
# Compute the conformal prediction set
R_test = self.get_nonconformity_scores(X_test)
C = R_test <= self.threshold
return C
class APSClassificationConformalPredictor(BaseConformalPredictor):
def __init__(self, num_classes, model='randomforest'):
super().__init__('discrete', 'continuous')
# Set the number of classes
self.num_classes = num_classes
# The machine learning model
if model == 'randomforest':
self.model = RandomForestClassifier()
# The multiplicative factor is unknown
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_unknown_multiplicative_factor
def get_model_prediction(self, X):
# Compute the machine learning model prediction
Yh = self.model.predict_proba(X)
return Yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
n = X.shape[0]
Yh = self.get_model_prediction(X)
yh = Yh[np.arange(n), y]
r = \
((yh[:, np.newaxis] < Yh) * Yh).sum(1) \
+ (np.random.uniform(size=n) * yh)
return r
def get_nonconformity_scores(self, X):
# Compute the non-conformity scores for all labels
n = X.shape[0]
Yh = self.get_model_prediction(X)
R = []
for y in range(self.num_classes):
yh = Yh[:, y]
r = \
((yh[:, np.newaxis] < Yh) * Yh).sum(1) \
+ (np.random.uniform(size=n) * yh)
R.append(r[:, np.newaxis])
R = np.hstack(R)
return R
def get_prediction(self, X_test):
# Compute the conformal prediction set
R_test = self.get_nonconformity_scores(X_test)
C = R_test <= self.threshold
return C
class LPHighDimensionRegressionConformalPredictor(BaseConformalPredictor):
def __init__(self, y, p, model='randomforest'):
super().__init__('continuous', 'continuous')
# Set the order of the norm
self.p = p
# Set the dimensionality
self.d = y.shape[1]
# Set the minimum and the maximum non-conformity scores
self.r_min = 0.
self.r_max = (
(np.abs(y[np.newaxis, :, :] - y[:, np.newaxis, :]) ** self.p).sum(2)
).max()
# The machine learning model
if model == 'randomforest':
self.model = RandomForestRegressor()
# The multiplicative factor is known
self.get_prediction_set_size_estimates = \
self.get_prediction_set_size_estimates_known_multiplicative_factor
def get_model_prediction(self, X):
# Compute the machine learning model prediction
yh = self.model.predict(X)
return yh
def get_nonconformity_score(self, X, y):
# Compute the non-conformity score
yh = self.get_model_prediction(X)
r = (np.abs(yh - y) ** self.p).sum(1)
return r
def get_prediction(self, X_test):
# Compute the conformal prediction set
yh_test = self.get_model_prediction(X_test)
return yh_test, self.threshold, self.p
def integrate_multiplicative_factor(self, r):
# Compute the integral of the multiplicative factor over each
# (r[i], r[i + 1]] region
int_mf = \
((r[1 :] ** (self.d / self.p)) - (r[: -1] ** (self.d / self.p))) \
* ((2. * math.gamma((1. / self.p) + 1.)) ** self.d) \
/ math.gamma((self.d / self.p) + 1.)
return int_mf