Exemplo n.º 1
0
    def setup_class(cls):
        cls.rng = RandomState(12345)
        am = arch_model(None, mean="Constant", vol="Constant")
        data = am.simulate(np.array([0.0, 10.0]), 1000)
        data.index = pd.date_range("2000-01-01", periods=data.index.shape[0])
        cls.zero_mean = data.data

        am = arch_model(None, mean="AR", vol="Constant", lags=[1])
        data = am.simulate(np.array([1.0, 0.9, 2]), 1000)
        data.index = pd.date_range("2000-01-01", periods=data.index.shape[0])
        cls.ar1 = data.data

        am = arch_model(None, mean="AR", vol="Constant", lags=[1, 2])
        data = am.simulate(np.array([1.0, 1.9, -0.95, 2]), 1000)
        data.index = pd.date_range("2000-01-01", periods=data.index.shape[0])
        cls.ar2 = data.data

        am = arch_model(None, mean="HAR", vol="Constant", lags=[1, 5, 22])
        data = am.simulate(np.array([1.0, 0.4, 0.3, 0.2, 2]), 1000)
        data.index = pd.date_range("2000-01-01", periods=data.index.shape[0])
        cls.har3 = data.data

        am = arch_model(None, mean="AR", vol="GARCH", lags=[1, 2], p=1, q=1)
        data = am.simulate(np.array([1.0, 1.9, -0.95, 0.05, 0.1, 0.88]), 1000)
        data.index = pd.date_range("2000-01-01", periods=data.index.shape[0])
        cls.ar2_garch = data.data
Exemplo n.º 2
0
    def setup_class(cls):
        cls.rng = RandomState(12345)
        am = arch_model(None, mean='Constant', vol='Constant')
        data = am.simulate(np.array([0.0, 1.0]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.zero_mean = data.data

        am = arch_model(None, mean='AR', vol='Constant', lags=[1])
        data = am.simulate(np.array([1.0, 0.9, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar1 = data.data

        am = arch_model(None, mean='AR', vol='Constant', lags=[1, 2])
        data = am.simulate(np.array([1.0, 1.9, -0.95, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar2 = data.data

        am = arch_model(None, mean='HAR', vol='Constant', lags=[1, 5, 22])
        data = am.simulate(np.array([1.0, 0.4, 0.3, 0.2, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.har3 = data.data

        am = arch_model(None, mean='AR', vol='GARCH', lags=[1, 2], p=1, q=1)
        data = am.simulate(np.array([1.0, 1.9, -0.95, 0.1, 0.1, 0.88]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar2_garch = data.data
Exemplo n.º 3
0
    def setup_class(cls):
        np.random.seed(12345)
        am = arch_model(None, mean='Constant', vol='Constant')
        data = am.simulate(np.array([0.0, 1.0]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.zero_mean = data.data

        am = arch_model(None, mean='AR', vol='Constant', lags=[1])
        data = am.simulate(np.array([1.0, 0.9, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar1 = data.data

        am = arch_model(None, mean='AR', vol='Constant', lags=[1, 2])
        data = am.simulate(np.array([1.0, 1.9, -0.95, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar2 = data.data

        am = arch_model(None, mean='HAR', vol='Constant', lags=[1, 5, 22])
        data = am.simulate(np.array([1.0, 0.4, 0.3, 0.2, 0.1]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.har3 = data.data

        am = arch_model(None, mean='AR', vol='GARCH', lags=[1, 2], p=1, q=1)
        data = am.simulate(np.array([1.0, 1.9, -0.95, 0.1, 0.1, 0.88]), 1000)
        data.index = pd.date_range('2000-01-01', periods=data.index.shape[0])
        cls.ar2_garch = data.data
Exemplo n.º 4
0
 def test_holdback_ar(self):
     y = self.ar2_garch
     mod = arch_model(y, mean='AR', lags=1, hold_back=1)
     res_holdback = mod.fit()
     mod = arch_model(y, mean='AR', lags=1)
     res = mod.fit()
     assert_allclose(res_holdback.params, res.params)
Exemplo n.º 5
0
 def test_holdback_ar(self):
     y = self.ar2_garch
     mod = arch_model(y, mean='AR', lags=1, hold_back=1)
     res_holdback = mod.fit()
     mod = arch_model(y, mean='AR', lags=1)
     res = mod.fit()
     assert_allclose(res_holdback.params, res.params, rtol=1e-4, atol=1e-4)
Exemplo n.º 6
0
 def test_holdback_ar(self):
     y = self.ar2_garch
     mod = arch_model(y, mean="AR", lags=1, hold_back=1)
     res_holdback = mod.fit(disp="off")
     mod = arch_model(y, mean="AR", lags=1)
     res = mod.fit(disp="off")
     assert_allclose(res_holdback.params, res.params, rtol=1e-4, atol=1e-4)
Exemplo n.º 7
0
def test_x_forecasting(nexog):
    rng = RandomState(12345)
    mod = arch_model(None, mean="ARX", lags=2)
    data = mod.simulate([0.1, 1.2, -0.6, 0.1, 0.1, 0.8], nobs=1000)
    cols = [f"x{i}" for i in range(1, nexog + 1)]
    x = pd.DataFrame(
        rng.standard_normal((data.data.shape[0], nexog)),
        columns=cols,
        index=data.data.index,
    )
    b = np.array([0.25, 0.5]) if x.shape[1] == 2 else np.array([0.25])
    y = data.data + x @ b
    y.name = "y"
    mod = arch_model(y, x, mean="ARX", lags=2)
    res = mod.fit(disp="off")
    x_fcast = np.zeros((x.shape[1], 1, 10))
    for i in range(x_fcast.shape[0]):
        x_fcast[i] = np.arange(100 * i, 100 * i + 10)

    forecasts = res.forecast(x=x_fcast, horizon=10, reindex=False)
    direct = np.zeros(12)
    direct[:2] = y.iloc[-2:]
    p0, p1, p2 = res.params[:3]
    b0 = res.params[3]
    b1 = res.params[4] if x.shape[1] == 2 else 0.0
    for i in range(10):
        direct[i + 2] = p0 + p1 * direct[i + 1] + p2 * direct[i]
        direct[i + 2] += b0 * (i)
        direct[i + 2] += b1 * (100 + i)
    assert_allclose(forecasts.mean.iloc[0], direct[2:])
Exemplo n.º 8
0
    def test_first_obs(self):
        y = self.ar2_garch
        mod = arch_model(y)
        res = mod.fit(disp='off', first_obs=y.index[100])
        mod = arch_model(y[100:])
        res2 = mod.fit(disp='off')
        assert_allclose(res.params, res2.params)
        mod = arch_model(y)
        res3 = mod.fit(disp='off', first_obs=100)
        assert res.fit_start == 100
        assert_allclose(res.params, res3.params)

        forecast = res.forecast(horizon=3)
        assert np.all(np.isnan(forecast.mean.iloc[:-1]))
        assert np.all(np.isfinite(forecast.mean.iloc[-1]))
        assert np.all(np.isnan(forecast.variance.iloc[:-1]))
        assert np.all(np.isfinite(forecast.variance.iloc[-1]))

        forecast = res.forecast(horizon=3, start=y.index[100])
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))

        forecast = res.forecast(horizon=3, start=100)
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))

        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=y.index[98])

        res = mod.fit(disp='off')
        forecast = res.forecast(horizon=3)
        assert np.all(np.isnan(forecast.mean.iloc[:-1]))
        assert np.all(np.isfinite(forecast.mean.iloc[-1]))
        assert np.all(np.isnan(forecast.variance.iloc[:-1]))
        assert np.all(np.isfinite(forecast.variance.iloc[-1]))

        forecast = res.forecast(horizon=3, start=y.index[100])
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))
        forecast = res.forecast(horizon=3, start=0)
        assert np.all(np.isfinite(forecast.mean))
        assert np.all(np.isfinite(forecast.variance))

        mod = arch_model(y, mean='AR', lags=[1, 2])
        res = mod.fit(disp='off')
        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=0)

        forecast = res.forecast(horizon=3, start=1)
        assert np.all(np.isnan(forecast.mean.iloc[0]))
        assert np.all(np.isfinite(forecast.mean.iloc[1:]))
        assert np.all(np.isnan(forecast.variance.iloc[0]))
        assert np.all(np.isfinite(forecast.variance.iloc[2:]))
Exemplo n.º 9
0
    def test_first_obs(self):
        y = self.ar2_garch
        mod = arch_model(y)
        res = mod.fit(disp='off', first_obs=y.index[100])
        mod = arch_model(y[100:])
        res2 = mod.fit(disp='off')
        assert_allclose(res.params, res2.params)
        mod = arch_model(y)
        res3 = mod.fit(disp='off', first_obs=100)
        assert res.fit_start == 100
        assert_allclose(res.params, res3.params)

        forecast = res.forecast(horizon=3)
        assert np.all(np.isnan(forecast.mean.iloc[:-1]))
        assert np.all(np.isfinite(forecast.mean.iloc[-1]))
        assert np.all(np.isnan(forecast.variance.iloc[:-1]))
        assert np.all(np.isfinite(forecast.variance.iloc[-1]))

        forecast = res.forecast(horizon=3, start=y.index[100])
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))

        forecast = res.forecast(horizon=3, start=100)
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))

        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=y.index[98])

        res = mod.fit(disp='off')
        forecast = res.forecast(horizon=3)
        assert np.all(np.isnan(forecast.mean.iloc[:-1]))
        assert np.all(np.isfinite(forecast.mean.iloc[-1]))
        assert np.all(np.isnan(forecast.variance.iloc[:-1]))
        assert np.all(np.isfinite(forecast.variance.iloc[-1]))

        forecast = res.forecast(horizon=3, start=y.index[100])
        assert np.all(np.isnan(forecast.mean.iloc[:100]))
        assert np.all(np.isfinite(forecast.mean.iloc[100:]))
        assert np.all(np.isnan(forecast.variance.iloc[:100]))
        assert np.all(np.isfinite(forecast.variance.iloc[100:]))
        forecast = res.forecast(horizon=3, start=0)
        assert np.all(np.isfinite(forecast.mean))
        assert np.all(np.isfinite(forecast.variance))

        mod = arch_model(y, mean='AR', lags=[1, 2])
        res = mod.fit(disp='off')
        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=0)

        forecast = res.forecast(horizon=3, start=1)
        assert np.all(np.isnan(forecast.mean.iloc[0]))
        assert np.all(np.isfinite(forecast.mean.iloc[1:]))
        assert np.all(np.isnan(forecast.variance.iloc[0]))
        assert np.all(np.isfinite(forecast.variance.iloc[2:]))
Exemplo n.º 10
0
 def test_last_obs(self):
     y = self.ar2_garch
     mod = arch_model(y)
     res = mod.fit(disp='off', last_obs=y.index[900])
     res_2 = mod.fit(disp='off', last_obs=900)
     assert_allclose(res.params, res_2.params)
     mod = arch_model(y[:900])
     res_3 = mod.fit(disp='off')
     assert_allclose(res.params, res_3.params)
Exemplo n.º 11
0
 def test_last_obs(self):
     y = self.ar2_garch
     mod = arch_model(y)
     res = mod.fit(disp='off', last_obs=y.index[900])
     res_2 = mod.fit(disp='off', last_obs=900)
     assert_allclose(res.params, res_2.params)
     mod = arch_model(y[:900])
     res_3 = mod.fit(disp='off')
     assert_allclose(res.params, res_3.params)
Exemplo n.º 12
0
    def test_holdback_first_obs(self):
        y = self.ar2_garch
        mod = arch_model(y, hold_back=20)
        res_holdback = mod.fit(disp='off')
        mod = arch_model(y)
        res_first_obs = mod.fit(disp='off', first_obs=20)
        assert_allclose(res_holdback.params, res_first_obs.params)

        with pytest.raises(ValueError):
            res_holdback.forecast(start=18)
Exemplo n.º 13
0
    def test_holdback_first_obs(self):
        y = self.ar2_garch
        mod = arch_model(y, hold_back=20)
        res_holdback = mod.fit(disp='off')
        mod = arch_model(y)
        res_first_obs = mod.fit(disp='off', first_obs=20)
        assert_allclose(res_holdback.params, res_first_obs.params)

        with pytest.raises(ValueError):
            res_holdback.forecast(start=18)
Exemplo n.º 14
0
    def test_first_last_obs(self):
        y = self.ar2_garch
        mod = arch_model(y)
        res = mod.fit(disp="off", first_obs=y.index[100], last_obs=y.index[900])
        res_2 = mod.fit(disp="off", first_obs=100, last_obs=900)
        assert_allclose(res.params, res_2.params)
        mod = arch_model(y.iloc[100:900])
        res_3 = mod.fit(disp="off")
        assert_allclose(res.params, res_3.params)

        mod = arch_model(y)
        res_4 = mod.fit(disp="off", first_obs=100, last_obs=y.index[900])
        assert_allclose(res.params, res_4.params)
Exemplo n.º 15
0
    def test_holdback_lastobs(self):
        y = self.ar2_garch
        mod = arch_model(y, hold_back=20)
        res_holdback_last_obs = mod.fit(disp="off", last_obs=800)
        mod = arch_model(y)
        res_first_obs_last_obs = mod.fit(disp="off", first_obs=20, last_obs=800)
        assert_allclose(res_holdback_last_obs.params, res_first_obs_last_obs.params)
        mod = arch_model(y[20:800])
        res_direct = mod.fit(disp="off")
        assert_allclose(res_direct.params, res_first_obs_last_obs.params)

        with pytest.raises(ValueError):
            res_holdback_last_obs.forecast(start=18)
Exemplo n.º 16
0
 def test_fit_options(self):
     am = arch_model(self.zero_mean, mean="Constant", vol="Constant")
     res = am.fit(first_obs=100)
     res.forecast()
     res = am.fit(last_obs=900)
     res.forecast()
     res = am.fit(first_obs=100, last_obs=900)
     res.forecast()
     res.forecast(start=100)
     res.forecast(start=200)
     am = arch_model(self.zero_mean, mean="Constant", vol="Constant", hold_back=20)
     res = am.fit(first_obs=100)
     res.forecast()
Exemplo n.º 17
0
    def test_first_last_obs(self):
        y = self.ar2_garch
        mod = arch_model(y)
        res = mod.fit(disp='off', first_obs=y.index[100], last_obs=y.index[900])
        res_2 = mod.fit(disp='off', first_obs=100, last_obs=900)
        assert_allclose(res.params, res_2.params)
        mod = arch_model(y.iloc[100:900])
        res_3 = mod.fit(disp='off')
        assert_allclose(res.params, res_3.params)

        mod = arch_model(y)
        res_4 = mod.fit(disp='off', first_obs=100, last_obs=y.index[900])
        assert_allclose(res.params, res_4.params)
Exemplo n.º 18
0
 def test_fit_options(self):
     am = arch_model(self.zero_mean, mean='Constant', vol='Constant')
     res = am.fit(first_obs=100)
     res.forecast()
     res = am.fit(last_obs=900)
     res.forecast()
     res = am.fit(first_obs=100, last_obs=900)
     res.forecast()
     res.forecast(start=100)
     res.forecast(start=200)
     am = arch_model(self.zero_mean, mean='Constant', vol='Constant',
                     hold_back=20)
     res = am.fit(first_obs=100)
     res.forecast()
Exemplo n.º 19
0
    def test_first_obs(self):
        y = self.ar2_garch
        mod = arch_model(y)
        res = mod.fit(disp="off", first_obs=y.index[100])
        mod = arch_model(y[100:])
        res2 = mod.fit(disp="off")
        assert_allclose(res.params, res2.params)
        mod = arch_model(y)
        res3 = mod.fit(disp="off", first_obs=100)
        assert res.fit_start == 100
        assert_allclose(res.params, res3.params)

        forecast = res.forecast(horizon=3, reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))

        forecast = res.forecast(horizon=3, start=y.index[100], reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))

        forecast = res.forecast(horizon=3, start=100, reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))

        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=y.index[98], reindex=False)

        res = mod.fit(disp="off")
        forecast = res.forecast(horizon=3, reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))

        forecast = res.forecast(horizon=3, start=y.index[100], reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))
        forecast = res.forecast(horizon=3, start=0, reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isfinite(forecast.variance)))

        mod = arch_model(y, mean="AR", lags=[1, 2])
        res = mod.fit(disp="off")
        with pytest.raises(ValueError):
            res.forecast(horizon=3, start=0, reindex=False)

        forecast = res.forecast(horizon=3, start=1, reindex=False)
        assert np.all(np.asarray(np.isfinite(forecast.mean)))
        assert np.all(np.asarray(np.isnan(forecast.variance.iloc[:1])))
        assert np.all(np.asarray(np.isfinite(forecast.variance.iloc[1:])))
Exemplo n.º 20
0
    def test_zero_mean_forecast(self):
        am = arch_model(self.zero_mean, mean='Zero', vol='Constant')
        res = am.fit()
        fcast = res.forecast(res.params, horizon=3)
        alt_fcast = res.forecast(horizon=3)
        assert_frame_equal(fcast.mean, alt_fcast.mean)
        assert_frame_equal(fcast.variance, alt_fcast.variance)
        assert_frame_equal(fcast.residual_variance,
                           alt_fcast.residual_variance)

        assert np.all(np.isnan(fcast.mean[:-1]))
        assert np.all(np.isnan(fcast.variance[:-1]))
        assert np.all(np.isnan(fcast.residual_variance[:-1]))

        params = np.asarray(res.params)
        assert np.all(0.0 == fcast.mean.iloc[-1])
        assert_allclose(fcast.variance.iloc[-1],
                        np.ones(3) * params[0])
        assert_allclose(fcast.residual_variance.iloc[-1],
                        np.ones(3) * params[0])

        res = am.fit(last_obs=500)
        params = np.asarray(res.params)
        fcast = res.forecast(horizon=3)
        assert np.all(np.isnan(fcast.mean[:499]))
        assert np.all(np.isnan(fcast.variance[:499]))
        assert np.all(np.isnan(fcast.residual_variance[:499]))

        assert np.all(0.0 == fcast.mean[499:])
        assert_allclose(fcast.variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
        assert_allclose(fcast.residual_variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
Exemplo n.º 21
0
def get_GARCH_coeff(r):
    '''
    Finds the GARCH parameters.
    '''
    am = arch_model(r)
    tmp = am.fit()
    print(tmp.summary())
Exemplo n.º 22
0
def forecast_horizon_1(past_sales, arma_params, garch_params):
    sales = pd.DataFrame({'sales': np.sqrt(past_sales)})

    # Fit ARMA
    arma_model = ARMA(sales.sales, order=arma_params)
    arma_fit = arma_model.fit(disp=0)

    # Fit GARCH
    garch_model = arch_model(arma_fit.resid,
                             vol='garch',
                             p=garch_params[0],
                             q=garch_params[1])
    garch_fit = garch_model.fit(disp='off')

    # Forecast
    arma_forec = arma_fit.forecast()
    arma_forec_mean = arma_forec[0][0]
    arma_forec_ci = arma_forec[2][0]

    garch_forec = garch_fit.forecast(horizon=1, method='simulation')
    garch_forec_mean = garch_forec.mean['h.1'].iloc[-1]
    garch_forec_variance = garch_forec.variance['h.1'].iloc[-1]

    interval = [
        (arma_forec_ci[0] + garch_forec_mean - garch_forec_variance)**2,
        (arma_forec_ci[1] + garch_forec_mean + garch_forec_variance)**2
    ]
    return interval, arma_fit.aic, garch_fit.aic
Exemplo n.º 23
0
    def test_ar1_forecast(self):
        am = arch_model(self.ar1, mean='AR', vol='Constant', lags=[1])
        res = am.fit()

        fcast = res.forecast(horizon=5, start=0)
        params = np.asarray(res.params)
        direct = self.ar1.values

        for i in range(5):
            direct = params[0] + params[1] * direct
            assert_allclose(direct, fcast.mean.iloc[:, i])
            scale = np.sum((params[1] ** np.arange(i + 1)) ** 2.0)
            var = fcast.variance.iloc[1:, i]
            assert_allclose(var, scale * params[2] * np.ones_like(var))

        assert np.all(fcast.residual_variance[1:] == params[2])

        fcast = res.forecast(horizon=5)
        params = np.asarray(res.params)
        assert np.all(np.isnan(fcast.mean[:-1]))
        assert np.all(np.isnan(fcast.variance[:-1]))
        assert np.all(np.isnan(fcast.residual_variance[:-1]))
        assert np.all(fcast.residual_variance.iloc[-1] == params[-1])
        means = np.zeros(5)
        means[0] = params[0] + params[1] * self.ar1.iloc[-1]
        for i in range(1, 5):
            means[i] = params[0] + params[1] * means[i - 1]
        assert_allclose(means, fcast.mean.iloc[-1].values)
Exemplo n.º 24
0
    def test_zero_mean_forecast(self):
        am = arch_model(self.zero_mean, mean='Zero', vol='Constant')
        res = am.fit()
        fcast = res.forecast(res.params, horizon=3)
        alt_fcast = res.forecast(horizon=3)
        assert_frame_equal(fcast.mean, alt_fcast.mean)
        assert_frame_equal(fcast.variance, alt_fcast.variance)
        assert_frame_equal(fcast.residual_variance,
                           alt_fcast.residual_variance)

        assert np.all(np.isnan(fcast.mean[:-1]))
        assert np.all(np.isnan(fcast.variance[:-1]))
        assert np.all(np.isnan(fcast.residual_variance[:-1]))

        params = np.asarray(res.params)
        assert np.all(0.0 == fcast.mean.iloc[-1])
        assert_allclose(fcast.variance.iloc[-1], np.ones(3) * params[0])
        assert_allclose(fcast.residual_variance.iloc[-1],
                        np.ones(3) * params[0])

        res = am.fit(last_obs=500)
        params = np.asarray(res.params)
        fcast = res.forecast(horizon=3)
        assert np.all(np.isnan(fcast.mean[:499]))
        assert np.all(np.isnan(fcast.variance[:499]))
        assert np.all(np.isnan(fcast.residual_variance[:499]))

        assert np.all(0.0 == fcast.mean[499:])
        assert_allclose(fcast.variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
        assert_allclose(fcast.residual_variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
Exemplo n.º 25
0
    def test_zero_mean_forecast(self):
        am = arch_model(self.zero_mean, mean="Zero", vol="Constant")
        res = am.fit()
        fcast = res.forecast(res.params, horizon=3)
        alt_fcast = res.forecast(horizon=3)
        assert_frame_equal(fcast.mean, alt_fcast.mean)
        assert_frame_equal(fcast.variance, alt_fcast.variance)
        assert_frame_equal(fcast.residual_variance,
                           alt_fcast.residual_variance)

        assert np.all(np.asarray(np.isnan(fcast.mean[:-1])))
        assert np.all(np.asarray(np.isnan(fcast.variance[:-1])))
        assert np.all(np.asarray(np.isnan(fcast.residual_variance[:-1])))

        params = np.asarray(res.params)
        assert np.all(0.0 == fcast.mean.iloc[-1])
        assert_allclose(fcast.variance.iloc[-1], np.ones(3) * params[0])
        assert_allclose(fcast.residual_variance.iloc[-1],
                        np.ones(3) * params[0])

        res = am.fit(last_obs=500)
        params = np.asarray(res.params)
        fcast = res.forecast(horizon=3)
        assert np.all(np.asarray(np.isnan(fcast.mean[:499])))
        assert np.all(np.asarray(np.isnan(fcast.variance[:499])))
        assert np.all(np.asarray(np.isnan(fcast.residual_variance[:499])))

        assert np.all(np.asarray(0.0 == fcast.mean[499:]))
        assert_allclose(fcast.variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
        assert_allclose(fcast.residual_variance.iloc[499:],
                        np.ones((501, 3)) * params[0])
        with pytest.raises(ValueError,
                           match="horizon must be an integer >= 1"):
            res.forecast(horizon=0)
Exemplo n.º 26
0
def getegarchreturn(start, startfromenum, end, endfromenum, datefunc,
                    returnfunc):
    if startfromenum == True and endfromenum == True:
        startdate = start.value
        enddate = end.value
    elif startfromenum == True and endfromenum == False:
        startdate = start.value
        enddate = pandas.to_datetime(end)
    elif startfromenum == False and endfromenum == True:
        startdate = pandas.to_datetime(start)
        enddate = end.value
    elif startfromenum == False and endfromenum == False:
        startdate = pandas.to_datetime(start)
        enddate = pandas.to_datetime(end)
    else:
        print("invalid booleans")
    date = datefunc.tolist()
    startindex = date.index(startdate)
    endindex = date.index(enddate)
    archrange = returnfunc[startindex:endindex]
    am = arch.arch_model(archrange)
    am.volatility = arch.EGARCH(p=2, o=1, q=1)
    amres = am.fit(update_freq=0)
    print(amres)
    return amres.params
Exemplo n.º 27
0
    def test_ar1_forecast(self):
        am = arch_model(self.ar1, mean='AR', vol='Constant', lags=[1])
        res = am.fit()

        fcast = res.forecast(horizon=5, start=0)
        params = np.asarray(res.params)
        direct = self.ar1.values

        for i in range(5):
            direct = params[0] + params[1] * direct
            assert_allclose(direct, fcast.mean.iloc[:, i])
            scale = np.sum((params[1]**np.arange(i + 1))**2.0)
            var = fcast.variance.iloc[1:, i]
            assert_allclose(var, scale * params[2] * np.ones_like(var))

        assert np.all(fcast.residual_variance[1:] == params[2])

        fcast = res.forecast(horizon=5)
        params = np.asarray(res.params)
        assert np.all(np.isnan(fcast.mean[:-1]))
        assert np.all(np.isnan(fcast.variance[:-1]))
        assert np.all(np.isnan(fcast.residual_variance[:-1]))
        assert np.all(fcast.residual_variance.iloc[-1] == params[-1])
        means = np.zeros(5)
        means[0] = params[0] + params[1] * self.ar1.iloc[-1]
        for i in range(1, 5):
            means[i] = params[0] + params[1] * means[i - 1]
        assert_allclose(means, fcast.mean.iloc[-1].values)
Exemplo n.º 28
0
    def forecast_horizon_1(self):
        # Fit ARMA
        arma_model = SARIMAX(self.past_sales,
                             order=self.arma_params,
                             trend=self.arma_trend)
        arma_fit = arma_model.fit(disp=0)

        # Fit GARCH
        garch_model = arch_model(arma_fit.resid,
                                 vol='garch',
                                 p=self.garch_params[0],
                                 q=self.garch_params[1])
        garch_fit = garch_model.fit(disp='off')

        # Forecast
        arma_forec = arma_fit.get_forecast()
        arma_forec_mean = arma_forec.predicted_mean
        arma_forec_ci = [
            arma_forec.conf_int()[0][0],
            arma_forec.conf_int()[0][1]
        ]

        garch_forec = garch_fit.forecast(horizon=1, method='simulation')
        garch_forec_mean = garch_forec.mean['h.1'].iloc[-1]
        garch_forec_variance = garch_forec.variance['h.1'].iloc[-1]

        interval = [
            (arma_forec_ci[0] + garch_forec_mean - garch_forec_variance)**2,
            (arma_forec_ci[1] + garch_forec_mean + garch_forec_variance)**2
        ]
        return interval, arma_fit.aic, garch_fit.aic
Exemplo n.º 29
0
    def find_best_model_params(self):
        """
        This method finds the best parameters for the ARMA-GARCH model for the
        current time frame.
        
        The number of combinations is small and the method will only be called
        on occasion, so a more complex method for choosing the right parameters
        should not be needed.
        """
        print('Finding ARMA-GARCH parameters...')

        best_arma_aic = float('inf')
        best_arma_params = []
        for p in range(6):
            for q in range(6):
                for t in ['c', 't']:
                    try:
                        model = SARIMAX(self.past_sales,
                                        order=[p, 0, q],
                                        trend=t)
                        model_fit = model.fit(disp=0, iprint=0)
                        if model_fit.aic < best_arma_aic:
                            best_arma_aic = model_fit.aic
                            best_arma_params = [p, 0, q, t]
                    except:
                        pass

        arma_model = SARIMAX(
            self.past_sales,
            order=best_arma_params[:3],
            trend=best_arma_params[3],
        )
        arma_fit = arma_model.fit(disp=0)

        best_garch_aic = float('inf')
        best_garch_params = []
        for p in range(4):
            for q in range(4):
                try:
                    am = arch_model(arma_fit.resid, vol='garch', p=p, q=q)
                    arch_model_fitted = am.fit(disp='off')
                    if arch_model_fitted.aic < best_garch_aic:
                        best_garch_aic = arch_model_fitted.aic
                        best_garch_params = [p, q]
                except:
                    pass

        self.arma_params = best_arma_params[:3]
        self.arma_trend = best_arma_params[3]
        self.arma_aic = best_arma_aic
        self.garch_params = best_garch_params
        self.garch_aic = best_garch_aic
        print('...new model found: ARMA({},{})-GARCH({},{}), trend: {}'.format(
            self.arma_params[0],
            self.arma_params[2],
            self.garch_params[0],
            self.garch_params[1],
            self.arma_trend,
        ))
Exemplo n.º 30
0
def test_invalid_horizon():
    res = arch_model(SP500).fit(disp="off")
    with pytest.raises(ValueError, match="horizon must be"):
        res.forecast(horizon=-1, reindex=False)
    with pytest.raises(ValueError, match="horizon must be"):
        res.forecast(horizon=1.0, reindex=False)
    with pytest.raises(ValueError, match="horizon must be"):
        res.forecast(horizon="5", reindex=False)
Exemplo n.º 31
0
def forecast_sp500(y):
    mdl2 = arch_model(y, vol='GARCH', p=1, q=1, dist='t', rescale=False)
    estMdl2 = mdl2.fix([0, 0.0488, 0.1556, 0.8030,
                        7.45828])  #ask walter how he changed it xdd
    forecast2 = estMdl2.forecast(horizon=1, start=1500)
    vf_sp500 = np.square(((np.sqrt(forecast2.variance)) / SCALING))
    data2 = pd.DataFrame({'vf_sp500': [vf_sp500]})
    return data2, vf_sp500
Exemplo n.º 32
0
def forecast_stoxx(y):
    mdl1 = arch_model(y, vol='GARCH', p=1, q=1, dist='t', rescale=False)
    estMdl1 = mdl1.fix([0, 0.07714, 0.10801, 0.85755,
                        6.7707])  #ask walter how he changed it xdd
    forecast1 = estMdl1.forecast(horizon=1, start=1500)
    vf_stoxx = np.square(((np.sqrt(forecast1.variance)) / SCALING))
    data1 = pd.DataFrame({'vf_stoxx': [vf_stoxx]})
    return data1, vf_stoxx
Exemplo n.º 33
0
 def test_ar1_forecast_simulation_one(self):
     # Bug found when simulation=1
     am = arch_model(self.ar1, mean="AR", vol="GARCH", lags=[1])
     res = am.fit(disp="off")
     forecast = res.forecast(horizon=10,
                             method="simulation",
                             reindex=False,
                             simulations=1)
     assert forecast.simulations.variances.shape == (1, 1, 10)
Exemplo n.º 34
0
def build_garch11(series):
    ret = None

    if series is None:
        return ret

    garch11 = arch_model(series, dist='studentst')
    ret = garch11.fit(update_freq=5, disp='off', show_warning=False)

    return ret
Exemplo n.º 35
0
 def test_ar1_forecast_bootstrap(self):
     am = arch_model(self.ar1, mean='AR', vol='GARCH', lags=[1])
     res = am.fit(disp='off')
     rs = np.random.RandomState(98765432)
     state = rs.get_state()
     forecast = res.forecast(horizon=5, start=900, method='bootstrap',
                             random_state=rs)
     rs.set_state(state)
     repeat = res.forecast(horizon=5, start=900, method='bootstrap',
                           random_state=rs)
     assert_frame_equal(forecast.mean, repeat.mean, check_less_precise=True)
     assert_frame_equal(forecast.variance, repeat.variance, check_less_precise=True)
Exemplo n.º 36
0
 def test_ar1_forecast_bootstrap(self):
     am = arch_model(self.ar1, mean="AR", vol="GARCH", lags=[1])
     res = am.fit(disp="off")
     rs = np.random.RandomState(98765432)
     state = rs.get_state()
     forecast = res.forecast(
         horizon=5, start=900, method="bootstrap", random_state=rs
     )
     rs.set_state(state)
     repeat = res.forecast(horizon=5, start=900, method="bootstrap", random_state=rs)
     assert_frame_equal(forecast.mean, repeat.mean)
     assert_frame_equal(forecast.variance, repeat.variance)
Exemplo n.º 37
0
    def test_ar2_forecast(self):
        am = arch_model(self.ar2, mean="AR", vol="Constant", lags=[1, 2])
        res = am.fit()

        fcast = res.forecast(horizon=5)
        params = np.asarray(res.params)
        expected = np.zeros(7)
        expected[:2] = self.ar2.iloc[-2:]
        for i in range(2, 7):
            expected[i] = (
                params[0] + params[1] * expected[i - 1] + params[2] * expected[i - 2]
            )

        expected = expected[2:]
        assert np.all(np.asarray(np.isnan(fcast.mean.iloc[:-1])))
        assert_allclose(fcast.mean.iloc[-1], expected)

        expected = np.zeros(5)
        comp = np.array([res.params.iloc[1:3], [1, 0]])
        a = np.eye(2)
        for i in range(5):
            expected[i] = a[0, 0]
            a = a.dot(comp)
        expected = res.params.iloc[-1] * np.cumsum(expected ** 2)
        assert_allclose(fcast.variance.iloc[-1], expected)

        expected = np.empty((1000, 5))
        expected[:2] = np.nan
        expected[2:] = res.params.iloc[-1]

        fcast = res.forecast(horizon=5, start=1)
        expected = np.zeros((999, 7))
        expected[:, 0] = self.ar2.iloc[0:-1]
        expected[:, 1] = self.ar2.iloc[1:]
        for i in range(2, 7):
            expected[:, i] = (
                params[0]
                + params[1] * expected[:, i - 1]
                + params[2] * expected[:, i - 2]
            )
        fill = np.empty((1, 5))
        fill.fill(np.nan)
        expected = np.concatenate((fill, expected[:, 2:]))
        assert_allclose(np.asarray(fcast.mean), expected)

        expected = np.empty((1000, 5))
        expected[:2] = np.nan
        expected[2:] = res.params.iloc[-1]
        assert_allclose(np.asarray(fcast.residual_variance), expected)

        with pytest.raises(ValueError):
            res.forecast(horizon=5, start=0)
Exemplo n.º 38
0
 def updateVolatility(self):
     returns = self.assetHistory[:, 0].tolist()
     am = arch_model(returns)
     res = am.fit(disp='off')
     omega = res.params[1]
     alpha = res.params[2]
     beta = res.params[3]
     if self.lastVolatility == 0:
         prevVol = np.array(returns).var()
     else:
         prevVol = self.lastVolatility
     sigma2 = omega + alpha * (returns[-1]**2) + beta * prevVol
     self.lastVolatility = sigma2
     return (self.lastVolatility)
Exemplo n.º 39
0
    def test_ar2_forecast(self):
        am = arch_model(self.ar2, mean='AR', vol='Constant', lags=[1, 2])
        res = am.fit()

        fcast = res.forecast(horizon=5)
        params = np.asarray(res.params)
        expected = np.zeros(7)
        expected[:2] = self.ar2.iloc[-2:]
        for i in range(2, 7):
            expected[i] = params[0] + \
                          params[1] * expected[i - 1] + \
                          params[2] * expected[i - 2]

        expected = expected[2:]
        assert np.all(np.isnan(fcast.mean.iloc[:-1]))
        assert_allclose(fcast.mean.iloc[-1], expected)

        expected = np.zeros(5)
        comp = np.array([res.params.iloc[1:3], [1, 0]])
        a = np.eye(2)
        for i in range(5):
            expected[i] = a[0, 0]
            a = a.dot(comp)
        expected = res.params.iloc[-1] * np.cumsum(expected ** 2)
        assert_allclose(fcast.variance.iloc[-1], expected)

        expected = np.empty((1000, 5))
        expected[:2] = np.nan
        expected[2:] = res.params.iloc[-1]

        fcast = res.forecast(horizon=5, start=1)
        expected = np.zeros((999, 7))
        expected[:, 0] = self.ar2.iloc[0:-1]
        expected[:, 1] = self.ar2.iloc[1:]
        for i in range(2, 7):
            expected[:, i] = params[0] + \
                             params[1] * expected[:, i - 1] + \
                             params[2] * expected[:, i - 2]
        fill = np.empty((1, 5))
        fill.fill(np.nan)
        expected = np.concatenate((fill, expected[:, 2:]))
        assert_allclose(fcast.mean.values, expected)

        expected = np.empty((1000, 5))
        expected[:2] = np.nan
        expected[2:] = res.params.iloc[-1]
        assert_allclose(fcast.residual_variance.values, expected)

        with pytest.raises(ValueError):
            fcast = res.forecast(horizon=5, start=0)
Exemplo n.º 40
0
    def test_zero_mean_forecast(self):
        am = arch_model(self.zero_mean, mean="Zero", vol="Constant")
        res = am.fit()
        fcast = res.forecast(res.params, horizon=3, reindex=False)
        alt_fcast = res.forecast(horizon=3, reindex=False)
        assert_frame_equal(fcast.mean, alt_fcast.mean)
        assert_frame_equal(fcast.variance, alt_fcast.variance)
        assert_frame_equal(fcast.residual_variance,
                           alt_fcast.residual_variance)

        with pytest.raises(ValueError, match="horizon must"):
            res.forecast(res.params, horizon=3.0, reindex=False)
        with pytest.raises(ValueError, match="horizon must"):
            res.forecast(res.params, horizon=-1, reindex=False)
        with pytest.raises(ValueError, match="horizon must"):
            res.forecast(res.params, horizon="3", reindex=False)

        fcast_reindex = res.forecast(res.params, horizon=3, reindex=True)
        assert_frame_equal(fcast.mean, fcast_reindex.mean.iloc[-1:])
        assert_frame_equal(fcast.variance, fcast_reindex.variance.iloc[-1:])
        assert_frame_equal(fcast.residual_variance,
                           fcast_reindex.residual_variance.iloc[-1:])
        assert fcast_reindex.mean.shape[0] == self.zero_mean.shape[0]

        assert np.all(np.asarray(np.isnan(fcast.mean[:-1])))
        assert np.all(np.asarray(np.isnan(fcast.variance[:-1])))
        assert np.all(np.asarray(np.isnan(fcast.residual_variance[:-1])))

        params = np.asarray(res.params)
        assert np.all(0.0 == fcast.mean.iloc[-1])
        assert_allclose(fcast.variance.iloc[-1], np.ones(3) * params[0])
        assert_allclose(fcast.residual_variance.iloc[-1],
                        np.ones(3) * params[0])

        res = am.fit(last_obs=500)
        params = np.asarray(res.params)
        fcast = res.forecast(horizon=3, reindex=False)
        assert fcast.mean.shape == (501, 3)
        assert fcast.variance.shape == (501, 3)
        assert fcast.residual_variance.shape == (501, 3)
        assert np.all(np.asarray(np.isfinite(fcast.mean)))
        assert np.all(np.asarray(np.isfinite(fcast.variance)))
        assert np.all(np.asarray(np.isfinite(fcast.residual_variance)))

        assert np.all(np.asarray(0.0 == fcast.mean))
        assert_allclose(fcast.variance, np.ones((501, 3)) * params[0])
        assert_allclose(fcast.residual_variance, np.ones((501, 3)) * params[0])
        with pytest.raises(ValueError,
                           match="horizon must be an integer >= 1"):
            res.forecast(horizon=0, reindex=False)
Exemplo n.º 41
0
    def test_frame_labels(self):
        am = arch_model(self.zero_mean, mean='Zero', vol='Constant')
        res = am.fit()
        fcast = res.forecast(horizon=12)
        assert fcast.mean.shape[1] == 12
        assert fcast.variance.shape[1] == 12
        assert fcast.residual_variance.shape[1] == 12
        for i in range(1, 13):
            if i < 10:
                col = 'h.0' + str(i)
            else:
                col = 'h.' + str(i)

            assert col in fcast.mean.columns
            assert col in fcast.variance.columns
            assert col in fcast.residual_variance.columns
Exemplo n.º 42
0
    def test_constant_mean_forecast(self):
        am = arch_model(self.zero_mean, mean='Constant', vol='Constant')
        res = am.fit()
        fcast = res.forecast(horizon=5)

        assert np.all(np.isnan(fcast.mean[:-1]))
        assert np.all(np.isnan(fcast.variance[:-1]))
        assert np.all(np.isnan(fcast.residual_variance[:-1]))
        params = np.asarray(res.params)
        assert_allclose(params[0] * np.ones(5), fcast.mean.iloc[-1])
        assert_allclose(params[1] * np.ones(5), fcast.variance.iloc[-1])
        assert_allclose(params[1] * np.ones(5), fcast.residual_variance.iloc[-1])

        assert fcast.mean.shape == (self.zero_mean.shape[0], 5)
        assert fcast.variance.shape == (self.zero_mean.shape[0], 5)
        assert fcast.residual_variance.shape == (self.zero_mean.shape[0], 5)
Exemplo n.º 43
0
    def test_forecast_start_alternatives(self):
        am = arch_model(self.har3, mean='HAR', vol='Constant', lags=[1, 5, 22])
        res = am.fit()
        date = self.har3.index[21]
        fcast_1 = res.forecast(start=21)
        fcast_2 = res.forecast(start=date)
        for field in ('mean', 'variance', 'residual_variance'):
            assert_frame_equal(getattr(fcast_1, field),
                               getattr(fcast_2, field))
        pydt = dt.datetime(date.year, date.month, date.day)
        fcast_2 = res.forecast(start=pydt)
        for field in ('mean', 'variance', 'residual_variance'):
            assert_frame_equal(getattr(fcast_1, field),
                               getattr(fcast_2, field))

        strdt = pydt.strftime('%Y-%m-%d')
        fcast_2 = res.forecast(start=strdt)
        for field in ('mean', 'variance', 'residual_variance'):
            assert_frame_equal(getattr(fcast_1, field),
                               getattr(fcast_2, field))

        npydt = np.datetime64(pydt).astype('M8[ns]')
        fcast_2 = res.forecast(start=npydt)
        for field in ('mean', 'variance', 'residual_variance'):
            assert_frame_equal(getattr(fcast_1, field),
                               getattr(fcast_2, field))

        with pytest.raises(ValueError):
            date = self.har3.index[20]
            res.forecast(start=date)

        with pytest.raises(ValueError):
            date = self.har3.index[0]
            res.forecast(start=date)

        fcast_0 = res.forecast()
        fcast_1 = res.forecast(start=999)
        fcast_2 = res.forecast(start=self.har3.index[999])
        for field in ('mean', 'variance', 'residual_variance'):
            assert_frame_equal(getattr(fcast_0, field),
                               getattr(fcast_1, field))

            assert_frame_equal(getattr(fcast_0, field),
                               getattr(fcast_2, field))
Exemplo n.º 44
0
    def test_har_forecast(self):
        am = arch_model(self.har3, mean='HAR', vol='Constant', lags=[1, 5, 22])
        res = am.fit()
        fcast_1 = res.forecast(horizon=1)
        fcast_5 = res.forecast(horizon=5)
        assert_allclose(fcast_1.mean, fcast_5.mean.iloc[:, :1])

        with pytest.raises(ValueError):
            res.forecast(horizon=1, start=0)
        with pytest.raises(ValueError):
            res.forecast(horizon=1, start=20)

        fcast_66 = res.forecast(horizon=66, start=21)
        expected = np.empty((1000, 66 + 22))
        expected.fill(np.nan)
        for i in range(22):
            if i < 21:
                expected[21:, i] = self.har3.iloc[i:(-21 + i)]
            else:
                expected[21:, i] = self.har3.iloc[i:]
        params = np.asarray(res.params)
        const = params[0]
        arp = np.zeros(22)
        arp[0] = params[1]
        arp[:5] += params[2] / 5
        arp[:22] += params[3] / 22
        arp_rev = arp[::-1]
        for i in range(22, 88):
            expected[:, i] = const + expected[:, i - 22:i].dot(arp_rev)
        expected = expected[:, 22:]
        assert_allclose(fcast_66.mean, expected)

        expected[:22] = np.nan
        expected[22:] = res.params.iloc[-1]
        assert_allclose(fcast_66.residual_variance, expected)

        impulse = _ar_to_impulse(66, arp)
        expected = expected * np.cumsum(impulse ** 2)
        assert_allclose(fcast_66.variance, expected)
Exemplo n.º 45
0
    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)