ARIMA Example

This is a simple example of ARIMA (AutoRegressive Integrated Moving Average) using Python and the statsmodels library.

ARIMA Overview

ARIMA is a popular time series forecasting method that combines autoregression (AR), differencing (I), and moving averages (MA). It is suitable for univariate time series data and is widely used for predicting future values based on past observations.

Key concepts of ARIMA:

ARIMA models are effective for capturing trends and seasonality in time series data.

Python Source Code:

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

# Generate synthetic time series data
np.random.seed(42)
time = pd.date_range(start='2022-01-01', end='2023-12-31', freq='D')
values = np.random.randn(len(time)).cumsum()
series = pd.Series(values, index=time)

# Plot the time series data
plt.figure(figsize=(10, 6))
plt.plot(series)
plt.title('Synthetic Time Series Data')
plt.xlabel('Time')
plt.ylabel('Values')
plt.show()

# Plot autocorrelation and partial autocorrelation functions
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 4))
plot_acf(series, ax=ax1, lags=40)
plot_pacf(series, ax=ax2, lags=40)
plt.suptitle('Autocorrelation and Partial Autocorrelation Functions')
plt.show()

# Fit ARIMA model
order = (2, 1, 1)  # Example order (p, d, q)
arima_model = ARIMA(series, order=order)
arima_results = arima_model.fit()

# Generate forecast
forecast_steps = 365
forecast_values = arima_results.get_forecast(steps=forecast_steps).predicted_mean

# Plot the original time series and forecast
plt.figure(figsize=(10, 6))
plt.plot(series, label='Original Time Series')
plt.plot(forecast_values, label='ARIMA Forecast', color='red')
plt.title('ARIMA Forecast')
plt.xlabel('Time')
plt.ylabel('Values')
plt.legend()
plt.show()

Explanation: