Skip to content

Commit

Permalink
Backport PR #1077: BUG: Allow 1d exog in ARMAX forecasting.
Browse files Browse the repository at this point in the history
Closes #1076. Another candidate for backporting.
  • Loading branch information
jseabold committed Nov 23, 2013
1 parent 452c3f4 commit c944f98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 17 additions & 2 deletions statsmodels/tsa/arima_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@
want out of sample prediction.
exog : array-like, optional
If the model is an ARMAX and out-of-sample forecasting is
requested, exog must be given.
requested, exog must be given. Note that you'll need to pass
`k_ar` additional lags for any exogenous variables. E.g., if you
fit an ARMAX(2, q) model and want to predict 5 steps, you need 7
observations to do this.
dynamic : bool, optional
The `dynamic` keyword affects in-sample prediction. If dynamic
is False, then the in-sample lagged values are used for
Expand Down Expand Up @@ -203,6 +206,15 @@ def _get_predict_out_of_sample(endog, p, q, k_trend, k_exog, start, errors,
if k_exog > 0:
#TODO: technically should only hold for MLE not
# conditional model. See #274.
# ensure 2-d for conformability
if np.ndim(exog) == 1 and k_exog == 1:
# have a 1d series of observations -> 2d
exog = exog[:, None]
elif np.ndim(exog) == 1:
# should have a 1d row of exog -> 2d
if len(exog) != k_exog:
raise ValueError("1d exog given and len(exog) != k_exog")
exog = exog[None, :]
X = lagmat(np.dot(exog, exparams), p, original='in', trim='both')
mu = trendparam * (1 - arparams.sum())
# arparams were reversed in unpack for ease later
Expand Down Expand Up @@ -1337,7 +1349,10 @@ def forecast(self, steps=1, exog=None, alpha=.05):
exog : array
If the model is an ARMAX, you must provide out of sample
values for the exogenous variables. This should not include
the constant.
the constant. Note that you'll need to pass `k_ar` additional
lags for any exogenous variables. E.g., if you fit an ARMAX(2, q)
model and want to predict 5 steps, you need 7 observations
to do this.
alpha : float
The confidence intervals for the forecasts are (1 - alpha) %
Expand Down
9 changes: 9 additions & 0 deletions statsmodels/tsa/tests/test_arima.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,15 @@ def test_arima_dataframe_integer_name():
df = pandas.DataFrame(ts)
mod = sm.tsa.ARIMA(df, (2, 0, 2))

def test_arima_exog_predict_1d():
# test 1067
np.random.seed(12345)
y = np.random.random(100)
x = np.random.random(100)
mod = ARMA(y, (2, 1), x).fit(disp=-1)
newx = np.random.random(12)
results = mod.forecast(steps=10, alpha=0.05, exog=newx)

if __name__ == "__main__":
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb'], exit=False)

0 comments on commit c944f98

Please sign in to comment.