Skip to content

ankane/AnomalyDetection.rb

Repository files navigation

AnomalyDetection.rb

🔥 Time series AnomalyDetection for Ruby

Learn how it works

Build Status

Installation

Add this line to your application’s Gemfile:

gem "anomaly_detection"

Getting Started

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)

Options

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
)

Plotting

Add Vega to your application’s Gemfile:

gem "vega"

And use:

AnomalyDetection.plot(series, anomalies)

Credits

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.

References

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

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

About

Time series anomaly detection for Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published