Interactive statistics
Module 8: Time series & forecasting
Progresscompleted 0 / 63

What a series is made of: trend, seasonality, noise

Regression in the previous module answered the question «what drives the metric». The second question a business asks is «what happens next», and you cannot answer it by plugging time into that same regression. A line on a dashboard is several movements at once: a slow climb, a weekly cycle and random wobble. Until they are separated, a Sunday bump is easily mistaken for the result of your own work. The sliders set the strength of each part; the «decompose» button shows them one by one.

what the dashboard shows15585days →

The top line is the sum of the three below it. The cyan line on it is the trend estimated by a 7-day moving average.

In front of you is daily revenue over three months — an ordinary chart from a report. The line goes up, but it jumps around: a rise here, a dip there, and it is not obvious which part is a pattern and which is chance. The first step with a series is not to forecast it, but to take that line apart. Press «decompose».

What it means

The first thing worth doing with any metric over time is to look at it decomposed rather than raw. In pandas and statsmodels that is one line: seasonal_decompose or STL. After that, discussing whether the metric «dropped» only makes sense on the trend.

The period comes from the domain, it is not tuned: 7 for daily data, 24 for hourly, 12 for monthly. Get it wrong and the seasonality leaks into the residual, which makes the decomposition meaningless.

Calendar effects are not described by seasonality: Black Friday, national holidays, shifted weekends. They land in the residual and look like outliers until they are fed into the model as separate features — which is what the SARIMAX lesson is about.

Where it shows up

Retail lives on a weekly cycle: Friday and Saturday bring noticeably more than weekdays. Food delivery adds a daily one: the lunch and evening peaks.

Trend and seasonality are easy to confuse in a short window. Three rising days inside a weekly wave are not a trend, they are the upswing of the cycle.

Government statistics publish «seasonally adjusted» figures for exactly this reason: without the adjustment, comparing January with December is meaningless — and decisions are made by comparison.

Definitions

Time series
a sequence of values of a metric ordered in time with a fixed step: day, week, month.
Trend
the slow directed movement of a series that remains once cyclical and random fluctuations are removed.
Seasonality
a fluctuation with a known period that repeats from cycle to cycle: day of week, month of year, hour of day.
Residual
what is left of the series after the trend and the seasonality are subtracted.
Additive and multiplicative decompositiony = T + S + E or y = T · S · E
the parts either add up (the size of the cycle stays constant) or multiply (the size grows with the level).

When the method lies (assumptions)

Decomposition assumes the period of the cycle is known and constant. Moving holidays (Easter, lunar calendars) and shifted weekends break that assumption — they are usually handled by separate features.

A moving average as a trend estimate is undefined at the edges: the first and last days have no full window. The edge is exactly where a forecaster needs it most, and exactly where the trend estimate is least reliable.

Deep dive: the math and the mechanism (optional)

Classical decomposition by moving average is the simplest of the methods. STL (Seasonal-Trend decomposition using Loess) is more flexible: trend and seasonality are estimated iteratively by local regression, the seasonal shape is allowed to change slowly over time, and the method is robust to outliers. For series longer than a couple of years it is usually the one to take.

Splitting into «trend» and «seasonality» is a convention, not a property of nature. The same series can be decomposed differently by changing the window length: a short window assigns part of the slow movement to the trend, a long one pushes it into the residual. So the correct phrasing is «the trend at this level of smoothing», not «the true trend of the series».

Next →
Breakdowns like this live in the channel

Data analytics in plain words: how to count metrics, how not to fool yourself in an A/B test, what gets asked in interviews and how this site gets built. The channel is in Russian.

Open the Telegram channel