Changepoint & Anomaly Detection Guide
polars-ts provides trend tests, changepoint detection, regime inference, and anomaly detection — all group-aware and Polars-native.
Trend detection
Mann-Kendall test
Non-parametric test for monotonic trend. Returns a struct with tau, p_value, and trend (increasing/decreasing/no trend). Implemented in Rust.
import polars as pl
import polars_ts as pts
result = df.group_by("unique_id").agg(
pts.mann_kendall(pl.col("y")).alias("trend"),
)
Sen's slope
Robust trend magnitude estimator (median of all pairwise slopes). Implemented in Rust.
Changepoint detection
CUSUM
Cumulative sum control chart for detecting mean shifts. Implemented in Rust.
PELT
Pruned Exact Linear Time algorithm for finding multiple changepoints with configurable cost functions.
Cost functions: "mean", "var", "meanvar".
BOCPD
Bayesian Online Changepoint Detection — detects changepoints in a streaming fashion using a Student-t model.
Regime detection
Hidden Markov Model state inference via Baum-Welch EM.
Anomaly detection
Decomposition-based
Flag anomalies from decomposition residuals exceeding a threshold.
Isolation Forest
Unsupervised anomaly detection on engineered features.
Further reading
- Notebook 06: Changepoint & anomaly detection