Context Matters: Leveraging Contextual Features for Time Series Forecasting

Sameep Chattopadhyay, Pulkit Paliwal, Sai Shankar Narasimhan, Shubhankar Agarwal, Sandeep P. Chinchali · DOI

Abstract

Standard time series models often fail to account for external factors that drive real-world fluctuations. We present ContextFormer, a modular framework that integrates multimodal contextual information—such as news, weather, and market indicators—into pre-trained forecasting models. By employing cross-attention and a zero-initialization strategy, ContextFormer achieves up to a 30% improvement in accuracy across diverse datasets without compromising the stability of the base forecaster.

Introduction

Most state-of-the-art time series models are context-agnostic, relying solely on historical numerical sequences to predict future values. However, real-world events are rarely isolated. A sudden market correction or a local holiday can drastically alter trends in ways that historical data alone cannot explain. The challenge lies in the messy, multimodal nature of contextual data (text, categories, continuous variables), which varies significantly across domains.

Overview of the ContextFormer framework compared to context-agnostic models.

Methodology: The ContextFormer Architecture

ContextFormer is designed as a "plug-and-play" enhancement for existing forecasters like PatchTST or iTransformer. Its architecture consists of several key innovations:

  • Multimodal Embedding Modules: These pipelines process categorical data via one-hot encoding, continuous data through dense layers, and temporal data by decomposing timestamps into granular units (year, month, day, hour).
  • Surgical Cross-Attention: Instead of a complete redesign, cross-attention layers are integrated into the pre-trained model to allow it to weigh historical features against new contextual signals.
  • Zero-Initialization Strategy: To prevent performance degradation during the initial stages of fine-tuning, new weights are initialized to zero. This ensures the model starts with the performance of the original context-agnostic version and improves as it learns the contextual correlations.
  • Efficient Fine-tuning: The base forecaster remains frozen, with only the ContextFormer modules and final projection layers being trained, significantly reducing computational overhead.

Experimental Evaluation

The framework was tested across multiple domains, including air quality, retail sales, traffic occupancy, and electricity consumption. Notably, the authors introduced a new dataset pairing hourly Bitcoin prices with daily financial news article embeddings generated by Large Language Models (LLMs).

Key Results

ContextFormer consistently outperformed base models, showing an average accuracy gain of 11.6% on synthetic data and up to 30% on real-world tasks. In electric load forecasting, the model achieved a remarkable 42.1% improvement in Mean Squared Error (MSE).

Impact of increasing the number of contextual features on forecasting error.

The relationship between context and accuracy is further supported by the authors' information-theoretic proof, which demonstrates that relevant metadata reduces forecast uncertainty. As shown in the figure above, increasing the number of contextual features leads to a significant and consistent reduction in MSE compared to context-agnostic baselines.

Discussion and Insights

A critical finding of this work is that fine-tuning a pre-trained model is superior to training a context-aware model from scratch. Models trained from scratch often struggle to learn fundamental time series trends and complex contextual relationships simultaneously. By starting with a "stable" base, ContextFormer can focus on learning the delta provided by the context.

Furthermore, the integration of LLM-generated embeddings for news articles allows the model to capture market sentiment. In the Bitcoin case study, this enabled the model to distinguish between random volatility and structural market corrections, improving Mean Absolute Error (MAE) by 11.5%.

Conclusion and Future Work

While ContextFormer provides a robust mechanism for incorporating metadata, the selection of relevant features remains a manual process. Future research could explore automated metadata selection and the inclusion of even richer data types, such as images or video. Additionally, a two-step pipeline involving the forecasting of the metadata itself could further enhance predictive power.

Summary updated

BibTeX
@misc{chattopadhyay2024context,
  author = {Sameep Chattopadhyay and Pulkit Paliwal and Sai Shankar Narasimhan and Shubhankar Agarwal and Sandeep P. Chinchali},
  title = {{Context Matters: Leveraging Contextual Features for Time Series Forecasting}},
  year = {2024},
  eprint = {2410.12672},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.12672},
}
Figure