Can LLMs Serve As Time Series Anomaly Detectors?

Manqing Dong, Hao Huang, Longbing CaoarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) have demonstrated remarkable capabilities in natural language tasks and time series forecasting. However, their utility in time series anomaly detection—a task requiring both pattern recognition and contextual reasoning—remains under-explored. This paper evaluates GPT-4 and LLaMA3 across various prompting strategies and introduces a synthetic data generation framework, TTGenerator, for instruction fine-tuning. Results indicate that while "out-of-the-box" performance is limited, sophisticated prompting and fine-tuning enable LLMs to match state-of-the-art specialized models while providing interpretable explanations.

Introduction

While LLMs have shown impressive results in time series forecasting, anomaly detection presents a unique challenge. It requires identifying point exceptions (sudden spikes) and contextual exceptions (patterns that deviate from established trends). Traditional deep learning models for anomaly detection often operate as "black boxes," flagging data points without providing reasoning. This research investigates whether LLMs can leverage their reasoning capabilities to serve as explainable detectors, bridging the gap between numerical analysis and human logic.

Methodology

The researchers explored two primary avenues for activating LLM capabilities in anomaly detection: prompt engineering and instruction fine-tuning.

Prompt Engineering

Three strategies were tested on GPT-4 and LLaMA3:

  1. Multi-modal Instruction: Encouraging the model to "visualize" data trends alongside numerical input.
  2. In-Context Learning (ICL): Providing few-shot examples of anomalies and their explanations.
  3. Chain-of-Thought (CoT): Guiding the model through step-by-step reasoning (e.g., identifying trends before flagging deviations).
Prompting strategies and comparative performance for LLaMA3 and GPT-4.

Instruction Fine-Tuning

For smaller models like LLaMA3 that struggle with prompting alone, the authors developed TTGenerator (Time Series and Text Explanation Generator). This tool synthesizes time series data with corresponding textual explanations. LLaMA3 was then fine-tuned using LoRA (Low-Rank Adaptation) on this synthetic dataset to improve its detection and reasoning capabilities.

Datasets

The study utilized a combination of real-world and synthetic data:

  • Real-world Benchmarks: YAHOO (service monitoring), ECG (heart rate), SVDB (electrocardiogram), and IOPS (server metrics).
  • TTGenerator Synthetic Dataset: A novel benchmark featuring five anomaly types: global point, local point, seasonality, trend, and shape anomalies.

Key Results and Insights

The evaluation revealed that LLMs can indeed compete with specialized models when properly guided.

Performance and Explainability

GPT-4, utilizing Chain-of-Thought prompting, achieved an average rank of 3rd across benchmarks, outperforming specialized models like Informer and DLinear. Its F-score and Range-F metrics were competitive with state-of-the-art models such as TimesNet. Furthermore, GPT-4 provided accurate explanations in a majority of cases for the YAHOO and IOPS datasets.

Correctness comparison across models, prompting strategies, and anomaly types.

The Impact of Fine-Tuning

Fine-tuning LLaMA3 on synthetic data significantly enhanced its performance, particularly in detecting complex seasonality anomalies where it previously failed. This suggests that synthetic data can serve as a vital bridge for teaching LLMs the "language" of time series anomalies.

Challenges: Hallucination and Precision

A significant finding was the prevalence of index hallucination, where models correctly identify an anomaly but provide an index outside the valid range. GPT-4 exhibited this behavior in 21% to 29% of test segments. Additionally, LLMs still struggle with precise numerical calculations, suggesting that future models might benefit from image-based or embedding-based representations.

Conclusion

This research demonstrates that while LLMs are not yet ready to replace specialized detectors, they are viable candidates for explainable time series analysis. Through advanced prompting and targeted fine-tuning, LLMs can provide both competitive detection performance and the human-readable logic necessary for root-cause analysis.

Summary updated

BibTeX
@misc{dong2024can,
  author = {Manqing Dong and Hao Huang and Longbing Cao},
  title = {{Can LLMs Serve As Time Series Anomaly Detectors?}},
  year = {2024},
  eprint = {2408.03475},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.03475},
}
Figure