Classical Decomposition
The Classical Decomposition estimates the trend with a centred moving average over one season and the seasonal part with the average deviation from that trend at each position in the season. It is the oldest form of seasonal adjustment and the base of the methods that followed, such as X-11.
The formula for the Classical Decomposition starts from the trend \(T_{t}\), the centred moving average of the series \(x_{t}\) over a season of m periods; for an even \(m = 2 \cdot k\) it is the 2 x m moving average, which gives the two ends half weight:
\[T_{t} = \frac{\frac{x_{t-k}}{2} + \sum_{j=1 - k}^{k - 1} x_{t+j} + \frac{x_{t+k}}{2}}{m}\]For an odd \(m = 2 \cdot k + 1\) it is the plain average of the m values around t:
\[T_{t} = \frac{\sum_{j=-k}^{k} x_{t+j}}{m}\]The seasonal part \(S_{t}\) is the average \(A_{p}\) of the detrended series \(x_{t} - T_{t}\) over the dates at the same position p in the season as t, minus the average of those m averages, so the season sums to zero:
\[S_{t} = A_{p} - \frac{\sum_{q=1}^{m} A_{q}}{m}\]The remainder is what the trend and the season leave:
\[R_{t} = x_{t} - T_{t} - S_{t}\]The seasonal pattern is the same in every year and the trend is missing for the first and last k dates. The moving average makes the trend smooth but bends it around outliers, which also end up in the season.
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.
A seasonal decomposition splits a series into a trend, the slow movement of its level; a seasonal part, the pattern that repeats every year, quarter or week; and a remainder, what neither explains. The series without its seasonal part is the seasonally adjusted series, the form in which statistical offices publish most macroeconomic data, as it shows whether a change is more than the usual pattern of the time of year. Seasonal patterns are strong in series published without seasonal adjustment, such as retail sales, employment, energy demand and consumer prices, and weak in the prices of liquid assets. The decompositions here are additive, the parts add up to the series, so a pattern that grows with the level of the series fits better on its logarithm. The trend and the season near the ends of the sample are the least certain and move as new data arrives.
Also known as: classical seasonal decomposition, moving average decomposition, seasonal_decompose.
In the Finance Toolkit this is get_seasonal_decomposition with method="classical", the same function as Seasonal Decomposition.
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(method="classical")["Germany"]
| Parameter | Type | Description | Example |
|---|---|---|---|
method | str | The method. Options are: “stl” (Seasonal-Trend Decomposition Based on Loess, STL), “mstl” (Multiple Seasonal-Trend Decomposition Based on Loess, several seasons), “classical” (Classical Decomposition, moving averages). 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 |
Which returns:
| Item | Trend | Seasonal | Remainder | Seasonally Adjusted |
|---|---|---|---|---|
| 2015-01-31 | -1.0532 | 99.2032 | ||
| 2015-02-28 | -0.6124 | 99.6624 | ||
| 2015-03-31 | -0.0526 | 99.7226 | ||
| 2015-04-30 | 0.2728 | 99.9672 | ||
| 2015-05-31 | 0.3505 | 100.2100 | ||
| 2015-06-30 | 0.3592 | 100.1510 | ||
| 2015-07-31 | 100.0150 | 0.6480 | 0.1974 | 100.2120 |
| 2015-08-31 | 100.0230 | 0.4425 | 0.3541 | 100.3770 |
| 2015-09-30 | 100.0240 | 0.4178 | 0.0885 | 100.1120 |
| 2015-10-31 | 100.0160 | 0.3873 | 0.0669 | 100.0830 |
| 2015-11-30 | 99.9983 | -0.5926 | 0.1242 | 100.1230 |
| 2015-12-31 | 99.9996 | -0.5672 | 0.1776 | 100.1770 |
| 2016-01-31 | 100.0190 | -1.0532 | -0.4655 | 99.5532 |
| 2016-02-29 | 100.0450 | -0.6124 | -0.5221 | 99.5224 |
| 2016-03-31 | 100.0780 | -0.0526 | -0.2053 | 99.8726 |
| 2016-04-30 | 100.1310 | 0.2728 | -0.5036 | 99.6272 |
| 2016-05-31 | 100.1920 | 0.3505 | -0.0622 | 100.1290 |
| 2016-06-30 | 100.2870 | 0.3592 | -0.0259 | 100.2610 |
| 2016-07-31 | 100.4250 | 0.6480 | 0.1374 | 100.5620 |
| 2016-08-31 | 100.5850 | 0.4425 | 0.0625 | 100.6470 |
| 2016-09-30 | 100.7340 | 0.4178 | -0.0915 | 100.6420 |
| 2016-10-31 | 100.8780 | 0.3873 | -0.0552 | 100.8230 |
| 2016-11-30 | 101.0170 | -0.5926 | -0.1749 | 100.8430 |
| 2016-12-31 | 101.1430 | -0.5672 | 0.5939 | 101.7370 |
| 2017-01-31 | 101.2820 | -1.0532 | 0.0207 | 101.3030 |
| 2017-02-28 | 101.4320 | -0.6124 | 0.1904 | 101.6220 |
| 2017-03-31 | 101.5920 | -0.0526 | -0.2490 | 101.3430 |
| 2017-04-30 | 101.7340 | 0.2728 | -0.1165 | 101.6170 |
| 2017-05-31 | 101.8700 | 0.3505 | -0.3809 | 101.4900 |
| 2017-06-30 | 102.0090 | 0.3592 | -0.0880 | 101.9210 |
| 2017-07-31 | 102.1320 | 0.6480 | 0.1104 | 102.2420 |
| 2017-08-31 | 102.2410 | 0.4425 | 0.3162 | 102.5580 |
| 2017-09-30 | 102.3620 | 0.4178 | 0.2006 | 102.5620 |
| 2017-10-31 | 102.4870 | 0.3873 | -0.1740 | 102.3130 |
| 2017-11-30 | 102.6430 | -0.5926 | -0.0104 | 102.6330 |
| 2017-12-31 | 102.8310 | -0.5672 | 0.4364 | 103.2670 |
| 2018-01-31 | 103.0120 | -1.0532 | -0.2884 | 102.7230 |
| 2018-02-28 | 103.1970 | -0.6124 | -0.3642 | 102.8320 |
| 2018-03-31 | 103.3830 | -0.0526 | -0.3607 | 103.0230 |
| 2018-04-30 | 103.5920 | 0.2728 | -0.6549 | 102.9370 |
| 2018-05-31 | 103.7950 | 0.3505 | 0.1249 | 103.9190 |
| 2018-06-30 | 103.9530 | 0.3592 | 0.0479 | 104.0010 |
| 2018-07-31 | 104.0940 | 0.6480 | 0.4079 | 104.5020 |
| 2018-08-31 | 104.2390 | 0.4425 | 0.4987 | 104.7370 |
| 2018-09-30 | 104.3700 | 0.4178 | 0.4918 | 104.8620 |
| 2018-10-31 | 104.5220 | 0.3873 | 0.5006 | 105.0230 |
| 2018-11-30 | 104.6720 | -0.5926 | 0.1101 | 104.7830 |
| 2018-12-31 | 104.7990 | -0.5672 | 0.1181 | 104.9170 |
| 2019-01-31 | 104.9200 | -1.0532 | -0.4572 | 104.4630 |
| 2019-02-28 | 105.0190 | -0.6124 | -0.4563 | 104.5620 |
| 2019-03-31 | 105.1040 | -0.0526 | -0.6515 | 104.4530 |
| 2019-04-30 | 105.1810 | 0.2728 | -0.0340 | 105.1470 |
| 2019-05-31 | 105.2710 | 0.3505 | 0.0486 | 105.3200 |
| 2019-06-30 | 105.3930 | 0.3592 | 0.2479 | 105.6410 |
| 2019-07-31 | 105.5340 | 0.6480 | 0.2379 | 105.7720 |
| 2019-08-31 | 105.6790 | 0.4425 | 0.1483 | 105.8280 |
| 2019-09-30 | 105.8100 | 0.4178 | 0.0126 | 105.8220 |
| 2019-10-31 | 105.9010 | 0.3873 | 0.0114 | 105.9130 |
| 2019-11-30 | 105.9560 | -0.5926 | 0.0863 | 106.0430 |
| 2019-12-31 | 106.0130 | -0.5672 | 0.5743 | 106.5870 |
| 2020-01-31 | 106.0470 | -1.0532 | 0.1357 | 106.1830 |
| 2020-02-29 | 106.0420 | -0.6124 | 0.2808 | 106.3220 |
| 2020-03-31 | 106.0210 | -0.0526 | -0.1986 | 105.8230 |
| 2020-04-30 | 105.9850 | 0.2728 | -0.0078 | 105.9770 |
| 2020-05-31 | 105.9350 | 0.3505 | -0.1251 | 105.8090 |
| 2020-06-30 | 105.8760 | 0.3592 | 0.6349 | 106.5110 |
| 2020-07-31 | 105.9170 | 0.6480 | -0.1851 | 105.7320 |
| 2020-08-31 | 106.0580 | 0.4425 | -0.3300 | 105.7280 |
| 2020-09-30 | 106.2150 | 0.4178 | -0.7832 | 105.4320 |
| 2020-10-31 | 106.3920 | 0.3873 | -0.9598 | 105.4330 |
| 2020-11-30 | 106.5900 | -0.5926 | -1.2779 | 105.3130 |
| 2020-12-31 | 106.7900 | -0.5672 | -0.8824 | 105.9070 |
| 2021-01-31 | 107.0200 | -1.0532 | 0.8328 | 107.8530 |
| 2021-02-28 | 107.3120 | -0.6124 | 0.7104 | 108.0220 |
| 2021-03-31 | 107.6400 | -0.0526 | 0.2731 | 107.9130 |
| 2021-04-30 | 108.0200 | 0.2728 | 0.1172 | 108.1370 |
| 2021-05-31 | 108.4850 | 0.3505 | -0.0859 | 108.4000 |
| 2021-06-30 | 108.9950 | 0.3592 | -0.2938 | 108.7010 |
| 2021-07-31 | 109.4710 | 0.6480 | -0.3892 | 109.0820 |
| 2021-08-31 | 109.9420 | 0.4425 | -0.5650 | 109.3770 |
| 2021-09-30 | 110.5280 | 0.4178 | -0.8861 | 109.6420 |
| 2021-10-31 | 111.2260 | 0.3873 | -0.8736 | 110.3530 |
| 2021-11-30 | 111.9740 | -0.5926 | -0.4112 | 111.5630 |
| 2021-12-31 | 112.7440 | -0.5672 | -0.8665 | 111.8770 |
| 2022-01-31 | 113.5060 | -1.0532 | -0.1830 | 113.3230 |
| 2022-02-28 | 114.2950 | -0.6124 | -0.4321 | 113.8620 |
| 2022-03-31 | 115.2020 | -0.0526 | 0.9306 | 116.1330 |
| 2022-04-30 | 116.2390 | 0.2728 | 0.4281 | 116.6670 |
| 2022-05-31 | 117.2960 | 0.3505 | 0.5136 | 117.8090 |
| 2022-06-30 | 118.2640 | 0.3592 | -0.4934 | 117.7710 |
| 2022-07-31 | 119.1420 | 0.6480 | -0.8296 | 118.3120 |
| 2022-08-31 | 120.0150 | 0.4425 | -0.9471 | 119.0670 |
| 2022-09-30 | 120.8290 | 0.4178 | 0.9031 | 121.7320 |
| 2022-10-31 | 121.5710 | 0.3873 | 1.5814 | 123.1530 |
| 2022-11-30 | 122.2490 | -0.5926 | 1.8734 | 124.1230 |
| 2022-12-31 | 122.8910 | -0.5672 | -0.3336 | 122.5570 |
| 2023-01-31 | 123.5450 | -1.0532 | 0.1582 | 123.7030 |
| 2023-02-28 | 124.1860 | -0.6124 | 0.2462 | 124.4320 |
| 2023-03-31 | 124.7220 | -0.0526 | 0.3910 | 125.1130 |
| 2023-04-30 | 125.0890 | 0.2728 | 0.4081 | 125.4970 |
| 2023-05-31 | 125.3560 | 0.3505 | -0.1064 | 125.2490 |
| 2023-06-30 | 125.6620 | 0.3592 | 0.0687 | 125.7310 |
| 2023-07-31 | 126.0080 | 0.6480 | 0.0441 | 126.0520 |
| 2023-08-31 | 126.3040 | 0.4425 | 0.4137 | 126.7180 |
| 2023-09-30 | 126.5670 | 0.4178 | 0.3656 | 126.9320 |
| 2023-10-31 | 126.8150 | 0.3873 | -0.0419 | 126.7730 |
| 2023-11-30 | 127.0840 | -0.5926 | -0.1812 | 126.9030 |
| 2023-12-31 | 127.3620 | -0.5672 | -0.2344 | 127.1270 |
| 2024-01-31 | 127.6320 | -1.0532 | -0.1989 | 127.4330 |
| 2024-02-29 | 127.8800 | -0.6124 | -0.0771 | 127.8020 |
| 2024-03-31 | 128.0900 | -0.0526 | -0.0369 | 128.0530 |
| 2024-04-30 | 128.3140 | 0.2728 | 0.1931 | 128.5070 |
| 2024-05-31 | 128.5670 | 0.3505 | 0.1328 | 128.7000 |
| 2024-06-30 | 128.8430 | 0.3592 | 0.1079 | 128.9510 |
| 2024-07-31 | 129.1390 | 0.6480 | 0.1829 | 129.3220 |
| 2024-08-31 | 129.4250 | 0.4425 | -0.0375 | 129.3870 |
| 2024-09-30 | 129.6900 | 0.4178 | -0.3874 | 129.3020 |
| 2024-10-31 | 129.9350 | 0.3873 | -0.1419 | 129.7930 |
| 2024-11-30 | 130.1670 | -0.5926 | -0.2245 | 129.9430 |
| 2024-12-31 | 130.3870 | -0.5672 | 0.3306 | 130.7170 |
| 2025-01-31 | 130.5940 | -1.0532 | 0.3595 | 130.9530 |
| 2025-02-28 | 130.8040 | -0.6124 | 0.3383 | 131.1420 |
| 2025-03-31 | 131.0410 | -0.0526 | 0.0214 | 131.0630 |
| 2025-04-30 | 131.2930 | 0.2728 | 0.0843 | 131.3770 |
| 2025-05-31 | 131.5550 | 0.3505 | -0.1455 | 131.4100 |
| 2025-06-30 | 131.8030 | 0.3592 | -0.2921 | 131.5110 |
| 2025-07-31 | 0.6480 | 131.7320 | ||
| 2025-08-31 | 0.4425 | 132.0280 | ||
| 2025-09-30 | 0.4178 | 132.3520 | ||
| 2025-10-31 | 0.3873 | 132.7830 | ||
| 2025-11-30 | -0.5926 | 133.2430 | ||
| 2025-12-31 | -0.5672 | 133.3670 |
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()
References
- Paper Hyndman, R.J. and Athanasopoulos, G. (2021). “Forecasting: Principles and Practice.” 3rd edition, OTexts, section 3.4.
- Library Referenced against statsmodels’ seasonal_decompose (Classical Decomposition)