-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCQR.py
320 lines (266 loc) · 10.6 KB
/
CQR.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
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
import copy
import torch
import numpy as np
import scipy.special
import scipy.spatial
from numpy.random import rand
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 24})
from torch.autograd import Variable
cuda = True if torch.cuda.is_available() else False
FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if cuda else torch.LongTensor
class CQR():
'''
The CQR class implements Conformalized Quantile Regression, i.e. it applies CP to a QR
Inputs:
- Xc, Yc: the calibration set
- trained_qr_model: pre-trained quantile regressor
- quantiles: the quantiles used to train the quantile regressor
- test_hist_size, cal_hist_size: number of observations per point in the test and calibration set respectively
- comb_flag = False: performs normal CQR over a single property
- comb_flag = True: combine the prediction intervals of the CQR of two properties
'''
def __init__(self, Xc, Yc, trained_qr_model, test_hist_size = 2000, cal_hist_size = 50, quantiles = [0.05, 0.95], comb_flag = False):
super(CQR, self).__init__()
self.Xc = Xc
self.Yc = Yc
self.trained_qr_model = trained_qr_model
self.q = len(Yc) # number of points in the calibration set
self.test_hist_size = test_hist_size
self.cal_hist_size = cal_hist_size
self.quantiles = quantiles
self.epsilon = 2*quantiles[0]
self.M = len(quantiles) # number of quantiles
self.col_list = ['yellow', 'orange', 'red', 'orange', 'yellow']
self.comb_flag = comb_flag
def get_pred_interval(self, inputs):
'''
Apply the trained QR to inputs and returns the QR prediction interval
'''
if not self.comb_flag:
return self.trained_qr_model(Variable(FloatTensor(inputs))).cpu().detach().numpy()
else:
interval1 = self.trained_qr_model[0](Variable(FloatTensor(inputs))).cpu().detach().numpy()
interval2 = self.trained_qr_model[1](Variable(FloatTensor(inputs))).cpu().detach().numpy()
pis = []
for i in range(inputs.shape[0]):
# LB = min of the lbs
# UB = min of the ubs
pis.append([min(interval1[i][0],interval2[i][0]), min(interval1[i][-1],interval2[i][-1])])
return np.array(pis)
def get_calibr_nonconformity_scores(self, y, pred_interval, sorting = True):
'''
Compute the nonconformity scores over the calibration set
if sorting = True the returned scores are ordered in a descending order
'''
n = pred_interval.shape[0]
m = len(y)
ncm = np.empty(m)
c = 0
for i in range(n):
for j in range(self.cal_hist_size):
ncm[c] = max(pred_interval[i,0]-y[c], y[c]-pred_interval[i,-1]) # pred_interval[i,0] = q_lo(x), pred_interval[i,1] = q_hi(x)
c += 1
if sorting:
ncm = np.sort(ncm)[::-1] # descending order
return ncm
def get_scores_threshold(self):
'''
This method extract the threshold value tau (the quantile at level epsilon) from the
calibration nonconformity scores (computed by the 'self.get_calibr_nonconformity_scores' method)
'''
self.calibr_pred = self.get_pred_interval(self.Xc)
# nonconformity scores on the calibration set
self.calibr_scores = self.get_calibr_nonconformity_scores(self.Yc, self.calibr_pred)
Q = (1-self.epsilon)*(1+1/self.q)
self.tau = np.quantile(self.calibr_scores, Q)
print("self.tau: ", self.tau)
def get_cpi(self, inputs, pi_flag = False):
'''
Returns the conformalized prediction interval (cpi) by enlarging the
QR prediction interval by adding an subtracting tau from the lower and upper bound resp.
'''
pi = self.get_pred_interval(inputs)
self.get_scores_threshold()
n_quant = pi.shape[1]
cpi = pi[:,0]-self.tau
for j in range(1,n_quant-1):
cpi = np.vstack((cpi, pi[:,j]))
cpi = np.vstack((cpi, pi[:,-1]+self.tau))
if pi_flag:
return cpi.T, pi
else:
return cpi.T
def get_coverage_efficiency(self, y_test, test_pred_interval):
'''
Compute the empirical coverage and the efficiency of a prediction interval (test_pred_interval).
y_test are the observed target values
'''
n_points = len(y_test)//self.test_hist_size
y_test_hist = np.reshape(y_test, (n_points, self.test_hist_size))
c = 0
for i in range(n_points):
for j in range(self.test_hist_size):
if y_test_hist[i,j] >= test_pred_interval[i,0] and y_test_hist[i, j] <= test_pred_interval[i,-1]:
c += 1
coverage = c/(n_points*self.test_hist_size)
efficiency = np.mean(np.abs(test_pred_interval[:,-1]-test_pred_interval[:,0]))
return coverage, efficiency
def get_coverage_efficiency_coupled(self, y_test, test_pred_interval1, test_pred_interval2):
'''
Compute the empirical coverage and the efficiency of the union of two prediction intervals (test_pred_interval1 and test_pred_interval2).
y_test are the observed values of robustness
'''
n_points = len(y_test)//self.test_hist_size
y_test_hist = np.reshape(y_test, (n_points, self.test_hist_size))
c = 0
for i in range(n_points):
for j in range(self.test_hist_size):
# if it lies in at least one of the two intervals, i.e. in the union
if (y_test_hist[i,j] >= test_pred_interval1[i,0] and y_test_hist[i, j] <= test_pred_interval1[i,-1]) or (y_test_hist[i,j] >= test_pred_interval2[i,0] and y_test_hist[i, j] <= test_pred_interval2[i,-1]):
c += 1
coverage = c/(n_points*self.test_hist_size)
efficiency = np.mean(self.measure_efficiency_union(test_pred_interval1,test_pred_interval2))
return coverage, efficiency
def measure_efficiency_union(self, I1, I2):
'''
Measures the width of the interval resulting from the union of two intervals (I1 and I2)
'''
L1 = I1[:,1]-I1[:,0]
L2 = I2[:,1]-I2[:,0]
union_len = []
for i in range(len(I1)):
if I1[i,0] < I2[i,0]:
len_intersection = I1[i,1]-I2[i,0]
if len_intersection > 0:
union_len.append(L1[i]+L2[i]-len_intersection)
else:
union_len.append(L1[i]+L2[i])
else:
len_intersection = I2[i,1]-I1[i,0]
if len_intersection > 0:
union_len.append(L1[i]+L2[i]-len_intersection)
else:
union_len.append(L1[i]+L2[i])
return np.array(union_len)
def compute_accuracy_and_uncertainty(self, test_pred_interval, L_test):
'''
Computes the number of correct, uncertain and wrong prediction intervals and the number of false positives.
L_test is the sign of the observed quantile interval (-1: negative, 0: uncertain, +1: positive)
'''
n_points = len(L_test)
correct = 0
wrong = 0
uncertain = 0
fp = 0
for i in range(n_points):
if L_test[i,2]: # sign +1
if test_pred_interval[i,0] >= 0 and test_pred_interval[i,-1] > 0:
correct += 1
elif test_pred_interval[i,0] <= 0 and test_pred_interval[i,-1] >= 0:
uncertain += 1
else:
wrong +=1
elif L_test[i,1]: # sign 0
if test_pred_interval[i,0] <= 0 and test_pred_interval[i,-1] >= 0:
correct += 1
else:
wrong +=1
if test_pred_interval[i,0] > 0:
fp+= 1
else: # sign -1
if test_pred_interval[i,-1] <= 0 and test_pred_interval[i,0] < 0:
correct += 1
elif test_pred_interval[i,-1] >= 0 and test_pred_interval[i,0] <= 0:
uncertain += 1
else:
wrong +=1
fp += 1
return correct/n_points, uncertain/n_points, wrong/n_points, fp/n_points
def plot_errorbars(self, y, qr_interval, cqr_interval, title_string, plot_path, extra_info = ''):
'''
Create barplots
'''
n_points_to_plot = 40
n_test_points = len(y)//self.test_hist_size
y_resh = np.reshape(y,(n_test_points,self.test_hist_size))
y_resh = y_resh[:n_points_to_plot]
yq = []
yq_out = []
xline_rep = []
xline_rep_out = []
for i in range(n_points_to_plot):
lower_yi = np.quantile(y_resh[i], self.epsilon/2)
upper_yi = np.quantile(y_resh[i], 1-self.epsilon/2)
for j in range(self.test_hist_size):
if y_resh[i,j] <= upper_yi and y_resh[i,j] >= lower_yi:
yq.append(y_resh[i,j])
xline_rep.append(i)
else:
yq_out.append(y_resh[i,j])
xline_rep_out.append(i)
n_quant = qr_interval.shape[1]
xline = np.arange(n_points_to_plot)
xline1 = np.arange(n_points_to_plot)+0.2
xline2 = np.arange(n_points_to_plot)+0.4
fig = plt.figure(figsize=(20,4))
plt.scatter(xline_rep_out, yq_out, c='peachpuff', s=6, alpha = 0.25)
plt.scatter(xline_rep, yq, c='orange', s=6, alpha = 0.25,label='test')
plt.plot(xline, np.zeros(n_points_to_plot), '-.', color='k')
qr_med = qr_interval[:n_points_to_plot,1]
qr_dminus = qr_med-qr_interval[:n_points_to_plot,0]
qr_dplus = qr_interval[:n_points_to_plot,-1]-qr_med
plt.errorbar(x=xline1, y=qr_med, yerr=[qr_dminus,qr_dplus], color = 'c', fmt='o', capsize = 4, label='QR')
cqr_med = cqr_interval[:n_points_to_plot,1]
cqr_dminus = cqr_med-cqr_interval[:n_points_to_plot,0]
cqr_dplus = cqr_interval[:n_points_to_plot,-1]-cqr_med
plt.errorbar(x=xline2, y=cqr_med, yerr=[cqr_dminus,cqr_dplus], color = 'darkviolet', fmt='o', capsize = 4,label='CQR')
plt.ylabel('robustness')
plt.title(title_string)
plt.legend()
plt.grid(True)
plt.tight_layout()
fig.savefig(plot_path+"/"+extra_info+"_errorbar_merged.png")
plt.close()
def plot_comb_errorbars(self, y, cqr_interval, title_string, plot_path, extra_info = ''):
'''
Create barplots for conjuction of properties:
when combining two prediction intervals there is no qr_interval and no median
'''
n_points_to_plot = 40
n_test_points = len(y)//self.test_hist_size
y_resh = np.reshape(y,(n_test_points,self.test_hist_size))
y_resh = y_resh[:n_points_to_plot]
yq = []
yq_out = []
xline_rep = []
xline_rep_out = []
for i in range(n_points_to_plot):
lower_yi = np.quantile(y_resh[i], self.epsilon/2)
upper_yi = np.quantile(y_resh[i], 1-self.epsilon/2)
for j in range(self.test_hist_size):
if y_resh[i,j] <= upper_yi and y_resh[i,j] >= lower_yi:
yq.append(y_resh[i,j])
xline_rep.append(i)
else:
yq_out.append(y_resh[i,j])
xline_rep_out.append(i)
xline = np.arange(n_points_to_plot)
xline1 = np.arange(n_points_to_plot)+0.2
xline2 = np.arange(n_points_to_plot)+0.4
fig = plt.figure(figsize=(20,4))
plt.scatter(xline_rep_out, yq_out, c='peachpuff', s=6, alpha = 0.25)
plt.scatter(xline_rep, yq, c='orange', s=6, alpha = 0.25,label='test')
plt.plot(xline, np.zeros(n_points_to_plot), '-.', color='k')
cqr_med = (cqr_interval[:n_points_to_plot,-1]+cqr_interval[:n_points_to_plot,0])/2
cqr_dminus = cqr_med-cqr_interval[:n_points_to_plot,0]
cqr_dplus = cqr_interval[:n_points_to_plot,-1]-cqr_med
plt.errorbar(x=xline2, y=cqr_med, yerr=[cqr_dminus,cqr_dplus], fmt = 'none', capsize = 4, color = 'darkviolet', label='Conj of CQRs')
plt.ylabel('robustness')
plt.title(title_string)
plt.legend()
plt.grid(True)
plt.tight_layout()
fig.savefig(plot_path+"/"+extra_info+"_errorbar_merged.png")
plt.close()