未加星标

How to Make Manual Predictions for ARIMA Models with Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

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.

Let’s dive in.


How to Make Manual Predictions for ARIMA Models with Python

How to Make Manual Predictions for ARIMA Models with Python

Photo by Bernard Spragg. NZ , some rights reserved.

Minimum Daily Temperatures Dataset

This dataset describes the minimum daily temperatures over 10 years (1981-1990) in the city Melbourne, Australia.

The units are in degrees Celsius and there are 3,650 observations. The source of the data is credited as the Australian Bureau of Meteorology.

You can learn more and download the dataset from the Data Market website .

Download the dataset and place it into your current working directory with the filename “ daily-minimum-temperatures.csv “. The dataset contains some “?” characters, open it in a text editor and delete those values.

The example below demonstrates how to load the dataset as a Pandas Series and graph the loaded dataset.

frompandasimportSeries frommatplotlibimportpyplot series = Series.from_csv('daily-minimum-temperatures.csv', header=0) series.plot() pyplot.show()

Running the example creates a line plot of the time series.


How to Make Manual Predictions for ARIMA Models with Python

Minimum Daily Temperatures Dataset Plot

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.

defpredict(coef, history): yhat = 0.0 for i in range(1, len(coef)+1): yhat += coef[i-1] * history[-i] return yhat

For reference, you may find the following resources useful:

ARIMA API Documentation ARIMAResults API Documentation ARIMA statsmodels Source Code

Let’s look at some simple but specific models and how to make manual predictions with this test setup.

Autoregression Model

The autoregression model, or AR, is a linear regression model on the lag observations.

An AR model with a lag of k can be specified in the ARIMA model as follows:

model = ARIMA(history, order=(k,0,0))

In this example, we will use a simple AR(1) for demonstration purposes.

Making a prediction requires that we retrieve the AR coefficients from the fit model and use them with the lag of observed values and call the custom predict() function defined above.

The complete example is listed below.

frompandasimportSeries frommatplotlibimportpyplot fromstatsmodels.tsa.arima_modelimportARIMA fromsklearn.metricsimportmean_squared_error frommathimportsqrt defpredict(coef, history): yhat = 0.0 for i in range(1, len(coef)+1): yhat += coef[i-1] * history[-i] return yhat series = Series.from_csv('daily-minimum-temperatures.csv', header=0) X = series.values size = len(X) - 7 train, test = X[0:size], X[size:] history = [x for x in train] predictions = list() for t in range(len(test)): model = ARIMA(history, order=(1,0,0)) model_fit = model.fit(trend='nc', disp=False) ar_coef = model_fit.arparams yhat = predict(ar_coef, history) predictions.append(yhat) obs = test[t] history.append(obs) print('>predicted=%.3f, expected=%.3f' % (yhat, obs)) rmse = sqrt(mean_squared_error(test, predictions)) print('Test RMSE: %.3f' % rmse) 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()

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: RIMPython
分页:12
转载请注明
本文标题:How to Make Manual Predictions for ARIMA Models with Python
本站链接:http://www.codesec.net/view/531313.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(106)