Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 40 ยท Projects and Practice

Project: Retail Demand Forecast

A complete forecasting project: build a realistic daily sales series, set baselines, engineer calendar and lag features, train a boosted model, backtest it against baselines and add prediction intervals.

In this project you forecast the next 14 days of daily sales for a shop. It chains together everything from the course: exploring structure, baselines, feature engineering, a machine learning model, rolling-origin evaluation and prediction intervals. The data are generated in code (so the project runs anywhere and every number is reproducible), but with realistic ingredients: a trend, weekly and yearly seasonality, promotions, holidays and noise.

The plan:

flowchart LR
    A["1. Data and structure"] --> B["2. Baselines"]
    B --> C["3. Features"]
    C --> D["4. Model"]
    D --> E["5. Backtest"]
    E --> F["6. Intervals"]

Always get a baseline score before building anything clever.

Step 1: the data

Python Generate and inspect the sales series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weekday table shows the strong weekly cycle (Saturday is the peak). A monthly view would show the yearly wave and the rising trend. Both are things the model must know.

Step 2: baselines

Hold out the last 14 days as the test set (and check later with a backtest that one test window is not a fluke). Two baselines: naive (the last value) and seasonal naive (the same weekday last week).

Python Baseline scores

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Anything we build must beat the seasonal naive error (the lower of the two).

Step 3 and 4: features and a boosted model

A separate model per horizon would be thorough; to keep the project compact we use a single model whose features are known in advance for any day in the horizon: the calendar, the promotion plan, and sales values from at least 14 days earlier. (Lags no shorter than the horizon avoid feeding predictions back.)

Python Calendar and lag features with HistGradientBoosting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On this one window the model (about 22.6) is no better than the seasonal naive baseline (about 21.9). That is a warning, not a verdict: a single window is a weak test, which is exactly why Step 5 evaluates many origins.

Two design choices matter. Lags start at H (14 days), so every feature exists at the forecast origin and no recursion is needed. And the target is a ratio to the recent level, so the trend cannot cap the trees (see tree models).

Step 5: backtest

One 14-day window could be lucky. Evaluate over eight rolling origins, step 14 days.

Python Rolling-origin backtest against the baseline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Over the eight origins the model's mean error is about half the baseline's (about 16 against 34) and it wins at six of the eight; the last origin is the window from Step 3 where the two tie. A model that wins on average but loses on some origins is worth investigating origin by origin (holiday weeks and promotion clusters are the usual culprits). Your exact numbers should match, since the data and seeds are fixed.

Step 6: intervals for stock planning

Replenishment needs a high quantile, not the mean. Train an upper-quantile model (for example the 90th percentile) with the same features; stocking to it keeps stock-outs near 10%. See prediction intervals for the pinball loss and coverage checks, and check calibration on the backtest origins before relying on it.

Going further

  • Add price and promotion depth as features; use ARIMAX as a statistical competitor.
  • Forecast many products with one global model and a product identifier feature.
  • Replace the seasonal naive baseline with STL plus ETS and compare with MASE.
  • Monitor the live forecast error and retrain when it drifts (see change point detection).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do the features in this project use lags of at least 14 days?

    A direct, non-recursive model may use only information available when the forecast is made.
  2. 2. Why model the ratio of sales to the recent level instead of raw sales?

    Trees cannot predict outside the training target range, so the trend is factored out first.
  3. 3. What is the purpose of evaluating at eight rolling origins?

    Several origins show the typical error and how consistently the model beats the baseline.
  4. 4. For stocking decisions you need:

    Stock-outs and overstock have different costs, so planners need an upper quantile rather than the average.

Summary

  • A complete forecasting project runs: structure, baselines, features known at the origin, model, backtest, intervals.
  • Use lags at least as long as the horizon and a level-ratio target to keep trees from capping the trend.
  • Report accuracy over many origins and against a seasonal naive baseline.
  • Planning decisions need quantiles, not just means.

Next: Project: Sensor Monitoring with Anomaly Alerts