-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathr_forecast.py
342 lines (290 loc) · 11.4 KB
/
r_forecast.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
"""
This file contains various baselines which use the R package "forecast".
This requires R to be installed to run:
conda install r-essentials r-forecast r-unix
"""
import pandas as pd
import numpy as np
import time
_rpy2_initialized = False
try:
import rpy2.robjects.packages as rpackages
from rpy2.robjects import numpy2ri, pandas2ri
from rpy2 import rinterface, robjects
_rpy2_initialized = True
except:
pass
from .base import Baseline
from ..base import BaseTask
class R_ETS(Baseline):
__version__ = "0.0.1" # Modification will trigger re-caching
def __init__(
self,
model: str = "ZZZ",
):
"""
Get predictions from the ETS model.
Parameters:
-----------
model: str
Which model to use, a 3 letters code for error, trend, and seasonality.
The letters can be Z (automatic), A (additive), M (multiplicative) or N (none)
"""
if not _rpy2_initialized:
raise RuntimeError("The rpy2 package has not been successfully imported.")
super().__init__()
self.model = model
# Required R packages:
self._stats_pkg = None # "stats"
self._forecast_pkg = None # "forecast"
def __call__(self, task_instance: BaseTask, n_samples: int) -> np.ndarray:
starting_time = time.time()
samples = self.forecast(
past_time=task_instance.past_time,
future_time=task_instance.future_time,
seasonal_periods=task_instance.seasonal_period,
n_samples=n_samples,
)
extra_info = {
"total_time": time.time() - starting_time,
}
return samples, extra_info
def forecast(
self,
past_time: pd.DataFrame,
future_time: pd.DataFrame,
seasonal_periods: int,
n_samples: int,
) -> np.ndarray:
"""
This method allows a forecast to be done without requiring a complete BaseTask instance.
This is primarly meant to be called inside a BaseTask constructor when doing rejection sampling or similar approaches.
Note: If seasonal_periods is <= 0, then we set the period to 1, which skips it if the model uses "Z" as its seasonal component.
If the model uses "A" or "M", then it will fail.
"""
# Lazy initialization, since otherwise the parallel dispatching using pickle will not work
if self._stats_pkg is None:
self._stats_pkg = rpackages.importr("stats")
if self._forecast_pkg is None:
self._forecast_pkg = rpackages.importr("forecast")
history = pandas2ri.py2rpy(past_time[past_time.columns[-1]])
ts = self._stats_pkg.ts(history, frequency=max(1, seasonal_periods))
fit = self._forecast_pkg.ets(ts, model=self.model)
samples = np.stack(
[
numpy2ri.rpy2py(
self._forecast_pkg.simulate_ets(fit, nsim=len(future_time))
)
for _ in range(n_samples)
]
)
samples = samples[:, :, None]
if np.isnan(samples).any():
# If the model fails, then switch off the trend component of the model, then rerun it.
no_trend_model = self.model[0] + "N" + self.model[2]
fit = self._forecast_pkg.ets(ts, model=no_trend_model)
samples = np.stack(
[
numpy2ri.rpy2py(
self._forecast_pkg.simulate_ets(fit, nsim=len(future_time))
)
for _ in range(n_samples)
]
)
samples = samples[:, :, None]
return samples
@property
def cache_name(self) -> str:
args_to_include = ["model"]
return f"{self.__class__.__name__}_" + "_".join(
[f"{k}={getattr(self, k)}" for k in args_to_include]
)
def __getstate__(self):
state = self.__dict__.copy()
# Don't pickle the R packages
state["_stats_pkg"] = None
state["_forecast_pkg"] = None
return state
_R_ARIMA_CODE = r"""
require(parallel)
# require(pbmcapply) # mcapply with progress bars
require(forecast)
require(unix) # eval_safe
# Compute an affine transform to be applied to y before fitting the
# ARIMA model, of the form (y - A) / B + C.
# The last term (C) might be 1.0 for positive data, and corresponds to
# an additional bias that makes fitting ARIMA models more stable.
arima_scale_factors <- function(y, eps = 1.0, C = 0.0) {
A <- 0
B <- 1
sdy <- sd(y)
if (sdy > 0 && min(y) >= 0) {
miny <- min(y)
#maxy <- max(y)
A <- miny - eps
B <- sdy # Better behaved than the max for ARIMA fitting
if (is.null(C))
C <- min(1.0, miny)
}
else if (sdy > 0 && max(y) < 0) {
#miny <- min(y)
maxy <- max(y)
A <- maxy + eps
B <- sdy
if (is.null(C))
C <- max(-1.0, maxy)
}
stopifnot(B > 0)
list(A = A, B = B, C = C)
}
fit_arima_and_simulate <- function(y, prediction_length, num_samples,
max.p = 3, max.q = 3, max.P = 2, max.Q = 2,
stepwise=FALSE, num.cores=2,
timeout = 1800, ...)
{
# Filter simulation results (by column) that either:
# - are NA
# - are Infinite
# - Are more than 1000x (in absolute value) away from train values
filter_sim <- function(y, sim, thresh=1000) {
maxy <- max(abs(y))
keepsim <- is.finite(sim) & abs(sim) <= thresh * maxy
ii <- which(apply(keepsim, 2, all))
sim[, ii]
}
# Internal function that can be called a few times with ever simpler
# specifications if the fitting doesn't work.
fitsim <- function(y, max.p = 3, max.q = 3, max.P = 2, max.Q = 2,
stepwise, num.cores, timeout,
print_elapsed=FALSE, ...) {
tryCatch({
eval_safe({
tau <- system.time({
fit <- auto.arima(y,
max.p = max.p, max.q = max.q,
max.P = max.Q, max.Q = max.Q,
lambda=NULL, stepwise=stepwise,
num.cores=num.cores, ...)
}, gcFirst=FALSE)
if (print_elapsed) {
message("ARIMA fitting time: ",
tau["elapsed"])
flush.console()
}
# Now simulate. Replicate 5x more than the nominal num_samples,
# and keep num_samples post filtering
sim <- replicate(5*num_samples, simulate(fit, nsim=prediction_length))
finitesim <- filter_sim(y, sim)
return(list(fit=fit, fitting_time=tau["elapsed"],
sim=finitesim[, seq_len(min(num_samples,
ncol(finitesim)))]))
}, timeout=timeout)
},
error=function(e) {
message("Error in fit_arima_and_simulate: ", str(e))
flush.console()
return(NULL)
} )
}
simplified <- 0 # Number of times we've simplified
sc <- arima_scale_factors(y)
y <- (y - sc$A) / sc$B + sc$C
# Try fitting with the specified parameters, and if that fails, try again with a
# simpler model
fit <- fitsim(y,
max.p = max.p, max.q = max.q,
max.P = max.Q, max.Q = max.Q,
stepwise=stepwise,
num.cores=num.cores, timeout=timeout, ...)
if (!is.null(fit) && ncol(fit$sim) == num_samples) {
sim <- ((fit$sim - sc$C) * sc$B) + sc$A
fitted <- fitted.values(fit$fit)
fitted <- ((fitted - sc$C) * sc$B) + sc$A
if (all(is.finite(fitted)) && all(is.finite(sim)))
return(list(fit=fit$fit, sim=sim, fitted=fitted,
scaled_y=y, scale=sc, simplified=simplified,
fitting_time=fit$fitting_time))
}
# If we get here, the fit failed. Try again with a much simpler model.
# Voluntarily don't include ellipsis here.
simplified <- simplified + 1
fit <- fitsim(y, max.p=1, max.q=1, seasonal=FALSE,
stepwise=stepwise, num.cores=num.cores, timeout=timeout)
if (!is.null(fit) && ncol(fit$sim) == num_samples) {
sim <- ((fit$sim - sc$C) * sc$B) + sc$A
fitted <- fitted.values(fit$fit)
fitted <- ((fitted - sc$C) * sc$B) + sc$A
if (all(is.finite(fitted)) && all(is.finite(sim)))
return(list(fit=fit$fit, sim=sim, fitted=fitted,
scaled_y=y, scale=sc, simplified=simplified,
fitting_time=fit$fitting_time))
}
message("Returning NULL")
flush.console()
return(NULL)
}
"""
class R_Arima(Baseline):
__version__ = "0.0.1" # Modification will trigger re-caching
def __init__(
self,
):
"""
Get predictions from the Arima model.
"""
if not _rpy2_initialized:
raise RuntimeError("The rpy2 package has not been successfully imported.")
super().__init__()
# The R function to call:
self._fit_and_simulate = None
# Required R packages:
self._stats_pkg = None # "stats"
def __call__(self, task_instance: BaseTask, n_samples: int) -> np.ndarray:
starting_time = time.time()
samples = self.forecast(
past_time=task_instance.past_time,
future_time=task_instance.future_time,
seasonal_periods=task_instance.seasonal_period,
n_samples=n_samples,
)
extra_info = {
"total_time": time.time() - starting_time,
}
return samples, extra_info
def forecast(
self,
past_time: pd.DataFrame,
future_time: pd.DataFrame,
seasonal_periods: int,
n_samples: int,
) -> np.ndarray:
"""
This method allows a forecast to be done without requiring a complete BaseTask instance.
This is primarly meant to be called inside a BaseTask constructor when doing rejection sampling or similar approaches.
Note: If seasonal_periods is <= 0 (aka: no periods), then we set the period to 1.
"""
# Lazy initialization, since otherwise the parallel dispatching using pickle will not work
if self._stats_pkg is None:
self._stats_pkg = rpackages.importr("stats")
if self._fit_and_simulate is None:
rinterface.initr()
robjects.r(_R_ARIMA_CODE)
self._fit_and_simulate = robjects.r("fit_arima_and_simulate")
history = pandas2ri.py2rpy(past_time[past_time.columns[-1]])
ts = self._stats_pkg.ts(history, frequency=max(1, seasonal_periods))
prediction_length = len(future_time)
result = self._fit_and_simulate(
ts,
prediction_length=prediction_length,
num_samples=n_samples,
)
result_dict = dict(zip(result.names, list(result)))
samples = numpy2ri.rpy2py(result_dict["sim"]).T
samples = samples[:, :, None]
return samples
def __getstate__(self):
state = self.__dict__.copy()
# Don't pickle the R method and packages
state["_fit_and_simulate"] = None
state["_stats_pkg"] = None
return state