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
Here are several methods worth exploring for time series subsequence anomaly detection:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Discrete Wavelet Transform (DWT): Can detect localized anomalies
Specialized Libraries/Frameworks
PyOD: Comprehensive Python library for anomaly detection
Prophet (by Facebook): Good for time series with seasonal patterns
STUMPY: Matrix profile based anomaly detection
Each method has strengths depending on your specific data characteristics. I recommend experimenting and comparing multiple approaches.
Would you like me to elaborate on implementation strategies for any of these methods?
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 correct, and I appreciate you pointing that out. Isolation Forest is primarily designed for point anomaly detection, not subsequence anomaly detection. It's good at identifying individual outlier data points but not well-suited for detecting anomalous subsequences in time series.
For subsequence anomaly detection, more appropriate methods include:
Matrix Profile Techniques
STUMPY library's approach
Allows sliding window detection of anomalous subsequences
Computationally efficient
Can handle variable-length subsequence anomalies
Deep Learning Approaches
Sequence-to-Sequence Autoencoders
Specifically designed to capture temporal dependencies
Can reconstruct normal subsequence patterns
High reconstruction error indicates anomalous subsequence
Statistical Methods
ARIMA/SARIMA residual analysis
Sliding window statistical tests
Cumulative Sum (CUSUM) control charts
Exponentially Weighted Moving Average (EWMA)
Advanced Techniques
Dynamic Time Warping (DTW) based anomaly detection
Gaussian Process Regression for subsequence modeling
Recurrent Neural Networks (RNNs) with anomaly scoring
Would you like me to dive deeper into implementation strategies for subsequence anomaly detection?
User
can you focus on one method and give me the implementation on python
Assistant
I'll provide an implementation for subsequence anomaly detection using Matrix Profile with the STUMPY library, which is particularly effective for this task.
python
import numpy as np
import stumpy
import matplotlib.pyplot as plt
def subsequence_anomaly_detection(time_series, window_size, threshold_percentile=95):
"""
Detect anomalous subsequences using Matrix Profile
Parameters:
- time_series: Input time series data
- window_size: Length of subsequences to analyze
- threshold_percentile: Percentile to determine anomaly threshold
Returns:
- Indices of anomalous subsequences
"""
# Compute Matrix Profile
matrix_profile = stumpy.mp(time_series, m=window_size)
# Extract Matrix Profile distances
distances = matrix_profile[0]
# Compute threshold based on percentile
anomaly_threshold = np.percentile(distances, threshold_percentile)
# Identify anomalous subsequences
anomaly_indices = np.where(distances > anomaly_threshold)[0]
return anomaly_indices
# Example usage
def main():
# Generate sample time series with some anomalies
np.random.seed(42)
normal_data = np.random.normal(0, 1, 1000)
# Inject some anomalous subsequences
anomalous_subsequence1 = np.random.normal(5, 2, 50)
anomalous_subsequence2 = np.random.normal(-5, 2, 50)
time_series = np.concatenate([
normal_data[:300],
anomalous_subsequence1,
normal_data[350:700],
anomalous_subsequence2,
normal_data[750:]
])
# Set window size (adjust based on your data)
window_size = 50
# Detect anomalies
anomaly_indices = subsequence_anomaly_detection(time_series, window_size)
# Visualization
plt.figure(figsize=(12, 6))
plt.plot(time_series, label='Time Series')
# Highlight anomalous regions
for idx in anomaly_indices:
plt.axvspan(idx, idx + window_size, color='red', alpha=0.3)
plt.title('Subsequence Anomaly Detection')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.show()
print("Anomalous subsequence start indices:", anomaly_indices)
if __name__ == "__main__":
main()