def test_x_exceptions(): res = ARX(SP500, lags=1).fit(disp="off") with pytest.raises(TypeError, match="x is not None but"): res.forecast(reindex=False, x=SP500) x = SP500.copy() x[:] = np.random.standard_normal(SP500.shape) res = ARX(SP500, lags=1, x=x).fit(disp="off") with pytest.raises(TypeError, match="x is None but the model"): res.forecast(reindex=False) res = ARX(SP500, lags=1, x=x).fit(disp="off") with pytest.raises(ValueError, match="x must have the same"): res.forecast(reindex=False, x={}) with pytest.raises(ValueError, match="x must have the same"): res.forecast(reindex=False, x={"x0": x, "x1": x}) with pytest.raises(KeyError, match="The keys of x must exactly"): res.forecast(reindex=False, x={"z": x}) with pytest.raises(ValueError, match="The arrays contained in the dictionary"): _x = np.asarray(x).reshape((1, x.shape[0], 1)) res.forecast(reindex=False, x={"x0": _x}) x2 = pd.concat([x, x], 1) x2.columns = ["x0", "x1"] x2.iloc[:, 1] = np.random.standard_normal(SP500.shape) res = ARX(SP500, lags=1, x=x2).fit(disp="off") with pytest.raises(ValueError, match="The shapes of the arrays contained"): res.forecast(reindex=False, x={ "x0": x2.iloc[:, 0], "x1": x2.iloc[10:, 1:] }) with pytest.raises(ValueError, match="1- and 2-dimensional x values"): res.forecast(reindex=False, x=x2) with pytest.raises(ValueError, match="The leading dimension of x"): _x2 = np.asarray(x2) _x2 = _x2.reshape((1, -1, 2)) res.forecast(reindex=False, x=_x2) with pytest.raises(ValueError, match="The number of values passed"): res.forecast(reindex=False, x=np.empty((2, SP500.shape[0], 3))) with pytest.raises(ValueError, match="The shape of x does not satisfy the"): res.forecast(reindex=False, x=np.empty((2, SP500.shape[0] // 2, 1)))
from arch.univariate import ARCH, GARCH ar.volatility = GARCH(p=3, o=0, q=3) res = ar.fit(update_freq=0, disp='off') p(res.summary()) # In[265]: from arch.univariate import StudentsT ar.distribution = StudentsT() res = ar.fit(update_freq=0, disp='off') p(res.summary()) # In[266]: arf = ar.forecast(horizon=forecast_steps, start=Y.index[-1], params=res.params, method='simulation') # In[267]: plt.plot(idx, arf.simulations.values[-1].T, color='blue', alpha=0.1) plt.show() # In[269]: plt.style.use('bmh') fig, ax = plt.subplots() plt.plot(idx, arf.simulations.variances[-1, ::].T, color='blue', alpha=0.1,