Use statsmodels to decompose unemployment data into trend, season, and noise
By PyQuant News
Published: 2026-04-18
URL: https://pyquantnews.substack.com/p/your-forecast-failed-for-one-reason
Each week, I send out one Python tutorial to help you get started with algorithmic trading, market data analysis, and quant finance. Upgrade to a paid plan to access the code notebooks.

In today's post, you'll use Python to break US unemployment data into trend, seasonal, and noise components using two different methods, then compare the results.
Breaking a time series into parts
Time series decomposition means splitting a single data series into separate components, each representing a different pattern.
The most common ones are trend (the long-term direction), seasonality (repeating patterns at regular intervals), and residual noise (the leftover randomness that doesn’t fit either pattern).
The idea has been around for decades.
Economists in the mid-20th century developed early decomposition methods to study business cycles and strip out seasonal effects from employment and GDP data. The techniques stuck because they work.
They give you a clearer picture of what’s actually happening beneath the surface of a noisy chart.
How professionals use decomposition
Today, decomposition is a standard first step for anyone forecasting economic or financial time series (a sequence of data points recorded over time, like monthly unemployment or daily stock prices).
Macro investors use it to separate the long-term direction of indicators like unemployment or inflation from short-term seasonal swings.
Portfolio managers use the seasonal component to time rebalancing decisions, adjusting their holdings around predictable patterns in the data. Risk teams look at the residual noise to check whether a model captures the real structure of the data or just fits to randomness.
Why this matters for beginners
If you’re learning Python for trading or market analysis, decomposition is one of the most practical skills you can pick up early.
It teaches you to look at data critically instead of taking a chart at face value. Once you can separate trend from seasonality and noise, you’ll build better models, spot problems faster, and avoid the common trap of fitting a prediction to patterns that are really just random fluctuations.
This Substack is reader-supported. To receive new posts and get the code notebooks, consider becoming a free or paid subscriber.
Library installation
This installs the core libraries for data manipulation and time series decomposition used in this notebook.

Imports and setup
pandas handles tabular data and time series indexing. statsmodels provides two decomposition methods: seasonal\_decompose for classical additive decomposition and STL for a more robust, flexible alternative. openbb provides a convenient API for pulling economic data like US unemployment directly into a DataFrame.

Fetch and prepare unemployment data
We pull US unemployment data starting from 2010 using OpenBB, then filter it to end before 2020 so we have a clean pre-pandemic window without the extreme COVID-era distortions.

Choosing a stable date range matters more than most beginners realize. We’ll see this clearly.
Visualize raw data with rolling statistics
A 12-month rolling mean and rolling standard deviation help us visually separate the long-term trend from short-term volatility before we run any formal decomposition.

The result is a chart that looks like this.

The rolling mean smooths out monthly noise and reveals the underlying direction of unemployment over time. The rolling standard deviation shows whether the data’s variability is stable or changing.
If the standard deviation shrinks as the mean falls, that tells us the series is becoming more predictable, which is useful information before we choose a decomposition model.
This kind of quick visual check is how professionals decide whether a simple additive model is appropriate or whether they need something more flexible.
Decompose with classical additive method
Classical additive decomposition assumes the original series equals trend plus seasonality plus residual noise. This is the simplest approach and a good starting point for data where seasonal swings stay roughly constant in size over time.

The result is a chart that looks like this.

The plot shows four panels: the original series, the extracted trend, the seasonal pattern, and the residuals. If the residuals look random with no visible structure, the decomposition captured the meaningful patterns well.
If you see leftover trends or cycles in the residuals, that signals the additive model missed something. One limitation of seasonal\_decompose is that it uses a fixed seasonal pattern across the entire series, which can be too rigid for real-world economic data where seasonal effects shift over time.
Decompose with STL for comparison
STL (Seasonal and Trend decomposition using Loess) fits the seasonal component locally rather than assuming it stays fixed, making it more adaptive to changes in the data’s structure over time.

The result is a chart that looks like this.

Comparing the STL output to the classical decomposition above is where the real learning happens. Look at the seasonal panels side by side. If the seasonal pattern in STL shifts slightly across years while the classical version repeats identically, that tells you the seasonal effect in unemployment isn’t perfectly stable.
STL’s flexibility is why macro research desks tend to prefer it for economic indicators. The residuals from STL are also typically smaller and more random, which means the model is capturing more of the actual signal and leaving less structure on the table.
Your next steps
You can now decompose any economic or financial time series into trend, seasonal, and noise components using Python. That single step will improve every forecast you build because you’ll be modeling real structure instead of fitting to randomness.