Introduction to Online (Real-Time) Anomaly Detection
Learn how to use the detect_anomalies_online method for real-time anomaly detection in streaming time series data with TimeGPT.
This guide introduces the detect_anomalies_online method for monitoring streaming time series data in real time. You’ll learn how to quickly get started with this method and explore its key differences from the historical anomaly detection endpoint.
Overview
Online (real-time) anomaly detection helps you identify unusual behavior in time series data as soon as it appears. With TimeGPT, you can:
Flexible Control
More flexibility and control over the anomaly detection process.
Univariate & Multivariate
Perform univariate and multivariate anomaly detection.
Stream Processing
Detect anomalies on stream data as it arrives.
Quick Start
1. Set up your environment
Initialize your Python environment by importing the required libraries:
2. Configure your NixtlaClient
Provide your API key (and optionally a custom base URL).
Using an Azure AI endpoint
To use an Azure AI endpoint, set the base_url
argument:
3. Load your dataset
We use a minute-level time series dataset that monitors server usage. This dataset is ideal for showcasing streaming data scenarios, where the task is to detect server failures or downtime in real time.
We observe that the time series remains stable during the initial period; however, a spike occurs in the last 20 steps, indicating anomalous behavior. Our goal is to capture this abnormal jump as soon as it appears.
Server Data with Spike Anomaly
4. Detect anomalies in real time
Use the detect_anomalies_online
method to identify anomalies by leveraging TimeGPT’s forecasting capabilities.
Key Parameters
• df: A DataFrame containing your time series.
• time_col: Datestamp column.
• target_col: Variable to forecast.
• h: Forecast horizon (steps ahead).
• freq: Frequency (e.g., ‘min’).
• level: Confidence level (default 99%).
• detection_size: Number of recent steps to analyze for anomalies.
Here we use a detection size of 100 to illustrate the anomaly detection process. In production, running detections more frequently with smaller detection sizes can help identify anomalies as soon as they occur.
Identified Anomalies
5. Next steps
From the plot, we observe that the anomalous period is promptly detected. For a deeper dive into detect_anomalies_online
—including parameter tuning and strategies for fine-tuning anomaly detection—stay tuned for our upcoming tutorial.