Machine Learning
Topic 23 ยท Applied ML ยท โฑ about 6 h

Time Series Forecasting

Trend, seasonality, baselines, lag features, time-based validation and backtesting: predicting demand, visits and sales.

Show:
In this topic
Beginner

What makes time series different

A time series is a sequence of measurements in time order: daily clinic visits, hourly electricity use, weekly sales. Forecasting them is one of the most common ML jobs in industry (staffing, stock, energy, finance), and it breaks two assumptions most of the course relies on. Observations are not independent (today looks like yesterday and like the same day last week), and the future may not look like the past (trends, holidays, shocks like a pandemic).

Most series are a mix of a trend (slow growth or decline), seasonality (repeating patterns: weekly, yearly, daily), special events (holidays, promotions) and noise. A good first step is always to plot the series and look for each of these.

Beginner

Baselines and honest validation

Before any model, compute simple baselines: the naive forecast (tomorrow = today), the seasonal naive forecast (next Monday = last Monday), and a moving average. They are surprisingly hard to beat, and a model that doesn't beat seasonal naive is not worth deploying.

Validation must respect time. Never split randomly: that trains on the future. Instead hold out the most recent period, or better, use a rolling-origin backtest: train up to week 40 and forecast weeks 41โ€“42, then train to week 42 and forecast 43โ€“44, and so on, averaging the errors. Common error metrics are MAE (mean absolute error, in the units of the series), RMSE (penalises big misses) and MAPE (percentage error, which breaks when actual values are near zero).

Try it

Try it: forecast clinic visits

Choose a method and a horizon. The grey line is history, the dark line is what actually happened, the coloured line is the forecast. Compare the MAE of each method.

Interactive: this part runs in your browser.

Beginner

Machine learning for forecasting

To use regression models (linear models, gradient boosting) for forecasting, turn the series into a table where each row is a time point and the columns are features known before that time:

  • Lags: the value 1, 7, 14 days ago (shift(7)).
  • Rolling statistics of the past: mean of the previous 7 days (shift(1).rolling(7).mean(): shift first, or today leaks into its own feature).
  • Calendar features: day of week, month, holiday flags, school terms.
  • Known future inputs: planned promotions, bookings already made, weather forecasts.

For forecasting several days ahead, a lag of 1 day isn't available when predicting day 7: either train one model per horizon, or only use lags at least as long as the horizon. Classical statistical models (exponential smoothing, ARIMA) remain strong for single series; gradient boosting with lag features works well across many related series; deep learning models help mainly at very large scale.

Try it

Code example: beat the seasonal baseline

A worked example: read it step by step (every part is explained), then run it and experiment.

Interactive: this part runs in your browser.

Intermediate ยท Maths

Exponential smoothing

Simple exponential smoothing forecasts with a weighted average of past values whose weights shrink geometrically: y^t+1=ฮฑyt+(1โˆ’ฮฑ)y^t\hat y_{t+1} = \alpha y_t + (1-\alpha)\hat y_t, with 0<ฮฑโ‰ค10\lt\alpha\le 1. Expanding the recursion, y^t+1=ฮฑโˆ‘kโ‰ฅ0(1โˆ’ฮฑ)kytโˆ’k\hat y_{t+1} = \alpha\sum_{k\ge 0}(1-\alpha)^k y_{t-k}: recent observations matter most. Large ฮฑ\alpha reacts fast; small ฮฑ\alpha smooths more.

Holtโ€“Winters adds a trend term and a seasonal term, each with its own smoothing parameter: level โ„“t\ell_t, trend btb_t, season sts_t, and forecast y^t+h=โ„“t+hโ€‰bt+st+hโˆ’m\hat y_{t+h} = \ell_t + h\,b_t + s_{t+h-m} for season length mm. The parameters are chosen by minimising one-step-ahead squared error on the training data.

Code

Code: a forecasting workflow

Baseline, features, gradient boosting, and a time-series split with scikit-learn.

Python ยท pandas + scikit-learn
import numpy as np, pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import TimeSeriesSplit
from sklearn.metrics import mean_absolute_error

df = pd.read_csv("daily_visits.csv", parse_dates=["date"]).sort_values("date")
df["dow"] = df["date"].dt.dayofweek
df["month"] = df["date"].dt.month
for lag in [7, 14, 28]:
    df[f"lag_{lag}"] = df["visits"].shift(lag)
df["roll_7"] = df["visits"].shift(7).rolling(7).mean()   # shifted by the horizon (7 days)
df = df.dropna()
X, y = df.drop(columns=["date", "visits"]), df["visits"]

maes, naive = [], []
for tr, te in TimeSeriesSplit(n_splits=5, test_size=28).split(X):   # rolling-origin backtest
    m = HistGradientBoostingRegressor(random_state=0).fit(X.iloc[tr], y.iloc[tr])
    maes.append(mean_absolute_error(y.iloc[te], m.predict(X.iloc[te])))
    naive.append(mean_absolute_error(y.iloc[te], X["lag_7"].iloc[te]))   # seasonal naive
print("model MAE", np.mean(maes), "seasonal naive MAE", np.mean(naive))
Open in playground โ†—
Before it runs, the course writes two years of synthetic daily clinic visits (with weekly and yearly patterns) to daily_visits.csv.
Check yourself

Quiz

1. Why is a random train/test split wrong for forecasting?

2. The seasonal naive forecast for next Tuesday (weekly pattern) isโ€ฆ

3. Which feature leaks?

4. MAPE is a poor metric whenโ€ฆ

5. A rolling-origin backtestโ€ฆ

Go deeper

๐Ÿ“š Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready