Skip to main content

EwmaDetector

Struct EwmaDetector 

Source
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

Source

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 reactive
  • threshold_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 は「正常」と読めるので、こちらの方が害が大きい

Source

pub fn observe(&mut self, value: f64)

Observe a new value and update statistics

Source

pub fn is_anomaly(&self, value: f64) -> bool

Check if a value is an anomaly (without updating)

Source

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 が値で固定する

Source

pub const fn ewma(&self) -> f64

Get current EWMA

Source

pub fn std_dev(&self) -> f64

Get current standard deviation estimate

Source

pub const fn alpha(&self) -> f64

Get smoothing factor

Source

pub const fn set_alpha(&mut self, alpha: f64)

Set smoothing factor

§Panics

Self::new と同じ条件 (alpha が (0.0, 1.0] の外) setter が丸めたままだと 構築時の assert を後から回避できるので、同じ検査を置く

Source

pub const fn threshold_k(&self) -> f64

Get threshold multiplier

Source

pub const fn set_threshold_k(&mut self, k: f64)

Set threshold multiplier

Source

pub const fn count(&self) -> u64

Get count of observations

Source

pub const fn reset(&mut self)

Reset the detector

Trait Implementations§

Source§

impl Clone for EwmaDetector

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EwmaDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.