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