Everything so far treated observations as interchangeable, a bag of rows you could shuffle without loss. A time series breaks that assumption: the observations arrive in order, and each one leans on the ones before it. That memory is both the challenge and the opportunity. The first skill is to decompose a series into its parts, and to check whether it is stationary, the property nearly every forecasting model assumes.
Most models (ARIMA and friends) assume a constant mean and variance. Real series rarely oblige, they trend and they cycle. The fix is differencing: model the change from one period to the next instead of the level. Getting a series stationary is the doorway to every forecasting method ahead.
A Time Series Is a Sum of Parts
Look at almost any real series, monthly sales, daily temperature, hourly traffic, and you can pick out four distinct behaviors layered on top of each other. Decomposition is the act of separating them.
- ●Trend, the long-run direction. It need not be a straight line; it is whatever remains after the shorter wiggles are smoothed away (a moving average is the simplest smoother).
- ●Seasonality, a pattern that repeats on a fixed calendar period, every 12 months, every 7 days, every 24 hours. Retail peaks in December; traffic peaks at rush hour.
- ●Cyclical, longer swings with no fixed length (multi-year business cycles). Unlike seasonality, you cannot set your calendar by it.
- ●Irregular, the random remainder once trend, season, and cycle are removed. The goal is for this leftover to look like pure noise.
Decomposition: Additive vs Multiplicative
The components combine in one of two ways, and knowing which one you have decides how you model it.
In an additive series the seasonal swing is a roughly constant amount (say, always about $6,000 more
in December). In a multiplicative series it grows with the level (always about 20 percent more), so
the wave gets visibly taller as the trend climbs. A neat trick: taking logs of a multiplicative
series turns it additive, because log(T × S) = log T + log S. In the companion notebook, one line,
seasonal_decompose(sales, model='additive', period=12), splits the series and plots all four panels; the
residual sits in a flat band, confirming an additive fit here.
Stationarity, Differencing & White Noise
A series is stationary when its statistical behavior does not depend on when you look: a constant mean, a constant variance, and no trend or seasonality drifting the picture. It matters because most models are built on that assumption, and most raw series violate it.
The formal check is the Augmented Dickey-Fuller (ADF) test: a small p-value (below 0.05) says stationary. Our raw sales series fails badly (p about 0.90) because its mean climbs. Differencing, replacing each value with its change from the month before, strips the trend, and the differenced series passes (p about 0.02). That “difference until stationary” step is the I (integrated) in ARIMA, which the next chapter builds on. Seasonal patterns get their own seasonal difference at the season's lag.
The endpoint of all this is white noise: a series with zero mean, constant variance, and no autocorrelation, no memory from one step to the next. White noise is the part you cannot forecast, and it is the target for your model's residuals. The autocorrelation function (ACF) makes memory visible: our sales series correlates strongly with last month (lag-1 about 0.79) and shows a telltale spike at lag 12 (the yearly echo), while white noise has every bar sitting near zero. When residuals finally look like white noise, the signal has been fully captured.
Real-World Example: Six Years of Online Sales
The companion notebook works a real monthly series end to end, decomposing it, testing stationarity, and reading its autocorrelation, all with statsmodels.
Monthly sales for a growing online store, 72 observations from January
2018 to December 2023. Two columns: month (the calendar month) and sales (total dollars).
A clean, compact series with all four components on display.
- ●Trend: sales grow about $284 a month (roughly $3,400 a year); the first-year average near $22,000 rises to about $37,000 by the last year.
- ●Seasonality: a 12-month pattern peaking in December (about +$6,300) and bottoming in January (about -$3,800).
- ●Stationarity: the raw series is non-stationary (ADF p ≈ 0.90); after one difference it is stationary (p ≈ 0.02).
Time Series in Machine Learning & AI
Decomposition and stationarity are not just classical-statistics housekeeping; they shape how modern models are built, fed, and judged. The same components resurface as engineered features and as the structure deep models learn.
| Where it shows up | What it looks like | Examples |
|---|---|---|
| Classical forecasting | Model the components directly after making the series stationary | ARIMA / SARIMA, exponential smoothing (ETS), Prophet |
| Feature engineering | Turn trend and seasonality into columns: lags, rolling means, and calendar parts, then use a tabular model | Lag features + XGBoost / LightGBM for demand forecasting |
| Deep sequence models | Networks that learn temporal dependence directly from the raw sequence | RNN, LSTM, GRU, temporal CNNs, Temporal Fusion Transformer |
| Anomaly detection | Flag points whose value or residual departs from the expected pattern | Fraud, sensor faults, monitoring model drift (see Anomaly Detection) |
| Foundation models | Large pre-trained models that forecast a new series zero-shot | TimeGPT, Amazon Chronos, Lag-Llama, Google TimesFM |
The hottest area is time-series foundation models, transformers pre-trained on billions of points across many domains that forecast an unseen series with little or no fine-tuning, echoing what large language models did for text. Whatever the architecture, the ideas in this chapter remain the vocabulary: models still succeed or fail on how well they handle trend, seasonality, and non-stationarity, and they are still judged by whether their residuals look like white noise.
See the components in Python
The companion notebook loads the six-year sales series, smooths out the trend with a moving average, decomposes it into trend, seasonal, and residual with seasonal_decompose, compares additive versus multiplicative, tests stationarity with the ADF test before and after differencing, and reads the autocorrelation function against white noise, every concept with a plot.
View opens the rendered notebook instantly.
Open in Colab runs it live. To run locally, install numpy, pandas,
matplotlib, statsmodels, and openpyxl.
🎓 Key Takeaways
- ✓Order carries meaning: a time series cannot be shuffled, each value leans on the ones before it.
- ✓Four components: trend (long-run direction), seasonality (fixed-period repeat), cycle (variable-length swing), and irregular noise.
- ✓Additive vs multiplicative: a constant seasonal swing versus one that grows with the level, take logs to turn the second into the first.
- ✓Stationarity is the gate: constant mean and variance; test with ADF and reach it by differencing (the “I” in ARIMA).
- ✓White noise is the target: no autocorrelation left; when residuals look like it, the signal is captured, and the ACF is how you check.
Practice Challenges
Five exercises on the sales series. Full solutions are in the companion solutions notebook.
Year-over-year growth
Compute the 12-month percent change and report the average annual growth rate. Why does this cancel seasonality?
s.pct_change(12).The seasonal profile
Chart the average sales for each calendar month and name the peak and trough.
s.groupby(s.index.month).mean().Additive or multiplicative?
Decompose the series both ways and compare how flat the residuals are. Which fits better here?
seasonal_decompose(model=...).Make it stationary
Difference the series and run the ADF test; also try a seasonal difference at lag 12. Report the p-values.
adfuller(s.diff().dropna()).Autocorrelation vs white noise
Plot the ACF and PACF, find the seasonal lag, and contrast with the ACF of a white-noise series.
plot_acf, plot_pacf.Solutions notebook
All five challenges worked in code, year-over-year growth, the seasonal profile, additive versus multiplicative, differencing to stationarity, and the ACF/PACF against white noise, each with a short explanation.
Quiz: Test Yourself
Eight questions on components, decomposition, stationarity, and white noise. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved.
Now that you can decompose a series and make it stationary, you are ready to predict its future. Forecasting Models builds moving averages, exponential smoothing, and ARIMA/SARIMA on exactly these foundations.