Mastering Time Series Forecasting

Revealing the Power of Fourier Terms in ARIMA

Şeyma Aysu Demir
The Deep Hub
3 min readJul 23, 2023

--

Seasonal ARIMA (SARIMA) models are frequently utilized for time series data that exhibit seasonality. However, when dealing with high-frequency data or various seasonal patterns, typical SARIMA models can become complicated and computationally expensive. Using Fourier terms is a less well-known technique to include seasonality in ARIMA models.

What is the Fourier Series? A Fourier series is an expansion of a periodic function f(x) in terms of an infinite sum of sines and cosines. This technique can be applied to model and forecast time series data with multiple seasonal patterns, such as daily, weekly, or monthly seasonality.

The basic idea behind the Fourier series is that any periodic function can be broken down into a sum of sine and cosine waves with different frequencies, amplitudes, and phases. These waves are known as Fourier components or Fourier harmonics.

The trick involves introducing Fourier terms into the ARIMA model, which allows us to capture the seasonal patterns more flexibly and efficiently. The general idea is to use the Fourier terms to approximate the seasonal components of the time series, eliminating the need for explicitly specifying seasonal orders (P, D, Q) in traditional SARIMA and ARIMA models.

Follow these procedures to include Fourier terms in the ARIMA model:

1. Determine the seasonal times: Identify the time series’ seasonal peaks and valleys. For instance, the seasonal period would be 7 if the data showed weekly seasonality.

2. Create Fourier terms: Create a set of Fourier terms for each recognized seasonal period. The complexity of the seasonal pattern you want to represent will determine how many Fourier terms you use. In general, a few Fourier terms — say, three or four — can frequently give a good depiction of the seasonality.

3. Fourier terms should be used in the model. In the ARIMA model, include the Fourier terms as exogenous variables. During model fitting, a corresponding coefficient for each Fourier term will be estimated.

Here is an applied scenario in Python. I have used pmdarima instead of statsmodels.tsa.arima.model. Simply because statsmodel library doesn’t accept exogenous variables.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pmdarima import auto_arima

# Generate synthetic time series data with daily seasonality
np.random.seed(42)
n = 365 # Number of data points
t = np.arange(n)
seasonality = 7 # Weekly seasonality
data = 50 + 10 * np.sin(2 * np.pi * t / seasonality) + np.random.normal(0, 5, n)

# Convert the data to a pandas DataFrame
df = pd.DataFrame({'Date': pd.date_range(start='2020-01-01', periods=n), 'Value': data})
df.set_index('Date', inplace=True)

# Create Fourier terms for weekly seasonality
def create_fourier_terms(t, period, num_terms):
terms = []
for i in range(1, num_terms + 1):
terms.append(np.sin(2 * np.pi * i * t / period))
terms.append(np.cos(2 * np.pi * i * t / period))
return np.column_stack(terms)

num_fourier_terms = 4
fourier_terms = create_fourier_terms(t, seasonality, num_fourier_terms)

# Fit the ARIMA model using pmdarima's auto_arima with Fourier terms as exogenous variables
model = auto_arima(df['Value'],exogenous=fourier_terms[:n], seasonal=True, suppress_warnings=True)
model.fit(df['Value'], exogenous=fourier_terms[:n])

# Forecast future values with the fitted model
forecast_steps =30
forecast_exog = create_fourier_terms(np.arange(n, n + forecast_steps), seasonality, num_fourier_terms)

# Get the forecast for the future steps with exogenous variables
forecast_df = pd.DataFrame(forecast_exog, columns=[f'Fourier_{i+1}' for i in range(num_fourier_terms * 2)])
#forecast_values = model.predict(n_periods=forecast_steps)
forecast_values = model.predict(n_periods=forecast_steps, exogenous=forecast_df)

# Retrieve the index for forecasting
forecast_index = pd.date_range(start='2021-01-01', periods=forecast_steps)

# Plot the original data and the forecasted values
plt.figure(figsize=(10, 5))
plt.plot(df.index, df['Value'], label='Original Data')
plt.plot(forecast_index, forecast_values, label='Forecasted Values', color='red')
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('ARIMA with Fourier Terms Forecast')
plt.legend()
plt.show()

Advantages of Fourier Terms:

Flexibility: Without explicitly defining seasonal ordering, Fourier terms offer a flexible technique to simulate various seasonal patterns. This adaptability comes in handy when working with data that display several different seasonal trends.

The efficiency of computation: Classical SARIMA models can be computationally expensive, particularly when dealing with high-frequency data or lengthy seasonal periods. We use Fourier terms to simplify the model’s parameters and speed up model estimation and forecasting.

Better representation: Complex seasonal patterns that may be difficult to depict using typical seasonal differencing and autoregressive terms adequately can be captured by Fourier terms.

Thanks for reading! See you in the next blog :)

--

--