forked from cdcepi/Flusight-forecast-data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
225 lines (193 loc) · 6.51 KB
/
metrics.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
from typing import Callable, Dict, List
import numpy as np
import properscoring as ps
from scipy.stats import norm
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
EPS = 1e-6
def rmse(predictions, targets):
"""
Root Mean Squared Error
Args:
predictions (np.ndarray): Point Predictions of the model
targets (np.ndarray): Point Targets of the model
Returns:
float: RMSE
"""
return np.sqrt(((predictions - targets) ** 2).mean())
def norm_rmse(predictions, targets):
"""
Root Mean Squared Error
Args:
predictions (np.ndarray): Point Predictions of the model
targets (np.ndarray): Point Targets of the model
Returns:
float: RMSE
"""
try:
scale = MinMaxScaler()
targets = scale.fit_transform(targets[:,None])
predictions = scale.transform(predictions[:,None])
finally:
return np.sqrt(((predictions - targets) ** 2).mean())
def mape(predictions, targets):
"""
Mean Absolute Percentage Error
Args:
predictions (np.ndarray): Predictions of the model
targets (np.ndarray): Targets of the model
Returns:
float: MAPE
"""
return np.mean(np.abs((predictions - targets) / targets)) * 100
# target ground truth
# mean -
def crps(mean, std, targets):
"""
Quantile-based CRPS
Args:
mean (np.ndarray): Mean of the distribution (N)
std (np.ndarray): Standard Deviation of the distribution (N)
targets (np.ndarray): Targets of the model (N)
Returns:
float: CRPS
"""
std_clip = np.clip(std, EPS, None)
ans1 = ps.crps_gaussian(targets, mean, std_clip)
ans2 = np.abs(targets - mean)
ans = (~np.isclose(std, 0, atol=1e-6)) * ans1 + (np.isclose(std, 0, atol=1e-6)) * ans2
return ans.mean()
# -1
def crps_samples(samples, targets):
"""
Quantile-based CRPS
Args:
samples (np.ndarray): Samples of the distribution (N, samples)
targets (np.ndarray): Targets of the model (N)
Returns:
float: CRPS
"""
return ps.crps_ensemble(targets, samples).mean()
def log_score(mean, std, targets, window=0.1):
"""
Log Score
Args:
mean (np.ndarray): Mean of the distribution (N)
std (np.ndarray): Standard Deviation of the distribution (N)
targets (np.ndarray): Targets of the model (N)
Returns:
float: Log Score
"""
# rescale, changed code
std = np.clip(std, EPS, None)
scale = MinMaxScaler()
targets = scale.fit_transform(targets)
mean = scale.transform(mean)
std = scale.scale_ * std
t1 = norm.cdf(targets - window / 2.0, mean, std)
t2 = norm.cdf(targets + window / 2.0, mean, std)
a = np.log(np.clip(t2 - t1, EPS, 1.0)).mean()
return np.clip(a, -10, 10)
# put in slack
def interval_score(mean, std, targets, window=1.0):
"""
Interval Score
Args:
mean (np.ndarray): Mean of the distribution (N)
std (np.ndarray): Standard Deviation of the distribution (N)
targets (np.ndarray): Targets of the model (N)
Returns:
float: Interval Score
"""
# rescale, changed code
std = np.clip(std, EPS, None)
scale = MinMaxScaler()
targets = scale.fit_transform(targets)
mean = scale.transform(mean)
std = scale.scale_ * std
rd_val = np.round(targets, decimals=1)
low_val = np.clip(rd_val - window / 2, a_min=0.0, a_max=None)
high_val = np.clip(rd_val + window / 2, a_min=None, a_max=13)
t1 = norm.cdf(low_val, loc=mean, scale=std)
t2 = norm.cdf(high_val, loc=mean, scale=std)
return np.log(np.clip(t2 - t1, a_min=EPS, a_max=1.0)).mean()
def conf_interval(mean, var, conf):
"""
Confintance Interval for given confidence level
Args:
mean (np.ndarray): Mean of the distribution (N)
var (np.ndarray): Variance of the distribution (N)
conf (float): Confidence level
Returns:
tuple: (low, high) interval
"""
var = np.clip(var, EPS, None)
out_prob = 1.0 - conf
high = norm.ppf(1.0 - (out_prob / 2), loc=mean, scale=var**0.5)
low = norm.ppf((1.0 - conf) / 2, loc=mean, scale=var**0.5)
return low, high
def pres_recall(mean, var, target, conf):
"""
Fraction of GT points within the confidence interval
Args:
mean (np.ndarray): Mean of the distribution (N)
var (np.ndarray): Variance of the distribution (N)
target (np.ndarray): Target of the model (N)
conf (float): Confidence level
Returns:
np.ndarray: Fraction of GT points within the confidence interval
"""
low, high = conf_interval(mean, var, conf)
truth = ((target > low) & (target < high)).astype("float32")
return truth.mean(-1)
# Plot
def get_pr(pred, var, target, color="blue", label="FluFNP"):
"""
Plot confidence and return Confidence score and AUC
Args:
pred (np.ndarray): Predictions of the model (N)
var (np.ndarray): Variance of the distribution (N)
target (np.ndarray): Target of the model (N)
color (str): Color of the line
label (str): Label of the model
Returns:
tuple: (Confidence score, AUC, fraction values)
"""
pred_, var_, target_ = pred.squeeze(), var.squeeze(), target.squeeze()
x = np.arange(0.05, 1.0, 0.01)
y = np.array([pres_recall(pred_, var_, target_, c) for c in x])
# plt.plot(list(x) + [1.0], list(y) + [1.0], label=label, color=color)
conf_score = np.abs(y - x).sum() * 0.01
auc = y.sum() * 0.01
return auc, conf_score, list(y) + [1.0]
def dist_from_quantiles(quantiles: Dict[float, float]) -> Callable[[float], float]:
"""
Returns an approximation cdf for a given quantile
Args:
quantiles (Dict[float, float]): Quantiles and corresponding values
Returns:
Callable: Approximation cdf
"""
def cdf(x: float) -> float:
for q, v in quantiles.items():
if x < v:
return q
return 1.0
return cdf
def crps_integrated(quantiles: List[Dict[float, float]], target: np.ndarray) -> float:
"""
Returns CRPS for a given quantile
Args:
quantiles (Dict[float, float]): Quantiles and corresponding values
target (float): Target of the model
Returns:
float: CRPS
"""
cdfs = [dist_from_quantiles(q) for q in quantiles]
# TODO: This is veeery slow. Can we do better?
return np.array(
[
ps.crps_quadrature([t], cdf, tol=1e-3)
for cdf, t in zip(cdfs, target.flatten())
]
).mean()