Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

python - ARMA out-of-sample prediction with statsmodels

I'm using statsmodels to fit a ARMA model.

import statsmodels.api as sm
arma    = sm.tsa.ARMA(data, order =(4,4));
results = arma.fit( full_output=False, disp=0);

Where data is a one-dimensional array. I know to get in-sample predictions:

pred = results.predict();

Now, given a second data set data2, how can I use the previously calibrated model to generate a series with forecasts (predictions) based in this observations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I thought there was an issue for this. If you file one on github, I'll be more likely to remember to add something like this. The prediction machinery is not (yet) available as user-facing functions, so you'd have to do something like this.

If you've fit a model already, then you can do this.

# this is the nsteps ahead predictor function
from statsmodels.tsa.arima_model import _arma_predict_out_of_sample
res = sm.tsa.ARMA(y, (3, 2)).fit(trend="nc")

# get what you need for predicting one-step ahead
params = res.params
residuals = res.resid
p = res.k_ar
q = res.k_ma
k_exog = res.k_exog
k_trend = res.k_trend
steps = 1

_arma_predict_out_of_sample(params, steps, residuals, p, q, k_trend, k_exog, endog=y, exog=None, start=len(y))

This is a new prediction 1 step ahead. You can tack this on to y, and you'll need to update your residuals.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...