Long-horizon forecasting
Multi-step prediction is a key challenge in time series forecasting. However, forecasting accuracy typically decreases as predictions are made further into the future. This is caused by both decreasing predictability and error propagation along the horizon.
This module implements methods specifically designed to improve long-horizon forecasting accuracy:
Forecast Trajectory Neighbors (FTN): A meta-learning strategy that can be integrated with any forecasting model. FTN works by using training observations to correct errors in multi-step predictions through nearest neighbor matching of forecast trajectories [1].
[1] Cerqueira, V., Torgo, L., & Bontempi, G. (2024). “Instance-based meta-learning for conditionally dependent univariate multistep forecasting.” International Journal of Forecasting.
- class MLForecastFTN(n_neighbors: int, horizon: int, apply_ewm: bool = False, apply_weighting: bool = False, apply_diff1: bool = False, apply_global: bool = False, ewm_smooth: float = 0.6)[source]
Bases:
ForecastTrajectoryNeighborsImprove multi-step forecasts using nearest neighbor adjustments.
An implementation of Forecasted Trajectory Neighbors (FTN) [1] for the MLForecast framework. FTN is a meta-learning strategy that improves long-horizon forecasts by: - Finding similar forecast trajectories in training data - Using these to adjust predictions - Reducing error propagation across horizons - Adding conditional dependency constraints
Notes
Key advantages: - Improves long-horizon accuracy - Reduces error accumulation - Maintains temporal consistency - Works with any base model - Computationally efficient - Integration with MLForecast models
Method details: 1. Generate base model forecasts 2. Find similar forecast patterns in training 3. Average neighbor trajectories 4. Apply trajectory-based corrections
References
[1] Cerqueira, V., Torgo, L., & Bontempi, G. (2024). “Instance-based meta-learning for conditionally dependent univariate multistep forecasting.” International Journal of Forecasting.
See also
MLForecastBase forecasting framework
Examples
>>> import lightgbm as lgb >>> from mlforecast import MLForecast >>> from datasetsforecast.m3 import M3 >>> >>> from metaforecast.longhorizon import MLForecastFTN as FTN >>> >>> # loading data >>> df, *_ = M3.load('.', group='Monthly') >>> horizon = 18 >>> # setting up forecasting model >>> models = {'lgbm': lgb.LGBMRegressor(verbosity=-1), } >>> >>> mlf = MLForecast( >>> models=models, >>> freq='ME', >>> lags=range(1, 13), >>> ) >>> >>> # setting up FTN >>> ftn = FTN(horizon=horizon, >>> n_neighbors=30, >>> apply_diff1=True) >>> >>> mlf.fit(df=df) >>> ftn.fit(df) >>> >>> fcst_mlf = mlf.predict(h=horizon) >>> fcst_ftn = ftn.predict(fcst_mlf)
- __init__(n_neighbors: int, horizon: int, apply_ewm: bool = False, apply_weighting: bool = False, apply_diff1: bool = False, apply_global: bool = False, ewm_smooth: float = 0.6)[source]
Initialize FTN meta-learner
- Parameters:
n_neighbors (int) – Number of nearest neighbors for trajectory matching:
horizon (int) – Number of future periods to forecast. Affects computational complexity and memory usage. Must be positive.
apply_ewm (bool, default=False) – Whether to smooth series before neighbor matching: - True: Apply exponential moving average - False: Use raw values Helps reduce noise influence.
apply_weighting (bool, default=False) – Whether to combine FTN with original forecasts: - True: Use weighted combination - False: Use pure FTN predictions Weights must be set via set_alpha_weights().
apply_diff1 (bool, default=False) – Whether to use first differences for matching: - True: Match on changes (stationary) - False: Match on levels Helps with non-stationary series.
apply_global (bool, default=False) – Neighbor search scope (experimental): - True: Search across all series - False: Search within each series Per-series generally performs better.
ewm_smooth (float, default=0.6) – Smoothing factor for exponential averaging: - Closer to 1: Less smoothing - Closer to 0: More smoothing Only used if apply_ewm=True.
- fit(df: pandas.DataFrame)[source]
Fit nearest neighbor model on training trajectories.
Prepares FTN for predictions by building KNN index for fast neighbor lookup
- Parameters:
df (pd.DataFrame) – Training time series data with required columns: - unique_id: Series identifier - ds: Timestamp - y: Target values Must follow nixtla framework conventions.
- predict(fcst: pandas.DataFrame)[source]
Adjust forecasts using nearest neighbor trajectories.
Improves base model predictions by: 1. Finding similar historical trajectories 2. Computing trajectory-based corrections 3. Optionally combining with original forecasts
- Parameters:
fcst (pd.DataFrame) – Base model forecasts with required columns: - unique_id: Series identifier - ds: Timestamp - model_name: Predicted values by model with name model_name
- Returns:
Adjusted forecasts with same structure
- Return type:
pd.DataFrame
- alpha_cv_scoring(cv: pandas.DataFrame, model_names: List[str] | None = None)[source]
Compute optimal FTN combination weights using validation data.
Uses cross-validation or validation results to determine optimal weights for combining FTN predictions with original forecasts at each horizon. Optimizes based on forecast accuracy metrics.
- Parameters:
cv (pd.DataFrame) – Validation results with required columns: - unique_id: Series identifier - ds: Timestamp - y: Actual values - model_name: Predicted values by a model with name model_name
model_names (List[str], optional) – Models to compute weights for. If None, uses all models from fitting