Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 40 ยท Components and Decomposition

Classical Decomposition

Split a series into trend, seasonal and remainder parts with classical additive and multiplicative decomposition: the moving-average algorithm by hand and with statsmodels.

Decomposition separates a series into the components you met in Trend, Seasonality and Noise. The classical method is the oldest and simplest one: it is only a few averaging steps, which makes it a great way to understand what every later method (STL, ETS, SARIMA) is trying to improve.

You will first build the algorithm by hand, then compare it with seasonal_decompose from statsmodels.

The algorithm in four steps

For a series with seasonal period m (12 for monthly data, 7 for daily data with a weekly pattern):

  1. Trend: compute a centered moving average of length m (a 2 x m average when m is even; see moving averages).
  2. Detrend: subtract the trend (additive) or divide by it (multiplicative).
  3. Seasonal: average the detrended values for each position in the cycle (all Januaries, all Februaries, ...), then adjust so the seasonal figures sum to 0 (additive) or average 1 (multiplicative).
  4. Remainder: what is left, y - trend - seasonal (additive) or y / (trend * seasonal) (multiplicative).

Classical decomposition

Trend Centered moving average.
Detrend Subtract or divide.
Seasonal Average per cycle position.
Remainder What is left over.

Each step is a plain average, nothing is iterated.

1 / 2

Doing it by hand

Python Classical additive decomposition from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The twelve seasonal figures sum to zero by construction, and they follow the sine wave we simulated. The trend loses m / 2 values at both ends because a centered window needs neighbours on both sides.

With statsmodels

seasonal_decompose does the same in one call:

Python seasonal_decompose

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If your series has a proper DatetimeIndex with a frequency, period can be omitted and statsmodels infers it. Otherwise pass it explicitly, as above.

Additive or multiplicative?

Choose by looking at the data: if seasonal swings grow with the level, use model="multiplicative". A multiplicative decomposition gives seasonal indices around 1.0 (for example 1.25 means "25 percent above normal"). It needs strictly positive data.

Python Multiplicative decomposition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the additive residuals are large and not constant, because a fixed seasonal amount cannot match swings that grow. Taking a log first and using the additive model is equivalent; see transformations.

Weaknesses of classical decomposition

Know these limits before relying on it:

  • No values at the ends. The trend and remainder are missing for the first and last m / 2 points, which are exactly the points you need for forecasting.
  • Constant seasonal pattern. Every January gets the same figure, so a pattern that slowly changes is averaged away.
  • Sensitive to outliers. One strange month shifts the seasonal figure and the trend nearby.
  • Over-smooths sharp turns in the trend.
  • Only one seasonal period at a time.

STL decomposition fixes most of them, which is why modern work prefers it. Classical decomposition remains valuable for quick exploration, for teaching, and as the basis of seasonal adjustment ideas.

Seasonal adjustment and strength

Subtracting the seasonal component gives a seasonally adjusted series, used in economics to compare consecutive months fairly: adjusted = y - seasonal. You can also measure how strong the components are. A common measure is the seasonal strength max(0, 1 - Var(remainder) / Var(seasonal + remainder)); values near 1 mean a clear season, near 0 mean almost none.

Python Seasonally adjusting and measuring strength

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does classical decomposition leave gaps at both ends of the trend?

    A centered window of length m cannot be computed for the first and last m/2 points.
  2. 2. In an additive decomposition the seasonal figures are adjusted so that they:

    Additive figures are centred on zero; multiplicative indices are centred on 1.
  3. 3. Seasonal swings grow as the level of the series rises. Which model fits?

    When the amplitude scales with the level the components multiply.
  4. 4. What is the biggest advantage of STL over classical decomposition?

    STL fits the season with loess, so it can evolve, and it can down-weight outliers.

Summary

  • Classical decomposition: centered moving average for the trend, per-position averages for the season, remainder is the rest.
  • Additive for constant swings; multiplicative (indices around 1) when swings scale with the level.
  • seasonal_decompose(y, model=..., period=...) does it in one call.
  • Weaknesses: missing ends, fixed seasonality, outlier sensitivity, one period only.
  • Seasonal adjustment subtracts (or divides out) the seasonal component.

Next: STL Decomposition