ahead.VAR.VAR
1import numpy as np 2 3from ..Base import Base 4from ..utils import multivariate as mv 5from ..utils import unimultivariate as umv 6from .. import config 7 8 9class VAR(Base): 10 """Vector AutoRegressive model 11 12 Parameters: 13 14 h: an integer; 15 forecasting horizon 16 17 level: an integer; 18 Confidence level for prediction intervals 19 20 lags: an integer; 21 the lag order 22 23 type_VAR: a string; 24 Type of deterministic regressors to include 25 ("const", "trend", "both", "none") 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::varf` through `rpy2` 36 37 averages_: a list of lists; 38 mean forecast in a list for each series 39 40 ranges_: a list of lists; 41 lower and upper prediction intervals in a list for each series 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_dfs_: a tuple of data frames; 56 each element of the tuple contains 3 columns, 57 mean forecast, lower + upper prediction intervals, 58 and a date index 59 60 Examples: 61 62 ```python 63 import pandas as pd 64 from ahead import VAR 65 66 # Data frame containing the time series 67 dataset = { 68 'date' : ['2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01'], 69 'series1' : [34, 30, 35.6, 33.3, 38.1], 70 'series2' : [4, 5.5, 5.6, 6.3, 5.1], 71 'series3' : [100, 100.5, 100.6, 100.2, 100.1]} 72 df = pd.DataFrame(dataset).set_index('date') 73 print(df) 74 75 # multivariate time series forecasting 76 v1 = VAR(h = 5, date_formatting = "original", type_VAR="none") 77 v1.forecast(df) 78 print(v1.result_dfs_) 79 ``` 80 81 """ 82 83 def __init__( 84 self, h=5, level=95, lags=1, type_VAR="none", date_formatting="original" 85 ): # type_VAR = "const", "trend", "both", "none" 86 87 assert type_VAR in ( 88 "const", 89 "trend", 90 "both", 91 "none", 92 ), "must have: type_VAR in ('const', 'trend', 'both', 'none')" 93 94 super().__init__( 95 h=h, 96 level=level, 97 ) 98 99 self.lags = lags 100 self.type_VAR = type_VAR 101 self.date_formatting = date_formatting 102 self.input_df = None 103 104 self.fcast_ = None 105 self.averages_ = None 106 self.ranges_ = None 107 self.output_dates_ = [] 108 self.mean_ = None 109 self.lower_ = None 110 self.upper_ = None 111 self.result_dfs_ = None 112 113 def forecast(self, df): 114 """Forecasting method from `VAR` 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("var") 130 131 # result ----- 132 ( 133 self.averages_, 134 self.ranges_, 135 _, 136 ) = mv.format_multivariate_forecast( 137 n_series=self.n_series, 138 date_formatting=self.date_formatting, 139 output_dates=self.output_dates_, 140 horizon=self.h, 141 fcast=self.fcast_, 142 ) 143 144 self.mean_ = np.asarray(self.fcast_.rx2["mean"]) 145 self.lower_ = np.asarray(self.fcast_.rx2["lower"]) 146 self.upper_ = np.asarray(self.fcast_.rx2["upper"]) 147 148 self.result_dfs_ = tuple( 149 umv.compute_result_df(self.averages_[i], self.ranges_[i]) 150 for i in range(self.n_series) 151 ) 152 153 return self
10class VAR(Base): 11 """Vector AutoRegressive model 12 13 Parameters: 14 15 h: an integer; 16 forecasting horizon 17 18 level: an integer; 19 Confidence level for prediction intervals 20 21 lags: an integer; 22 the lag order 23 24 type_VAR: a string; 25 Type of deterministic regressors to include 26 ("const", "trend", "both", "none") 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::varf` through `rpy2` 37 38 averages_: a list of lists; 39 mean forecast in a list for each series 40 41 ranges_: a list of lists; 42 lower and upper prediction intervals in a list for each series 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_dfs_: a tuple of data frames; 57 each element of the tuple contains 3 columns, 58 mean forecast, lower + upper prediction intervals, 59 and a date index 60 61 Examples: 62 63 ```python 64 import pandas as pd 65 from ahead import VAR 66 67 # Data frame containing the time series 68 dataset = { 69 'date' : ['2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01'], 70 'series1' : [34, 30, 35.6, 33.3, 38.1], 71 'series2' : [4, 5.5, 5.6, 6.3, 5.1], 72 'series3' : [100, 100.5, 100.6, 100.2, 100.1]} 73 df = pd.DataFrame(dataset).set_index('date') 74 print(df) 75 76 # multivariate time series forecasting 77 v1 = VAR(h = 5, date_formatting = "original", type_VAR="none") 78 v1.forecast(df) 79 print(v1.result_dfs_) 80 ``` 81 82 """ 83 84 def __init__( 85 self, h=5, level=95, lags=1, type_VAR="none", date_formatting="original" 86 ): # type_VAR = "const", "trend", "both", "none" 87 88 assert type_VAR in ( 89 "const", 90 "trend", 91 "both", 92 "none", 93 ), "must have: type_VAR in ('const', 'trend', 'both', 'none')" 94 95 super().__init__( 96 h=h, 97 level=level, 98 ) 99 100 self.lags = lags 101 self.type_VAR = type_VAR 102 self.date_formatting = date_formatting 103 self.input_df = None 104 105 self.fcast_ = None 106 self.averages_ = None 107 self.ranges_ = None 108 self.output_dates_ = [] 109 self.mean_ = None 110 self.lower_ = None 111 self.upper_ = None 112 self.result_dfs_ = None 113 114 def forecast(self, df): 115 """Forecasting method from `VAR` class 116 117 Parameters: 118 119 df: a data frame; 120 a data frame containing the input time series (see example) 121 122 """ 123 124 # get input dates, output dates, number of series, series names, etc. 125 self.init_forecasting_params(df) 126 127 # obtain time series object ----- 128 self.format_input() 129 130 self.get_forecast("var") 131 132 # result ----- 133 ( 134 self.averages_, 135 self.ranges_, 136 _, 137 ) = mv.format_multivariate_forecast( 138 n_series=self.n_series, 139 date_formatting=self.date_formatting, 140 output_dates=self.output_dates_, 141 horizon=self.h, 142 fcast=self.fcast_, 143 ) 144 145 self.mean_ = np.asarray(self.fcast_.rx2["mean"]) 146 self.lower_ = np.asarray(self.fcast_.rx2["lower"]) 147 self.upper_ = np.asarray(self.fcast_.rx2["upper"]) 148 149 self.result_dfs_ = tuple( 150 umv.compute_result_df(self.averages_[i], self.ranges_[i]) 151 for i in range(self.n_series) 152 ) 153 154 return self
Vector AutoRegressive model
Parameters:
h: an integer;
forecasting horizon
level: an integer;
Confidence level for prediction intervals
lags: an integer;
the lag order
type_VAR: a string;
Type of deterministic regressors to include
("const", "trend", "both", "none")
date_formatting: a string;
Currently:
- "original": yyyy-mm-dd
- "ms": milliseconds
Attributes:
fcast_: an object;
raw result from fitting R's `ahead::varf` through `rpy2`
averages_: a list of lists;
mean forecast in a list for each series
ranges_: a list of lists;
lower and upper prediction intervals in a list for each series
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_dfs_: a tuple of data frames;
each element of the tuple contains 3 columns,
mean forecast, lower + upper prediction intervals,
and a date index
Examples:
import pandas as pd
from ahead import VAR
# Data frame containing the time series
dataset = {
'date' : ['2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01'],
'series1' : [34, 30, 35.6, 33.3, 38.1],
'series2' : [4, 5.5, 5.6, 6.3, 5.1],
'series3' : [100, 100.5, 100.6, 100.2, 100.1]}
df = pd.DataFrame(dataset).set_index('date')
print(df)
# multivariate time series forecasting
v1 = VAR(h = 5, date_formatting = "original", type_VAR="none")
v1.forecast(df)
print(v1.result_dfs_)