def test_ar1_forecast_simulation(self): am = arch_model(self.ar1, mean="AR", vol="GARCH", lags=[1]) res = am.fit(disp="off") with preserved_state(self.rng): forecast = res.forecast(horizon=5, start=0, method="simulation") y = np.asarray(self.ar1) index = self.ar1.index t = y.shape[0] params = np.array(res.params) resids = np.asarray(y[1:] - params[0] - params[1] * y[:-1]) vol = am.volatility params = np.array(res.params) backcast = vol.backcast(resids) var_bounds = vol.variance_bounds(resids) rng = am.distribution.simulate([]) vfcast = vol.forecast( params[2:], resids, backcast, var_bounds, start=0, method="simulation", rng=rng, horizon=5, ) const, ar = params[0], params[1] means = np.zeros((t, 5)) means[:, 0] = const + ar * y for i in range(1, 5): means[:, i] = const + ar * means[:, i - 1] means = pd.DataFrame(means, index=index, columns=["h.{0}".format(j) for j in range(1, 6)]) assert_frame_equal(means, forecast.mean) var = np.concatenate([[[np.nan] * 5], vfcast.forecasts]) rv = pd.DataFrame(var, index=index, columns=["h.{0}".format(j) for j in range(1, 6)]) assert_frame_equal(rv, forecast.residual_variance) lrv = rv.copy() for i in range(5): weights = (ar**np.arange(i + 1))**2 weights = weights[:, None] lrv.iloc[:, i:i + 1] = rv.values[:, :i + 1].dot(weights[::-1]) assert_frame_equal(lrv, forecast.variance)
def test_ar1_forecast_simulation(self): am = arch_model(self.ar1, mean='AR', vol='GARCH', lags=[1]) res = am.fit(disp='off') with preserved_state(self.rng): forecast = res.forecast(horizon=5, start=0, method='simulation') y = np.asarray(self.ar1) index = self.ar1.index t = y.shape[0] params = np.array(res.params) resids = np.asarray(y[1:] - params[0] - params[1] * y[:-1]) vol = am.volatility params = np.array(res.params) backcast = vol.backcast(resids) var_bounds = vol.variance_bounds(resids) rng = am.distribution.simulate([]) vfcast = vol.forecast(params[2:], resids, backcast, var_bounds, start=0, method='simulation', rng=rng, horizon=5) const, ar = params[0], params[1] means = np.zeros((t, 5)) means[:, 0] = const + ar * y for i in range(1, 5): means[:, i] = const + ar * means[:, i - 1] means = pd.DataFrame(means, index=index, columns=['h.{0}'.format(j) for j in range(1, 6)]) assert_frame_equal(means, forecast.mean) var = np.concatenate([[[np.nan] * 5], vfcast.forecasts]) rv = pd.DataFrame(var, index=index, columns=['h.{0}'.format(j) for j in range(1, 6)]) assert_frame_equal(rv, forecast.residual_variance) lrv = rv.copy() for i in range(5): weights = (ar ** np.arange(i + 1)) ** 2 weights = weights[:, None] lrv.iloc[:, i:i + 1] = rv.values[:, :i + 1].dot(weights[::-1]) assert_frame_equal(lrv, forecast.variance)