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:
| Item | Trend | Seasonal | Remainder | Seasonally Adjusted |
|---|---|---|---|---|
| 2015-01-31 | 99.9863 | -1.6627 | -0.1736 | 99.8127 |
| 2015-02-28 | 99.9898 | -0.9314 | -0.0083 | 99.9814 |
| 2015-03-31 | 99.9930 | -0.2347 | -0.0883 | 99.9047 |
| 2015-04-30 | 99.9962 | 0.1196 | 0.1241 | 100.1200 |
| 2015-05-31 | 99.9996 | 0.4069 | 0.1535 | 100.1530 |
| 2015-06-30 | 100.0040 | 0.4018 | 0.1047 | 100.1080 |
| 2015-07-31 | 100.0080 | 0.7906 | 0.0613 | 100.0690 |
| 2015-08-31 | 100.0130 | 0.7079 | 0.0987 | 100.1120 |
| 2015-09-30 | 100.0190 | 0.5940 | -0.0833 | 99.9360 |
| 2015-10-31 | 100.0250 | 0.5867 | -0.1421 | 99.8833 |
| 2015-11-30 | 100.0310 | -0.6134 | 0.1126 | 100.1430 |
| 2015-12-31 | 100.0330 | -0.2545 | -0.1683 | 99.8645 |
| 2016-01-31 | 100.0530 | -1.5131 | -0.0398 | 100.0130 |
| 2016-02-29 | 100.0840 | -0.8905 | -0.2838 | 99.8005 |
| 2016-03-31 | 100.1300 | -0.2810 | -0.0287 | 100.1010 |
| 2016-04-30 | 100.1910 | 0.0800 | -0.3713 | 99.8200 |
| 2016-05-31 | 100.2700 | 0.3940 | -0.1841 | 100.0860 |
| 2016-06-30 | 100.3660 | 0.4275 | -0.1736 | 100.1920 |
| 2016-07-31 | 100.4770 | 0.8623 | -0.1293 | 100.3480 |
| 2016-08-31 | 100.5980 | 0.7589 | -0.2671 | 100.3310 |
| 2016-09-30 | 100.7260 | 0.4855 | -0.1517 | 100.5750 |
| 2016-10-31 | 100.8580 | 0.3241 | 0.0275 | 100.8860 |
| 2016-11-30 | 100.9930 | -0.5554 | -0.1880 | 100.8050 |
| 2016-12-31 | 101.1330 | -0.1840 | 0.2214 | 101.3540 |
| 2017-01-31 | 101.2760 | -1.3680 | 0.3415 | 101.6180 |
| 2017-02-28 | 101.4240 | -0.8493 | 0.4354 | 101.8590 |
| 2017-03-31 | 101.5720 | -0.3239 | 0.0418 | 101.6140 |
| 2017-04-30 | 101.7190 | 0.0544 | 0.1171 | 101.8360 |
| 2017-05-31 | 101.8610 | 0.3982 | -0.4193 | 101.4420 |
| 2017-06-30 | 101.9980 | 0.4634 | -0.1817 | 101.8170 |
| 2017-07-31 | 102.1300 | 0.9426 | -0.1823 | 101.9470 |
| 2017-08-31 | 102.2580 | 0.8112 | -0.0698 | 102.1890 |
| 2017-09-30 | 102.3900 | 0.2995 | 0.2909 | 102.6800 |
| 2017-10-31 | 102.5280 | 0.0107 | 0.1619 | 102.6890 |
| 2017-11-30 | 102.6750 | -0.4880 | -0.1475 | 102.5280 |
| 2017-12-31 | 102.8330 | -0.1010 | -0.0320 | 102.8010 |
| 2018-01-31 | 102.9970 | -1.2146 | -0.1123 | 102.8850 |
| 2018-02-28 | 103.1630 | -0.7732 | -0.1697 | 102.9930 |
| 2018-03-31 | 103.3240 | -0.3617 | 0.0073 | 103.3320 |
| 2018-04-30 | 103.4760 | 0.0686 | -0.3348 | 103.1410 |
| 2018-05-31 | 103.6180 | 0.4355 | 0.2166 | 103.8350 |
| 2018-06-30 | 103.7530 | 0.5155 | 0.0911 | 103.8440 |
| 2018-07-31 | 103.8890 | 1.0477 | 0.2137 | 104.1020 |
| 2018-08-31 | 104.0260 | 0.8938 | 0.2599 | 104.2860 |
| 2018-09-30 | 104.1660 | 0.0222 | 1.0922 | 105.2580 |
| 2018-10-31 | 104.3030 | -0.3628 | 1.4699 | 105.7730 |
| 2018-11-30 | 104.4350 | -0.3746 | 0.1295 | 104.5650 |
| 2018-12-31 | 104.5610 | -0.0134 | -0.1972 | 104.3630 |
| 2019-01-31 | 104.6770 | -1.1350 | -0.1323 | 104.5450 |
| 2019-02-28 | 104.7850 | -0.7319 | -0.1035 | 104.6820 |
| 2019-03-31 | 104.8880 | -0.3767 | -0.1109 | 104.7770 |
| 2019-04-30 | 104.9900 | 0.1222 | 0.3073 | 105.2980 |
| 2019-05-31 | 105.0990 | 0.5772 | -0.0059 | 105.0930 |
| 2019-06-30 | 105.2110 | 0.5808 | 0.2080 | 105.4190 |
| 2019-07-31 | 105.3230 | 1.1916 | -0.0948 | 105.2280 |
| 2019-08-31 | 105.4320 | 0.9436 | -0.1054 | 105.3260 |
| 2019-09-30 | 105.5350 | -0.4916 | 1.1963 | 106.7320 |
| 2019-10-31 | 105.6340 | -0.7903 | 1.4567 | 107.0900 |
| 2019-11-30 | 105.7270 | -0.1883 | -0.0882 | 105.6380 |
| 2019-12-31 | 105.8170 | 0.1923 | 0.0102 | 105.8280 |
| 2020-01-31 | 105.9120 | -1.0702 | 0.2881 | 106.2000 |
| 2020-02-29 | 106.0130 | -0.7385 | 0.4360 | 106.4480 |
| 2020-03-31 | 106.1200 | -0.1854 | -0.1643 | 105.9550 |
| 2020-04-30 | 106.2380 | 0.3015 | -0.2899 | 105.9480 |
| 2020-05-31 | 106.3740 | 0.8159 | -1.0298 | 105.3440 |
| 2020-06-30 | 106.5220 | 0.6191 | -0.2711 | 106.2510 |
| 2020-07-31 | 106.6770 | 1.0476 | -1.3450 | 105.3320 |
| 2020-08-31 | 106.8380 | 0.8780 | -1.5457 | 105.2920 |
| 2020-09-30 | 107.0050 | -0.9789 | -0.1766 | 106.8290 |
| 2020-10-31 | 107.1870 | -1.2010 | -0.1665 | 107.0210 |
| 2020-11-30 | 107.4110 | -0.1809 | -2.5103 | 104.9010 |
| 2020-12-31 | 107.7460 | 0.3793 | -2.7855 | 104.9610 |
| 2021-01-31 | 108.1740 | -1.0492 | -0.3246 | 107.8490 |
| 2021-02-28 | 108.6300 | -0.8513 | -0.3690 | 108.2610 |
| 2021-03-31 | 109.0900 | 0.1139 | -1.3443 | 107.7460 |
| 2021-04-30 | 109.5560 | 0.4739 | -1.6204 | 107.9360 |
| 2021-05-31 | 110.0470 | 1.0041 | -2.3007 | 107.7460 |
| 2021-06-30 | 110.5960 | 0.6595 | -2.1959 | 108.4000 |
| 2021-07-31 | 111.1700 | 1.0153 | -2.4553 | 108.7150 |
| 2021-08-31 | 111.7350 | 0.8248 | -2.7398 | 108.9950 |
| 2021-09-30 | 112.3040 | -2.0791 | -0.1648 | 112.1390 |
| 2021-10-31 | 112.8670 | -0.5712 | -1.5559 | 111.3110 |
| 2021-11-30 | 113.4270 | -0.3215 | -2.1358 | 111.2910 |
| 2021-12-31 | 114.0110 | 0.3950 | -3.0964 | 110.9150 |
| 2022-01-31 | 114.6170 | -1.0661 | -1.2803 | 113.3360 |
| 2022-02-28 | 115.2230 | -0.7374 | -1.2361 | 113.9870 |
| 2022-03-31 | 115.8290 | 0.1397 | 0.1108 | 115.9400 |
| 2022-04-30 | 116.4390 | 0.4598 | 0.0409 | 116.4800 |
| 2022-05-31 | 117.0550 | 1.1438 | -0.0392 | 117.0160 |
| 2022-06-30 | 117.6800 | 0.5731 | -0.1226 | 117.5570 |
| 2022-07-31 | 118.3130 | 0.7605 | -0.1133 | 118.2000 |
| 2022-08-31 | 118.9540 | 0.6319 | -0.0762 | 118.8780 |
| 2022-09-30 | 119.6010 | -1.2070 | 3.7560 | 123.3570 |
| 2022-10-31 | 120.2480 | -0.2394 | 3.5318 | 123.7790 |
| 2022-11-30 | 120.8920 | -0.4411 | 3.0792 | 123.9710 |
| 2022-12-31 | 121.5320 | 0.3415 | 0.1169 | 121.6480 |
| 2023-01-31 | 122.1520 | -1.0575 | 1.5553 | 123.7070 |
| 2023-02-28 | 122.7430 | -0.6830 | 1.7606 | 124.5030 |
| 2023-03-31 | 123.3160 | 0.0208 | 1.7237 | 125.0390 |
| 2023-04-30 | 123.8870 | 0.3969 | 1.4860 | 125.3730 |
| 2023-05-31 | 124.4560 | 0.8515 | 0.2928 | 124.7490 |
| 2023-06-30 | 124.9800 | 0.4521 | 0.6583 | 125.6380 |
| 2023-07-31 | 125.4410 | 0.7845 | 0.4749 | 125.9150 |
| 2023-08-31 | 125.8720 | 0.5321 | 0.7558 | 126.6280 |
| 2023-09-30 | 126.2730 | -0.6828 | 1.7596 | 128.0330 |
| 2023-10-31 | 126.6230 | 0.0694 | 0.4675 | 127.0910 |
| 2023-11-30 | 126.9260 | -0.7274 | 0.1117 | 127.0370 |
| 2023-12-31 | 127.2170 | -0.0052 | -0.6518 | 126.5650 |
| 2024-01-31 | 127.5050 | -0.9717 | -0.1532 | 127.3520 |
| 2024-02-29 | 127.7860 | -0.5480 | -0.0480 | 127.7380 |
| 2024-03-31 | 128.0580 | -0.0256 | -0.0328 | 128.0260 |
| 2024-04-30 | 128.3240 | 0.3889 | 0.0671 | 128.3910 |
| 2024-05-31 | 128.5890 | 0.5329 | -0.0713 | 128.5170 |
| 2024-06-30 | 128.8530 | 0.3461 | 0.1107 | 128.9640 |
| 2024-07-31 | 129.1180 | 0.6648 | 0.1873 | 129.3050 |
| 2024-08-31 | 129.3810 | 0.3966 | 0.0521 | 129.4330 |
| 2024-09-30 | 129.6410 | -0.1648 | 0.2437 | 129.8850 |
| 2024-10-31 | 129.8940 | 0.3216 | -0.0359 | 129.8580 |
| 2024-11-30 | 130.1400 | -0.5972 | -0.1928 | 129.9470 |
| 2024-12-31 | 130.3790 | -0.2635 | 0.0345 | 130.4130 |
| 2025-01-31 | 130.6130 | -0.8784 | 0.1653 | 130.7780 |
| 2025-02-28 | 130.8430 | -0.4037 | 0.0909 | 130.9340 |
| 2025-03-31 | 131.0770 | -0.1021 | 0.0354 | 131.1120 |
| 2025-04-30 | 131.3120 | 0.3577 | -0.0193 | 131.2920 |
| 2025-05-31 | 131.5470 | 0.1905 | 0.0228 | 131.5700 |
| 2025-06-30 | 131.7820 | 0.2090 | -0.1206 | 131.6610 |
| 2025-07-31 | 132.0160 | 0.5188 | -0.1550 | 131.8610 |
| 2025-08-31 | 132.2500 | 0.2493 | -0.0298 | 132.2210 |
| 2025-09-30 | 132.4850 | 0.3737 | -0.0884 | 132.3960 |
| 2025-10-31 | 132.7190 | 0.4889 | -0.0378 | 132.6810 |
| 2025-11-30 | 132.9530 | -0.4441 | 0.1408 | 133.0940 |
| 2025-12-31 | 133.1880 | -0.4974 | 0.1093 | 133.2970 |
132 rows
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.
| Parameter | Type | Description | Example |
|---|---|---|---|
method | str | The method. Options are: “stl” (STL), “mstl” (MSTL, several seasons), “classical” (classical moving average decomposition). Defaults to “stl”. | "stl" |
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to decompose. Defaults to “Adj Close”. | "Return" |
seasonal_periods | int | list[int] | None | The 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) |
robust | bool | Whether STL and MSTL down-weight outliers. Defaults to True. | True |
include_benchmark | bool | Whether to include “Benchmark” among the assets. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |
show_columns | str | list[str] | None | The 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)