Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 40 ยท Components and Decomposition

STL Decomposition

Use STL (Seasonal-Trend decomposition using Loess) in statsmodels: evolving seasonal patterns, robust fitting against outliers, tuning smoothness, and multiplicative series via logs.

STL stands for Seasonal and Trend decomposition using Loess. Loess is a way of fitting smooth curves by running many small weighted regressions on local pieces of the data. STL uses it repeatedly to estimate the seasonal pattern and the trend, and it fixes the main weaknesses of classical decomposition:

  • It gives trend and remainder values all the way to the ends of the series.
  • The seasonal pattern may change slowly over time.
  • It can be made robust to outliers.
  • You control how smooth the trend and season are.

It is the default choice for decomposing a series with a known period.

Basic usage

Python Decompose with STL

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.

Unlike seasonal_decompose, nothing is missing at the start or end. Always remember: observed = trend + seasonal + resid exactly, since STL is additive.

An evolving seasonal pattern

Suppose the amplitude of the season grows from 5 to 15 over the years. Classical decomposition averages them into one fixed pattern; STL can follow the change when seasonal is set small enough to be flexible, and seasonal_deg and seasonal_jump tune the fit.

Python Fixed versus flexible seasonality

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.

With the large value, STL behaves almost like a fixed seasonal pattern; with the small value, the peak size follows the growth from about 5 to about 15. The seasonal parameter is the loess window for each seasonal sub-series in number of cycles and must be an odd number of at least 7; larger means smoother.

Robust fitting against outliers

One wild value can distort a decomposition badly. robust=True downweights points that fit poorly.

Python Robust STL and outliers

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.

With robust=True the spike stays in the residual (as it should, because it is an outlier), while the trend and season are not dragged towards it, so the residuals elsewhere are smaller. The residual of an outlier is a convenient detection signal, which we use in the anomaly chapter.

Multiplicative series through logs

STL is additive only. For a multiplicative series, take the log, decompose, and exponentiate the components back (the parts then multiply).

Python STL on a multiplicative series

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.

Seasonally adjusted series and strength measures

Two useful by-products: the seasonally adjusted series trend + resid, and the strength of trend and seasonality, each between 0 and 1:

  • Trend strength Ft = max(0, 1 - Var(resid) / Var(trend + resid))
  • Seasonal strength Fs = max(0, 1 - Var(resid) / Var(seasonal + resid))

A value near 1 means the component dominates the noise.

Python How strong are trend and season?

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.

These two numbers summarize at a glance what kind of series you have, and they help to choose between models.

Choosing parameters

Parameter Meaning Hint
period observations per cycle required unless the index has a frequency
seasonal seasonal smoother length (odd, at least 7) large = stable season
trend trend smoother length (odd) larger = smoother trend
robust downweight outliers turn on for messy data

If the trend wiggles with the season, make trend larger; if the trend ignores real level changes, make it smaller.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is an advantage of STL over classical decomposition?

    STL's seasonal smoother lets the pattern evolve; classical decomposition forces a fixed pattern.
  2. 2. What does robust=True do?

    Iterative reweighting downweights points with large residuals so outliers do not distort the components.
  3. 3. How do you decompose a multiplicative series with STL?

    STL is additive; on the log scale a multiplicative series becomes additive.
  4. 4. A seasonal strength near 0 means...

    Fs measures how much of the seasonal-plus-residual variance is due to the seasonal part.

Summary

  • STL uses loess smoothing to split a series into trend, seasonal and remainder.
  • It has no edge gaps, allows an evolving seasonal pattern and can be robust.
  • Tune seasonal and trend smoothers; set robust=True for outliers.
  • Use logs for multiplicative series.
  • Trend and seasonal strength summarise what a series contains.

Next: Trend and Seasonality by Regression