Train one model to predict each step of the forecasting horizonBy default mlforecast uses the recursive strategy, i.e. a model is trained to predict the next value and if we’re predicting several values we do it one at a time and then use the model’s predictions as the new target, recompute the features and predict the next step. There’s another approach called direct forecasting where if we want to predict 10 steps ahead we train 10 different models, where each model is trained to predict the value at each specific step, i.e. one model predicts the next value, another one predicts the value two steps ahead and so on. This can be very time consuming but can also provide better results. mlforecast provides two ways to use direct forecasting:
-
max_horizon: Train models for all horizons from 1 tomax_horizon. For example,max_horizon=10trains 10 models (for steps 1, 2, 3, …, 10). -
horizons: Train models only for specific horizons. For example,horizons=[7, 14]trains only 2 models (for steps 7 and 14), which reduces computational cost when you only need predictions at certain steps.
Setup
Data
We will use four random series from the M4 datasetUsing max_horizon (all horizons)
| direct | recursive | |
|---|---|---|
| unique_id | ||
| H196 | 0.3% | 0.3% |
| H256 | 0.4% | 0.3% |
| H381 | 19.5% | 9.5% |
| H413 | 11.9% | 13.6% |
Using horizons (specific horizons only)
When you only need predictions at specific time steps (e.g., weekly and
bi-weekly forecasts), you can use the horizons parameter to train
models only for those steps. This significantly reduces computational
cost.
For example, if you have hourly data and only need 12-hour and 24-hour
ahead predictions:
| unique_id | ds | LGBMRegressor | |
|---|---|---|---|
| 0 | H196 | 972 | 16.095804 |
| 1 | H196 | 984 | 15.696618 |
| 2 | H256 | 972 | 13.295804 |
| 3 | H256 | 984 | 12.696618 |
| 4 | H381 | 972 | 12.271730 |
| 5 | H381 | 984 | 49.347744 |
| 6 | H413 | 972 | 23.099708 |
| 7 | H413 | 984 | 17.449030 |
horizons=[12, 24], the output only contains 2
predictions per series (at steps 12 and 24), not 24. This is the
sparse output behavior - you only get predictions for the horizons
you trained.
Partial predictions
If you callpredict(h=N) where N is less than some of your trained
horizons, you’ll only get predictions for horizons up to N:
| unique_id | ds | LGBMRegressor | |
|---|---|---|---|
| 0 | H196 | 972 | 16.095804 |
| 1 | H256 | 972 | 13.295804 |
| 2 | H381 | 972 | 12.271730 |
| 3 | H413 | 972 | 23.099708 |
Cross-validation with specific horizons
Thehorizons parameter also works with cross_validation:
| unique_id | ds | cutoff | y | LGBMRegressor | |
|---|---|---|---|---|---|
| 0 | H196 | 924 | 912 | 22.7 | 15.770231 |
| 1 | H196 | 936 | 912 | 16.4 | 15.317588 |
| 2 | H256 | 924 | 912 | 17.9 | 13.270231 |
| 3 | H256 | 936 | 912 | 13.3 | 12.617588 |
| 4 | H381 | 924 | 912 | 166.0 | 18.920395 |
| 5 | H381 | 936 | 912 | 50.0 | 51.129478 |
| 6 | H413 | 924 | 912 | 45.0 | 26.609079 |
| 7 | H413 | 936 | 912 | 31.0 | 24.801567 |
Summary
| Approach | Parameter | Use case |
|---|---|---|
| Recursive | (default) | General purpose, good when you need all horizons and want faster training |
| Direct (all horizons) | max_horizon=N | When you need predictions for all steps 1 to N and can afford training N models |
| Direct (specific horizons) | horizons=[h1, h2, ...] | When you only need predictions at specific steps (e.g., weekly/monthly forecasts) |
max_horizon and horizons are mutually exclusive - With
horizons, the output only contains predictions for the specified
horizons (sparse output) - Both direct forecasting approaches work with
cross_validation and exogenous features
