Sub topic 15
Adding External Regressors and Known-Future Covariates
This chapter assumes you can build lag and calendar features, avoid target leakage, and run the recursive forecast from earlier chapters. It shows how to add outside variables to the feature table without breaking the forecasting setup.
Two kinds of external variables
An external regressor is a column that is not the target but may help predict it. Examples are price, promotions, temperature and holidays. What matters is whether you will know its value at forecast time.
| Kind | Known when you forecast? | Examples | How to use it |
|---|---|---|---|
| Known-future covariate | Yes, for every step in the horizon | Planned promotions, holiday calendar, list price | Use the value at the target time `t` directly |
| Observed-only covariate | No, only up to now | Actual temperature, competitor sales, web traffic | Use only lagged values, such as `t-1` or earlier |
Using the wrong treatment is a leakage error. If you feed today's actual temperature into a model that must forecast next week, the model learns from information it will not have in production. The offline score looks good and the live forecast fails.
Building the features
For a known-future covariate, align the value with the row it describes. For an observed-only covariate, shift it so each row sees only past values.
import pandas as pd
def add_covariates(df: pd.DataFrame, promo: pd.Series, temp: pd.Series) -> pd.DataFrame:
# df has a DatetimeIndex; promo and temp share the same index
out = df.copy()
out['promo'] = promo.reindex(out.index) # known in advance: no shift
out['promo_lag_7'] = promo.shift(7).reindex(out.index)
out['temp_lag_1'] = temp.shift(1).reindex(out.index) # observed only: lagged
return out.dropna()The column promo is safe because the promotion plan exists before the sales happen. The column temp_lag_1 is safe because yesterday's temperature is already observed. A column named temp with no shift would leak.
Effect on multi-step forecasting
Known-future covariates are easy in a multi-step forecast. You look up the plan for each future date and put it in the row. Observed-only covariates are harder. A recursive forecast would need their future values, which you do not have. You have three options:
- Drop them and keep only lags that fit inside the horizon.
- Use a direct model whose smallest lag is at least the horizon, so every input is already observed.
- Forecast the covariate separately, and accept that its errors flow into the target forecast.
Common errors
- Merging on the wrong key, which shifts a covariate by a day or more. Check the join on a few rows by eye.
- Filling missing covariate values with the column mean computed over the full dataset. Compute fill values on the training block only.
- Including a covariate in training that the business will not be able to supply in production.
Exercise
You forecast daily sales 14 days ahead. Planned discount percentage is set a month in advance. Daily footfall is measured at the store door. How should each column enter the feature table?
Show answer
Use the discount percentage at the target date with no shift, because it is a known-future covariate. Footfall is observed only, so use lags of at least 14 days (for example footfall shifted by 14), or drop it. Unshifted footfall would leak information that is unavailable at forecast time.