Decomposing a time series
Use statsmodels seasonal_decompose to extract trend, seasonal, and residual components — and choose between additive and multiplicative models.
- Build a synthetic time series with trend, seasonality, and noise
- Run seasonal_decompose with model='additive' and inspect each component
- Decide when multiplicative decomposition is more appropriate
Decomposition is diagnostic before it is operational. It answers: how much of the variation in this series is trend? How much is seasonality? What remains in the residual? Those proportions guide model choice and tell you whether preprocessing (differencing, log transform) is necessary.
The decomposition works by estimating the trend with a centred moving average, subtracting it, then averaging across periods to get the seasonal pattern. The residual is whatever is left. If the residual looks roughly like white noise (mean near zero, constant variance, no autocorrelation), the additive decomposition has captured the main structure.
Additive vs multiplicative
The model argument controls the decomposition arithmetic:
Additive (model='additive'): y = trend + seasonal + residual. The
seasonal component has a roughly constant amplitude regardless of trend level.
Use this when a December peak is consistently about 20 units above the monthly
average, independent of whether the average is 100 or 500.
Multiplicative (model='multiplicative'): y = trend × seasonal × residual.
The seasonal component is expressed as a ratio. Use this when the December
peak is consistently 20% above the monthly average — so as the average grows,
the absolute peak grows proportionally.
A quick diagnostic: plot the residuals. If you used additive decomposition but the series is multiplicative, the residuals will have a funnel shape (variance grows with trend level). Switching to multiplicative — or equivalently, taking a log transform before additive decomposition — should flatten them.
extrapolate_trend="freq" fills the NaN values that appear at the edges of
the trend component due to the moving-average window. Without it, the first
and last few observations have no trend estimate. For long series this is
irrelevant; for short series (fewer than 3–4 full periods) it matters.
Where to go next
With the components visible, the next question is: does the series remember its own past? Next: autocorrelation — the measure that quantifies how today's value relates to yesterday's, last week's, and last year's.
Time series structure
Trend, seasonality, and noise are the three components of any time series — and stationarity is the property most forecasting models require.
Autocorrelation
Autocorrelation measures how strongly a series predicts its own future — and the ACF plot is the first tool to reach for when choosing a forecasting model.