Discussion:
[pystatsmodels] ARMA out-of-sample forecasts
George Milunovich
2018-09-28 06:00:20 UTC
Permalink
Hi all,

I have a data sample y which is split into y_train and y_test. I would like
to fit ARMA on y_train, and then generate a series of 1-step forecasts for
y_test. Lets say the model is a simple AR(1). This doesn't seem to be that
straightforward in statsmodels. I've tried using predict but the training
set is not set there. Note that I do not want to do rolling or recursive
estimation. i want to fit the model only once on y_train and then forecast
all y_test with 1-step ahead forecasts, taking into account realized y(t-1)
contained in y_test.

Can anyone help with this? So far I have the following code

model = sm.tsa.ARMA(y_train, order = (1,0))
results = model.fit(trend='c')

Any help much appreciated!

George
Maxime De Bois
2018-09-28 06:24:11 UTC
Permalink
Hi George,

I suggest to use SARIMAX instead of ARMA.

model = SARIMAX(y_train, order=(p, d, q).fit() # fitting the model on
training set)
model_2 = SARIMAX(y_test, order=(p, d, q).filter(model.params) # new model
with fitted model parameters
prediction = model_2.forecast(ph) # rolling forecast at prediction horizon
'ph'

Mind that only one rolling forecast is computed, which is out-of-sample
compared to the y_test time series. If you want to have many predictions,
at every time step of the y_test time series, you may need to split the
y_test into subsamples and do the forecasting for every subsampling.

Hope it helps,

Maxime
Post by George Milunovich
Hi all,
I have a data sample y which is split into y_train and y_test. I would
like to fit ARMA on y_train, and then generate a series of 1-step forecasts
for y_test. Lets say the model is a simple AR(1). This doesn't seem to be
that straightforward in statsmodels. I've tried using predict but the
training set is not set there. Note that I do not want to do rolling or
recursive estimation. i want to fit the model only once on y_train and then
forecast all y_test with 1-step ahead forecasts, taking into account
realized y(t-1) contained in y_test.
Can anyone help with this? So far I have the following code
model = sm.tsa.ARMA(y_train, order = (1,0))
results = model.fit(trend='c')
Any help much appreciated!
George
George Milunovich
2018-09-28 08:10:58 UTC
Permalink
Hi Maxime,

Thanks for your helpful suggestion! The filter() method is exactly what I
was looking for. I've modified your code to get 1-step-ahead forecasts for
all observations in y_test as follows:

model_1 = sm.tsa.statespace.SARIMAX(y_train, order=(2,0,0), trend='c').fit()

one_step_predictions = []
for t in range(len(y_test)):
y_train2 = y.iloc[:len(y_train)+t].copy()
model_2 = sm.tsa.statespace.SARIMAX(y_train2, order=(2,0,0), trend='c').filter(model_1.params)
forecast = model_2.forecast(1)
one_step_predictions.append((forecast.index[0], forecast.values[0]))

one_step_predictions = pd.DataFrame(one_step_predictions, columns = ['Date', '1-step-forecast'])



Best,
george
Post by Maxime De Bois
Hi George,
I suggest to use SARIMAX instead of ARMA.
model = SARIMAX(y_train, order=(p, d, q).fit() # fitting the model on
training set)
model_2 = SARIMAX(y_test, order=(p, d, q).filter(model.params) # new model
with fitted model parameters
prediction = model_2.forecast(ph) # rolling forecast at prediction horizon
'ph'
Mind that only one rolling forecast is computed, which is out-of-sample
compared to the y_test time series. If you want to have many predictions,
at every time step of the y_test time series, you may need to split the
y_test into subsamples and do the forecasting for every subsampling.
Hope it helps,
Maxime
Post by George Milunovich
Hi all,
I have a data sample y which is split into y_train and y_test. I would
like to fit ARMA on y_train, and then generate a series of 1-step forecasts
for y_test. Lets say the model is a simple AR(1). This doesn't seem to be
that straightforward in statsmodels. I've tried using predict but the
training set is not set there. Note that I do not want to do rolling or
recursive estimation. i want to fit the model only once on y_train and then
forecast all y_test with 1-step ahead forecasts, taking into account
realized y(t-1) contained in y_test.
Can anyone help with this? So far I have the following code
model = sm.tsa.ARMA(y_train, order = (1,0))
results = model.fit(trend='c')
Any help much appreciated!
George
Maxime De Bois
2018-09-28 08:23:42 UTC
Permalink
Perfect!

Your code seems to have been modified appropriately.

Cheers,

Maxime
Post by George Milunovich
Hi Maxime,
Thanks for your helpful suggestion! The filter() method is exactly what I
was looking for. I've modified your code to get 1-step-ahead forecasts for
model_1 = sm.tsa.statespace.SARIMAX(y_train, order=(2,0,0), trend='c').fit()
one_step_predictions = []
y_train2 = y.iloc[:len(y_train)+t].copy()
model_2 = sm.tsa.statespace.SARIMAX(y_train2, order=(2,0,0), trend='c').filter(model_1.params)
forecast = model_2.forecast(1)
one_step_predictions.append((forecast.index[0], forecast.values[0]))
one_step_predictions = pd.DataFrame(one_step_predictions, columns = ['Date', '1-step-forecast'])
Best,
george
Post by Maxime De Bois
Hi George,
I suggest to use SARIMAX instead of ARMA.
model = SARIMAX(y_train, order=(p, d, q).fit() # fitting the model on
training set)
model_2 = SARIMAX(y_test, order=(p, d, q).filter(model.params) # new
model with fitted model parameters
prediction = model_2.forecast(ph) # rolling forecast at prediction
horizon 'ph'
Mind that only one rolling forecast is computed, which is out-of-sample
compared to the y_test time series. If you want to have many predictions,
at every time step of the y_test time series, you may need to split the
y_test into subsamples and do the forecasting for every subsampling.
Hope it helps,
Maxime
Post by George Milunovich
Hi all,
I have a data sample y which is split into y_train and y_test. I would
like to fit ARMA on y_train, and then generate a series of 1-step forecasts
for y_test. Lets say the model is a simple AR(1). This doesn't seem to be
that straightforward in statsmodels. I've tried using predict but the
training set is not set there. Note that I do not want to do rolling or
recursive estimation. i want to fit the model only once on y_train and then
forecast all y_test with 1-step ahead forecasts, taking into account
realized y(t-1) contained in y_test.
Can anyone help with this? So far I have the following code
model = sm.tsa.ARMA(y_train, order = (1,0))
results = model.fit(trend='c')
Any help much appreciated!
George
Loading...