AI from scratch · Elective · Time series and forecasting

Time series and forecasting

How many people will ride the subway next Tuesday? How much power will the city draw tomorrow at 6 pm? How many orders will the warehouse ship next week? Forecasting is one of the most common jobs a junior ML engineer gets, and it breaks the rules most of this path took for granted: the examples aren't independent, the order matters, and the future may not look like the past. This lesson works on real data, every day of NYC subway ridership from March 2020 to January 2025: a pandemic collapse, a slow recovery, a weekly rhythm and the holidays.

Data: MTA Daily Ridership Data: 2020–2025, Metropolitan Transportation Authority, data.ny.gov (New York State open data).

1. A series has parts

daily riders 7-day average (the trend)

Look at the whole range, then zoom in. Three parts stack up: a trend (the collapse in spring 2020 to about 0.4 million a day and the climb back past 3 million), a season (every week: weekdays high, Saturdays lower, Sundays lowest), and events (holidays, storms). What's left is noise. The 7-day average smooths out exactly one week's season, so it shows the trend alone.

A time series is trend plus season plus events plus noise, and most of forecasting is modeling the first three.

2. Baselines that are hard to beat

Before any model, the simple forecasts, scored on October 2024 to January 2025 by mean absolute error (MAE: the average miss, in riders):

Forecast for a dayMAEWhy it's that good or bad
Same as yesterday658,000Monday after a Sunday is off by a million
Average of the last 7 days713,000Throws away the weekly pattern entirely
Same weekday last week (seasonal naive)382,000Copies the weekly pattern; about 11% of an average 3.4 million day

The seasonal naive forecast wins because ridership correlates 0.83 with the same day a week earlier, but only 0.49 with the day before. Any model you build has to beat it, by a margin you can measure; papers and teams that skip this step regularly publish models worse than "last week".

A forecast for hourly electricity use is scored against "same hour yesterday" and against "same hour last week". Which is likely the harder baseline to beat on weekdays?

The best naive forecast copies the strongest repeating period. For subway riders it's the week (weekends differ hugely); for weekday electricity it's often the day. Measure the correlation at each lag and pick.

Score every forecast against the naive ones, especially "same time last season": they're surprisingly strong.

Why copying last week works so well
  1. Write a day's riders as \(y_t = \text{trend}_t + s_{w(t)} + \varepsilon_t\): a slowly moving trend, a weekday effect \(s\), noise.
  2. Same weekday last week: \(\hat y_t = y_{t-7} = \text{trend}_{t-7} + s_{w(t)} + \varepsilon_{t-7}\). The weekday effect is copied exactly.
  3. So the error is \(y_t - \hat y_t = (\text{trend}_t - \text{trend}_{t-7}) + (\varepsilon_t - \varepsilon_{t-7})\): a week of trend change plus two days of noise.
  4. Trends move slowly, so the first part is small; the second has twice the noise's variance (two independent days, Probability 4). That's the floor this baseline can't get under.
  5. Yesterday's value has the same noise term but a different weekday effect: \(s_{\text{Mon}} - s_{\text{Sun}}\) is about a million riders here. That's why it loses by so much.

3. Forecasting as regression: lags and calendars

Any regression model (chapter 13's boosted trees) can forecast, once each day becomes a row of features known in advance: lags (riders 7, 14, 21, 28 days before), rolling statistics (the average of the week before that), and the calendar (weekday, month, a holiday flag). For a forecast 7 days ahead every feature must be at least 7 days old: a lag of 1 would be the future.

About 285,000 against 382,000: a quarter better than last week. Without the holiday flag the same model scores 340,000, so one column bought half the gain. The biggest misses are still holidays: Thanksgiving, the Friday after it (not a federal holiday, so the flag doesn't know), Christmas Eve.

Forecasting becomes regression with features from the past (lags, rolling stats) and the calendar; every feature must be old enough for the forecast horizon.

4. Evaluating a forecast honestly

A team computes a centered 7-day moving average (3 days before, the day, 3 days after) and uses it as a feature to forecast tomorrow. What's wrong?

Centered averages are great for drawing a trend, never for forecasting. Features must use trailing windows that end before the forecast's moment, shifted by the horizon.

Evaluate forecasts on the future only, repeatedly (walk-forward), at the horizon you'll use, and give a range, not just a number.

Where it breaks: the future that doesn't rhyme

5. What to carry forward

All lessons: AI from scratch. Hyndman and Athanasopoulos, "Forecasting: Principles and Practice" (free online) is the standard text.

Last updated