Time series AnomalyDetection for C++
Learn how it works
Add the headers to your project (supports C++20 and greater).
There is also support for CMake and FetchContent:
include(FetchContent)
FetchContent_Declare(anomaly_detection GIT_REPOSITORY https://github.com/ankane/AnomalyDetection.cpp.git GIT_TAG v0.3.0)
FetchContent_MakeAvailable(anomaly_detection)
target_link_libraries(app PRIVATE anomaly_detection::anomaly_detection)Include the header
#include "anomaly_detection.hpp"Detect anomalies in a time series
std::vector<float> series{
5.0, 9.0, 2.0, 9.0, 0.0, 6.0, 3.0, 8.0, 5.0, 18.0,
7.0, 8.0, 8.0, 0.0, 2.0, 15.0, 0.0, 5.0, 6.0, 7.0,
3.0, 6.0, 1.0, 4.0, 4.0, 4.0, 30.0, 7.0, 5.0, 8.0
};
size_t period = 7; // number of observations in a single period
anomaly_detection::AnomalyDetection ad{series, period};Get anomalies
const std::vector<size_t>& anomalies = ad.anomalies();Set parameters
anomaly_detection::AnomalyDetectionParams params{
.alpha = 0.05, // level of statistical significance
.max_anoms = 0.1, // maximum number of anomalies as percent of data
.direction = Direction::Both, // Positive, Negative, or Both
.verbose = false // show progress
};
anomaly_detection::AnomalyDetection ad{series, period, params};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-c 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.cpp.git
cd AnomalyDetection.cpp
cmake -S . -B build
cmake --build build
build/test