Reputation: 303
I would like to forecast a time series as in this example, with an ARIMA of order p=2.
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(series, order=(2,0,0))
fit = model.fit()
forecast = fit.forecast()[0]
This code fit.forecast()[0]
gives the forecast for the next one step, given the last two steps in the series series
.
If I use fit.forecast()[0:n]
it will give the forecast for the next n steps.
We have a series from time 0 to time t and the function forecast the next n steps (t+1, t+2...t+n). Since this is an ARIMA with p=2, every step is forecasted by using the last 2 steps as predictors. With this method, however, the last 2 step are actually the past forecasted values...
I would like to do a different thing. I would like to forecast each step (t+1, t+2...t+n) given the past actual values, not forecasted values. And more importantly, I don't want to update the model at every step, i.e. the regression coefficient should be always the same (the ones found by fitting in the range 0 to t)
Upvotes: 2
Views: 1497
Reputation: 3195
You are probably looking for details on how to perform an expanding window cross-validation exercise. Details can be found in the notebook https://www.statsmodels.org/devel/examples/notebooks/generated/statespace_forecasting.html and in particular in the "Cross Validation" section.
In brief, you want to do something like:
from statsmodels.tsa.arima.model import ARIMA
# Split data into test and training
nobs = len(series)
n_train = ...
series_train = series.iloc[:n_train]
# Fit the model using the training dataset
model = ARIMA(series_train, order=(2,0,0))
fit = model.fit()
# Compute the first forecast based only on the training dataset
forecasts = []
res = fit
forecasts.append(res.forecast())
# Now step through the test observations:
# (a) add the new observation without refitting the model
# (b) produce a new forecast
for t in range(n_train, nobs):
# Update the results by appending the next observation
res = res.append(series.iloc[t:t + 1], refit=False)
# Produce a forecast for t+1 based on data through t
forecasts.append(res.forecast())
Upvotes: 3