The autoregression integrated moving average model or ARIMA model can seem intimidating to beginners.
A good way to pull back the curtain in the method is to to use a trained model to make predictions manually. This demonstrates that ARIMA is a linear regression model at its core.
Making manual predictions with a fit ARIMA models may also be a requirement in your project, meaning that you can save the coefficients from the fit model and use them as configuration in your own code to make predictions without the need for heavy Python libraries in a production environment.
In this tutorial, you will discover how to make manual predictions with a trained ARIMA model in Python.
Specifically, you will learn:
How to make manual predictions with an autoregressive model.
How to make manual predictions with a moving average model.
How to make predictions with an autoregression integrated moving average model.
Running the example creates a line plot of the time series.
Minimum Daily Temperatures Dataset Plot
Stop learning Time Series Forecasting the slow way!
Take my free 7-day email course and discover how to get started (with sample code).
Click to sign-up and also get a free PDF Ebook version of the course.
ARIMA Test Setup
We will use a consistent test harness to fit ARIMA models and evaluate their predictions.
First, the loaded dataset is split into a train and test dataset. The majority of the dataset is used to fit the model and the last 7 observations (one week) are held back as the test dataset to evaluate the fit model.
A walk-forward validation, or rolling forecast, method is used as follows:
Each time step in the test dataset is iterated.
Within each iteration, a new ARIMA model is trained on all available historical data.
The model is used to make a prediction for the next day.
The prediction is stored and the “real” observation is retrieved from the test set and added to the history for use in the next iteration.
The performance of the model is summarized at the end by calculating the root mean squared error (RMSE) of all predictions made compared to expected values in the test dataset.
Simple AR, MA, ARMA and ARMA models are developed. They are unoptimized and are used for demonstration purposes. You will surely be able to achieve better performance with a little tuning.
The ARIMA implementation from the statsmodels Python library is used and AR and MA coefficients are extracted from the ARIMAResults object returned from fitting the model.
The ARIMA model supports forecasts via the predict() and the forecast() functions.
Nevertheless, we will make manual predictions in this tutorial using the learned coefficients.
This is useful as it demonstrates that all that is required from a trained ARIMA model is the coefficients.
The coefficients in the statsmodels implementation of the ARIMA model do not use intercept terms. This means we can calculate the output values by taking the dot product of the learned coefficients and lag values (in the case of an AR model) and lag residuals (in the case of an MA model). For example:
y = dot_product(ar_coefficients, lags) + dot_product(ma_coefficients, residuals)
The coefficients of a learned ARIMA model can be accessed from aARIMAResults object as follows:
AR Coefficients: model_fit.arparams
MA Coefficients: model_fit.maparams
We can use these retrieved coefficients to make predictions using the following manual predict() function.
For reference, you may find the following resources useful:
Note that the ARIMA implementation will automatically model a trend in the time series. This adds a constant to the regression equation that we do not need for demonstration purposes. We turn this convenience off by setting the ‘trend’ argument in the fit() function to the value ‘nc‘ for ‘no constant‘.
The fit() function also outputs a lot of verbose messages that we can turn off by setting the ‘disp‘ argument to ‘False‘.
Running the example prints the prediction and expected value each iteration for 7 days. The final RMSE is printed showing an average error of about 1.9 degrees Celsius for this simple model.
Test RMSE: 1.928
Experiment with AR models with different orders, such as 2 or more.
Moving Average Model
The moving average model, or MA, is a linear regression model of the lag residual errors.
An MA model with a lag of k can be specified in the ARIMA model as follows:
In this example, we will use a simple MA(1) for demonstration purposes.
Much like above, making a prediction requires that we retrieve the MA coefficients from the fit model and use them with the lag of residual error values and call the custom predict() function defined above.
The residual errors during training are stored in the ARIMA model under the ‘resid‘ parameter of the ARIMAResults object.
You can see that the lag observations are differenced prior to their use in the call to the predict() function with the AR coefficients. The residual errors will also be calculated with regard to these differenced input values.
Running the example prints the prediction and expected value each iteration and summarizes the performance of all predictions made.
Test RMSE: 1.232
In this tutorial, you discovered how to make manual predictions for an ARIMA model with Python.
Specifically, you learned:
How to make manual predictions for an AR model.
How to make manual predictions for an MA model.
How to make manual predictions for an ARMA and ARIMA model.
Do you have any questions about making manual predictions?
Ask your questions in the comments below and I will do my best to answer.
Want to Develop Time Series Forecasts with Python?