pub struct EwmaDetector { /* private fields */ }Expand description
EWMA-based anomaly detector
Uses exponentially weighted moving average and standard deviation for change point detection and anomaly flagging.
§Example
use alice_analytics::anomaly::EwmaDetector;
let mut detector = EwmaDetector::new(0.1, 3.0);
// Feed normal values
for v in [10.0, 10.5, 9.5, 10.2, 9.8, 10.1] {
detector.observe(v);
}
// Check for anomalies
assert!(!detector.is_anomaly(10.0));
assert!(detector.is_anomaly(50.0));Implementations§
Source§impl EwmaDetector
impl EwmaDetector
Sourcepub const fn new(alpha: f64, threshold_k: f64) -> Self
pub const fn new(alpha: f64, threshold_k: f64) -> Self
Create a new EWMA detector
§Arguments
alpha- Smoothing factor in(0.0, 1.0], higher = more reactivethreshold_k- Number of standard deviations for anomaly threshold
§Panics
alpha が (0.0, 1.0] の外 (NaN / 無限大を含む、どちらも比較が偽に
なるので同じ assert で弾かれる) の場合
定義域の扱いは同 crate の
ExponentialMovingAverage::new
に合わせた (範囲外は黙って丸めず panic する) 2026-10-07 までは
alpha.clamp(0.001, 1.0) だったので、0.0 と負値が黙って 0.001 になり
(⚠️ -1.0 と 0.0 が同一結果になっていた)、NaN は clamp が NaN を
返すので丸められずに残り、以後 ewma / std_dev / anomaly_score が
すべて NaN になって is_anomaly が常に false を返していた
= 検出器が黙って無効化される 無限大の score は「異常」と読めるが、
false は「正常」と読めるので、こちらの方が害が大きい
Sourcepub fn is_anomaly(&self, value: f64) -> bool
pub fn is_anomaly(&self, value: f64) -> bool
Check if a value is an anomaly (without updating)
Sourcepub fn anomaly_score(&self, value: f64) -> f64
pub fn anomaly_score(&self, value: f64) -> f64
Get anomaly score (number of standard deviations)
分散推定が 0 のとき (定数 stream、または alpha = 1.0 で平滑化しない場合)
は、ewma と一致する値に 0.0、それ以外に f64::INFINITY を返す
⚠️ これは alpha = 1.0 の境界固有の挙動ではない: alpha = 0.3 でも
同じ値を 50 回入れれば std_dev は 0 になり、以後 ewma 以外は inf に
なる (実測) 「散らばりが 0 の系列に対する逸脱は無限に異常」が契約で、
tests/panic_contract.rs が値で固定する
Sourcepub const fn threshold_k(&self) -> f64
pub const fn threshold_k(&self) -> f64
Get threshold multiplier
Sourcepub const fn set_threshold_k(&mut self, k: f64)
pub const fn set_threshold_k(&mut self, k: f64)
Set threshold multiplier