/
DynamicRegressor.py
135 lines (97 loc) · 3.44 KB
/
DynamicRegressor.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
import numpy as np
from ..Base import Base
from ..utils import univariate as uv
from ..utils import unimultivariate as umv
from .. import config
class DynamicRegressor(Base):
"""Dynamic Regression Model adapted from R's `forecast::nnetar`
Parameters:
h: an integer;
forecasting horizon
level: an integer;
Confidence level for prediction intervals
type_pi: a string;
Type of prediction interval (currently "gaussian",
ETS: "E", Arima: "A" or Theta: "T")
date_formatting: a string;
Currently:
- "original": yyyy-mm-dd
- "ms": milliseconds
Attributes:
fcast_: an object;
raw result from fitting R's `ahead::dynrmf` through `rpy2`
averages_: a list;
mean forecast in a list
ranges_: a list;
lower and upper prediction intervals in a list
output_dates_: a list;
a list of output dates (associated to forecast)
mean_: a numpy array
contains series mean forecast as a numpy array
lower_: a numpy array
contains series lower bound forecast as a numpy array
upper_: a numpy array
contains series upper bound forecast as a numpy array
result_df_: a data frame;
contains 3 columns, mean forecast, lower + upper
prediction intervals, and a date index
Examples:
```python
import pandas as pd
from ahead import DynamicRegressor
# Data frame containing the time series
dataset = {
'date' : ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01'],
'value' : [34, 30, 35.6, 33.3, 38.1]}
df = pd.DataFrame(dataset).set_index('date')
print(df)
# univariate time series forecasting
d1 = DynamicRegressor(h = 5)
d1.forecast(df)
print(d1.result_df_)
```
"""
def __init__(self, h=5, level=95, type_pi="E", date_formatting="original"):
super().__init__(
h=h,
level=level,
)
self.type_pi = type_pi
self.date_formatting = date_formatting
self.input_df = None
self.type_input = "univariate"
self.fcast_ = None
self.averages_ = None
self.ranges_ = None
self.output_dates_ = []
self.mean_ = []
self.lower_ = []
self.upper_ = []
self.result_df_ = None
def forecast(self, df):
"""Forecasting method from `DynamicRegressor` class
Parameters:
df: a data frame;
a data frame containing the input time series (see example)
"""
# get input dates, output dates, number of series, series names, etc.
self.init_forecasting_params(df)
# obtain time series object -----
self.format_input()
self.get_forecast("dynrm")
# result -----
(
self.averages_,
self.ranges_,
_,
) = uv.format_univariate_forecast(
date_formatting=self.date_formatting,
output_dates=self.output_dates_,
horizon=self.h,
fcast=self.fcast_,
)
self.mean_ = np.asarray(self.fcast_.rx2["mean"])
self.lower_ = np.asarray(self.fcast_.rx2["lower"])
self.upper_ = np.asarray(self.fcast_.rx2["upper"])
self.result_df_ = umv.compute_result_df(self.averages_, self.ranges_)
return self