pub struct DriftAwareModel<M, D, A>where
D: DriftDetector,
A: DriftStrategy,{ /* private fields */ }Expand description
A wrapper that feeds prediction errors to a drift detector and applies the strategy’s action when drift is detected.
The wrapper is generic over the model M, detector D, and strategy A
to avoid trait-object overhead and preserve concrete types. See the
module documentation for the full contract.
§Examples
use rill_ml::drift::{
DriftAction, DriftAwareModel, PageHinkley, StaticStrategy,
};
use rill_ml::models::{BaselineConfig, MeanRegressor};
let model = MeanRegressor::new(BaselineConfig::default()).unwrap();
let detector = PageHinkley::default();
let strategy = StaticStrategy::new(DriftAction::NotifyOnly, DriftAction::ResetModel);
let mut aware = DriftAwareModel::new(model, detector, strategy);
// Stable stream: no events.
for i in 0..50 {
let x = [i as f64 * 0.1];
aware.learn(&x, 1.0).unwrap();
}
assert!(aware.events().is_empty());Implementations§
Source§impl<M, D, A> DriftAwareModel<M, D, A>
impl<M, D, A> DriftAwareModel<M, D, A>
Sourcepub fn new(model: M, detector: D, strategy: A) -> Self
pub fn new(model: M, detector: D, strategy: A) -> Self
Create a new drift-aware model with the default event log capacity
(1000 entries).
Sourcepub fn with_max_events(
model: M,
detector: D,
strategy: A,
max_events: usize,
) -> Result<Self, RillError>
pub fn with_max_events( model: M, detector: D, strategy: A, max_events: usize, ) -> Result<Self, RillError>
Create a new drift-aware model with a custom event log capacity.
Returns an error if max_events is zero.
Sourcepub fn predict(&self, features: &[f64]) -> Result<f64, RillError>
pub fn predict(&self, features: &[f64]) -> Result<f64, RillError>
Predict the target for the given features.
This is a pure delegation to the wrapped model’s predict and does
not update any state.
Sourcepub fn learn(&mut self, features: &[f64], target: f64) -> Result<(), RillError>
pub fn learn(&mut self, features: &[f64], target: f64) -> Result<(), RillError>
Update the model using a single labeled sample.
The learning sequence is:
- Predict with the current model (no state change).
- Feed the absolute prediction error to the drift detector.
- Ask the strategy for the response action.
- If the detector reported a change, record a
DriftEvent. - If the action is
ResetModel, reset the wrapped model. - Learn from the sample.
Sourcepub fn events(&self) -> &[DriftEvent]
pub fn events(&self) -> &[DriftEvent]
The recorded drift events, oldest first.
The returned slice is guaranteed to be contiguous. When the event log
exceeds max_events, the oldest entries are dropped.
Sourcepub const fn last_action(&self) -> Option<DriftAction>
pub const fn last_action(&self) -> Option<DriftAction>
The most recent action taken by the wrapper, or None if learn has
not been called yet.
Sourcepub const fn samples_seen(&self) -> u64
pub const fn samples_seen(&self) -> u64
The number of samples processed by learn.
Sourcepub const fn max_events(&self) -> usize
pub const fn max_events(&self) -> usize
The maximum number of retained drift events.
Trait Implementations§
Source§impl<M: Clone, D, A> Clone for DriftAwareModel<M, D, A>
impl<M: Clone, D, A> Clone for DriftAwareModel<M, D, A>
Source§fn clone(&self) -> DriftAwareModel<M, D, A>
fn clone(&self) -> DriftAwareModel<M, D, A>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more