🔥 Time series AnomalyDetection for Ruby
Learn how it works
Add this line to your application’s Gemfile:
gem "anomaly_detection"
Detect anomalies in a time series
series = {
Date.parse("2025-01-01") => 100,
Date.parse("2025-01-02") => 150,
Date.parse("2025-01-03") => 136,
# ...
}
AnomalyDetection.detect(series, period: 7)
Works great with Groupdate
series = User.group_by_day(:created_at).count
AnomalyDetection.detect(series, period: 7)
Series can also be an array without times (the index is returned)
series = [100, 150, 136, ...]
AnomalyDetection.detect(series, period: 7)
Pass options
AnomalyDetection.detect(
series,
period: 7, # number of observations in a single period
alpha: 0.05, # level of statistical significance
max_anoms: 0.1, # maximum number of anomalies as percent of data
direction: "both", # pos, neg, or both
verbose: false # show progress
)
Add Vega to your application’s Gemfile:
gem "vega"
And use:
AnomalyDetection.plot(series, anomalies)
This library was ported from the AnomalyDetection R package and is available under the same license. It uses stl-cpp for seasonal-trend decomposition and dist.h for the quantile function.
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/AnomalyDetection.rb.git
cd AnomalyDetection.rb
bundle install
bundle exec rake compile
bundle exec rake test