-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconformal_prediction.py
More file actions
457 lines (362 loc) · 22.3 KB
/
Copy pathconformal_prediction.py
File metadata and controls
457 lines (362 loc) · 22.3 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
from config import conf
import numpy as np
import torch
import torch.nn.functional as F
class ConformalPrediction:
# Implements system with both standard and modified conformal prediction.
"""Implementation and evaluation of the conformal prediction based support system"""
def __init__(self, X_cal, y_cal, X_est, y_est,model, delta, has_groups=False) -> None:
self.model = model
self.X_cal = X_cal
self.y_cal = y_cal
self.X_est = X_est
self.y_est = y_est
self.calibration_size = len(y_cal)
self.delta = delta
# conformal scores of true labels in calibration set
model_out = self.model.predict_prob(self.X_cal)
self.has_groups = has_groups
if not has_groups:
one_hot = np.eye(conf.n_labels)[self.y_cal]
else:
one_hot = np.eye(conf.n_labels)[self.y_cal[:,0]]
true_label_logits = model_out*one_hot
conf_scores = sorted(1 - true_label_logits.sum(axis=1))
self.conf_scores_t = torch.tensor(conf_scores, device=conf.device)
def epsilon_fn(self, delta_n_alphas, all_a1_a2):
"""Estimation error"""
delta_n_alphas_t = torch.tensor(delta_n_alphas)
n_alphas = self.calibration_size if not all_a1_a2 else (self.calibration_size*(self.calibration_size+1)/2)
epsilon = torch.sqrt((torch.log(delta_n_alphas_t))/(2*n_alphas))
return epsilon
def find_all_alpha_values(self):
"""Returns all 0<alpha<1 values that can be considered given a fixed calibration set"""
alphas = 1 - (np.arange(1,self.calibration_size + 1) / (self.calibration_size + 1))
self.alphas = alphas
self.n_alphas = self.calibration_size
return alphas
def find_a_star(self, w_matrix, a1_star_idx=None, all_a1_a2=False):
"""Returns the best alpha value or the best alpha_2 value given alpha_1"""
a_star_idx = -1
curr_criterion = 0
# alphas for standard conformal prediction method
alphas = self.alphas
if a1_star_idx is not None:
# alphas and quantiles for shifted quantile method given a_1
quant_prob_a1 = np.ceil((1 - alphas[a1_star_idx])*(self.calibration_size+1))/self.calibration_size
qhat_a1 = torch.zeros((1,1), device=conf.device)
qhat_a1 = torch.quantile(self.conf_scores_t, quant_prob_a1)
alphas = self.alphas[self.alphas > self.alphas[a1_star_idx]]
if all_a1_a2 is not None:
alphas = np.append(alphas, 1)
# quantile probabilities for each alpha value
quant_prob = (np.ceil((1 - alphas)*(self.calibration_size+1))/self.calibration_size).flatten()
# output scores for each sample in estimation set
output_scores = 1 - self.model.predict_prob(self.X_est)
# move data to gpu if available
quant_probs_t = torch.tensor(quant_prob, device=conf.device)
qhats_t = torch.quantile(self.conf_scores_t, quant_probs_t, keepdim=True)
qhats_t = qhats_t.unsqueeze(1)
y_est_t = torch.tensor(self.y_est, device=conf.device, dtype=torch.int64)
fill_value_t = torch.tensor(0, dtype=torch.double, device=conf.device)
output_scores_t = torch.tensor(output_scores, device=conf.device)
if not self.has_groups:
ws_t = torch.tensor(w_matrix[self.y_est], device=conf.device)
else:
ws_t = torch.tensor(w_matrix[self.y_est[:,1],self.y_est[:,0]], device=conf.device)
# estimation error
delta_n_alphas = (alphas.shape[0]/self.delta) if not all_a1_a2 else (self.calibration_size*(self.calibration_size + 1)/2)/self.delta
epsilon = self.epsilon_fn(delta_n_alphas, all_a1_a2)
for i,q in enumerate(qhats_t):
qhats = q.expand(self.calibration_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
if a1_star_idx is not None:
# sets for shifted quantile method given a_1
qhats_a1 = qhat_a1.expand(self.calibration_size, conf.n_labels)
sets_upper = torch.where(output_scores_t <= qhats_a1, 1, 0)
sets_lower = torch.where(qhats < output_scores_t, 1, 0)
sets = sets_upper* sets_lower
else:
# sets for standard conformal prediction method
sets = torch.where(output_scores_t <= qhats, 1, 0)
sets_exp_ws = sets * torch.exp(ws_t)
# denominators for all P[\hat Y = Y ; C_alpha | Y \in C_alpha(X), Y=y]
denominators = torch.sum(sets_exp_ws, axis=1)
if not self.has_groups:
one_hot_ycal = F.one_hot(y_est_t)
else:
one_hot_ycal = F.one_hot(y_est_t[:,0])
# mask for prediction sets that include the true label
mask = sets * one_hot_ycal
true_label_in_sets_idx = torch.sum(mask, axis=1)
# nominators for all P[\hat Y = Y ; C_alpha | Y \in C_alpha(X), Y=y]
nominators = torch.sum(sets_exp_ws*one_hot_ycal, axis=1)
# apply mask so that Y \in C_alpha(X) is satisfied
masked_prob = torch.where(true_label_in_sets_idx==1, nominators/denominators, fill_value_t)
# empirical estimation of human expected success probability when choosing from the prediction sets.
expected_correct_prob = masked_prob.sum()/self.calibration_size
criterion = (expected_correct_prob - epsilon)
if criterion > curr_criterion:
a_star_idx = i
curr_criterion = criterion
if all_a1_a2:
# set all_a1_a2=True when searching for the best a1, a2
return a_star_idx, curr_criterion, criterion
return a_star_idx
def error_given_test_set_given_a(self, X_test, y_test, w_matrix, alpha1_value, alpha2_value=None):
"""Empirical expert misprediction probability given the value of alpha or the values alpha_1, alpha_2 during test"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
# alphas and quantiles for shifted quantile method
if alpha2_value is not None:
quant_prob_a2 = (np.ceil((1 - alpha2_value)*(self.calibration_size+1))/self.calibration_size)
qhat_a2 = torch.quantile(self.conf_scores_t, quant_prob_a2)
quant_prob = (np.ceil((1 - alpha1_value)*(self.calibration_size+1))/self.calibration_size)
# move data to gpu if available
quanta1_prob_t = torch.tensor(quant_prob, device=conf.device)
qhata1_t = torch.quantile(self.conf_scores_t, quanta1_prob_t, keepdim=True).unsqueeze(1)
y_test_t = torch.tensor(y_test, device=conf.device, dtype=torch.int64)
output_scores_t = torch.tensor(output_scores, device=conf.device)
if not self.has_groups:
ws_t = torch.tensor(w_matrix[y_test], device=conf.device)
else:
ws_t = torch.tensor(w_matrix[y_test[:,1], y_test[:,0]], device=conf.device)
fill_value_t = torch.exp(ws_t)/(torch.exp(ws_t).sum(axis=1).unsqueeze(1).expand(-1,conf.n_labels))
qhats_a1 = qhata1_t.expand(test_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
sets = torch.where(output_scores_t <= qhats_a1, 1, 0)
if alpha2_value is not None:
# sets for shifted quantile method given alpha_1
qhats_a2 = torch.ones((test_size,conf.n_labels), device=conf.device)*qhat_a2
sets_lower = torch.where(qhats_a2 < output_scores_t, 1, 0)
sets = sets * sets_lower
# denominators for P[\hat Y = y ; C_alpha | y \in C_alpha(X)]
sets_exp_ws = sets * torch.exp(ws_t)
denominators_col = torch.sum(sets_exp_ws, axis=1)
denominators = denominators_col.unsqueeze(1).expand(-1, conf.n_labels)
# nominators for P[\hat Y = y ; C_alpha| y \in C_alpha(X)]
nominators = sets_exp_ws
# confusion matrix for each prediction set
cm = torch.where(denominators>0, nominators/denominators, fill_value_t)
# human predictions from prediction sets
y_h = cm.multinomial(num_samples=1, replacement=True, generator=conf.torch_rng).squeeze()
# set dummy prediction -1 for empty sets, so that it is counted as misprediction
y_hats = torch.where(denominators_col>0, y_h , -1)
# misprediction probability
if not self.has_groups:
errors = (y_hats!=y_test_t).count_nonzero().double()
else:
errors = (y_hats!=y_test_t[:,0]).count_nonzero().double()
return errors/test_size
def error_given_test_set_per_a(self, X_test, y_test, w_matrix, alphas, a_star_idx=None):
"""Empirical expert misprediction probability for each value of alpha or alpha_2 given alpha_1 during test"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
# alphas and quantiles for shifted quantile method
if a_star_idx is not None:
quant_a1 = (np.ceil((1 - self.alphas[a_star_idx])*(self.calibration_size+1))/self.calibration_size)
qhat_a1 = torch.quantile(self.conf_scores_t, quant_a1)
alphas = self.alphas[self.alphas > self.alphas[a_star_idx]]
quant_prob = (np.ceil((1 - alphas)*(self.calibration_size+1))/self.calibration_size)
error_rate_per_a = torch.zeros((len(quant_prob),), device=conf.device)
# move data to gpu if available
quant_prob_t = torch.tensor(quant_prob, device=conf.device)
qhats_t = torch.quantile(self.conf_scores_t, quant_prob_t, keepdim=True).unsqueeze(1)
y_test_t = torch.tensor(y_test, device=conf.device, dtype=torch.int64)
output_scores_t = torch.tensor(output_scores, device=conf.device)
if not self.has_groups:
ws_t = torch.tensor(w_matrix[y_test], device=conf.device)
else:
ws_t = torch.tensor(w_matrix[y_test[:,1], y_test[:,0]], device=conf.device)
a_empty_sets = 0
fill_value_t = torch.exp(ws_t)/(torch.exp(ws_t).sum(axis=1).unsqueeze(1).expand(-1, conf.n_labels))
for i,q in enumerate(qhats_t):
qhats = q.expand(test_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
if a_star_idx is not None:
# sets for shifted quantile method given alpha_1
qhats_a1 = qhat_a1.expand(test_size,conf.n_labels )
sets_upper = torch.where(output_scores_t <= qhats_a1 ,1 ,0)
sets_lower = torch.where(qhats < output_scores_t, 1, 0)
sets = sets_upper * sets_lower
else:
sets = torch.where(output_scores_t <= qhats, 1, 0)
non_empty_sets = sets.sum(axis=1).count_nonzero()
if non_empty_sets==0 :
a_empty_sets+=1
# denominators for P[\hat Y = y ; C_alpha | y \in C_alpha(X)]
sets_exp_ws = sets * torch.exp(ws_t)
denominators_col = torch.sum(sets_exp_ws, axis=1)
denominators = denominators_col.unsqueeze(1).expand(-1, conf.n_labels)
# nominators for P[\hat Y = y ; C_alpha | y \in C_alpha(X)]
nominators = sets_exp_ws
# confusion matrix for each prediction set
cm = torch.where(denominators>0, nominators/denominators, fill_value_t)
# human predictions from prediction sets
y_h = cm.multinomial(num_samples=1, replacement=True, generator=conf.torch_rng).squeeze()
# set dummy prediction -1 for empty sets, so that it is counted as misprediction
y_hats = torch.where(denominators_col>0, y_h , -1)
# misprediction probability
if not self.has_groups:
errors = (y_hats!=y_test_t).count_nonzero().double()
else:
errors = (y_hats!=y_test_t[:,0]).count_nonzero().double()
error_rate_per_a[i] = errors/test_size
return error_rate_per_a
def test_error_robustness(self, p, X_test, y_test, w_matrix, alpha1_value, alpha2_value=None):
"""Empirical expert misprediction probability during test under IIA violations"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
# alphas and quantiles for shifted quantile method
if alpha2_value is not None:
quant_prob_a2 = (np.ceil((1 - alpha2_value)*(self.calibration_size+1))/self.calibration_size)
qhat_a2 = torch.quantile(self.conf_scores_t, quant_prob_a2)
quant_prob = (np.ceil((1 - alpha1_value)*(self.calibration_size+1))/self.calibration_size)
# move data to gpu if available
quanta1_prob_t = torch.tensor(quant_prob, device=conf.device)
qhata1_t = torch.quantile(self.conf_scores_t, quanta1_prob_t, keepdim=True).unsqueeze(1)
y_test_t = torch.tensor(y_test, device=conf.device, dtype=torch.int64)
output_scores_t = torch.tensor(output_scores, device=conf.device)
ws_t = torch.tensor(w_matrix[y_test], device=conf.device)
fill_value_t = torch.exp(ws_t)/(torch.exp(ws_t).sum(axis=1).unsqueeze(1).expand(-1, conf.n_labels))
qhats_a1 = qhata1_t.expand(test_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
sets = torch.where(output_scores_t <= qhats_a1, 1, 0)
if alpha2_value is not None:
# sets for shifted quantile method given alpha_1
qhats_a2 = torch.ones((test_size,conf.n_labels), device=conf.device)*qhat_a2
sets_lower = torch.where(qhats_a2 < output_scores_t, 1, 0)
sets = sets * sets_lower
# denominators for P[\hat Y = y ; C_alpha | y \in C_alpha(X)]
sets_exp_ws = sets * torch.exp(ws_t)
# nominators for P[\hat Y = y ; C_alpha | y \in C_alpha(X)]
nominators = sets_exp_ws
# sets that include the true label
one_hot_ycal = F.one_hot(y_test_t)
mask_sets_with_true_labels = (sets * one_hot_ycal).sum(axis=1, keepdim=True).expand(test_size, conf.n_labels)
# labels excluded from the sets
mass_of_labels_not_in_the_set = p * ((1 - sets)*torch.exp(ws_t)).sum(axis=1, keepdim=True).expand(test_size, conf.n_labels)
# sizes of prediction sets - 1
sets_sizes_minus1 = sets.sum(axis=1, keepdim=True).expand(test_size, conf.n_labels) - 1
mass_to_add_in_false_labels = torch.where( (one_hot_ycal==0) & (sets==1) & (mask_sets_with_true_labels==1), mass_of_labels_not_in_the_set/sets_sizes_minus1, 0)
# tweaked nominators
tweaked_nominators = nominators+mass_to_add_in_false_labels
denominators_col = tweaked_nominators.sum(axis=1)
denominators = tweaked_nominators.sum(axis=1, keepdim=True).expand(test_size, conf.n_labels)
tweaked_cm = torch.where(denominators>0, tweaked_nominators/denominators, fill_value_t)
# human predictions from prediction sets
y_h = tweaked_cm.multinomial(num_samples=1, replacement=True, generator=conf.torch_rng).squeeze()
# set dummy prediction -1 for empty sets, so that it is counted as misprediction
y_hats = torch.where(denominators_col>0, y_h , -1)
# misprediction probability
errors = (y_hats!=y_test_t).count_nonzero().double()
return errors/test_size
def size_given_test_set_per_a(self, X_test, alphas, a_star_idx=None):
"""Empirical average set size for each value of alpha or alpha_2 given alpha_1"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
if a_star_idx is not None:
# alphas and quantiles for shifted quantile method
quant_a1 = (np.ceil((1 - alphas[a_star_idx])*(self.calibration_size+1))/self.calibration_size)
qhat_a1 = torch.quantile(self.conf_scores_t, quant_a1)
alphas = alphas[alphas > alphas[a_star_idx]]
quant_prob_t = torch.tensor(np.ceil((1 - alphas)*(self.calibration_size+1))/self.calibration_size, device=conf.device)
set_size_per_a = torch.zeros((len(alphas),), device=conf.device)
# move data to gpu if available
qhats_t = torch.quantile(self.conf_scores_t, quant_prob_t, keepdim=True).unsqueeze(1)
output_scores_t = torch.tensor(output_scores, device=conf.device)
for i,q in enumerate(qhats_t):
qhats = q.expand(test_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
if a_star_idx is not None:
# sets for shifted quantile method
qhats_a1 = qhat_a1.expand(test_size, conf.n_labels)
sets_upper = torch.where(output_scores_t <= qhats_a1, 1, 0)
sets_lower = torch.where(qhats < output_scores_t, 1, 0)
sets = sets_upper * sets_lower
else:
sets = torch.where(output_scores_t <= qhats, 1, 0)
size_per_set = sets.sum(axis=1)
set_size_per_a[i] = size_per_set.sum()/size_per_set.numel()
return set_size_per_a
def error_given_test_set_topk(self, X_test, y_test, w_matrix, k=5):
"""Emprical misprediction probability of an expert using a top-k predictor"""
test_size = len(X_test)
output_scores = self.model.predict_prob(X_test)
error_rate_per_a = torch.zeros((1,), device=conf.device)
# move data to gpu if available
y_test_t = torch.tensor(y_test, device=conf.device, dtype=torch.int64)
output_scores_t = torch.tensor(output_scores, device=conf.device)
ws_t = torch.tensor(w_matrix[y_test], device=conf.device)
fill_value_t = torch.exp(ws_t)/(torch.exp(ws_t).sum(axis=1).unsqueeze(1).expand(-1, conf.n_labels))
# compute topk labels for each sample
sorted_output_scores = torch.topk(output_scores_t, k=k, dim=1).indices
# prediction sets with topk labels
sets = torch.any(F.one_hot(sorted_output_scores, num_classes=conf.n_labels), 1).int()
# denominators for P[\hat Y = y ; C_k | y \in C_k(X)]
sets_exp_ws = sets * torch.exp(ws_t)
denominators_col = torch.sum(sets_exp_ws, axis=1)
denominators = denominators_col.unsqueeze(1).expand(-1, conf.n_labels)
# nominators for P[\hat Y = y ; C_k | y \in C_k(X)]
nominators = sets_exp_ws
# confusion matrix for each prediction set
cm = torch.where(denominators>0, nominators/denominators, fill_value_t)
# human predictions from prediction sets
y_h = cm.multinomial(num_samples=1, replacement=True, generator=conf.torch_rng).squeeze()
# set dummy prediction -1 for empty sets, so that it is counted as misprediction
y_hats = torch.where(denominators_col>0, y_h , -1)
# misprediction probability
errors = (y_hats!=y_test_t).count_nonzero().double()
error_rate_per_a = errors/test_size
return error_rate_per_a
def size_given_test_set_given_a(self, X_test, alpha, alpha2=None):
"""Set size distribution for given alpha or alpha_1, alpha_2 during test"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
if alpha2 is not None:
# alphas and quantiles for shifted quantile method
quant_a1 = (np.ceil((1 - alpha)*(self.calibration_size+1))/self.calibration_size)
qhat_a1 = torch.quantile(self.conf_scores_t, quant_a1)
alpha = alpha2
quant_prob_t = torch.tensor(np.ceil((1 - alpha)*(self.calibration_size+1))/self.calibration_size, device=conf.device)
# move data to gpu if available
qhat_t = torch.quantile(self.conf_scores_t, quant_prob_t)
output_scores_t = torch.tensor(output_scores,device=conf.device)
qhats = qhat_t.expand(test_size, conf.n_labels)
# sets[sample][label] is 1 for the labels in the prediction set for each sample
if alpha2 is not None:
# sets for shifted quantile method
qhats_a1 = qhat_a1.expand(test_size, conf.n_labels)
sets_upper = torch.where(output_scores_t <= qhats_a1, 1, 0)
sets_lower = torch.where(qhats < output_scores_t, 1, 0)
sets = sets_upper * sets_lower
else:
sets = torch.where(output_scores_t <= qhats, 1, 0)
size_per_set = sets.sum(axis=1)
set_sizes_t, counts_t = torch.unique(size_per_set, return_counts=True)
return set_sizes_t, counts_t
def empirical_coverage(self, X_test, y_test, alpha1, alpha2=None):
"""Empirical coverage on test set given alpha or alpha1, alpha2"""
test_size = len(X_test)
output_scores = 1 - self.model.predict_prob(X_test)
# quantiles for shifted quantile method
if alpha2 is not None:
quant_a2 = (np.ceil((1 - alpha2)*(self.calibration_size+1))/self.calibration_size)
qhat_a2 = torch.quantile(self.conf_scores_t, quant_a2)
quant_prob_t = torch.tensor(np.ceil((1 - alpha1)*(self.calibration_size+1))/self.calibration_size, device=conf.device)
# move data to gpu if available
qhat = torch.quantile(self.conf_scores_t, quant_prob_t)
y_test_t = torch.tensor(y_test, device=conf.device, dtype=torch.int64)
output_scores_t = torch.tensor(output_scores, device=conf.device)
qhats = torch.ones((test_size,conf.n_labels), device=conf.device)*qhat
# sets[sample][label] is 1 for the labels in the prediction set for each sample
sets = torch.where(output_scores_t <= qhats, 1, 0)
if alpha2 is not None:
# sets for shifted quantile method given alpha_1
qhats_a2 = torch.ones((test_size,conf.n_labels), device=conf.device)*qhat_a2
sets_lower = torch.where(qhats_a2 < output_scores_t, 1, 0)
sets = sets * sets_lower
one_hot_ycal = F.one_hot(y_test_t)
# mask for prediction sets that include the true label
true_label_in_sets = sets * one_hot_ycal
return true_label_in_sets.sum()/test_size