Nearest Neighbor Multivariate Time Series Forecasting

Huiliang Zhang, Ping Nie, Lijun Sun, Benoit Boulet · DOI

TL;DR

This paper introduces kNN-MTS, a novel framework that enhances multivariate time series (MTS) forecasting by leveraging a k-nearest neighbor (kNN) approach on learned representations. By creating a datastore of historical patterns, kNN-MTS allows models to access and utilize the entire dataset at test time, overcoming the limitations of finite input sequences and leading to significant performance improvements.

What Problem Does This Paper Address?

Multivariate time series (MTS) forecasting is crucial for many applications, but current state-of-the-art methods, particularly Spatial-Temporal Graph Neural Networks (STGNNs), struggle with the vastness and complexity of real-world data. These models are often limited by the computational cost of processing long historical sequences, forcing them to rely on finite input windows. This limitation prevents them from capturing important, sparsely distributed, and cyclical patterns that occur over extended periods and across multiple variables. Consequently, existing methods often yield only marginal improvements, especially when dealing with complex, real-world datasets where subtle, long-range dependencies are key to accurate forecasting.

Methods

The core of the kNN-MTS framework is a non-parametric approach that augments existing MTS forecasting models. Instead of retraining, it uses a nearest neighbor retrieval mechanism.

  1. Datastore Creation: The process begins by creating a datastore of "keys" and "values." The keys are high-dimensional representations of historical MTS data, generated by a Hybrid Spatial-Temporal Encoder (HSTEncoder). The values are the corresponding future time series segments. This HSTEncoder is designed to capture both long-term temporal dependencies (using Transformer-like segment embeddings) and short-term spatial-temporal dependencies (using graph convolutional networks).
  2. Forecasting: During forecasting, the input MTS is first encoded by the HSTEncoder to generate a query representation. This query is then used to search the datastore for the K nearest neighbor representations.
  3. Aggregation: The future time series segments associated with these K neighbors are retrieved. Their predictions are then weighted and combined with the original model's prediction to produce the final forecast. A scaling parameter ($\lambda$) controls the influence of the kNN retrieval versus the base model's prediction.

A key advantage is that kNN-MTS requires no additional training for the base MTS model, making it highly adaptable and efficient.

Datasets

The paper evaluates kNN-MTS on five commonly used real-world MTS datasets:

  • PEMS-BAY: Traffic speed data.
  • PEMS04, PEMS07, PEMS08: Traffic flow data.
  • Electricity: Electricity consumption data.

These datasets vary in size, number of nodes (sensors/variables), and time span, providing a robust evaluation environment. The paper notes that PEMS-BAY, PEMS04, PEMS07, and PEMS08 come with pre-defined graphs, while Electricity does not.

Key Insights and Ideas

  • Leveraging the Entire Dataset: The fundamental insight is that accessing the entire historical dataset at test time, rather than just a fixed-length window, can unlock significant forecasting improvements.
  • Representations as Keys: Using learned, high-dimensional representations (embeddings) as keys for similarity search is more effective than using raw data, as these representations can encode complex spatial-temporal relationships.
  • kNN as a Complement, Not a Replacement: kNN-MTS doesn't replace existing models but augments them. It acts as a powerful "memory" mechanism, allowing models to recall and utilize past patterns that might be missed by standard sequential processing.
  • Interpretability through Retrieval: The ability to inspect which historical segments and sensors were retrieved for a given prediction offers a degree of interpretability, helping to understand why a particular forecast was made.
  • Training Efficiency: The framework demonstrates that a model trained on a smaller dataset can achieve superior performance when augmented with a kNN approach over a much larger datastore, suggesting a path to more efficient model training.

Key Results

  • Performance Improvement: kNN-MTS consistently outperforms state-of-the-art baselines across multiple datasets and forecasting horizons (3, 6, and 12 time steps) on metrics like MAE, RMSE, and MAPE. For instance, on PEMS-BAY, kNN-MTS achieved a statistically significant improvement in MAE, RMSE, and MAPE for all horizons compared to the best baseline.
  • Visualization of Similarity: t-SNE visualizations show that retrieved nearest neighbor representations cluster tightly, indicating high similarity, while randomly sampled representations are scattered, demonstrating the effectiveness of the representation learning and retrieval.
  • Qualitative Prediction Improvement: Visualizations of predictions show kNN-MTS accurately capturing rare patterns and rapid fluctuations that other models miss.
  • Training Efficiency: kNN-MTS trained on a small subset (3k data) with a large datastore (10k data) outperformed a model trained on the full 10k data, highlighting its efficiency gains.
  • Computational Cost: Building the datastore is comparable to a single epoch of training, and inference is fast, making it practical for real-world applications.

Limitations and Future Work

  • Storage Overhead: The primary limitation is the storage space required for the datastore, although the paper notes it's cost-effective as it's not GPU-based.
  • Hyperparameter Sensitivity: While generally robust, performance can be influenced by hyperparameters like K (number of neighbors) and $\lambda$ (aggregation weight), though the paper shows reasonable ranges for optimal performance.
  • Future Directions: The authors suggest future work could focus on:
  • Down-sampling frequent target time series in the datastore to manage its size.
  • Considering distribution shifts of new samples.
  • Enhancing representations and retrieval accuracy through contrastive learning.
  • Further improving efficiency and generalizability.

Summary updated

BibTeX
@article{zhang2025nearest,
  author = {Huiliang Zhang and Ping Nie and Lijun Sun and Benoit Boulet},
  title = {{Nearest Neighbor Multivariate Time Series Forecasting}},
  journal = {IEEE Trans. Neural Networks Learn. Syst.},
  year = {2025},
  doi = {10.1109/tnnls.2024.3490603},
}
Figure