-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring.py
486 lines (396 loc) · 16.5 KB
/
scoring.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"""
This code was adapted from the publicly available file at:
> https://github.com/adrian-lison/interval-scoring
Fetch date: 2023-02-02
No license was attributed to the code by the time of fetching.
"""
import numpy as np
# USER SCORES
# ----------------------------------------------
def alpha_covered_score(
observations,
alpha,
q_dict=None,
q_left=None,
q_right=None,
check_consistency=True,
return_type=None
):
"""
Calculates the simple "coverage" score for a given alpha value (representing the 1-alpha IQR).
For each data in the ``observations`` array and respective quantiles (q_dict), returns True if the observation
falls inside the range and False otherwise.
This function was extracted from:
https://github.com/adrian-lison/interval-scoring
- Accessed on 2023-02-02.
- No license information available as of the retrieval date.
Parameters
----------
observations : array_like
Ground truth observations.
alpha : numeric
Alpha level for (1-alpha) interval.
q_dict : dict, optional
Dictionary with predicted quantiles for all instances in `observations`.
q_left : array_like, optional
Predicted (alpha/2)-quantiles for all instances in `observations`.
q_right : array_like, optional
Predicted (1-(alpha/2))-quantiles for all instances in `observations`.
check_consistency : bool, optional
If `True`, quantiles in `q_dict` are checked for consistency. Default is `True`.
return_type : any, optional
Numerical type of the returned array elements. Can be `bool`, `int`, `float`.
Returns booleans as default.
Returns
-------
coverage : array_like
Array with the coverage score for each forecast/observation pair.
"""
# Preliminaries – Check and manage inputs
# ---------------------------------------
if q_dict is None:
if q_left is None or q_right is None:
raise ValueError(
"Either quantile dictionary or left and right quantile must be supplied."
)
else:
if q_left is not None or q_right is not None:
raise ValueError(
"Either quantile dictionary OR left and right quantile must be supplied, not both."
)
q_left = q_dict.get(alpha / 2)
if q_left is None:
raise ValueError(f"Quantile dictionary does not include {alpha/2}-quantile")
q_right = q_dict.get(1 - (alpha / 2))
if q_right is None:
raise ValueError(
f"Quantile dictionary does not include {1-(alpha/2)}-quantile"
)
if check_consistency and np.any(q_left > q_right):
raise ValueError("Left quantile must be smaller than right quantile.")
# Calculations
# ------------------------------------------
result: np.ndarray = np.logical_and(observations >= q_left, observations <= q_right)
if return_type:
result = result.astype(return_type)
return result
# EXTERNALLY IMPLEMENTED FUNCTIONS & SCORES
# -----------------------------------------------
# ## Interval Score
def interval_score(
observations,
alpha,
q_dict=None,
q_left=None,
q_right=None,
percent=False,
check_consistency=True,
):
"""
Compute interval scores (1) for an array of observations and
predicted intervals.
Either a dictionary with the respective (alpha/2) and (1-(alpha/2))
quantiles via q_dict needs to be
specified or the quantiles need to be specified via q_left and q_right.
This function was extracted from:
https://github.com/adrian-lison/interval-scoring
- Accessed on 2023-02-02.
- No license information available as of the retrieval date.
- May be modified from the original version.
Parameters
----------
observations : array_like
Ground truth observations.
alpha : numeric
Alpha level for (1-alpha) interval.
q_dict : dict, optional
Dictionary with predicted quantiles for all instances in `observations`.
q_left : array_like, optional
Predicted (alpha/2)-quantiles for all instances in `observations`.
q_right : array_like, optional
Predicted (1-(alpha/2))-quantiles for all instances in `observations`.
percent: bool, optional
If `True`, score is scaled by absolute value of observations to yield a percentage error. Default is `False`.
check_consistency: bool, optional
If `True`, quantiles in `q_dict` are checked for consistency. Default is `True`.
Returns
-------
total : array_like
Total interval scores.
sharpness : array_like
Sharpness component of interval scores.
calibration : array_like
Calibration component of interval scores.
(1) Gneiting, T. and A. E. Raftery (2007). Strictly proper scoring rules, prediction, and estimation. Journal of the American Statistical Association 102(477), 359–378.
"""
if q_dict is None:
if q_left is None or q_right is None:
raise ValueError(
"Either quantile dictionary or left and right quantile must be supplied."
)
else:
if q_left is not None or q_right is not None:
raise ValueError(
"Either quantile dictionary OR left and right quantile must be supplied, not both."
)
q_left = q_dict.get(alpha / 2)
if q_left is None:
raise ValueError(f"Quantile dictionary does not include {alpha/2}-quantile")
q_right = q_dict.get(1 - (alpha / 2))
if q_right is None:
raise ValueError(
f"Quantile dictionary does not include {1-(alpha/2)}-quantile"
)
if check_consistency and np.any(q_left > q_right):
raise ValueError("Left quantile must be smaller than right quantile.")
sharpness = q_right - q_left
calibration = (
(
np.clip(q_left - observations, a_min=0, a_max=None)
+ np.clip(observations - q_right, a_min=0, a_max=None)
)
* 2
/ alpha
)
if percent:
sharpness = sharpness / np.abs(observations)
calibration = calibration / np.abs(observations)
total = sharpness + calibration
return total, sharpness, calibration
# ## Weighted Interval Score
def weighted_interval_score(
observations, alphas, q_dict, weights=None, percent=False, check_consistency=True
):
"""
Compute weighted interval scores for an array of observations and a
number of different predicted intervals.
This function implements the WIS-score (2). A dictionary with the
respective (alpha/2) and (1-(alpha/2)) quantiles for all alpha
levels given in `alphas` needs to be specified.
This function was extracted from:
https://github.com/adrian-lison/interval-scoring
- Accessed on 2023-02-02.
- No license information available as of the retrieval date.
- May be modified from the original version.
Parameters
----------
observations : array_like
Ground truth observations.
alphas : iterable
Alpha levels for (1-alpha) intervals.
q_dict : dict
Dictionary with predicted quantiles for all instances
in `observations`.
weights : iterable, optional
Corresponding weights for each interval. If `None`, `weights`
is set to `alphas`, yielding the WIS^alpha-score.
percent: bool, optional
If `True`, score is scaled by absolute value of observations
to yield the double absolute percentage error. Default is `False`.
check_consistency: bool, optional
If `True`, quantiles in `q_dict` are checked for consistency.
Default is `True`.
Returns
-------
total : array_like
Total weighted interval scores.
sharpness : array_like
Sharpness component of weighted interval scores.
calibration : array_like
Calibration component of weighted interval scores.
(2) Bracher, J., Ray, E. L., Gneiting, T., & Reich, N. G. (2020).
Evaluating epidemic forecasts in an interval format.
arXiv preprint arXiv:2005.12881.
"""
if weights is None:
weights = np.array(alphas)/2
def weigh_scores(tuple_in, weight):
return tuple_in[0] * weight, tuple_in[1] * weight, tuple_in[2] * weight
interval_scores = [
i
for i in zip(
*[
weigh_scores(
interval_score(
observations,
alpha,
q_dict=q_dict,
percent=percent,
check_consistency=check_consistency,
),
weight,
)
for alpha, weight in zip(alphas, weights)
]
)
]
total = np.sum(np.vstack(interval_scores[0]), axis=0) / sum(weights)
sharpness = np.sum(np.vstack(interval_scores[1]), axis=0) / sum(weights)
calibration = np.sum(np.vstack(interval_scores[2]), axis=0) / sum(weights)
return total, sharpness, calibration
def weighted_interval_score_fast(
observations, alphas, q_dict, weights=None, percent=False, check_consistency=True
):
"""
Compute weighted interval scores for an array of observations and a
number of different predicted intervals.
This function implements the WIS-score (2). A dictionary with the
respective (alpha/2) and (1-(alpha/2)) quantiles for all alpha
levels given in `alphas` needs to be specified.
This is a more efficient implementation using array operations
instead of repeated calls of `interval_score`.
This function was extracted from:
https://github.com/adrian-lison/interval-scoring
- Accessed on 2023-02-02.
- No license information available as of the retrieval date.
- May be modified from the original version.
Parameters
----------
observations : array_like
Ground truth observations.
alphas : iterable
Alpha levels for (1-alpha) intervals.
q_dict : dict
Dictionary with predicted quantiles for all instances in
`observations`.
weights : iterable, optional
Corresponding weights for each interval. If `None`, `weights`
is set to `alphas`, yielding the WIS^alpha-score.
percent: bool, optional
If `True`, score is scaled by absolute value of observations
to yield a percentage error. Default is `False`.
check_consistency: bool, optional
If `True`, quantiles in `q_dict` are checked for consistency.
Default is `True`.
Returns
-------
total : array_like
Total weighted interval scores.
sharpness : array_like
Sharpness component of weighted interval scores.
calibration : array_like
Calibration component of weighted interval scores.
(2) Bracher, J., Ray, E. L., Gneiting, T., & Reich, N. G. (2020).
Evaluating epidemic forecasts in an interval format.
arXiv preprint arXiv:2005.12881.
"""
if weights is None:
weights = np.array(alphas)/2
if not all(alphas[i] <= alphas[i + 1] for i in range(len(alphas) - 1)):
raise ValueError("Alpha values must be sorted in ascending order.")
reversed_weights = list(reversed(weights))
lower_quantiles = [q_dict.get(alpha / 2) for alpha in alphas]
upper_quantiles = [q_dict.get(1 - (alpha / 2)) for alpha in reversed(alphas)]
if any(q is None for q in lower_quantiles) or any(
q is None for q in upper_quantiles
):
raise ValueError(
f"Quantile dictionary does not include all necessary quantiles."
)
lower_quantiles = np.vstack(lower_quantiles)
upper_quantiles = np.vstack(upper_quantiles)
# Check for consistency
if check_consistency and np.any(
np.diff(np.vstack((lower_quantiles, upper_quantiles)), axis=0) < 0
):
raise ValueError("Quantiles are not consistent.")
lower_q_alphas = (2 / np.array(alphas)).reshape((-1, 1))
upper_q_alphas = (2 / np.array(list(reversed(alphas)))).reshape((-1, 1))
# compute score components for all intervals
sharpnesses = np.flip(upper_quantiles, axis=0) - lower_quantiles
lower_calibrations = (
np.clip(lower_quantiles - observations, a_min=0, a_max=None) * lower_q_alphas
)
upper_calibrations = (
np.clip(observations - upper_quantiles, a_min=0, a_max=None) * upper_q_alphas
)
calibrations = lower_calibrations + np.flip(upper_calibrations, axis=0)
# scale to percentage absolute error
if percent:
sharpnesses = sharpnesses / np.abs(observations)
calibrations = calibrations / np.abs(observations)
totals = sharpnesses + calibrations
# weigh scores
weights = np.array(weights).reshape((-1, 1))
sharpnesses_weighted = sharpnesses * weights
calibrations_weighted = calibrations * weights
totals_weighted = totals * weights
# normalize and aggregate all interval scores
weights_sum = np.sum(weights)
sharpnesses_final = np.sum(sharpnesses_weighted, axis=0) / weights_sum
calibrations_final = np.sum(calibrations_weighted, axis=0) / weights_sum
totals_final = np.sum(totals_weighted, axis=0) / weights_sum
return totals_final, sharpnesses_final, calibrations_final
# ## Outside-Interval Count
def outside_interval(observations, lower, upper, check_consistency=True):
"""
Indicate whether observations are outside a predicted interval for an array of observations and predicted intervals.
Parameters
----------
observations : array_like
Ground truth observations.
lower : array_like, optional
Predicted lower interval boundary for all instances in `observations`.
upper : array_like, optional
Predicted upper interval boundary for all instances in `observations`.
check_consistency: bool, optional
If `True`, interval boundaries are checked for consistency. Default is `True`.
Returns
-------
Out : array_like
Array of zeroes (False) and ones (True) counting the number of times observations where outside the interval.
"""
if check_consistency and np.any(lower > upper):
raise ValueError("Lower border must be smaller than upper border.")
return ((lower > observations) + (upper < observations)).astype(int)
# ## Interval Consistency Score
def interval_consistency_score(
lower_old, upper_old, lower_new, upper_new, check_consistency=True
):
"""
Compute interval consistency scores for an old and a new interval.
Adapted variant of the interval score which measures the consistency of updated intervals over time.
Ideally, updated predicted intervals would always be within the previous estimates of the interval, yielding
a score of zero (best).
Parameters
----------
lower_old : array_like
Previous lower interval boundary for all instances in `observations`.
upper_old : array_like, optional
Previous upper interval boundary for all instances in `observations`.
lower_new : array_like
New lower interval boundary for all instances in `observations`. Ideally higher than the previous boundary.
upper_new : array_like, optional
New upper interval boundary for all instances in `observations`. Ideally lower than the previous boundary.
check_consistency: bool, optional
If interval boundaries are checked for consistency. Default is `True`.
Returns
-------
scores : array_like
Interval consistency scores.
"""
if check_consistency and (
np.any(lower_old > upper_old) or np.any(lower_new > upper_new)
):
raise ValueError("Left quantile must be smaller than right quantile.")
scores = np.clip(lower_old - lower_new, a_min=0, a_max=None) + np.clip(
upper_new - upper_old, a_min=0, a_max=None
)
return scores
# ## MAE
def mae_score(observations, point_forecasts):
return np.abs(observations - point_forecasts).mean(1)
# ## MAPE and sMAPE
def mape_score(observations, point_forecasts):
return 100 * np.abs(point_forecasts - observations) / np.abs(observations)
def smape_score(observations, point_forecasts):
return 100 * (
2
* np.abs(point_forecasts - observations)
/ (np.abs(observations) + np.abs(point_forecasts))
)
# ## MASE
def mase_score(observations, point_forecasts, horizon):
mae_naive = mae_score(observations[:, horizon:], observations[:, 0:-horizon])
mae_pred = mae_score(observations, point_forecasts)
return mae_pred / mae_naive