-
Notifications
You must be signed in to change notification settings - Fork 0
/
auc_delong_xu.py
375 lines (310 loc) · 10.2 KB
/
auc_delong_xu.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
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
"""
@author: yandexdataschool
Original Code found in:
https://github.com/yandexdataschool/roc_comparison
"""
from __future__ import absolute_import, division
import numpy as np
from scipy import stats
import scipy.stats
# AUC comparison adapted from
# https://github.com/Netflix/vmaf/
def compute_midrank(x):
"""
Computes midranks.
Parameters
----------
x : np.array
x - a 1D numpy array
Returns
-------
T2 : np.array
Array of midranks
"""
J = np.argsort(x)
Z = x[J]
N = len(x)
T = np.zeros(N, dtype=float)
i = 0
while i < N:
j = i
while j < N and Z[j] == Z[i]:
j += 1
T[i:j] = 0.5*(i + j - 1)
i = j
T2 = np.empty(N, dtype=float)
# Note(kazeevn) +1 is due to Python using 0-based indexing
# instead of 1-based in the AUC formula in the paper
T2[J] = T + 1
return T2
def compute_midrank_weight(x, sample_weight):
"""
Computes midranks.
Parameters
----------
x : np.array
sample_weigh : int
x - a 1D numpy array
Returns
-------
T2 : np.array
array of midranks
"""
J = np.argsort(x)
Z = x[J]
cumulative_weight = np.cumsum(sample_weight[J])
N = len(x)
T = np.zeros(N, dtype=float)
i = 0
while i < N:
j = i
while j < N and Z[j] == Z[i]:
j += 1
T[i:j] = cumulative_weight[i:j].mean()
i = j
T2 = np.empty(N, dtype=float)
T2[J] = T
return T2
def fastDeLong(predictions_sorted_transposed, label_1_count, sample_weight):
if sample_weight is None:
return fastDeLong_no_weights(
predictions_sorted_transposed,
label_1_count)
else:
return fastDeLong_weights(
predictions_sorted_transposed,
label_1_count,
sample_weight)
def fastDeLong_weights(pred_sorted_transposed, label_1_count, sample_weight):
"""
The fast version of DeLong's method for computing the covariance of
unadjusted AUC.
Reference
----------
@article{sun2014fast,
title={Fast Implementation of DeLong's Algorithm for
Comparing the Areas Under Correlated Receiver Oerating
Characteristic Curves},
author={Xu Sun and Weichao Xu},
journal={IEEE Signal Processing Letters},
volume={21},
number={11},
pages={1389--1393},
year={2014},
publisher={IEEE}
}
Parameters
----------
predictions_sorted_transposed : np.array
a 2D numpy.array[n_classifiers, n_examples] sorted such as the
examples with label "1" are first
Returns
-------
aucs : float
delongcov : float
(AUC value, DeLong covariance)
"""
# Short variables are named as they are in the paper
m = label_1_count
n = pred_sorted_transposed.shape[1] - m
positive_examples = pred_sorted_transposed[:, :m]
negative_examples = pred_sorted_transposed[:, m:]
k = pred_sorted_transposed.shape[0]
tx = np.empty([k, m], dtype=float)
ty = np.empty([k, n], dtype=float)
tz = np.empty([k, m + n], dtype=float)
for r in range(k):
tx[r, :] = compute_midrank_weight(
positive_examples[r, :], sample_weight[:m])
ty[r, :] = compute_midrank_weight(
negative_examples[r, :], sample_weight[m:])
tz[r, :] = compute_midrank_weight(
pred_sorted_transposed[r, :], sample_weight)
total_positive_weights = sample_weight[:m].sum()
total_negative_weights = sample_weight[m:].sum()
pair_weights = np.dot(
sample_weight[:m, np.newaxis],
sample_weight[np.newaxis, m:])
total_pair_weights = pair_weights.sum()
aucs = (
sample_weight[:m]*(tz[:, :m] - tx)
).sum(axis=1) / total_pair_weights
v01 = (tz[:, :m] - tx[:, :]) / total_negative_weights
v10 = 1. - (tz[:, m:] - ty[:, :]) / total_positive_weights
sx = np.cov(v01)
sy = np.cov(v10)
delongcov = sx / m + sy / n
return aucs, delongcov
def fastDeLong_no_weights(predictions_sorted_transposed, label_1_count):
"""
The fast version of DeLong's method for computing the covariance of
unadjusted AUC.
Reference:
@article{sun2014fast,
title={
Fast Implementation of DeLong's Algorithm for
Comparing the Areas Under Correlated Receiver Oerating
Characteristic Curves},
author={Xu Sun and Weichao Xu},
journal={IEEE Signal Processing Letters},
volume={21},
number={11},
pages={1389--1393},
year={2014},
publisher={IEEE}
}
Parameters
----------
predictions_sorted_transposed : ?
label_1_count : ?
predictions_sorted_transposed: a 2D
``numpy.array[n_classifiers, n_examples]``
sorted such as the examples with label "1" are first
Returns
-------
(AUC value, DeLong covariance)
"""
# Short variables are named as they are in the paper
m = label_1_count
n = predictions_sorted_transposed.shape[1] - m
positive_examples = predictions_sorted_transposed[:, :m]
negative_examples = predictions_sorted_transposed[:, m:]
k = predictions_sorted_transposed.shape[0]
tx = np.empty([k, m], dtype=float)
ty = np.empty([k, n], dtype=float)
tz = np.empty([k, m + n], dtype=float)
for r in range(k):
tx[r, :] = compute_midrank(positive_examples[r, :])
ty[r, :] = compute_midrank(negative_examples[r, :])
tz[r, :] = compute_midrank(predictions_sorted_transposed[r, :])
aucs = tz[:, :m].sum(axis=1) / m / n - float(m + 1.0) / 2.0 / n
v01 = (tz[:, :m] - tx[:, :]) / n
v10 = 1.0 - (tz[:, m:] - ty[:, :]) / m
sx = np.cov(v01)
sy = np.cov(v10)
delongcov = sx / m + sy / n
return aucs, delongcov
def calc_pvalue(aucs, sigma):
"""
Computes log(10) of p-values.
Parameters
----------
aucs: 1D array of AUCs
sigma: AUC DeLong covariances
Returns
-------
log10(pvalue)
"""
l_aux = np.array([[1, -1]])
z = np.abs(np.diff(aucs)) / np.sqrt(np.dot(np.dot(l_aux, sigma), l_aux.T))
return np.log10(2) + scipy.stats.norm.logsf(z, loc=0, scale=1) / np.log(10)
def compute_ground_truth_statistics(ground_truth, sample_weight):
assert np.array_equal(np.unique(ground_truth), [0, 1])
order = (-ground_truth).argsort()
label_1_count = int(ground_truth.sum())
if sample_weight is None:
ordered_sample_weight = None
else:
ordered_sample_weight = sample_weight[order]
return order, label_1_count, ordered_sample_weight
def delong_roc_variance(ground_truth, predictions, sample_weight=None):
"""
Computes ROC AUC variance for a single set of predictions
Parameters
----------
ground_truth: np.array
of 0 and 1
predictions: np.array
of floats of the probability of being class 1
"""
ground_truth_stats = compute_ground_truth_statistics(
ground_truth,
sample_weight)
order, label_1_count, ordered_sample_weight = ground_truth_stats
predictions_sorted_transposed = predictions[np.newaxis, order]
aucs, delongcov = fastDeLong(
predictions_sorted_transposed,
label_1_count,
ordered_sample_weight)
assert_msg = "There is a bug in the code, please forward this to the devs"
assert len(aucs) == 1, assert_msg
return aucs[0], delongcov
def delong_roc_test(ground_truth, pred_one, pred_two, sample_weight=None):
"""
Computes log(p-value) for hypothesis that two ROC AUCs are different
Parameters
----------
ground_truth: np.array
of 0 and 1
predictions_one: np.array
predictions of the first model,
np.array of floats of the probability of being class 1
predictions_two: np.array
predictions of the second model, np.array of floats of the
probability of being class 1
"""
order, label_1_count, _ = compute_ground_truth_statistics(
ground_truth,
sample_weight)
predictions_sorted_transposed = np.vstack(
(pred_one, pred_two))[:, order]
aucs, delongcov = fastDeLong(
predictions_sorted_transposed,
label_1_count,
sample_weight)
print(aucs, delongcov)
return calc_pvalue(aucs, delongcov)
def auc_ci_Delong(y_true, y_scores, alpha=.95):
"""AUC confidence interval via DeLong.
Computes de ROC-AUC with its confidence interval via delong_roc_variance
References
-----------
See this `Stack Overflow Question
<https://stackoverflow.com/questions/19124239/scikit-learn-roc-curve-with-confidence-intervals/53180614#53180614/>`_
for further details
Examples
--------
::
y_scores = np.array(
[0.21, 0.32, 0.63, 0.35, 0.92, 0.79, 0.82, 0.99, 0.04])
y_true = np.array([0, 1, 0, 0, 1, 1, 0, 1, 0])
auc, auc_var, auc_ci = auc_ci_Delong(y_true, y_scores, alpha=.95)
np.sqrt(auc_var) * 2
max(auc_ci) - min(auc_ci)
print('AUC: %s' % auc, 'AUC variance: %s' % auc_var)
print('AUC Conf. Interval: (%s, %s)' % tuple(auc_ci))
Out:
AUC: 0.8 AUC variance: 0.028749999999999998
AUC Conf. Interval: (0.4676719375452081, 1.0)
Parameters
----------
y_true : list
Ground-truth of the binary labels (allows labels between 0 and 1).
y_scores : list
Predicted scores.
alpha : float
Default 0.95
Returns
-------
auc : float
AUC
auc_var : float
AUC Variance
auc_ci : tuple
AUC Confidence Interval given alpha
"""
y_true = np.array(y_true)
y_scores = np.array(y_scores)
# Get AUC and AUC variance
auc, auc_var = delong_roc_variance(
y_true,
y_scores)
auc_std = np.sqrt(auc_var)
# Confidence Interval
lower_upper_q = np.abs(np.array([0, 1]) - (1 - alpha) / 2)
lower_upper_ci = stats.norm.ppf(
lower_upper_q,
loc=auc,
scale=auc_std)
lower_upper_ci[lower_upper_ci > 1] = 1
return auc, auc_var, lower_upper_ci