pub struct Kswin { /* private fields */ }Expand description
KSWIN (Kolmogorov-Smirnov Windowing) drift detector.
Detects distribution changes by comparing two fixed-size windows with a two-sample KS test. Unlike mean-based detectors (Page-Hinkley, ADWIN), KSWIN is sensitive to distribution shape changes (variance, skewness, multimodality), not just mean shifts.
The KS test p-value is computed via the Marsaglia-Tsang-Wang algorithm; no external statistics crate is required.
§Examples
use rill_ml::drift::{DriftDetector, DriftLevel, Kswin, KswinConfig};
let mut kswin_config = KswinConfig::default();
kswin_config.alpha = 0.01;
kswin_config.window_size = 50;
kswin_config.check_interval = 50;
let mut kswin = Kswin::new(kswin_config).unwrap();
// Stable stream around 0.
for _ in 0..100 {
kswin.update(0.0).unwrap();
}
assert_eq!(kswin.level(), DriftLevel::None);
// Distribution shifts to mean 5.
for _ in 0..100 {
kswin.update(5.0).unwrap();
}
// KSWIN should detect the distribution change.
assert!(kswin.samples_seen() > 100);Implementations§
Source§impl Kswin
impl Kswin
Sourcepub fn new(config: KswinConfig) -> Result<Self, RillError>
pub fn new(config: KswinConfig) -> Result<Self, RillError>
Create a new KSWIN detector with the given configuration.
Returns an error if:
alphais not in(0, 1).window_sizeis zero.check_intervalis zero.
Sourcepub const fn last_statistic(&self) -> f64
pub const fn last_statistic(&self) -> f64
The last computed KS statistic D (max CDF difference).
Sourcepub const fn last_pvalue(&self) -> f64
pub const fn last_pvalue(&self) -> f64
The last computed p-value from the KS test.
Sourcepub fn reference_window_len(&self) -> usize
pub fn reference_window_len(&self) -> usize
The number of values currently in the reference window.
Sourcepub fn current_window_len(&self) -> usize
pub fn current_window_len(&self) -> usize
The number of values currently in the current window.
Sourcepub const fn config(&self) -> &KswinConfig
pub const fn config(&self) -> &KswinConfig
The configuration of this detector.
Sourcepub fn export_state_v1(&self) -> KswinPortableStateV1
pub fn export_state_v1(&self) -> KswinPortableStateV1
Export the stable portable state.
Sourcepub fn restore_state_v1(
config: KswinConfig,
state: KswinPortableStateV1,
) -> Result<Self, RillError>
pub fn restore_state_v1( config: KswinConfig, state: KswinPortableStateV1, ) -> Result<Self, RillError>
Restore from a validated portable state with exact config matching.