Skip to content

Commit 814a754

Browse files
committed
BUG: Correct exog forecast
1 parent d00198e commit 814a754

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

arch/tests/univariate/test_forecast.py

+19
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,22 @@ def test_forecast_simulation_horizon_1():
12051205
res = mod.fit(first_obs=0, last_obs=98)
12061206
res.forecast(start=1, x=x, method="simulation", simulations=2)
12071207
res.forecast(start=1, x=x, method="simulation", simulations=1)
1208+
1209+
1210+
def test_forecast_start():
1211+
rg = np.random.default_rng(0)
1212+
y = rg.standard_normal(10)
1213+
x = pd.DataFrame(rg.standard_normal((10, 1)), columns=["x"])
1214+
mod = ARX(y, x=x, lags=3)
1215+
res = mod.fit(first_obs=0, last_obs=98)
1216+
fcast = res.forecast(start=2, x=x)
1217+
fcast2 = res.forecast(start=2, method="simulation", simulations=1, x=x)
1218+
assert_allclose(fcast.mean, fcast2.mean)
1219+
1220+
c, p1, p2, p3, b, _ = res.params
1221+
oos = np.full((8, 1), np.nan)
1222+
for i in range(2, 9):
1223+
oos[i - 2, 0] = (
1224+
c + p1 * y[i] + p2 * y[i - 1] + p3 * y[i - 2] + b * x.iloc[i + 1, 0]
1225+
)
1226+
assert_allclose(fcast.mean, oos)

arch/univariate/mean.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,12 @@ def _reformat_forecast_x(
918918
f"the original sample size ({self._y.shape[0]}) or the number of "
919919
f"forecasts ({self._y.shape[0] - start})."
920920
)
921-
if arr.shape[1] > (self._y.shape[0] - start):
922-
arr = arr[:, start:]
921+
if arr.shape[1] == self._y.shape[0]:
922+
arr = arr[:, start:].copy()
923+
arr[:, :-1, :] = arr[:, 1:, :]
924+
# Nan fill last value because true out-of-sample not available
925+
# When using x same size as original y
926+
arr[:, -1:, :] = np.nan
923927
return arr
924928

925929
def forecast(

0 commit comments

Comments
 (0)