Deep Learning PyTorch Course, ARIMA Model

Deep learning and time series analysis are two important pillars of modern data science. Today, we will take a look at the ARIMA model and explore how it can be utilized with PyTorch. The ARIMA (Autoregressive Integrated Moving Average) model is a useful statistical method for analyzing and forecasting large amounts of time series data. It is particularly applied in various fields such as economics, climate, and the stock market.

1. What is the ARIMA model?

The ARIMA model consists of three main components. Each of these components provides the necessary information for analyzing and forecasting time series data:

  • Autoregression (AR): Models the influence of past values on the current value. For example, the current weather is related to the weather a few days ago.
  • Integration (I): Uses differencing of data to transform a non-stationary time series into a stationary one. This removes trends and seasonality.
  • Moving Average (MA): Predicts the current value based on past errors. The errors refer to the difference between the predicted value and the actual value.

2. The formula of the ARIMA model

The ARIMA model is expressed with the following formula:

Y(t) = c + φ_1 * Y(t-1) + φ_2 * Y(t-2) + ... + φ_p * Y(t-p) 
         + θ_1 * ε(t-1) + θ_2 * ε(t-2) + ... + θ_q * ε(t-q) + ε(t)
    

Here, Y(t) is the current value of the time series, c is a constant, φ are the AR coefficients, θ are the MA coefficients, and ε(t) is white noise.

3. Steps of the ARIMA model

The main steps involved in constructing an ARIMA model are as follows:

  1. Data collection and preprocessing: Collect time series data and handle missing values and outliers.
  2. Qualitative check of data: Check whether the data is stationary.
  3. Model selection: Select the optimal parameters (p, d, q) for the ARIMA model. This is determined by analyzing the ACF (Autocorrelation Function) and PACF (Partial Autocorrelation Function).
  4. Model fitting: Fit the model based on the selected parameters.
  5. Model diagnostics: Check the residuals and assess the reliability of the model.
  6. Prediction: Use the model to forecast future values.

4. Implementing the ARIMA model in Python

Now let’s implement the ARIMA model in Python. We will use the statsmodels library to construct the ARIMA model.

4.1 Data collection and preprocessing

First, import the necessary libraries and load the data. We will use the `AirPassengers` dataset as an example.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

# Load data
data = pd.read_csv('AirPassengers.csv')
data['Month'] = pd.to_datetime(data['Month'])
data.set_index('Month', inplace=True)
data = data['#Passengers']

# Data visualization
plt.figure(figsize=(12, 6))
plt.plot(data)
plt.title('AirPassengers Data')
plt.xlabel('Date')
plt.ylabel('Number of Passengers')
plt.show()
    

4.2 Checking for stationarity

To check whether the data is stationary, we perform the ADF (Augmented Dickey-Fuller) test.

from statsmodels.tsa.stattools import adfuller

result = adfuller(data)
if result[1] <= 0.05:
    print("The data is stationary.")
else:
    print("The data is non-stationary.")
    # Normalize through differencing
    data_diff = data.diff().dropna()
    plt.figure(figsize=(12, 6))
    plt.plot(data_diff)
    plt.title('Differenced Data')
    plt.xlabel('Date')
    plt.ylabel('Differenced Passengers')
    plt.show()
    result_diff = adfuller(data_diff)
    if result_diff[1] <= 0.05:
        print("The data is stationary after differencing.")
    else:
        print("The data is still non-stationary after differencing.")
    

4.3 Selecting ARIMA model parameters

We use ACF and PACF plots to select the parameters p, d, and q.

plot_acf(data_diff)
plot_pacf(data_diff)
plt.show()
    

By analyzing the pattern of the autocorrelation function, we decide on the order of AR and MA. For example, let's assume we chose p=2, d=1, q=2.

4.4 Fitting the ARIMA model

model = ARIMA(data, order=(2, 1, 2))
model_fit = model.fit()
print(model_fit.summary())
    

4.5 Model diagnostics

We verify the model's adequacy through residual analysis.

residuals = model_fit.resid
plt.figure(figsize=(12, 6))
plt.subplot(211)
plt.plot(residuals)
plt.title('Residuals')
plt.subplot(212)
plt.hist(residuals, bins=20)
plt.title('Residuals Histogram')
plt.show()
    

4.6 Prediction

We forecast future values using the fitted model.

forecast = model_fit.forecast(steps=12)
forecast_index = pd.date_range(start='1961-01-01', periods=12, freq='M')
forecast_series = pd.Series(forecast, index=forecast_index)

plt.figure(figsize=(12, 6))
plt.plot(data, label='Historical Data')
plt.plot(forecast_series, label='Forecast', color='red')
plt.title('Passenger Forecast')
plt.xlabel('Date')
plt.ylabel('Number of Passengers')
plt.legend()
plt.show()
    

5. Limitations of the ARIMA model and conclusion

The ARIMA model captures the patterns of time series data well. However, it has several limitations:

  • Assumption of linearity: The ARIMA model is based on the assumption that the data is linear, which may not capture non-linear relationships well.
  • Seasonality of time series data: The ARIMA model is not suitable for data with seasonality. In this case, the SARIMA (Seasonal ARIMA) model is used.
  • Parameter selection: Choosing the optimal parameters is often a challenging task.

Deep learning and the ARIMA model complement each other significantly. When analyzing various data, deep learning models can capture non-linear patterns, while the ARIMA model helps understand the underlying trends of the data.

6. References