Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python for Machine Learning
  • Machine Learning with R
  • Machine Learning Algorithms
  • EDA
  • Math for Machine Learning
  • Machine Learning Interview Questions
  • ML Projects
  • Deep Learning
  • NLP
  • Computer vision
  • Data Science
  • Artificial Intelligence
Open In App
Next Article:
Time Series Decomposition Techniques
Next article icon

Time Series Decomposition Techniques

Last Updated : 20 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Time series data consists of observations taken at consecutive points in time. These data can often be decomposed into multiple components to better understand the underlying patterns and trends. Time series decomposition is the process of separating a time series into its constituent components, such as trend, seasonality, and noise. In this article, we will explore various time series decomposition techniques, their types, and provide code samples for each.

Time series decomposition helps us break down a time series dataset into three main components:

  1. Trend: The trend component represents the long-term movement in the data, representing the underlying pattern.
  2. Seasonality: The seasonality component represents the repeating, short-term fluctuations caused by factors like seasons or cycles.
  3. Residual (Noise): The residual component represents random variability that remains after removing the trend and seasonality.

By separating these components, we can gain insights into the behavior of the data and make better forecasts.

Types of Time Series Decomposition Techniques

Additive Decomposition:

  • In additive decomposition, the time series is expressed as the sum of its components:Y(t) = Trend(t) + Seasonal(t) + Residual(t)
  • It's suitable when the magnitude of seasonality doesn't vary with the magnitude of the time series.

Multiplicative Decomposition:

  • In multiplicative decomposition, the time series is expressed as the product of its components: Y(t) = Trend(t) * Seasonal(t) * Residual(t)
  • It's suitable when the magnitude of seasonality scales with the magnitude of the time series.

Methods of Decomposition

Moving Averages:

  • Moving averages involve calculating the average of a certain number of past data points.
  • It helps smooth out fluctuations and highlight trends.


Seasonal Decomposition of Time Series

  • The Seasonal and Trend decomposition using Loess (STL) is a popular method for decomposition, which uses a combination of local regression (Loess) to extract the trend and seasonality components.

Exponential Smoothing State Space Model

  • This method involves using the ETS framework to estimate the trend and seasonal components in a time series.

Implementation in Python

Let's go through an example of applying multiple time series decomposition techniques to a sample dataset. We'll use Python and some common libraries.

Step 1: Import the required libraries.

We have imported the following libraries:

  • NumPy for numeric computing and for scientific and data analysis tasks, offering high-performance array operations and mathematical capabilities.
  • Pandas for data manipulation and analysis library.
  • Matplotlib is a versatile library for creating static, animated, or interactive visualizations
  • Statsmodels Time Series Analysis (tsa) Module
    • The Statsmodels library is used for estimating and interpreting various statistical models in Python.
    • The tsa module within Statsmodels focuses on time series analysis and provides tools for decomposing time series, fitting models, and conducting statistical tests for time series data.
    • It is widely used for time series forecasting, econometrics, and statistical analysis of temporal data.


Python
import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose 


Step 2: Create a Synthetic Time Series Dataset

The following generates a synthetic time series dataset (ts) with daily data points that combine a sine wave pattern and random noise, simulating a time series with some underlying periodic behavior and variability. The time series spans one year, from "2021-01-01" to the end of the year. The random seed is set to ensure reproducibility.


Python
np.random.seed(0) date_rng = pd.date_range(start="2021-01-01", periods=365, freq="D") data = np.sin(np.arange(365) * 2 * np.pi / 365) + np.random.normal(0, 0.5, 365) ts = pd.Series(data, index=date_rng) ts 

Output:

2021-01-01    0.882026 2021-01-02    0.217292 2021-01-03    0.523791 2021-01-04    1.172066 2021-01-05    1.002581                 ...    2021-12-27    0.263264 2021-12-28   -0.066917 2021-12-29    0.414305 2021-12-30    0.135561 2021-12-31   -0.025054 Freq: D, Length: 365, dtype: float64 

Step 3: Visualize the Dataset

The following code creates a line plot of the original time series (ts) and sets up the plot with specific dimensions. It also adds a legend to the plot to label the time series data, making it more informative and easier to understand when interpreting the visualization. The resulting plot will display the time series data over time, with dates on the x-axis and the values on the y-axis.

Python3
plt.figure(figsize=(12, 3)) plt.plot(ts, label='Original Time Series') plt.legend() 

Output:

Screenshot-2023-10-16-164112
Generated Time Series


Step 3: Apply Additive Decomposition

The following code uses the seasonal_decomposition function from the Statsmodels library to decompose the original time series (ts) into its constituent components using an additive model.

Syntax of seasonal_decompose is provided below:

seasonal_decompose(x, model='additive', filt=None, period=None, two_sided=True, extrapolate_trend=0)

We can also set model='multiplicative' but, our data contains zero and negative values. Hence, we are only going to proceed with the additive model.

The code performs an additive decomposition of the original time series and stores the result in the result_add variable, allowing you to further analyze and visualize the decomposed components.

Python3
result_add = seasonal_decompose(ts, model='additive') 


Step 4: Plot the trend component

The following code creates a line plot of the trend component obtained from the additive decomposition of the time series and sets up the plot with specific dimensions. It also adds a legend to the plot to label the trend component, making it more informative and easier to understand when interpreting the visualization. The resulting plot will display the trend component of the time series over time.

Python3
plt.figure(figsize=(9, 3))  plt.plot(result_add.trend, label='Additive Trend') plt.legend() 

Output:

trend-

Step 5: Plot the seasonal component

The following code creates a line plot of the seasonal component obtained from the additive decomposition of the time series and sets up the plot with specific dimensions. It also adds a legend to the plot to label the seasonal component, making it more informative and easier to understand when interpreting the visualization. The resulting plot will display the seasonal component of the time series over time.

Python3
plt.figure(figsize=(9, 3))  plt.plot(result_add.seasonal, label='Additive Seasonal') plt.legend() 

Output:

seasonal--min


Step 6: Calculate the Simple Moving Average (SMA)

The provided code calculates a simple moving average (SMA) for the original time series ts with a 7-day moving window

Python
sma_window = 7  # 7-day moving average sma = ts.rolling(window=sma_window).mean() sma 

Output:

2021-01-01         NaN 2021-01-02         NaN 2021-01-03         NaN 2021-01-04         NaN 2021-01-05         NaN                 ...    2021-12-27   -0.326866 2021-12-28   -0.262944 2021-12-29   -0.142060 2021-12-30    0.030998 2021-12-31    0.081171 Freq: D, Length: 365, dtype: float64 


Step 7: Calculate Exponential Moving Average (EMA)

The provided code calculates an exponential moving average (EMA) for the original time series ts with a 30-day window.

Python
ema_window = 30  # 30-day moving average ema = ts.ewm(span=ema_window, adjust=False).mean() ema 

Output:

2021-01-01    0.882026 2021-01-02    0.839140 2021-01-03    0.818795 2021-01-04    0.841587 2021-01-05    0.851973                 ...    2021-12-27   -0.428505 2021-12-28   -0.405176 2021-12-29   -0.352307 2021-12-30   -0.320831 2021-12-31   -0.301749 Freq: D, Length: 365, dtype: float64 

Step 8: Plotting the Moving Averages

The provided code is responsible for creating a plot that overlays the original time series (ts) with both the 7-day simple moving average (SMA) and the 30-day exponential moving average (EMA).components:

Python3
plt.figure(figsize=(9, 3)) plt.plot(ts, label='Original Time Series') plt.plot(sma, label=f'{sma_window}-Day SMA') plt.plot(ema, label=f'{ema_window}-Day EMA') plt.legend() 

Output:

ema-sma-

Conclusion

Time series decomposition is a crucial step in understanding and analyzing time series data. By breaking down a time series into its trend, seasonality, and residual components, we can gain valuable insights for forecasting, anomaly detection, and decision-making. Depending on the nature of your data, you can choose between additive and multiplicative decomposition methods. Additionally, moving averages can be a useful tool for smoothing out data and simplifying complex patterns. These techniques are essential for anyone working with time series data, from financial analysts to data scientists, and can lead to more accurate predictions and better-informed decisions.


Next Article
Time Series Decomposition Techniques

E

excitedlord
Improve
Article Tags :
  • Python
  • Machine Learning
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    Time Series Clustering: Techniques and Applications
    Time series clustering is a powerful unsupervised learning technique used to group similar time series data points based on their characteristics. This method is essential in various domains, including finance, healthcare, meteorology, and retail, where understanding patterns over time can lead to v
    8 min read
    Decomposition Reaction
    Have you ever wondered what happens when compounds break apart into simpler substances? Or how energy is released, and new compounds are formed? The answers to these questions lie in the concept of Decomposition Reaction. Decomposition Reactions are a key concept in the understanding of the chemical
    7 min read
    QR Decomposition in Machine learning
    QR decomposition is a way of expressing a matrix as the product of two matrices: Q (an orthogonal matrix) and R (an upper triangular matrix). In this article, I will explain decomposition in Linear Algebra, particularly QR decomposition among many decompositions. What is QR Decomposition?Decompositi
    9 min read
    Eigen Decomposition of a Matrix
    Eigen decomposition is a method used in linear algebra to break down a square matrix into simpler components called eigenvalues and eigenvectors. This process helps us understand how a matrix behaves and how it transforms data.For Example - Eigen decomposition is particularly useful in fields like P
    4 min read
    Seasonal Decomposition of Time Series by Loess (STL)
    Researchers can uncover trends, seasonal patterns, and other temporal linkages by employing time series data, which gathers information across many time-periods. Understanding the underlying patterns and components within time series data is crucial for making informed decisions and predictions. One
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences