I'd like to do time serie subsequence anomaly detection, i've already used luminol and my own methods in python, what other method are worth trying and that aren't impossible to implement
Assistant
For time series subsequence anomaly detection, here are some methods worth trying that balance effectiveness with reasonable implementation complexity:
Matrix Profile - Using the stumpy library makes this powerful technique quite accessible. Great for finding discords (anomalous subsequences) and works well without parameter tuning.
Isolation Forest - Available in scikit-learn, this algorithm isolates anomalies efficiently and works well with minimal configuration.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
LSTM Autoencoders - With Keras/TensorFlow, these neural networks can learn normal patterns and flag reconstructions with high error. More complex but very powerful for temporal data.
Seasonal-Hybrid ESD - Python ports like pyculiarity implement Twitter's robust approach for detecting anomalies in seasonal data.
DBSCAN/LOF - These density-based clustering methods in scikit-learn can identify outliers effectively on sliding windows of your time series.
Prophet's uncertainty intervals - Using prediction bounds from Facebook's Prophet to flag anomalies that fall outside expected ranges.
What type of time series data are you working with?
User
you mentionned isolation forest but isn't this method specially used for single anomaly (ie outlier) detection ? and is thus crap for subsequence anomaly
Assistant
You're absolutely right - that was a poor recommendation on my part. Isolation Forest is designed for point anomaly detection and isn't well-suited for subsequence anomalies without significant modification.
For true subsequence anomaly detection, better approaches include:
Matrix Profile (stumpy) - Specifically designed for subsequence anomalies and discord discovery
LSTM Autoencoders - Can effectively learn normal sequence patterns and detect anomalous subsequences
DTW (Dynamic Time Warping) based methods - For comparing subsequence similarity with reference patterns
HOT-SAX algorithm - Specifically designed for subsequence anomaly detection
Shapelets - For finding discriminative subsequences that characterize anomalies
Contextual Anomaly Transformer (CAT) - A newer approach using attention mechanisms to identify contextual anomalies in sequences
Sorry for the confusion. Did you have specific characteristics of your data that might help narrow down which approach would be most suitable?
User
can you focus on one method and give me the implementation on python
Assistant
Matrix Profile Implementation for Subsequence Anomaly Detection
The Matrix Profile is an excellent technique specifically designed for subsequence anomaly detection. Here's a complete implementation using the stumpy library:
python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import stumpy
# 1. Load or generate your time series data
# For this example, I'll create synthetic data with an anomalous subsequence
np.random.seed(0)
n = 1000
# Normal pattern is a sine wave
normal_pattern = np.sin(np.linspace(0, 4*np.pi, n))
# Add noise
time_series = normal_pattern + 0.1 * np.random.randn(n)
# Insert an anomalous subsequence
anomaly_start = 600
anomaly_length = 50
# The anomaly is a different pattern (e.g., a square wave)
time_series[anomaly_start:anomaly_start+anomaly_length] = 0.5 * np.ones(anomaly_length)
# 2. Choose a window size (subsequence length)
window_size = 30 # Adjust based on your expected anomaly length
# 3. Compute the Matrix Profile
matrix_profile = stumpy.stump(time_series, window_size)
# 4. Find the top discords (anomalous subsequences)
# Lower Matrix Profile values indicate similarity, higher values indicate anomalies
discord_idx = np.argsort(matrix_profile[:, 0])[-3:][::-1] # Top 3 anomalies
# 5. Visualize the results
plt.figure(figsize=(15, 8))
# Plot the time series
plt.subplot(211)
plt.plot(time_series)
# Highlight the actual anomaly
plt.axvspan(anomaly_start, anomaly_start+anomaly_length, color='red', alpha=0.3, label='Actual Anomaly')
# Highlight the detected anomalies
for idx in discord_idx:
plt.axvspan(idx, idx+window_size, color='green', alpha=0.3)
plt.title('Time Series with Anomalies')
plt.legend()
# Plot the Matrix Profile
plt.subplot(212)
plt.plot(matrix_profile[:, 0])
plt.title('Matrix Profile')
# Mark the detected anomalies
for idx in discord_idx:
plt.axvline(x=idx, color='green', linestyle='--')
plt.xlabel('Position')
plt.ylabel('Distance')
plt.tight_layout()
plt.show()
# Print the locations of detected anomalies
print("Detected anomaly subsequences start at positions:", discord_idx)
Would you like me to explain or break down the code?