-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharima.py
67 lines (53 loc) · 2.14 KB
/
arima.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
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from sktime.utils.plotting import plot_series
from skforecast.ForecasterSarimax import ForecasterSarimax
from pmdarima import ARIMA as SARIMA
class ARIMA:
def __init__(self) -> None:
self.forecaster = ForecasterSarimax(
regressor=SARIMA(order=(12, 1, 1), seasonal_order=(0, 0, 0, 0), maxiter=5),
)
def preprocess(self, data: pd.DataFrame):
# skforecast expects specific format
data["datetime"] = pd.to_datetime(data["t"])
data = data.set_index("datetime")
data = data.asfreq("D")
return data
def sequential(self, Y_train: pd.Series, df_test, fh: list = [1], k: int = 5):
# initialize training concatenation
training = Y_train
self.forecaster.fit(y=training)
# we let arima forecast based on the training,
assert k >= 1, "K should be a positive integer"
predictions = pd.Series(dtype=object)
print("Start forecasting")
for i in tqdm(range(k)):
# predict with predictive interval (not conformal)
y_pred = self.forecaster.predict_interval(
steps=1, alpha=0.1, interval=[5, 95]
)
# store predictions
predictions = pd.concat([predictions, y_pred])
# expand training data with recent y true values
training = pd.concat([training, df_test.iloc[i : i + 1, :]["y"]])
# fit forecaster with new observations
self.forecaster.fit(y=training)
return predictions
def plot_interval(self, Y_series: pd.Series, predictions: pd.DataFrame):
# plot the observations
fig, ax = plot_series(Y_series)
# plot the predictions
predictions["pred"].plot(ax=ax, label="prediction", color="red")
# plot the confidence intervals
ax.fill_between(
predictions.index,
predictions["lower_bound"],
predictions["upper_bound"],
color="purple",
alpha=0.3,
label="90% interval",
)
ax.legend(loc="upper left")