Feature Engineering for Forecasting

Forecasting features must encode temporal structure without using information from after the forecast origin. The most common families are lag features, rolling-window statistics, calendar variables, event indicators, promotions, lifecycle features, categorical encodings, exposure variables, scaling, imputation, and target transformations.

Lag features

Lag features expose past target values:

Short-term lags capture recent momentum. Seasonal lags capture repeated patterns such as yesterday, last week, or last year. Sparse lag sets reduce dimensionality, while dense lag windows can help flexible models learn local temporal shape.

A lag configuration may be represented by a maximum lag, an explicit list, an inclusive range, or per-feature lag definitions. The phrase “lag 0” is ambiguous. For the target, lag 0 usually means , which is unavailable when predicting and therefore leaks unless the formulation explicitly predicts a later target from a contemporaneously observed feature.

Rolling-window features

Rolling features summarize recent history: rolling mean, median, minimum, maximum, standard deviation, sum, and exponentially weighted statistics. A seven-day rolling mean is:

The window uses through , not , when predicting timestamp . Rolling features must be shifted before computation whenever the value at the prediction timestamp is unknown.

Window size controls a bias-variance tradeoff. Short windows react quickly but are noisy. Long windows are stable but can miss recent shifts.

Calendar and cyclical features

Calendar features include year, quarter, month, ISO week, day of year, day of month, day of week, and weekend indicators. They are future-known because the calendar is known in advance.

Cyclical encoding represents periodic variables on a circle:

Here is the calendar position and is the cycle length. This makes the first and last values of a cycle adjacent, so December and January or Sunday and Monday are not artificially far apart.

Events, promotions, and lifecycle

Holiday and event features include public holidays, days before an event, days after an event, event duration, and region-specific calendars. Promotion features include promotion active, promotion depth, promotion count, promotion duration, time until promotion, and time since promotion. These features are valid only when the promotion or event plan is known at forecast time.

Lifecycle features describe maturity: time since launch, time since first observation, product age, cohort, or lifecycle phase. They help distinguish new series from mature series and can support cold-start forecasting.

Categorical variables

One-hot encoding is simple and portable, but high-cardinality variables can create many sparse columns. Ordinal encoding is compact, but imposes an artificial order unless the model treats categories natively. Target encoding can be effective but is leakage-prone unless fit inside each training fold and computed without future labels. Learned embeddings can represent high-cardinality categories in neural models. Some tree models provide native categorical handling, but behavior differs by library.

Scaling and imputation

Standard scaling subtracts a mean and divides by standard deviation. Min-max scaling maps values to a fixed range. Robust scaling uses median and interquartile range to reduce sensitivity to outliers.

Mean, median, and most-frequent imputation fill missing values with simple summaries. Missingness indicators are often useful because missing values can carry signal. Tree models usually need less scaling than linear, kernel, or neural models, but consistent preprocessing still matters for reproducibility.

Target transformations

Target transformations can stabilize variance, reduce skew, improve optimization, reduce the effect of extreme values, and improve comparability across series.

Standardization uses:

where and are fitted on training data. Min-max scaling, max-absolute scaling, and robust scaling are alternatives.

A logarithmic transformation such as:

handles zero-valued nonnegative targets while compressing large values. The Box-Cox transformation is:

Box-Cox requires positive . Quantile transformation maps ranks to a target distribution; it can improve optimization but distorts distances and extrapolation.

Residual modeling decomposes:

and forecasts the residual . This is useful when a known baseline explains much of the structure.

Predictions must be inverse-transformed before evaluation in the original unit. Clipping and inverse-transform rules should be validated explicitly.

Practical guidance

  • Generate all features relative to the forecast origin.
  • Start with interpretable lags, rolling means, calendar features, and known events.
  • Keep lag sets sparse unless there is enough data and memory to support dense windows.
  • Fit scalers, imputers, encoders, and target transformations inside each training fold.
  • Treat target encoding and rolling statistics as leakage risks until proven otherwise.

Common failure modes

  • Unshifted rolling features that include the prediction timestamp.
  • Target lag 0 leakage.
  • Incorrect season length, such as using 30 for monthly annual seasonality instead of 12.
  • Treating padded zeros as real observations for new products.
  • Forgetting to inverse-transform predictions before computing metrics.

Connections

Forecast features translate forecasting data and covariates into tabular inputs for machine learning forecasting. Lag features echo autoregressive models, and every feature must be built inside rolling-origin validation to avoid leakage.

References