Skillsbench time_series_anomaly_detection
Detect anomalies in time series data using Prophet Framework (Meta), which frames the seasonality, trend holiday effect and other needed regressors into its model, to identify unusual surges or slumps in trends. This is a general methodology analyst can use for understanding what changes of their tracking metrics are manifesting anomalies pattern.
git clone https://github.com/benchflow-ai/skillsbench
T=$(mktemp -d) && git clone --depth=1 https://github.com/benchflow-ai/skillsbench "$T" && mkdir -p ~/.claude/skills && cp -r "$T/tasks/trend-anomaly-causal-inference/environment/skills/time_series_anomaly_detection" ~/.claude/skills/benchflow-ai-skillsbench-time-series-anomaly-detection && rm -rf "$T"
tasks/trend-anomaly-causal-inference/environment/skills/time_series_anomaly_detection/SKILL.mdTime Series Anomaly Detection Framework
Comprehensive time series anomaly detection framework leverages Facebook Prophet's time-sereis trend prediction (counterfactural) in the target window and compare it with the actual data. An anomaly index that measures how much deviate the actual number is from the expected trend (counterfactural) for each of the categories or groups within that period. The anomaly index ranking can be used to identify outerstanding groups/categories that manifest unexpected surge and slump with quantitative index ranged -100 to 100.
Core Components
TimeSeriesAnomalyDetector
Main class for anomaly detection with configurable parameters.
Constructor Parameters
(int): Minimum days of the available training data required per category (default: 180). Groups have less than this number of days data would be skippedmin_training_days
(float): Prophet confidence interval width (default: 0.68 = ±1 std) as the starting point to measure the unexpected deviationconfidence_interval
(float): Flexibility of trend changes (default: 0.05) However, if dataset is not in a big size, use 0.1changepoint_prior_scale
(float): Strength of seasonality (default: 10.0)seasonality_prior_scale
Anomaly Index Calculation (This is CRITICAL and is the key to identify unsual pattern)
- Within confidence interval: Index = 0
- Above upper bound: Index = (actual - upper) / std
- Below lower bound: Index = (actual - lower) / std
- Scaled Index: 100 × tanh(raw_index) → maps to [-100, 100] so after this transformation
- Index=1 → ~76 points
- index=2 → ~96 points
- index=3 → ~99.5 points (approaches ±100)
Usage
from anomaly_detection import TimeSeriesAnomalyDetector # Initialize detector detector = TimeSeriesAnomalyDetector( min_training_days=180, # depends on the dataset span (usually 50%+ of the dataset time span could be good threshold) confidence_interval=0.68 # 1 std for a good intepretability visually and statistically ) # Run anomaly detection results = detector.detect_anomalies( df=transaction_df, date_col='<temporal column>', category_col='<grouping column>', value_col='<metric to analyze for anomalies>', cutoff_date='<training and prediction split date, format YYYY-MM-DD>', prediction_end='end of prediction window', agg_func='sum', # Aggregation: 'sum', 'mean', 'count', or None (e.g if data is not transactional data and value_col value is ready for the modeling) ) # Access results anomaly_summary = results['anomaly_summary'] # All categories with anomaly indices # Get top 10 postive anomaly groups top_surge = results['anomaly_summary'].head(10)['<grouping column>'].tolist() # Get top 10 negative anomaly groups (descendingly) top_slump = results['anomaly_summary'].tail(10)['<grouping column>'].tolist()
Input Format
Required DataFrame columns:
- Date column (e.g.,
,Order Date
,Transaction Date
) - temporal dimensionPurchase Date - Category column (e.g.,
,Category
) - grouping dimension | Granularity that the method will be applied in loopsRegion - Value column (e.g.,
,Price
,Spend
,Revenue
) - metric to detect anomalies in (This is very important as this, directly or indirectly, is the y of the modeling)Quantity - Optional: Additional columns (e.g.,
)User ID
Parameters:
: Divides training data (before) from prediction period (after)cutoff_date
: End of anomaly detection windowprediction_end
: How to aggregate values per category per dayagg_func
: Sum all values of the target column for each category-date (e.g user-level purchase data)'sum'
: Average values'mean'
: Count records'count'
: No aggregation (e.g non transactional data)None
Output
detect_anomalies() returns a dictionary with 3 keys:
{ 'anomaly_summary': pd.DataFrame, # Summary metrics per category 'predictions': Dict[str, pd.DataFrame], # Prophet forecasts per category 'models': Dict[str, Prophet] # Trained models per category }
Output anomaly_summary
Preview
anomaly_summary| Column | Type | Description |
|---|---|---|
| str | Category name (column name matches your input) |
| float | Scaled anomaly score in [-100, 100] |
| int | Days with actual data in prediction window |
| float | Average of daily scaled anomalies |
| float | Maximum daily scaled anomaly |
| float | Minimum daily scaled anomaly |
| float | Sum of actual values in prediction window |
| float | Sum of predicted (yhat) values |
Sorted by
descendingAnomaly_Index
predictions
Dictionary
predictionsKey = category name, Value = DataFrame with columns:
: Dateds
: Prophet predictionyhat
: Lower confidence boundyhat_lower
: Upper confidence boundyhat_upper
models
Dictionary
modelsKey = category name, Value = Fitted Prophet model object
Key Features
- Pure anomaly detection without business logic (no hardcoded top N)
- Automatic daily aggregation by grouping dimension
- Prophet model training with seasonality (daily, weekly, yearly)
- Counterfactual prediction for intervention analysis
- Scaled anomaly index [-100, 100] for interpretability
Best Practices
- Ensure sufficient training data (180+ days recommended)
- Use
to define pre/post intervention periodscutoff_date - Use
if data already at daily category levelagg_func=None - Apply your own filtering logic on
(top N, thresholds, etc.)anomaly_summary - Chain with DID analysis for causal inference on selected categories