-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathuncertainty.py
More file actions
374 lines (321 loc) · 16.1 KB
/
Copy pathuncertainty.py
File metadata and controls
374 lines (321 loc) · 16.1 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
import re
from dataclasses import dataclass
from typing import Any, List, Tuple, Union
import matplotlib
import numpy as np
import pandas as pd
from neuralprophet.plot_forecast_matplotlib import plot_interval_width_per_timestep, plot_nonconformity_scores
from neuralprophet.plot_forecast_plotly import (
plot_interval_width_per_timestep as plot_interval_width_per_timestep_plotly,
)
from neuralprophet.plot_forecast_plotly import plot_nonconformity_scores as plot_nonconformity_scores_plotly
from neuralprophet.plot_utils import log_warning_deprecation_plotly, select_plotting_backend
@dataclass
class Conformal:
"""Conformal prediction dataclass
Parameters
----------
alpha : float or tuple
user-specified significance level of the prediction interval, float if coverage error spread arbitrarily over
left and right tails, tuple of two floats for different coverage error over left and right tails respectively
method : str
name of conformal prediction technique used
Options
* ``naive``: Naive or Absolute Residual
* ``cqr``: Conformalized Quantile Regression
n_forecasts : int
optional, number of steps ahead of prediction time step to forecast
quantiles : list
optional, list of quantiles for quantile regression uncertainty estimate
"""
alpha: Union[float, Tuple[float, float]]
method: str
n_forecasts: int
quantiles: List[float]
def __post_init__(self):
if isinstance(self.alpha, float):
self.symmetrical = True
self.q_hats = pd.DataFrame(columns=["q_hat_sym"])
elif self.method == "naive":
raise ValueError(
"Asymmetrical coverage errors are not available for the naive method. "
"Please use one alpha or method='cqr'."
)
else:
self.symmetrical = False
self.alpha_lo, self.alpha_hi = self.alpha
self.q_hats = pd.DataFrame(columns=["q_hat_lo", "q_hat_hi"])
self.noncon_scores = dict()
def predict(self, df: pd.DataFrame, df_cal: pd.DataFrame, show_all_PI: bool = False) -> pd.DataFrame:
"""Apply a given conformal prediction technique to get the uncertainty prediction intervals (or q-hat) for test
dataframe.
Parameters
----------
df : pd.DataFrame
test dataframe
df_cal : pd.DataFrame
calibration dataframe
show_all_PI : bool
whether to return all prediction intervals (including quantile regression and conformal prediction)
Returns
-------
pd.DataFrame
test dataframe with uncertainty prediction intervals
"""
df_qr = df.copy()
for step_number in range(1, self.n_forecasts + 1):
# conformalize
noncon_scores = self._get_nonconformity_scores(df_cal, step_number)
q_hat = self._get_q_hat(df_cal, noncon_scores)
y_hat_col = f"yhat{step_number}"
y_hat_lo_col = f"{y_hat_col} {min(self.quantiles) * 100}%"
y_hat_hi_col = f"{y_hat_col} {max(self.quantiles) * 100}%"
if self.method == "naive" and self.symmetrical:
q_hat_sym = q_hat["q_hat_sym"]
df[y_hat_lo_col] = df[y_hat_col] - q_hat_sym
df[y_hat_hi_col] = df[y_hat_col] + q_hat_sym
elif self.method == "cqr" and self.symmetrical:
q_hat_sym = q_hat["q_hat_sym"]
df[y_hat_lo_col] = df[y_hat_lo_col] - q_hat_sym
df[y_hat_hi_col] = df[y_hat_hi_col] + q_hat_sym
elif self.method == "cqr" and not self.symmetrical:
q_hat_lo = q_hat["q_hat_lo"]
q_hat_hi = q_hat["q_hat_hi"]
df[y_hat_lo_col] = df[y_hat_lo_col] - q_hat_lo
df[y_hat_hi_col] = df[y_hat_hi_col] + q_hat_hi
else:
raise ValueError(
f"Unknown conformal prediction method '{self.method}'. Please input either 'naive' or 'cqr'."
)
if step_number == 1:
# save nonconformity scores of the first timestep
self.noncon_scores = noncon_scores
# append the dictionary of q_hats to the dataframe based on the keys of the dictionary
q_hat_df = pd.DataFrame([q_hat])
self.q_hats = pd.concat([self.q_hats, q_hat_df], ignore_index=True)
# if show_all_PI is True, add the quantile regression prediction intervals
if show_all_PI:
df_quantiles = [col for col in df_qr.columns if "%" in col and f"yhat{step_number}" in col]
df_add = df_qr[df_quantiles]
if self.method == "naive":
cp_lo_col = f"yhat{step_number} - qhat{step_number}" # e.g. yhat1 - qhat1
cp_hi_col = f"yhat{step_number} + qhat{step_number}" # e.g. yhat1 + qhat1
df.rename(columns={y_hat_lo_col: cp_lo_col, y_hat_hi_col: cp_hi_col}, inplace=True)
elif self.method == "cqr":
qr_lo_col = (
f"yhat{step_number} {max(self.quantiles) * 100}% - qhat{step_number}" # e.g. yhat1 95% - qhat1
)
qr_hi_col = (
f"yhat{step_number} {min(self.quantiles) * 100}% + qhat{step_number}" # e.g. yhat1 5% + qhat1
)
df.rename(columns={y_hat_lo_col: qr_lo_col, y_hat_hi_col: qr_hi_col}, inplace=True)
df = pd.concat([df, df_add], axis=1, ignore_index=False)
return df
def _get_nonconformity_scores(self, df_cal: pd.DataFrame, step_number: int) -> dict:
"""Get the nonconformity scores using the given conformal prediction technique.
Parameters
----------
df_cal : pd.DataFrame
calibration dataframe
step_number : int
i-th step ahead forecast
Returns
-------
Dict[str, np.ndarray]
dictionary with one entry (symmetrical) or two entries (asymmetrical) of nonconformity scores
"""
y_hat_col = f"yhat{step_number}"
if self.method == "cqr":
# CQR nonconformity scoring function
quantile_lo = str(min(self.quantiles) * 100)
quantile_hi = str(max(self.quantiles) * 100)
quantile_lo_col = f"{y_hat_col} {quantile_lo}%"
quantile_hi_col = f"{y_hat_col} {quantile_hi}%"
if self.symmetrical:
def cqr_scoring_func(row):
return (
[None, None]
if row[quantile_lo_col] is None or row[quantile_hi_col] is None
else [
max(row[quantile_lo_col] - row["y"], row["y"] - row[quantile_hi_col]),
0 if row[quantile_lo_col] - row["y"] > row["y"] - row[quantile_hi_col] else 1,
]
)
scores_df = df_cal.apply(cqr_scoring_func, axis=1, result_type="expand")
scores_df.columns = ["scores", "arg"]
noncon_scores = scores_df["scores"].values
else: # asymmetrical intervals
def cqr_scoring_func(row):
return (
[None, None]
if row[quantile_lo_col] is None or row[quantile_hi_col] is None
else [
row[quantile_lo_col] - row["y"],
row["y"] - row[quantile_hi_col],
0 if row[quantile_lo_col] - row["y"] > row["y"] - row[quantile_hi_col] else 1,
]
)
scores_df = df_cal.apply(cqr_scoring_func, axis=1, result_type="expand")
scores_df.columns = ["scores_lo", "scores_hi", "arg"]
noncon_scores_lo = scores_df["scores_lo"].values
noncon_scores_hi = scores_df["scores_hi"].values
# Remove NaN values
noncon_scores_lo: Any = noncon_scores_lo[~pd.isnull(noncon_scores_lo)]
noncon_scores_hi: Any = noncon_scores_hi[~pd.isnull(noncon_scores_hi)]
# Sort
noncon_scores_lo.sort()
noncon_scores_hi.sort()
# return dict of nonconformity scores
return {"noncon_scores_hi": noncon_scores_lo, "noncon_scores_lo": noncon_scores_hi}
else: # self.method == "naive"
# Naive nonconformity scoring function
noncon_scores = abs(df_cal["y"] - df_cal[y_hat_col]).values
# Remove NaN values
noncon_scores: Any = noncon_scores[~pd.isnull(noncon_scores)]
# Sort
noncon_scores.sort()
return {"noncon_scores": noncon_scores}
def _get_q_hat(self, df_cal: pd.DataFrame, noncon_scores: dict) -> dict:
"""Get the q_hat that is derived from the nonconformity scores.
Parameters
----------
df_cal : pd.DataFrame
calibration dataframe
noncon_scores : dict
dictionary with one entry (symmetrical) or two entries (asymmetrical) of nonconformity scores
Returns
-------
Dict[str, float]
upper and lower q_hat value, or the one-sided prediction interval width
"""
# Get the q-hat index and value
if self.method == "cqr" and self.symmetrical is False:
noncon_scores_lo = noncon_scores["noncon_scores_lo"]
noncon_scores_hi = noncon_scores["noncon_scores_hi"]
q_hat_idx_lo = int(len(noncon_scores_lo) * self.alpha_lo)
q_hat_idx_hi = int(len(noncon_scores_hi) * self.alpha_hi)
q_hat_lo = noncon_scores_lo[-q_hat_idx_lo]
q_hat_hi = noncon_scores_hi[-q_hat_idx_hi]
return {"q_hat_lo": q_hat_lo, "q_hat_hi": q_hat_hi}
else:
noncon_scores = noncon_scores["noncon_scores"]
q_hat_idx = int(len(noncon_scores) * self.alpha)
q_hat = noncon_scores[-q_hat_idx]
return {"q_hat_sym": q_hat}
def plot(self, plotting_backend=None):
"""Apply a given conformal prediction technique to get the uncertainty prediction intervals (or q-hats).
Parameters
----------
plotting_backend : str
specifies the plotting backend for the nonconformity scores plot, if any
Options
* ``plotly-resampler``: Use the plotly backend for plotting in resample mode. This mode uses the
plotly-resampler package to accelerate visualizing large data by resampling it. For some
environments (colab, pycharm interpreter) plotly-resampler might not properly vizualise the figures.
In this case, consider switching to 'plotly-auto'.
* ``plotly``: Use the plotly backend for plotting
* ``matplotlib``: use matplotlib for plotting
* (default) None: Plotting backend ist set automatically. Use plotly with resampling for jupyterlab
notebooks and vscode notebooks. Automatically switch to plotly without resampling for all other
environments.
"""
method = self.method.upper() if "cqr" in self.method.lower() else self.method.title()
# Check whether a local or global plotting backend is set.
plotting_backend = select_plotting_backend(model=self, plotting_backend=plotting_backend)
log_warning_deprecation_plotly(plotting_backend)
initial_q_hat = (
self.q_hats["q_hat_sym"][0]
if self.symmetrical
else [self.q_hats["q_hat_lo"][0], self.q_hats["q_hat_hi"][0]]
)
if plotting_backend.startswith("plotly"):
if self.n_forecasts == 1:
fig = plot_nonconformity_scores_plotly(
self.noncon_scores,
self.alpha,
initial_q_hat,
method,
resampler_active=plotting_backend == "plotly-resampler",
)
else:
fig = plot_interval_width_per_timestep_plotly(self.q_hats, method, resampler_active=False)
fig.show()
elif plotting_backend == "no-backend-installed":
return None
else:
if self.n_forecasts == 1:
# includes nonconformity scores of the first timestep
fig = plot_nonconformity_scores(self.noncon_scores, self.alpha, initial_q_hat, method)
else:
fig = plot_interval_width_per_timestep(self.q_hats, method)
if plotting_backend in ["matplotlib", "plotly", "plotly-resampler"] and matplotlib.is_interactive():
fig
def uncertainty_evaluate(df_forecast: pd.DataFrame) -> pd.DataFrame:
"""Evaluate conformal prediction on test dataframe.
Parameters
----------
df_forecast : pd.DataFrame
forecast dataframe with the conformal prediction intervals
Returns
-------
pd.DataFrame
table containing evaluation metrics such as interval_width and miscoverage_rate
"""
# Remove beginning rows used as lagged regressors (if any), or future dataframes without y-values
# therefore, this ensures that all forecast rows for evaluation contains both y and y-hat
df_forecast_eval = df_forecast.dropna(subset=["y", "yhat1"]).reset_index(drop=True)
# Get evaluation params
df_eval = pd.DataFrame()
cols = df_forecast_eval.columns
yhat_cols = [col for col in cols if "%" in col]
n_forecasts = int(re.search("yhat(\\d+)", yhat_cols[-1]).group(1))
# get the highest and lowest quantile percentages
quantiles = []
for col in yhat_cols:
match = re.search(r"\d+\.\d+", col)
if match:
quantiles.append(float(match.group()))
quantiles = sorted(set(quantiles))
# Begin conformal evaluation steps
for step_number in range(1, n_forecasts + 1):
y = df_forecast_eval["y"].values
# only relevant if show_all_PI is true
if len([col for col in cols if "qhat" in col]) > 0:
qhat_cols = [col for col in cols if f"qhat{step_number}" in col]
yhat_lo = df_forecast_eval[qhat_cols[0]].values
yhat_hi = df_forecast_eval[qhat_cols[-1]].values
else:
yhat_lo = df_forecast_eval[f"yhat{step_number} {quantiles[0]}%"].values
yhat_hi = df_forecast_eval[f"yhat{step_number} {quantiles[-1]}%"].values
interval_width, miscoverage_rate = _get_evaluate_metrics_from_dataset(y, yhat_lo, yhat_hi)
# Construct row dataframe with current timestep using its q-hat, interval width, and miscoverage rate
col_names = ["interval_width", "miscoverage_rate"]
row = [interval_width, miscoverage_rate]
df_row = pd.DataFrame([row], columns=pd.MultiIndex.from_product([[f"yhat{step_number}"], col_names]))
# Add row dataframe to overall evaluation dataframe with all forecasted timesteps
df_eval = pd.concat([df_eval, df_row], axis=1)
return df_eval
def _get_evaluate_metrics_from_dataset(y: np.ndarray, yhat_lo: np.ndarray, yhat_hi: np.ndarray) -> Tuple[float, float]:
# df_forecast_eval: pd.DataFrame,
# quantile_lo_col: str,
# quantile_hi_col: str,
# ) -> Tuple[float, float]:
"""Infers evaluation parameters based on the evaluation dataframe columns.
Parameters
----------
df_forecast_eval : pd.DataFrame
forecast dataframe with the conformal prediction intervals
Returns
-------
float, float
conformal prediction evaluation metrics
"""
# Interval width (efficiency metric)
quantile_lo_mean = np.mean(yhat_lo)
quantile_hi_mean = np.mean(yhat_hi)
interval_width = quantile_hi_mean - quantile_lo_mean
# Miscoverage rate (validity metric)
n_covered = np.sum((y >= yhat_lo) & (y <= yhat_hi))
coverage_rate = n_covered / len(y)
miscoverage_rate = 1 - coverage_rate
return interval_width, miscoverage_rate