rill_ml/drift/mod.rs
1//! Drift detection and adaptation.
2//!
3//! This module provides bounded-memory drift detection algorithms, a decoupled
4//! action/strategy layer, decay-aware learning utilities, and a
5//! [`DriftAwareModel`] wrapper that integrates drift detection into the
6//! predict → learn loop.
7//!
8//! ## Overview
9//!
10//! - **Detectors**: [`PageHinkley`], [`Adwin`], [`Kswin`] — each implements
11//! the [`DriftDetector`] trait and reports a [`DriftLevel`]
12//! (None / Warning / Drift).
13//! - **Actions**: [`DriftAction`] describes what to do when drift is detected.
14//! [`DriftStrategy`] maps a level to an action, keeping detection and
15//! response decoupled.
16//! - **Decay learning**: [`TimeDecayedMean`], [`LearningRateScheduler`],
17//! [`FixedWindowBuffer`] — utilities for adapting to non-stationary streams.
18//! - **Wrapper**: [`DriftAwareModel`] wraps a model + detector + strategy and
19//! automatically responds to drift during `learn`. It does **not**
20//! auto-reset the model by default.
21//!
22//! ## Quick start
23//!
24//! ```rust
25//! use rill_ml::drift::{DriftAction, DriftLevel, PageHinkley, StaticStrategy};
26//! use rill_ml::drift::{DriftDetector, DriftStrategy};
27//!
28//! let mut detector = PageHinkley::default();
29//! let strategy = StaticStrategy::new(
30//! DriftAction::ReduceConfidence,
31//! DriftAction::ResetModel,
32//! );
33//!
34//! // Feed a stable stream.
35//! for _ in 0..100 {
36//! detector.update(0.0).unwrap();
37//! }
38//! assert_eq!(detector.level(), DriftLevel::None);
39//!
40//! // Introduce a sudden shift.
41//! for _ in 0..50 {
42//! detector.update(5.0).unwrap();
43//! }
44//! assert!(detector.detected());
45//! let action = strategy.decide(detector.level(), detector.samples_seen());
46//! assert_eq!(action, DriftAction::ResetModel);
47//! ```
48//!
49//! [`DriftAwareModel`]: crate::drift::aware_model::DriftAwareModel
50//! [`PageHinkley`]: crate::drift::page_hinkley::PageHinkley
51//! [`Adwin`]: crate::drift::adwin::Adwin
52//! [`Kswin`]: crate::drift::kswin::Kswin
53//! [`TimeDecayedMean`]: crate::drift::decay::TimeDecayedMean
54//! [`LearningRateScheduler`]: crate::drift::decay::LearningRateScheduler
55//! [`FixedWindowBuffer`]: crate::drift::decay::FixedWindowBuffer
56
57pub mod action;
58pub mod adwin;
59pub mod aware_model;
60pub mod decay;
61pub mod detector;
62pub mod kswin;
63pub mod page_hinkley;
64pub mod strategy;
65
66pub use action::{DriftAction, DriftEvent};
67pub use adwin::{Adwin, AdwinConfig};
68pub use aware_model::DriftAwareModel;
69pub use decay::{FixedWindowBuffer, LearningRateScheduler, TimeDecayedMean};
70pub use detector::{DriftDetector, DriftLevel};
71pub use kswin::{Kswin, KswinConfig};
72pub use page_hinkley::{PageHinkley, PageHinkleyConfig};
73pub use strategy::{DriftStrategy, StaticStrategy};