2
$\begingroup$

I am fitting an ARMA model to my data and here is my code

import statsmodels.tsa.arima_model as ari

model=ari.ARMA(pivoted['price'],(2,1))
ar_res=model.fit()
preds=ar_res.predict(100,400)

What I want is to train the ARMA model up to the 100th data point and then test out-of-sample on the 100-400th data points. But I don't think that is what's happening. I think this is because the model is fit on the entire data set, so how can the predictions be out-of-sample? It says here that out-of-sample prediction is possible. Can someone please help me understand this?

OK here is my new code

model=ari.ARMA(pivoted['price'][:100],order=(2,1))
ar_res=model.fit()
preds=ar_res.predict(100,400)

however it return the error

AssertionError: Index length did not match values
$\endgroup$
1
  • $\begingroup$ You can import the tsa namespace itself, by import statsmodels.tsa.api as tsa then do tsa.ARMA. $\endgroup$ – jseabold Nov 11 '13 at 10:06
2
$\begingroup$

You want to only give the first 100 data points to the ARMA call. Then you can predict out of sample, as you are doing. Alternatively, you can train on the whole dataset and then do dynamic prediction (using lagged predicted values) via the dynamic keyword to predict. Note that ARMA will fairly quickly converge to the long-run mean, provided that your series is well-behaved, so don't expect to get too much out of these very long-run prediction exercises.

$\endgroup$
2
  • $\begingroup$ I tried training on only the first 100 data points and testing on data points 100-400 but get the error shown in my above edit $\endgroup$ – user1893354 Nov 11 '13 at 23:56
  • $\begingroup$ Ok, you've posted this error in 3 threads now. I am unable to reproduce on current master. I suspect that you're not using the most up to date code. You'll have to post a fully reproducible example (ideally on github), if you want help to debug further. $\endgroup$ – jseabold Nov 12 '13 at 12:24
3
$\begingroup$

I ran into the same problem, and the documentation of statsmodel is unclear about this issue. I check the source code of ar_model, find the following parts in predict method(line 205):

    if dynamic:
        out_of_sample += end - start + 1
        return _ar_predict_out_of_sample(endog, params, k_ar,
                                         k_trend, out_of_sample, start)

Seems that in order to use out-of-sample prediction, the dynamic parameter must be set to True. However, the documentation said dynamic parameter only relates to in-sample prediction.

You can try this:

preds=ar_res.predict(100,400,dynamic = True)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.