Semester 1 · Week 15
Time series analysis
- Ch 20 Time Series Analysis
Chapter 20: Time series analysis
Extrapolate the patterns, and be honest about the uncertainty.
Notes · slide 2
Time: 50 minutes. About 25 on the chapter, 10 on the worked example, 10 to close the semester, and 5 to check understanding. Keep the end of the session for the capstone wrap-up.
Say: Every model so far assumed rows are independent. Shuffle them and nothing changes. Time series breaks that. Today depends on yesterday, December on last December. A random split leaks the future and inflates every metric.
Key line: extrapolate the patterns, and be honest about the uncertainty.
Next: what a series is made of.
Three components
- Trend: the long-run direction.
- Seasonality: a pattern that repeats at a fixed period.
- Residual: what's left.
y=trend+seasonal+residual
Use a multiplicative form when the seasonal swing grows with the level.
Notes · slide 3
Say: Trend is the long-run direction. Seasonality repeats at a fixed period: weekly, yearly. The residual is what's left.
Say: Additive when the seasonal swing stays the same size. Multiplicative when it grows with the level, such as December sales that grow as the business grows. Plot it. It's usually obvious.
Key line: forecast the trend, forecast the season, treat the residual as uncertainty.
Ask: "Airline passengers over 20 years, with a summer peak that keeps growing. Additive or multiplicative?" Listen for: multiplicative.
Next: stationarity.
Stationarity
- A stationary series has a constant mean, variance, and autocorrelation over time.
- ARIMA and its relatives assume it.
- Most real series trend, so they aren't.
- Differencing usually fixes it: model yt−yt−1 instead of yt.
- Test with the augmented Dickey-Fuller (ADF) test.
Notes · slide 4
Say: A stationary series has a steady mean, variance, and autocorrelation. ARIMA assumes it, so the stretch of history you fit generalises to the next. Most real series trend, so they aren't stationary.
Say: Differencing usually fixes it. Model the change from yesterday instead of the level. Seasonal differencing removes a repeating pattern.
Say: The ADF test checks it. Its null is "not stationary", so a small p-value is what you want. That's the reverse of most tests people meet.
Next: how today relates to the past.
ACF and PACF
- Autocorrelation (ACF) at lag k: how similar yt is to yt−k.
- Partial autocorrelation (PACF): the same, with shorter lags removed. The direct link only.
- They're how you choose ARIMA's orders.
Notes · slide 5
Say: The autocorrelation at lag k says how similar today is to k days ago. The partial autocorrelation removes the effect of the shorter lags, leaving only the direct link.
Say: They choose ARIMA's orders. A sharp PACF cutoff after lag p suggests p autoregressive terms. A sharp ACF cutoff after lag q suggests q moving-average terms.
Key line: an ACF that decays slowly means the series isn't stationary yet. Difference it first.
Next: ARIMA.
ARIMA(p,d,q)
| Part | Meaning |
|---|---|
| AR(p) | Today depends on the last p values. Momentum |
| I(d) | Difference d times to make it stationary |
| MA(q) | Today depends on the last q forecast errors. Shocks that fade |
SARIMA adds the same three for the seasonal period.
Notes · slide 6
Say: Three ideas. AR: today depends on the last p values, like momentum. I: difference d times to make it stationary. Usually d = 1. MA: today depends on the last q forecast errors, like shocks that take a few days to fade.
Say: SARIMA adds the same three at the seasonal lag, say 7 for weekly or 12 for monthly data.
Ask: "Daily sales with a weekly pattern. ARIMA or SARIMA?" Listen for: SARIMA, with m = 7.
Next: a simpler alternative.
Exponential smoothing
- Forecast with a weighted average of the past. Weights decay exponentially.
- Holt adds a trend. Holt-Winters adds seasonality.
- Simpler than ARIMA, less tuning, often just as accurate.
Notes · slide 7
Say: Forecast with a weighted average of the past, with weights fading the further back you go. Simple smoothing tracks the level. Holt adds a trend. Holt-Winters adds a season. Three running averages, each nudged a little by every new value.
Key line: simpler than ARIMA, less tuning, and often as accurate. Fit it before anything more complex.
Next: using machine learning instead.
Forecasting as regression
- Build lag features (yt−1, yt−7, yt−30) and rolling statistics.
- Fit gradient-boosted trees to predict yt.
- Adds external drivers: price, weather, promotions.
- Loses ARIMA's built-in uncertainty intervals.
Notes · slide 8
Say: Turn the series into a table. Lag features: yesterday, a week ago, a month ago. Rolling averages. Then fit gradient-boosted trees to predict today.
Say: The gain: external drivers such as price, weather, and promotions. ARIMA has no natural place for them. The cost: no built-in uncertainty intervals, and you must build lags without leaking the future.
Ask: "Ice cream sales. What external feature would you add?" Listen for: temperature.
Next: splitting and evaluating.
Splitting and evaluating
- Never shuffle. Train on the past, test on the future.
- Use
TimeSeriesSplitfor cross-validation (Chapter 13). - Always compare with a naive baseline: repeat the last value, or last season's value.
- MAPE breaks near zero.
Notes · slide 9
Say: Never shuffle. Train on the past, test on the future. Use TimeSeriesSplit for cross-validation, from week 10.
Key line: always compare with a naive baseline: repeat the last value, or last season's value.
Lab: random splits creep back in. Check every pair's split first.
Next: the worked example.
Worked example: stationarity
Two years of daily data: slow trend, annual season, noise.
- Raw series, ADF test: p=0.682. Not stationary.
- First differences: p≈3×10−27. Stationary.
- Decomposition recovers the trend (about 109 → 127) and a seasonal swing of about 16.
Notes · slide 10
Say: Two years of synthetic daily data: a slow trend, a yearly season, and noise. The ADF test on the raw series gives p = 0.682. Not stationary. After first differences, p is about 3 × 10⁻²⁷. Stationary.
Say: Decomposition recovers what we built in: a trend from about 109 to 127 and a seasonal swing of about 16. That's a sanity check you can only do on synthetic data, where you know the answer.
Next: the forecast.
Worked example: the baseline wins
Forecast the last 28 days.
| Model | MAE | RMSE |
|---|---|---|
| ARIMA(2,1,2) | 7.228 | 8.664 |
| Naive: repeat the last value | 4.602 | 5.713 |
Over 28 days, against a yearly season and a slow trend, the series barely moves. ARIMA's extra flexibility fits noise it can't predict.
Notes · slide 11
Say: Forecast the last 28 days. ARIMA(2,1,2) has MAE 7.228. Repeating the last value has MAE 4.602. The naive forecast wins.
Say: Not a mistake. Over 28 days, against a yearly season and a slow trend, the series barely moves. "Stay near today's level" is close to right. ARIMA's extra flexibility fits noise it can't predict.
Key line: a model that loses to the baseline isn't a failure. Shipping it would be.
Ask: "What would have happened without the baseline?" Listen for: a reasonable-looking ARIMA would have shipped, worse than doing nothing.
Next: choosing a method.
Choosing a method
| Situation | Reach for |
|---|---|
| One series, clear trend and season | Exponential smoothing |
| Autocorrelation structure to exploit | ARIMA / SARIMA |
| External drivers matter | Gradient boosting on lag features |
| Many related series | A global model across all of them |
| Always | A naive baseline |
Notes · slide 12
Say: One series with a clear trend and season: exponential smoothing. Autocorrelation to exploit: ARIMA or SARIMA. External drivers: gradient boosting on lag features. Many related series, such as hundreds of products: one global model across all of them.
Key line: every row, always: a naive baseline.
Next: the chapter in one idea.
Mental model
A time series model is a weather forecaster.
- It extrapolates the patterns visible in the past.
- It should be less certain the further out it looks.
- Stationarity assumes the weather changes but the rules don't.
- A regime shift breaks the rules. Then every classical model needs refitting.
Notes · slide 13
Say: A time series model is a weather forecaster. It extrapolates the patterns it has seen, and should be less sure the further ahead it looks.
Say: Stationarity assumes the weather changes but the rules don't. A regime shift, like a pandemic or a new competitor, changes the rules. Then every classical model needs refitting.
Next: the semester in one slide.
The semester in one slide
- Frame the problem before touching data.
- Understand and clean the data before modelling it.
- Validate honestly: split first, pipelines always, test set once.
- Choose metrics from what each mistake costs.
- Prefer simple until the data proves you need more.
Next semester: modern AI.
Notes · slide 14
Say: Five habits. Frame the problem before touching data. Understand and clean the data before modelling it. Validate honestly: split first, pipelines always, test set once. Choose metrics from what each mistake costs. Prefer simple until the data shows you need more.
Ask: "Which of these did your capstone test hardest?" Take two or three answers.
Do: remind learners of the capstone deadline and the exam, and that every chapter quiz stays open for revision. Semester 2 is modern AI: shipping models, then neural networks, LLMs, RAG, and agents.
Next: check yourself.
Check yourself
- Why does random k-fold give a misleading score on time series?
- The ADF test gives p=0.4. What do you do next?
- Your ARIMA loses to "repeat last value". Is the model broken?
- When would you forecast with gradient boosting instead of ARIMA?
Notes · slide 15
Do: pairs, three minutes, then take answers. Then move to the capstone wrap-up.
Answers: 1. It trains on the future to predict the past. 2. Difference the series and test again. 3. Not necessarily. At short horizons on slow series, the naive forecast is hard to beat. Keep the baseline. 4. When external features, like price or weather, drive the series.
This week
- Read Chapter 20: Time Series Analysis.
- Take the Chapter 20 quiz.
- Run the notebooks: Chapter 20 (opens in a new tab).
- Optional: the further reading for week 15.
Slide 1 of 16: Time series analysis
Keys: → or Space next · ← back · Home/End · F full screen · N notes. Swipe on touch screens. "Save as PDF" opens the print dialog with one slide per page: choose "Save as PDF" as the printer. "Print handout" adds the speaker notes, in black on white.