Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Time Series Decomposition

Recommended resources

Hello! Welcome to your fourth lesson in the "Time Series Fundamentals" module.

In our last lesson, we focused on visualizing time series data. You learned to use line plots, box plots, and moving averages to visually identify underlying patterns like trends and seasonality. This exploratory analysis gave you an intuition for the structure hidden within the data.

Today, we're going to formalize that intuition. This lesson will teach you how to decompose a time series into its trend, seasonal, and residual components. We'll move from just seeing the patterns to mathematically separating them. This technique is fundamental for a deeper analysis of your data and is a key preprocessing step for many forecasting models.

1. The Building Blocks of a Time Series

As we hinted at in the last lesson, a time series can often be thought of as a combination of several underlying components. Decomposing a time series means breaking it down into these parts.

What is Time Series Decomposition

To start, let's watch a short, high-level overview from Aric LaBarr that introduces the core idea of decomposition: splitting a signal from the noise.

Watch the first 47 seconds of the video. It frames decomposition as the process of breaking down the 'signal' in your data into trend, cycle, and seasonal components, leaving the 'noise' or remainder.

The three primary components we will focus on are:

  • Trend (T): The long-term direction of the series. Is it increasing, decreasing, or staying flat over time?
  • Seasonality (S): A repeating pattern that occurs at a fixed and known frequency (e.g., daily, weekly, yearly).
  • Residuals (R): Also called noise or error. This is the random, irregular component left over after we've accounted for the trend and seasonality.
Time Series Decomposition Plot
This plot shows an example of a time series (top) broken down into its trend, seasonal, and residual components. Our goal is to understand how to produce such a breakdown.

2. Combining the Components: Additive vs. Multiplicative Models

To put the time series back together from its components, we need a model. There are two primary models for this: additive and multiplicative. Choosing the right one depends on how the components interact.

Additive and Multiplicative Time Series Decomposition Models
These are the mathematical formulas for the additive and multiplicative models. The key difference is whether the components are added or multiplied.

Additive Model

In an additive model, the components are simply added together:

You use an additive model when the magnitude of the seasonal fluctuations does not change as the trend changes. For example, if a company's sales increase by about 1,000 units every summer, regardless of whether the baseline sales are 10,000 or 50,000 units, the pattern is additive.

Multiplicative Model

In a multiplicative model, the components are multiplied:

You use a multiplicative model when the magnitude of the seasonal fluctuations does change in proportion to the level of the time series. For example, if a company's sales increase by 20% every summer, the absolute increase will be larger when baseline sales are high than when they are low. This is a very common scenario in real-world data.

What is Time Series Decomposition? - Time Series Analysis in Python

This video by Egor Howell provides a clear explanation of the difference between additive and multiplicative models with practical examples.

Watch from 00:18 to 02:57. Pay close attention to the ice cream sales example, as it clearly illustrates when to use an additive model (fixed increase) versus a multiplicative one (percentage increase).

The key visual cue is to look at the seasonal swings in your line plot from the last lesson.

  • If the swings are roughly the same size over time, think additive.
  • If the swings get wider as the level of the series goes up, think multiplicative.
Test your understanding!

You are analyzing monthly electricity consumption data for a growing city. You notice that consumption always peaks in August and is lowest in February. Over the years, as the city's population has grown, the difference between the August peak and the February low has also become much larger.

Which type of model (additive or multiplicative) would likely be more appropriate for decomposing this time series, and why?

Show answer

A multiplicative model would be more appropriate. The seasonal fluctuation (the difference between the summer peak and winter low) is growing as the overall trend of electricity consumption increases. This suggests the seasonal effect is proportional to the level of the series, which is the core idea behind a multiplicative model.


3. The "Classical" Decomposition Method

Now that we know the what and why, let's look at the how. "Classical decomposition" is one of the oldest and most intuitive methods for separating the components.

The process is straightforward and builds on the moving average concept from our previous lesson.

Time Series DIY: Seasonal Decomposition

To get a solid grasp of the theory, let's read about the step-by-step process. This article, 'Time Series DIY: Seasonal Decomposition', explains the mechanics clearly.

Read the 'Theory' section of the article. It breaks down how the trend, seasonal, and residual components are estimated for an additive model. The logic for a multiplicative model is analogous (using division instead of subtraction).

Here is a summary of the steps for an additive model:

  1. Estimate Trend (T): Calculate a centered moving average with a window size equal to the seasonal period (e.g., 12 for monthly data).
  2. De-trend the Series: Subtract the trend from the original series: Y(t) - T(t). This leaves the seasonal and residual components.
  3. Estimate Seasonality (S): For each seasonal period (e.g., for each month), calculate the average of the de-trended values. This gives you the typical seasonal effect for that period.
  4. Calculate Residuals (R): Subtract the trend and seasonal components from the original series: R(t) = Y(t) - T(t) - S(t).

The process for a multiplicative model is identical, just replacing subtraction with division.


4. Decomposition in Practice with Python

While it's valuable to understand the theory, you'll rarely implement this by hand. The statsmodels library in Python provides a convenient function, seasonal_decompose, to do this for you. Your background in Python will make this very straightforward.

The function takes the time series data and requires you to specify the model ('additive' or 'multiplicative').

What is Time Series Decomposition? - Time Series Analysis in Python

Let's see this in action. This part of Egor Howell's video demonstrates how to use seasonal_decompose on the classic airline passengers dataset, which has a clear multiplicative pattern.

Watch from 05:57 to 08:49. Notice how a single function call generates all four plots (Original, Trend, Seasonal, Residual). Pay attention to the model='multiplicative' argument and the resulting plot.

Choosing the wrong model can lead to poor results. A key diagnostic is the residuals plot. If the decomposition model has successfully captured the trend and seasonality, the residuals should look like random noise with no discernible pattern. If you see a pattern in the residuals, it means your model has failed to capture some of the underlying structure.

Time Series From Scratch - Decomposing Time Series Data

This article provides excellent code examples and visualizations of what happens when you apply the wrong model. Seeing the difference in the residual plots is crucial.

Read the section 'Decomposition in action'. It shows what the residuals look like when an additive model is incorrectly applied to a multiplicative series, and then shows the correct result with a multiplicative model. This reinforces the importance of analyzing the residuals.

Since you have a strong programming background, you might be interested in seeing how to perform these calculations from first principles using pandas. The article "Time Series DIY: Seasonal Decomposition" that we looked at for theory also contains a full manual implementation. This is optional, but highly recommended if you want to connect the theory directly to code. You can find it under the "Manual decomposition" section in resource LINK.


Conclusion

You've now learned how to statistically decompose a time series, moving beyond visual inspection to a formal separation of its core components. This is a powerful analytical tool that deepens your understanding of the data's structure.

Key Takeaways:

  • A time series can be decomposed into Trend, Seasonality, and Residuals.
  • The Additive model (Y = T + S + R) is used when seasonal effects are constant.
  • The Multiplicative model (Y = T * S * R) is used when seasonal effects are proportional to the trend.
  • Visual inspection helps you choose the right model: look for constant vs. growing seasonal fluctuations.
  • In Python, you can use statsmodels.tsa.seasonal.seasonal_decompose for easy implementation.
  • Always inspect the residuals plot: A good decomposition leaves behind only random noise, with no leftover patterns.

In our next lesson, we will introduce another critical concept: stationarity. We'll discuss what it means for a time series to be stationary and learn how to use statistical tests to check for it. De-trending, which you learned about today as part of decomposition, is one of the fundamental steps toward making a series stationary, so this lesson serves as a perfect bridge to our next topic.