Skip to content
Français

Forecasting as regression

  • Lesson 14
  • 40 min
  • Module 5: Machine learning forecasters

Where we are

The statistical forecasters fit one model per series, and they see only that series' past. Larkfield has hundreds of series, and promotions and prices that a model could learn from. In this lesson, we turn forecasting into regression, with lag, calendar, price and promotion features. Think first of how a café owner guesses tomorrow's croissants.

What you will be able to do

Turn a forecast into a regression problem. Each day becomes a row, the day’s sales are the target, and the features are known before the day. Then one model can learn from all products at once, and from the promotions and prices that Larkfield plans in advance.

NotebookDo this lesson in a notebookColab · Kaggle · GitHub

The notebook has every code cell, task and check of this lesson. It builds the parasol’s feature table and trains one linear regression for all 50 products and one per product. Then it adds both to the backtest table and shows a feature that leaks the future. Its setup cells install the packages and download the Larkfield data, so it needs no local setup. About 40 minutes.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 40 minutes

What you need to know

What you need on your computer

Nothing: the notebook runs in Colab or Kaggle. Its first cell also installs mlforecast 1.1.0 and LightGBM 4.7.0.

Tested with Python 3.12, pandas 2.3.3, mlforecast 1.1.0 and scikit-learn 1.9.1, in Jupyter on macOS.

A café owner bakes croissants every morning. Before she goes home, she guesses how many she will sell tomorrow. She keeps a notebook with one line per day, and each line has a few columns:

  • what she sold on the same weekday last week,
  • the mean of the last seven days,
  • the day of the week,
  • whether tomorrow is a holiday,
  • whether croissants are on offer tomorrow (she decides the offers herself, a week ahead).

After a few months, she sees patterns. Mondays are slow. An offer doubles sales. A holiday empties the street. Each column is a clue, and the line is a list of clues for one day.

Two things make this work. First, every clue is known the evening before. She cannot write tomorrow’s real sales into tomorrow’s line. Second, the offers and holidays are in the notebook in advance, because she plans them.

Larkfield plans its promotions weeks ahead. Kenji knows on Monday that a product goes on promotion next Thursday. But ETS, from Exponential smoothing, does not know it. ETS reads only the past values of one series. It has no column for “on promotion tomorrow”.

The course backtest shows the cost of this. Its 50 products have 154 promotion days inside the 13 test windows. On those days, they sold 1,927 units. ETS forecast only 1,089.

The course backtest (50 products, 13 origins, 14 days): the bias on the 154 promotion days and on the 8,946 other days. Both methods are far too low when a promotion runs, and about right on the other days.
Text description of the chart

Two panels on an axis from −50% to +10%. On the 154 promotion days, the seasonal naive forecast has a bias of -42.2% and ETS -43.5%. On the other days, the seasonal naive forecast has 0.8% and ETS +1.4%.

On a promotion day, ETS misses 43.5% of the units, and always on the low side. Each of those units is a lost sale for Kenji. On the other days, ETS is only 1.4% too high. The method is not bad. It is blind to a cause that Larkfield knows in advance.

There is a second limit. ETS fits one model per series: 50 products means 50 separate models. Each model learns only from its own product. A product had a median of 14 promotion days in two years. So a per-product model has very few promotions to learn from.

Do what the café owner does. Turn the series into a table:

  • One row per product and day.
  • The target is what you want to predict: the units sold on that day.
  • The features are the clues, the columns of the table. Each one must be known before the day.

Then any regression model can learn to predict the target from the features. This is called forecasting as regression, because the forecast becomes an ordinary regression problem from the machine learning course.

Forecasters use four kinds of feature:

Feature What it is Café owner’s column
Lag the value k days before; lag7 is the sales of 7 days before same weekday last week
Rolling mean the mean of several earlier values, for example of the 7 days before mean of the last seven days
Calendar the weekday, the month, the day of the year, a holiday flag day of the week, holiday
Known future input a value that the business plans: the price, the promotion flag the offer she decided

A lag is the “yesterday predicts today” idea of Module 2, written as a column. A known future input is the part that ETS could not use.

A tiny worked example: the parasol on Monday 9 June

Section titled “A tiny worked example: the parasol on Monday 9 June”

Take one product, the parasol, and one day, Monday 9 June 2025. The picture shows where each feature of that day’s row comes from.

The parasol's units sold on 22 days from 19 May to 9 June 2025, one square per day. Arrows lead from Monday 26 May (17 units), from the seven days 27 May to 2 June (mean 14.6) and from Monday 2 June (13 units) to the boxes lag 14, rolling mean and lag 7. Four boxes hold the calendar and planned values: weekday 0, month 6, promotion 0, price 79.99. The target box holds 1, the units sold on Monday 9 June.
One row of the feature table, built from the parasol's own series. Every input comes from days before 9 June, or from the calendar and Larkfield's plans. The target is the day's real sales.
Text description of the diagram

The picture shows 22 squares, one per day from Monday 19 May to Monday 9 June 2025, with the parasols sold that day. The last square, 9 June, is orange: the target, 1 parasol. Three blue arrows lead to the features. Lag 14 is Monday 26 May (17 parasols). The rolling mean is the mean of the seven days from 27 May to 2 June (14.6). Lag 7 is Monday 2 June (13 parasols). Four more boxes are known in advance: weekday 0 (Monday), month 6 (June), promotion 0 (not on promotion) and price 79.99 dollars.

The notebook builds these columns with pandas. shift(7) moves the series down by 7 days, so each row gets the value of 7 days before:

parasol = load_sales(["P025"]).set_index("ds")
t = pd.DataFrame({"y": parasol["y"]})
t["lag7"] = t["y"].shift(7)
t["lag14"] = t["y"].shift(14)
t["mean_of_lag7_7days"] = t["y"].shift(7).rolling(7).mean()
t["weekday"] = t.index.dayofweek
t["on_promotion"] = parasol["on_promotion"]
print(t.loc["2025-06-02":"2025-06-09"].to_string())

The output:

y lag7 lag14 mean_of_lag7_7days weekday on_promotion
ds
2025-06-02 13.0 17.0 13.0 16.571 0 0.0
2025-06-03 8.0 13.0 16.0 16.143 1 0.0
2025-06-04 9.0 9.0 25.0 13.857 2 0.0
2025-06-05 8.0 12.0 11.0 14.000 3 0.0
2025-06-06 8.0 16.0 16.0 14.000 4 0.0
2025-06-07 8.0 24.0 22.0 14.286 5 0.0
2025-06-08 5.0 15.0 9.0 15.143 6 0.0
2025-06-09 1.0 13.0 17.0 14.571 0 0.0

Read the last line. On 9 June, lag7 is 13: the value of Monday 2 June, in the first line. weekday is 0, because pandas counts Monday as 0. The parasol was not on promotion. The model will learn how y depends on these columns, from every row of every product.

A regression model does not know that the rows are in time order. It sees only the columns. So the columns must carry everything that the model should know about time: the recent level, the weekly pattern and the season.

Test like the future showed that the horizon sets a gap. Kenji orders on Monday for 14 days. For the second Monday of the order, lag7 is the first Monday of the order: a day that has not happened yet.

There are two honest ways out. You can use only lags of 14 days or more, which are known for every day of the order. Or you can forecast step by step: forecast day 1, then use that forecast as a lag for day 2. The second way is called a recursive forecast. The library that this lesson uses does it by default. The next lesson compares both.

Larkfield: one model for all products, or one per product

Section titled “Larkfield: one model for all products, or one per product”

Priya now builds the table for the 50 products of the course backtest. She has two choices.

  • A global model: one model learns from the rows of all 50 products together. It sees 50 times more rows and all 812 promotion days of the history. It learns patterns that the products share, such as the weekday or a promotion.
  • A local model: one model per product, as ETS does. Each model fits its own product closely, but it learns only from that product’s rows.

The library mlforecast builds the lag, rolling and calendar columns for many series at once, and it trains any scikit-learn model. It also keeps the gap: it computes each rolling mean from a lag, never from the day itself.

from mlforecast import MLForecast
from mlforecast.lag_transforms import RollingMean
from sklearn.linear_model import LinearRegression
sales = load_sales(BACKTEST_PRODUCTS)
EXOG = ["price", "on_promotion"]
def make_model(models):
return MLForecast(models=models, freq="D", lags=[7, 14, 28],
lag_transforms={7: [RollingMean(7), RollingMean(28)]},
date_features=["dayofweek", "month", "dayofyear"])
m = make_model({"lr": LinearRegression()})
features = m.preprocess(sales[sales["ds"] <= ORIGINS[-1]][["unique_id", "ds", "y"] + EXOG], static_features=[])
print(features.shape[0], "rows,", features.shape[1] - 3, "features:", [c for c in features.columns if c not in ("unique_id", "ds", "y")])

EXOG names the known future inputs. lag_transforms={7: [RollingMean(7)]} means “the mean of 7 values, starting from lag 7”. The output:

34000 rows, 10 features: ['price', 'on_promotion', 'lag7', 'lag14', 'lag28', 'rolling_mean_lag7_window_size7', 'rolling_mean_lag7_window_size28', 'dayofweek', 'month', 'dayofyear']

One table of 34,000 rows: 50 products, 680 days each, up to the last origin. The first 34 days of each product are dropped. The 28-day mean of lag 7 needs 34 earlier days, and those days do not exist.

The notebook wraps the model as a forecaster for backtest.py and fits it at all 13 origins. Then it does the same with 50 small models, one per product:

method horizon MASE WAPE % bias % pinball fit s cost $
linear regression, one model per product 14 0.849 42.2 -1.6 10.18 16007
linear regression, one global model 14 0.865 42.6 -0.7 0.29 17212

The course’s backtest table has the same two models with three more known inputs. They are holiday flags from calendar.csv: public holidays, the Black Friday weekend and the run-up to Christmas. The chart compares them with ETS.

  • Statistical method
  • Regression
The course backtest table: ETS, one linear regression for all products, and one per product. The local regression has the lowest cost of the three; the global one has the highest. Costs are the course's assumptions.
Text description of the chart

Two panels. WAPE: ETS 41.3%, one global linear regression 42.6%, one linear regression per product 41.7%. Cost of the 14-day order: ETS 16,578 dollars, global regression 17,264, per-product regression 15,137.

Three things stand out:

  • A linear model with good features is a real forecaster. Its WAPE is close to ETS (41.3%). The per-product model even has a lower cost: 15,137 dollars against 16,578.
  • Here the 50 local models beat the global one on every measure. Each product has two years of history: enough for its own small model.
  • The local models are slower. They take 11 to 13 seconds for the 13 origins, against 0.3 seconds for the global model.

The reason for features was the promotion. So Priya checks the 154 promotion days again, now with the regressions.

  • Baseline
  • Statistical method
  • Regression
The course backtest, bias on the 154 promotion days only. The two methods that do not see the promotion flag are far too low. The global regression, which learned from all 812 promotion days of the history, is a little too high.
Text description of the chart

Bias on the 154 promotion days: seasonal naive -42.2%, ETS -43.5%, one linear regression per product -12.7%, one global linear regression +5.9%.

The global model is now close: +5.9% instead of −43.5%. The per-product models are in between, at -12.7%. Each of them saw only about 14 promotion days of its own product. The global model saw all 812. This is the main reason for a global model: products share patterns, and a shared model learns each pattern from many more examples.

Why is the global model a little too high? A linear model adds a fixed number of units for “on promotion”, the same for every product. But a promotion multiplies sales: a slow product gains a few units, a fast one gains many. One fixed number cannot be right for both. The next lesson’s model can learn this.

When to use it, when not, and what it costs

Section titled “When to use it, when not, and what it costs”

Use forecasting as regression when:

  • you have known future inputs (promotions, prices, holidays, planned events) that move the series;
  • you have many related series, so that a global model can share what it learns;
  • you want one model to run instead of hundreds.

Keep a statistical method when you have a few long series with no known inputs. ETS needs no feature design and has fewer ways to go wrong.

Regression has costs:

  • Feature work. You choose the lags, the windows and the calendar columns. A bad choice gives a weak model, and nothing warns you.
  • The gap. Every feature must be known at forecast time, for every day of the horizon. This is easy to break by accident; the failure case below shows how.
  • No range for free. ETS gives a range around its forecast. A regression gives one number per day. Module 7 shows how to get a range.
  • A global model can hide a product. The total is good, but one odd product can be poor. Check the scores product by product, as in Exponential smoothing.

Common mistake

A feature from the future

Symptom: Tomás adds one more feature: the mean of the last 7 days, including the day itself. At the last origin, the WAPE drops from 48.4% to 31.6%. He wants to ship it.

Cause: for each day of the test, the feature contains that day’s real sales. On the Sunday before the order, nobody knows them. The model reads the answer from its input. This is leakage: the future reaches the model through a feature.

Fix: compute every feature only from data before the origin. Shift first, then roll: shift(14).rolling(7), or let mlforecast build the rolling means from its lags. Then run the backtest again.

Consequence in AI work: a leaky model looks excellent in the backtest and fails on the first real Monday. A sudden large gain from one new feature is a warning sign. Check how the feature is computed before you believe it.

  1. Open the notebook from the box Do this lesson in a notebook at the top of this page.
  2. Run the setup cells and the helper cell. The data cell ends with the Ready: line.
  3. In section 1, predict which day’s sales lag7 holds for 9 June. Then run the cell. You see 8 rows, with lag7 13.0 on 2025-06-09.
  4. In section 2, run the cell. You see 34000 rows, 10 features.
  5. In section 3, predict which is better: one global model or 50 local ones. Then run the cell (about 2 minutes).
  6. Read the two rows. The per-product models have WAPE 42.2; the global model 42.6.
  7. In section 5, run the failure case. You see 31.6 with the leak and 48.4 without it.

Before you run anything, predict: is the global model too high or too low on the promotion days? In the notebook, section 4, keep the rows of fc_global where the product was on promotion. Compute their bias in % and put it in promo_bias.

Show the answer
p = fc_global.merge(sales[["unique_id", "ds", "on_promotion"]], on=["unique_id", "ds"])
p = p[p["on_promotion"] == 1]
promo_bias = round(float(100 * (p["yhat"].sum() - p["y"].sum()) / p["y"].sum()), 1)
print(promo_bias, "% on", len(p), "promotion days")
6.8 % on 154 promotion days

The notebook’s global model is 6.8% too high on the promotion days. The course table’s version, with the three holiday flags, is 5.9% too high. Both are far better than ETS at −43.5%, because the model sees the promotion flag. Both are too high because a linear model adds the same number of units to every product on promotion.

Check your understanding

Explain with Larkfield's numbers why ETS misses on promotion days, and what a regression with features changes.

Show the answer

ETS reads only the past values of one series. It cannot see that Larkfield planned a promotion. On the 154 promotion days of the backtest, it forecast 1,089 units for 1,927 sold: 43.5% too low. On the other days it was only 1.4% too high. A regression gets the promotion flag as a known future input, one column of its feature table. One global linear regression was +5.9% on the same days. It learned the effect of a promotion from all 812 promotion days of the 50 products.

Check your understanding

Kenji orders on Monday for the next 14 days. Which of these features can a model use for the last day of the order without a recursive forecast: lag 1, lag 7, lag 14, the weekday, the planned price, the mean of the 7 days up to and including that day?

Show the answer

Lag 14, the weekday and the planned price. Lag 14 of the last day is the Sunday before the order, which Kenji knows. The weekday is in the calendar, and Larkfield plans the price in advance. Lag 1 and lag 7 fall inside the 14 days of the order, so they are not known yet. A recursive forecast would replace them with its own forecasts. The mean that includes the day itself contains the day’s real sales: that is leakage.

Check your understanding

Name one reason to choose a global model and one reason to choose one model per product. Which won on Larkfield's backtest?

Show the answer

A global model learns shared patterns, such as a promotion, from many more rows: all 812 promotion days instead of about 14 per product. It is also faster (0.3 seconds against 11 to 13 for the 13 origins) and there is only one model to run. One model per product can fit each product’s own level and season more closely when each product has enough history. On Larkfield, with two years per product, the 50 local linear regressions won. Their WAPE was 41.7% against 42.6%, and their cost 15,137 dollars against 17,264. But they were too low on promotion days (−12.7% against +5.9%).

Recap

  • Forecasting as regression: one row per product and day; the target is the day’s sales; the features are known before the day.
  • Four kinds of feature: lags, rolling means, calendar columns and known future inputs (price, promotion).
  • ETS cannot see a promotion: −43.5% on the 154 promotion days. A global linear regression with the promotion flag: +5.9%.
  • A global model learns from all products at once; a local model fits each product alone. On Larkfield, the local linear models won on WAPE (41.7%) and cost (15,137 dollars).
  • Keep the gap: use lags of 14 days or more, or forecast recursively. A feature that contains the day itself is leakage.

You can now turn any set of series into a regression table. But a linear model adds a fixed number of units for a promotion, and promotions multiply sales. Next, you train a model that learns such effects: gradient boosting with LightGBM.

Next: Gradient boosting with LightGBM