ahead.VAR
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_)