Skip to main content

DriftAwareModel

Struct DriftAwareModel 

Source
pub struct DriftAwareModel<M, D, A>{ /* 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>

Source

pub fn new(model: M, detector: D, strategy: A) -> Self

Create a new drift-aware model with the default event log capacity (1000 entries).

Source

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.

Source

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.

Source

pub fn learn(&mut self, features: &[f64], target: f64) -> Result<(), RillError>

Update the model using a single labeled sample.

The learning sequence is:

  1. Predict with the current model (no state change).
  2. Feed the absolute prediction error to the drift detector.
  3. Ask the strategy for the response action.
  4. If the detector reported a change, record a DriftEvent.
  5. If the action is ResetModel, reset the wrapped model.
  6. Learn from the sample.
Source

pub const fn model(&self) -> &M

Borrow the wrapped model.

Source

pub const fn detector(&self) -> &D

Borrow the wrapped detector.

Source

pub const fn strategy(&self) -> &A

Borrow the wrapped strategy.

Source

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.

Source

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.

Source

pub const fn samples_seen(&self) -> u64

The number of samples processed by learn.

Source

pub const fn max_events(&self) -> usize

The maximum number of retained drift events.

Source

pub fn reset(&mut self)

Reset the model, detector, and event log to their initial states.

The strategy is not reset (it is typically stateless). The max_events capacity is preserved.

Trait Implementations§

Source§

impl<M: Clone, D, A> Clone for DriftAwareModel<M, D, A>

Source§

fn clone(&self) -> DriftAwareModel<M, D, A>

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<M: Debug, D, A> Debug for DriftAwareModel<M, D, A>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M, D, A> Freeze for DriftAwareModel<M, D, A>
where M: Freeze, D: Freeze, A: Freeze,

§

impl<M, D, A> RefUnwindSafe for DriftAwareModel<M, D, A>

§

impl<M, D, A> Send for DriftAwareModel<M, D, A>
where M: Send, D: Send, A: Send,

§

impl<M, D, A> Sync for DriftAwareModel<M, D, A>
where M: Sync, D: Sync, A: Sync,

§

impl<M, D, A> Unpin for DriftAwareModel<M, D, A>
where M: Unpin, D: Unpin, A: Unpin,

§

impl<M, D, A> UnsafeUnpin for DriftAwareModel<M, D, A>

§

impl<M, D, A> UnwindSafe for DriftAwareModel<M, D, A>
where M: UnwindSafe, D: UnwindSafe, A: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V