ahead.EAT

1from .EAT import EAT
2
3__all__ = ["EAT"]
class EAT(ahead.Base.Base.Base):
 10class EAT(Base):
 11    """Combinations of ETS (exponential smoothing), auto.arima and Theta models
 12
 13    Parameters:
 14
 15        h: an integer;
 16            forecasting horizon
 17
 18        level: an integer;
 19            Confidence level for prediction intervals
 20
 21        weights: a list;
 22            coefficients assigned to each method in the ensemble
 23
 24        type_pi: a string;
 25            Type of prediction interval (currently "gaussian",
 26            ETS: "E", Arima: "A" or Theta: "T")
 27
 28        date_formatting: a string;
 29            Currently:
 30            - "original": yyyy-mm-dd
 31            - "ms": milliseconds
 32
 33    Attributes:
 34
 35        fcast_: an object;
 36            raw result from fitting R's `ahead::eatf` through `rpy2`
 37
 38        averages_: a list;
 39            mean forecast in a list
 40
 41        ranges_: a list;
 42            lower and upper prediction intervals in a list
 43
 44        output_dates_: a list;
 45            a list of output dates (associated to forecast)
 46
 47        mean_: a numpy array
 48            contains series mean forecast as a numpy array
 49
 50        lower_: a numpy array
 51            contains series lower bound forecast as a numpy array
 52
 53        upper_: a numpy array
 54            contains series upper bound forecast as a numpy array
 55
 56        result_df_: a data frame;
 57            contains 3 columns, mean forecast, lower + upper
 58            prediction intervals, and a date index
 59
 60    Examples:
 61
 62    ```python
 63    import pandas as pd
 64    from ahead import EAT
 65
 66    # Data frame containing the time series
 67    dataset = {
 68    'date' : ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01'],
 69    'value' : [34, 30, 35.6, 33.3, 38.1]}
 70
 71    df = pd.DataFrame(dataset).set_index('date')
 72    print(df)
 73
 74    # univariate time series forecasting
 75    e1 = EAT(h = 5) # default, equal weights for each model=[1/3, 1/3, 1/3]
 76    e1.forecast(df)
 77    print(e1.result_df_)
 78    ```
 79
 80    """
 81
 82    def __init__(
 83        self,
 84        h=5,
 85        level=95,
 86        weights=None,
 87        type_pi="E",
 88        date_formatting="original",
 89    ):
 90
 91        super().__init__(h=h, level=level)
 92
 93        if weights is None:
 94            weights = [1 / 3, 1 / 3, 1 / 3]
 95
 96        assert len(weights) == 3, "must have 'len(weights) == 3'"
 97
 98        self.weights = weights
 99        self.type_pi = type_pi
100        self.date_formatting = date_formatting
101        self.input_df = None
102        self.type_input = "univariate"
103
104        self.fcast_ = None
105        self.averages_ = None
106        self.ranges_ = None
107        self.output_dates_ = []
108        self.mean_ = []
109        self.lower_ = []
110        self.upper_ = []
111        self.result_df_ = None
112
113    def forecast(self, df):
114        """Forecasting method from `EAT` class
115
116        Parameters:
117
118            df: a data frame;
119                a data frame containing the input time series (see example)
120
121        """
122
123        # get input dates, output dates, number of series, series names, etc.
124        self.init_forecasting_params(df)
125
126        # obtain time series object -----
127        self.format_input()
128
129        self.get_forecast("eat")
130
131        # result -----
132        (
133            self.averages_,
134            self.ranges_,
135            _,
136        ) = uv.format_univariate_forecast(
137            date_formatting=self.date_formatting,
138            output_dates=self.output_dates_,
139            horizon=self.h,
140            fcast=self.fcast_,
141        )
142
143        self.mean_ = np.asarray(self.fcast_.rx2["mean"])
144        self.lower_ = np.asarray(self.fcast_.rx2["lower"])
145        self.upper_ = np.asarray(self.fcast_.rx2["upper"])
146
147        self.result_df_ = umv.compute_result_df(self.averages_, self.ranges_)
148
149        return self

Combinations of ETS (exponential smoothing), auto.arima and Theta models

Parameters:

h: an integer;
    forecasting horizon

level: an integer;
    Confidence level for prediction intervals

weights: a list;
    coefficients assigned to each method in the ensemble

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::eatf` 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:

import pandas as pd
from ahead import EAT

# 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
e1 = EAT(h = 5) # default, equal weights for each model=[1/3, 1/3, 1/3]
e1.forecast(df)
print(e1.result_df_)