Time Series Forecasting
Trend, seasonality, baselines, lag features, time-based validation and backtesting: predicting demand, visits and sales.
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.
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: 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.
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.
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.
Exponential smoothing
Simple exponential smoothing forecasts with a weighted average of past values whose weights shrink geometrically: , with . Expanding the recursion, : recent observations matter most. Large reacts fast; small smooths more.
HoltโWinters adds a trend term and a seasonal term, each with its own smoothing parameter: level , trend , season , and forecast for season length . The parameters are chosen by minimising one-step-ahead squared error on the training data.
Code: a forecasting workflow
Baseline, features, gradient boosting, and a time-series split with 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))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โฆ
๐ 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.
- Forecasting: Principles and Practice (free book) โ Hyndman & Athanasopoulos