Seasonal Decomposition

Decompose every series into its trend, its seasonal pattern and the remainder, and give the seasonally adjusted series: how much of a move is the usual pattern of the time of year (or week) and how much is news.

Seasonal patterns are strong in macroeconomic series published without seasonal adjustment, such as retail sales, employment, energy demand and prices, and weak in prices of liquid assets. The decomposition is additive, x = Trend + Seasonal + Remainder, so a pattern that grows with the level fits better on the log of the series.

The formula for STL (Cleveland, Cleveland, McRae and Terpenning, 1990) is as follows:

\[x_{t} = T_{t} + S_{t} + R_{t}\]

with \(x_{t}\) the column series (Adj Close by default). In the inner loop the series without its trend is smoothed per position in the season by local regression (Loess) into \(S_{t}\), and the series without \(S_{t}\) by Loess into \(T_{t}\). In the outer (robust) loop observations with a large remainder are down-weighted (bisquare weights) and the inner loop is repeated, so outliers do not bend the season; statsmodels’ default spans.

The formula for MSTL (Bandara, Hyndman and Bergmeir, 2021) is as follows:

\[x_{t} = T_{t} + \sum_{i=1}^{k} S_{i,t} + R_{t}\]

for seasons of \(m_{1} < \ldots < m_{k}\) periods: STL for each season in turn, from the shortest, on the series without the other seasonal parts, repeated twice.

The formula for the classical decomposition is as follows:

\[R_{t} = x_{t} - T_{t} - S_{t}\]

with \(T_{t}\) the centred moving average over m periods (2 x m for an even m), missing for the first and last floor(m / 2) dates, and \(S_{t}\) the average of \(x_{t} - T_{t}\) over the dates at the same position in the season, minus the average of those m averages, so the season sums to zero.

The Seasonally Adjusted series is \(x_{t}\) minus every seasonal part.

Also known as: seasonal adjustment, deseasonalize, STL, MSTL, classical decomposition, trend and seasonal component.

Example · Germany · 2015 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_seasonal_decomposition:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2015-01-01", end_date="2025-12-31")

consumer_prices = economics.get_consumer_price_index(
    countries=["Germany"], period="monthly"
)

econometrics = Econometrics(data=consumer_prices)

econometrics.get_seasonal_decomposition()["Germany"]

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

What were the trend, seasonal swing and seasonally adjusted level of Germany's monthly consumer price index in each month of 2025, from an STL decomposition of the data since 2015?

Which returns:

Using Your Own Data

get_seasonal_decomposition runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_seasonal_decomposition()

Parameters

get_seasonal_decomposition accepts the following parameters.

ParameterTypeDescriptionExample
methodstrThe method. Options are: “stl” (STL), “mstl” (MSTL, several seasons), “classical” (classical moving average decomposition). Defaults to “stl”."stl"
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to decompose. Defaults to “Adj Close”."Return"
seasonal_periodsint | list[int] | NoneThe periods in the season, or a list of them for MSTL (such as [5, 21] for a week and a month of trading days). Defaults to None, which takes a year of observations from the spacing of the dates (252 trading days, 52 weeks, 12 months, 4 quarters).None, which takes a year of observations from the spacing of the dates (252 trading days, 52 weeks, 12 months, 4 quarters)
robustboolWhether STL and MSTL down-weight outliers. Defaults to True.True
include_benchmarkboolWhether to include “Benchmark” among the assets. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Cleveland, R.B., Cleveland, W.S., McRae, J.E. and Terpenning, I. (1990). “STL: A Seasonal-Trend Decomposition Procedure Based on Loess.” Journal of Official Statistics, 6(1), 3-73.
  • Paper Bandara, K., Hyndman, R.J. and Bergmeir, C. (2021). “MSTL: A Seasonal-Trend Decomposition Algorithm for Time Series with Multiple Seasonal Patterns.” arXiv:2107.13462.
  • Library Shares similar logic as statsmodels’ STL
  • Library Shares similar logic as statsmodels’ MSTL
  • Library Referenced against statsmodels’ seasonal_decompose (classical)
Share