Skip to main content

TimeDecayedMean

Struct TimeDecayedMean 

Source
pub struct TimeDecayedMean { /* private fields */ }
Expand description

An exponentially time-decayed weighted mean.

Each observation (t_i, v_i) contributes v_i · exp(-decay · (t_now - t_i)) to the weighted sum. The mean is Σ(v_i · w_i) / Σ(w_i). Older observations receive exponentially smaller weights, making the statistic responsive to recent changes.

Time complexity per update: O(1). Space complexity: O(1).

§Examples

use rill_ml::drift::TimeDecayedMean;

let mut m = TimeDecayedMean::new(0.1).unwrap();
m.update(0.0, 10.0).unwrap();
m.update(1.0, 20.0).unwrap();
m.update(2.0, 30.0).unwrap();
// The mean should be closer to 30 than to the simple average (20)
// because recent observations are weighted higher.
let v = m.value().unwrap();
assert!(v > 20.0, "recent data should dominate: {}", v);

Implementations§

Source§

impl TimeDecayedMean

Source

pub fn new(decay: f64) -> Result<Self, RillError>

Create a new time-decayed mean with the given decay rate.

decay must be finite and strictly positive. Larger values cause faster forgetting.

Source

pub const fn decay(&self) -> f64

The configured decay rate.

Source

pub fn update(&mut self, time: f64, value: f64) -> Result<(), RillError>

Update with a new observation at time t with value v.

t must be finite and must not decrease (i.e., t >= last_time). v must be finite.

Source

pub fn value(&self) -> Option<f64>

The current decayed mean, or None if no observations have been seen.

Source

pub const fn weight_total(&self) -> f64

The total weight accumulated so far.

Source

pub const fn last_time(&self) -> Option<f64>

The last observation’s timestamp, or None if no observations yet.

Source

pub fn reset(&mut self)

Reset to the initial (no-data) state.

Trait Implementations§

Source§

impl Clone for TimeDecayedMean

Source§

fn clone(&self) -> TimeDecayedMean

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 TimeDecayedMean

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