A time series may contain a slowly varying trend, a repeating seasonal pattern, and irregular variation. Decomposition tries to separate these components so we can understand the structure before building a forecast.
Additive versus multiplicative structure
The additive model writes
where is the trend-cycle, the seasonal component, and the remainder. It is a natural starting point when seasonal swings are roughly constant in size.
When fluctuations grow with the level of the series, a multiplicative model may be more appropriate:
For positive observations, taking logarithms converts the multiplicative model into an additive one. Box–Cox transformations generalize this idea and can stabilize the scale of seasonal variation.
Classical decomposition uses moving averages
A centered moving average smooths short-term variation. For odd window size ,
With even seasonal periods, a centered moving average can be used to align the smoothed estimate with observations. After estimating the trend, classical decomposition averages detrended values by season to estimate a repeating seasonal pattern.
This works well when seasonality is reasonably stable, but it has limitations: endpoint estimates are missing for centered windows, unusual observations can distort the averages, and the same seasonal shape is assumed to repeat.
STL allows seasonality to evolve
STL uses locally weighted regression (LOESS) in an iterative decomposition. It has controls for the smoothness of the seasonal and trend components and can perform robust reweighting to reduce the influence of outliers.
The next experiment uses a real STL implementation rather than a visual approximation. I precomputed fits using statsmodels.tsa.seasonal.STL for a synthetic monthly series with a slowly changing trend, evolving seasonal amplitude, and optional outlier. Each combination of controls selects a different fit.
STL decomposition under changing seasonality
These are genuine precomputed statsmodels STL fits to a synthetic monthly series (period=12). Adjust the fitting windows or add an outlier to compare robust and ordinary fitting.
The panels have independent vertical scales to reveal structure. A smoother trend window responds more slowly to local changes. Robust STL downweights unusual observations during its iterative fit; it does not erase the outlier from the observed series.
Try turning the outlier on at month 76. Compare robust fitting on and off, keeping the smoothing windows fixed. Then change the seasonal window to see how much short-term seasonal movement the model allows.
Reproduce the experiment in Python
import numpy as npfrom statsmodels.tsa.seasonal import STL
rng = np.random.default_rng(41)t = np.arange(120)trend = 22 + 0.09*t + 2*np.sin(t/30)seasonal = (3 + 0.012*t)*np.sin(2*np.pi*t/12)noise = rng.normal(0, 0.65, len(t))y = trend + seasonal + noisey[75] += 13 # unusual observation
fit = STL( y, period=12, seasonal=13, trend=25, robust=True).fit()
trend_estimate = fit.trendseasonal_estimate = fit.seasonalremainder = fit.residThe interactive example includes an additional harmonic in the seasonal component, but the same decomposition method applies. The observed data is always the sum of the fitted components up to numerical rounding.
Decomposition is not forecasting
A smooth-looking trend is not automatically a good forecast. STL describes historical structure; it does not itself specify how the trend and seasonal components should evolve into the future. To forecast, we still need a model for future dynamics, plus a principled method for evaluating its errors on unseen periods.
There is also a danger in using decomposition features for supervised forecasting: if a centered smoother is fitted to the entire series before splitting training and test data, the decomposition at earlier times can depend on future observations. A leakage-safe forecasting pipeline must fit transformations using only information available at each forecast origin.
Takeaway
Classical decomposition is easy to understand and useful as a baseline. STL is more flexible when seasonal patterns evolve and can be more robust to outliers. Neither replaces time-aware evaluation or a forecasting model.