def _fit_forecaster(self, y, X=None): """Fit forecaster to training data. Wraps Statsmodel's VAR fit method. Parameters ---------- y : pd.DataFrame Target time series to which to fit the forecaster. fh : int, list, np.array or ForecastingHorizon, optional (default=None) The forecasters horizon with the steps ahead to to predict. X : pd.DataFrame, optional (default=None) Returns ------- self : returns an instance of self. """ self._forecaster = _VAR(endog=y, exog=X, missing=self.missing) self._fitted_forecaster = self._forecaster.fit( trend=self.trend, maxlags=self.maxlags, method=self.method, verbose=self.verbose, ) return self
def VAR(ts, max_order=15): """Vector Autoregressive Baseline Generator.""" # VAR model if isinstance(ts, pd.DataFrame): var = _VAR(ts.values) elif isinstance(ts, np.ndarray): var = _VAR(ts) # optimal order order = var.select_order(max_order)['aic'] # fit model model = var.fit(order) # simulation ts_gen = var_util.varsim(model.coefs, model.intercept, model.sigma_u, steps=len(ts.values)) return ts_gen
def VAR(df, max_order=15, return_model=False): """Vector Autoregressive Baseline Generator.""" # VAR model var = _VAR(df) # fit model model = var.fit(maxlags=max_order, ic='aic') # simulation ts_gen = model.simulate_var(len(df.values)) # Create pandas DataFrame df_gen = pd.DataFrame(ts_gen, columns=df.columns, index=df.index[-len(ts_gen):]) if return_model: return df_gen, model else: return df_gen
def test_VAR_against_statsmodels(): """Compares Sktime's and Statsmodel's VAR.""" train, test = temporal_train_test_split(df) sktime_model = VAR() fh = ForecastingHorizon([1, 3, 4, 5, 7, 9]) sktime_model.fit(train) y_pred = sktime_model.predict(fh=fh) stats = _VAR(train) stats_fit = stats.fit() fh_int = fh.to_relative(train.index[-1]) lagged = stats_fit.k_ar y_pred_stats = stats_fit.forecast(train.values[-lagged:], steps=fh_int[-1]) new_arr = [] for i in fh_int: new_arr.append(y_pred_stats[i - 1]) assert_allclose(y_pred, new_arr)