Python | ARIMA Model for Time Series Forecasting

Python | ARIMA Model for Time Series Forecasting

ARIMA, which stands for AutoRegressive Integrated Moving Average, is a popular time series forecasting model. It combines autoregression, differencing, and moving averages into a consolidated forecast model.

Here is how you can implement an ARIMA model for time series forecasting in Python using the statsmodels library:

Step 1: Install necessary libraries

Make sure you have the required libraries installed:

pip install pandas numpy statsmodels matplotlib 

Step 2: Create a sample time series data or load your data

For the sake of simplicity, let's use Python's pandas library to load a time series dataset:

import pandas as pd # For example, load 'AirPassengers' dataset (which is a classic dataset for time series forecasting) from statsmodels.datasets import air_passengers df = air_passengers.load_pandas().data df['Month'] = pd.to_datetime(df['Month']) df.set_index('Month', inplace=True) 

Step 3: Plot the time series data

It's always good to visualize the data first:

import matplotlib.pyplot as plt plt.figure(figsize=(10,6)) plt.plot(df) plt.xlabel('Date') plt.ylabel('Number of air passengers') plt.title('Monthly Total Air Passengers 1949-1960') plt.show() 

Step 4: Fit ARIMA model

Now, let's fit an ARIMA model to our time series data:

from statsmodels.tsa.arima.model import ARIMA # ARIMA(p,d,q) where: # p is the number of autoregressive terms (AR part) # d is the number of nonseasonal differences (I part) # q is the number of lagged forecast errors (MA part) model = ARIMA(df, order=(5,1,0)) model_fit = model.fit(disp=0) print(model_fit.summary()) 

Step 5: Forecast

Once the model is trained, we can use it to make forecasts:

# Plot actual versus predicted values plt.figure(figsize=(10,6)) # Training data plt.plot(df, label='Actual') # Forecast forecast, stderr, conf_int = model_fit.forecast(steps=24) forecast_index = pd.date_range(df.index[-1], periods=24, freq='M').shift(1) plt.plot(forecast_index, forecast, color='red', label='Forecast') # Confidence intervals plt.fill_between(forecast_index, forecast - 1.96*stderr, forecast + 1.96*stderr, color = 'r', alpha = 0.3) plt.title('ARIMA Forecast') plt.legend(loc='upper left', fontsize=8) plt.show() 

Remember, the parameters (p,d,q) for the ARIMA model should ideally be determined through a systematic search to find the best fit, which can minimize a chosen loss criterion. For this purpose, you can use methods such as grid search, ACF and PACF plots, and tools like the auto-arima function available in the pmdarima library.


More Tags

owin pyaudio qdialog bidirectional python-packaging intersection removeall kendo-asp.net-mvc fgets terminology

More Programming Guides

Other Guides

More Programming Examples