Skip to content Skip to footer

Preprocessing and Data Exploration for Time Series: Decomposing Time Series

In our last articles, we discussed a detailed introduction to the time series data and covered some technical methods and approaches to process time series data. We also discussed that time series data is different from any general tabular or other kind of data as it holds tabular information in a sequential format. While performing analysis on such data it is important to process this data to get accurate results out of it. There are multiple steps required to complete time series processing and decomposing time series is one of them that helps us analyse and understand a time series more deeply. In this article, we are going to take this course on time series processing to the next step where we will be discussing the time series decomposition using the following points.

Table of Content

  • What is Time Series Decomposition? 
  • The Reasons Behind Time Series Decomposition 
  • Methods of Time Series Decomposition
  1. Additive decomposition 
  2. Multiplicative decomposition 
  3. Seasonal-Trend Decomposition using LOESS (STL) 

What is Time Series Decomposition?

Trend: This represents the long-term direction or pattern in the data, indicating whether it’s increasing, decreasing, or stable over time.

Seasonality: This component reflects regular, repeating patterns within the data. For instance, sales of winter coats tend to rise in colder months and fall in warmer ones.

Cyclic:  The cyclic component represents fluctuations in the data that occur over a more extended period than seasonality but are not strictly periodic. Unlike seasonality, cyclical patterns don’t necessarily repeat at fixed intervals. An example of a cyclic pattern is the economic business cycle between 5 to 7 years. 

Noise (or Residuals): This is the random variation or irregularity that remains after removing the trend and seasonal components.

By decomposing a time series, you gain a clearer understanding of the underlying patterns and trends, making it easier to analyze and forecast the data accurately. This process helps in isolating and understanding the distinct features of the time series, which can be valuable for various analytical and forecasting tasks.

The Reasons Behind Time Series Decomposition 

When we dive into any time series analysis project, understanding the changes in the data with time becomes a crucial aspect to understand, and when we decompose a time series we get to know critical information such as changes with time in time series, its seasonal and cyclic behaviour and many more about that time series and can be utilized further in the next step in time series analysis. Here are some major reasons behind time series decomposition.

Pattern Identification: Time series decomposition helps identify and separate different patterns within the data, such as trends, seasonality, and cyclical variations. Recognizing these patterns is crucial for understanding the inherent structure of the time series.

Model Simplicity: Decomposing a time series simplifies the modelling process by breaking down the complex data into more manageable components. Simpler models are easier to interpret and can provide clearer insights into the behaviour of the time series.  

Anomaly Detection: Examining the residuals (the part of the time series not explained by trends, seasonality, or cycles) helps identify anomalies or irregularities. Detecting anomalies is crucial for understanding unexpected events and outliers that may impact the analysis.

Understanding Component Contributions: Decomposition provides a clear breakdown of how each component contributes to the overall behaviour of the time series. This understanding is valuable for attributing changes in the data to specific factors, aiding in decision-making.

Decomposition of any time series data not only helps in understanding time series but also enhances the efficiency of in-lined processes in time series analysis. Let’s take a look at how decomposing a time series helps further in any project.   

Further Applications in Time Series Analysis

Enhanced Modeling: The decomposed components can be used to build more sophisticated models, such as additive or multiplicative models, which incorporate the identified patterns for improved accuracy. Enhanced modelling leads to a better representation of the time series dynamics.

Strategic Planning: Understanding trends and cyclical variations aids in strategic planning for businesses, helping them align their strategies with anticipated changes in the market. This leads to Improved strategic decisions based on a comprehensive understanding of the time series components.

Optimized Resource Allocation:  Forecasting based on decomposed components facilitates optimized resource allocation, helping organizations allocate resources efficiently based on anticipated demand. Efficient resource allocation leads to cost savings and improved operational effectiveness.

Performance Monitoring:  Monitoring residuals over time allows for ongoing performance assessment, helping to identify deviations from expected patterns.Early detection of performance issues and the ability to adjust strategies in response to changing trends. 

Here are the few major reasons and applications behind a time series decomposition process, after knowing the reasons, we are required to understand how we can perform time series decomposition in real life. Let’s understand them using examples.

Methods of Time Series Decomposition

There are multiple ways to decompose a time series and here we are going to discuss the following most used ways to decompose the time series:

  1. Additive Decomposition
  2. Multiplicative Decomposition
  3. Seasonal-Trend Decomposition using LOESS (STL) 

Additive Decomposition

This method of decomposing a time series considers that the components of the time series are additive, meaning that the observed time series data can be  expressed as the sum of its components (trend, cycle, seasonality, and noise), as given below expression:

Y(t) =T(t) + S(t) + ε(t)   

Where:

Y(t) = Observed time series data at time t

T(t) = Trend component at time t

 S(t) = Seasonal component at time t 

ε(t) = Residuals (or error) at time t

Additive decomposition is particularly suitable for time series data where the magnitude of the seasonal fluctuations remains relatively constant over time, regardless of the level of the series. This type of decomposition can be performed using the following way:

  • Simple Moving Averages (SMA): This technique involves calculating the average of a fixed-size window of data points to smooth out short-term fluctuations. 
  • Exponential Smoothing: Specifically, single exponential smoothing for data with no clear trend or seasonality, and Holt-Winters method for data with both trend and seasonality.

Data Generation

Before performing additive decomposition let’s make a dummy data so that we can go further in the process.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from statsmodels.tsa.holtwinters import SimpleExpSmoothing

# Generate a dummy time series with trend, seasonality, and residual

np.random.seed(0)

date_rng = pd.date_range(start=‘2023-01-01’, end=‘2023-12-31’, freq=‘D’)

t = np.arange(len(date_rng))

trend = 0.3 * t

seasonality = 10 * np.sin(2*np.pi*t/365)

residuals = np.random.normal(02len(date_rng))

ts = pd.Series(trend + seasonality + residuals, index=date_rng)

# Visualize the dummy time series

plt.figure(figsize=(104))

plt.plot(ts)

plt.title(‘Dummy Time Series with Trend and Seasonality’)

plt.show()

Output:

Here we can see we have created a time series data, let’s take a look at how we can perform both kinds of additive decomposition.

Additive Decomposition Using Simple Moving Average 

# Define the window size for SMA

window_size = 30

trend_sma = ts.rolling(window=window_size).mean()

residual_sma = ts – trend_sma

seasonality = 10 * np.sin(2*np.pi*t/365)

# Visualize the components

plt.figure(figsize=(106))

plt.subplot(411)

plt.plot(ts, label=‘Original Data’)

plt.title(‘Original Time Series’)

plt.legend()

plt.subplot(412)

plt.plot(trend_sma, label=‘Trend (SMA)’, color=‘orange’)

plt.title(‘Trend Component (SMA)’)

plt.legend()

plt.subplot(413)

plt.plot(seasonality, label=‘Seasonality’, linestyle=‘–‘)

plt.title(‘Seasonality’)

plt.legend()

plt.subplot(414)

plt.plot(residual_sma, label=‘Residual (SMA)’, color=‘green’)

plt.title(‘Residual Component (SMA)’)

plt.legend()

plt.tight_layout()

plt.show()

Output: 

Here we can see that our data is decomposed into its components using the SMA method. Let’s do the same using Exponential Smoothing.

Additive Decomposition using Exponential Smoothing 

from statsmodels.tsa.holtwinters import SimpleExpSmoothing

# Perform exponential smoothing

model = SimpleExpSmoothing(ts)

fitted_model = model.fit(smoothing_level=0.2, optimized=False)  # Adjust smoothing level as needed

trend_exp_smooth = fitted_model.fittedvalues

residual_exp_smooth = ts – trend_exp_smooth

# Visualize the components

plt.figure(figsize=(106))

plt.subplot(411)

plt.plot(ts, label=‘Original Data’)

plt.title(‘Original Time Series’)

plt.legend()

plt.subplot(412)

plt.plot(trend_exp_smooth, label=‘Trend (Exp. Smoothing)’, color=‘orange’)

plt.title(‘Trend Component (Exp. Smoothing)’)

plt.legend()

plt.subplot(413)

plt.plot(seasonality, label=‘Seasonality’, linestyle=‘–‘)

plt.title(‘Seasonality’)

plt.legend()

plt.subplot(414)

plt.plot(residual_exp_smooth, label=‘Residual (Exp. Smoothing)’, color=‘green’)

plt.title(‘Residual Component (Exp. Smoothing)’)

plt.legend()

plt.tight_layout()

plt.show()

Output: 

 Here we got to learn how we can use different methods for decomposing a time series using the additive methods. Now let’s understand and explore how we can use the multiplicative methods of decomposing the time series. 

Multiplicative Methods

As the name suggests, components in multiplicative decomposition are considered to be multiplicative, meaning that the observed time series data can be expressed as the product of its components(trend, cycle, seasonality, and noise) as given in the below expression:

Y(t) =T(t) S(t) ε(t)    

Where:

Y(t) = Observed time series data at time t

T(t)= Trend component at time t

 S(t) = Seasonal component at time t 

ε(t) = Residuals (or error) at time t

Usually, this decomposition is suitable for time series data where the magnitude of the seasonal fluctuations varies in proportion to the level of the series. Let’s see how we can perform this type of decomposition using Python and statsmodels provided module:

from statsmodels.tsa.seasonal import seasonal_decompose

# Perform multiplicative decomposition

result = seasonal_decompose(ts, model=‘multiplicative’)

# Extract the components

trend_mul = result.trend.dropna()

seasonal_mul = result.seasonal.dropna()

residual_mul = result.resid.dropna()

Let’s visualise the results:

# Visualize the components

plt.figure(figsize=(108))

plt.subplot(411)

plt.plot(ts, label=‘Original Data’)

plt.title(‘Original Time Series’)

plt.legend()

plt.subplot(412)

plt.plot(trend_mul, label=‘Trend (Multiplicative)’, color=‘orange’)

plt.title(‘Trend Component (Multiplicative)’)

plt.legend()

plt.subplot(413)

plt.plot(seasonal_mul, label=‘Seasonal (Multiplicative)’, color=‘green’)

plt.title(‘Seasonal Component (Multiplicative)’)

plt.legend()

plt.subplot(414)

plt.plot(residual_mul, label=‘Residual (Multiplicative)’, color=‘red’)

plt.title(‘Residual Component (Multiplicative)’)

plt.legend()

Output:

Here we can see that the results for decomposition of the same time series are different from the additive decomposition because multiplicative decomposition considers the sequential data as the product of trend, seasonality and residual components. Let’s take a look at our next method to decompose a time series method.

Seasonal-Trend Decomposition using LOESS (STL) 

In addition to additive and multiplicative decomposition, another powerful method for decomposing time series data is Seasonal-Trend decomposition using LOESS (STL). we can use this method when we find the time series has complex seasonal patterns.

It works similarly to the other decomposition method but employing LOESS(locally estimated scatterplot smoothing) makes it different from the other methods. We can compare LOESS with the non-parametric regression technique we can use for smoothing data. It’s particularly useful for capturing local trends or patterns in noisy datasets.

This method can make us handle datasets with intricate seasonal variations. This method is particularly valuable in scenarios where other decomposition techniques may struggle to capture complex seasonal patterns effectively.  

Let’s take a look at how we can perform this type of time series decomposition.

Decomposing time series using the STL method:

from statsmodels.tsa.seasonal import STL

# Perform STL decomposition

stl_result = STL(ts, seasonal=13).fit()

# Extract the components

seasonal_stl = stl_result.seasonal

trend_stl = stl_result.trend

residual_stl = stl_result.resid

Visualising the decomposed time series   

# Visualize the components

plt.figure(figsize=(108))

plt.subplot(411)

plt.plot(ts, label=‘Original Data’)

plt.title(‘Original Time Series’)

plt.legend()

plt.subplot(412)

plt.plot(trend_stl, label=‘Trend (STL)’, color=‘orange’)

plt.title(‘Trend Component (STL)’)

plt.legend()

plt.subplot(413)

plt.plot(seasonal_stl, label=‘Seasonal (STL)’, color=‘green’)

plt.title(‘Seasonal Component (STL)’)

plt.legend()

plt.subplot(414)

plt.plot(residual_stl, label=‘Residual (STL)’, color=‘red’)

plt.title(‘Residual Component (STL)’)

plt.legend()

plt.tight_layout()

plt.show()

Output:

Here we can see how this method worked to decompose the time series into its components. 

Final words:

In this article, we’ve explored another crucial time series preprocessing technique. This method not only enhances the clarity of time series data but also lays the foundation for various types of forecasting, including trend and seasonal forecasting. Integrating these forecasts often leads to more accurate predictions. By dissecting a time series into its individual components, we gain a profound understanding of its underlying patterns before proceeding with further modelling. This approach serves as a crucial step in the time series analytical process.

Reference