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
impl TimeDecayedMean
Sourcepub fn new(decay: f64) -> Result<Self, RillError>
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.
Sourcepub fn update(&mut self, time: f64, value: f64) -> Result<(), RillError>
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.
Sourcepub fn value(&self) -> Option<f64>
pub fn value(&self) -> Option<f64>
The current decayed mean, or None if no observations have been seen.
Sourcepub const fn weight_total(&self) -> f64
pub const fn weight_total(&self) -> f64
The total weight accumulated so far.
Trait Implementations§
Source§impl Clone for TimeDecayedMean
impl Clone for TimeDecayedMean
Source§fn clone(&self) -> TimeDecayedMean
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TimeDecayedMean
impl RefUnwindSafe for TimeDecayedMean
impl Send for TimeDecayedMean
impl Sync for TimeDecayedMean
impl Unpin for TimeDecayedMean
impl UnsafeUnpin for TimeDecayedMean
impl UnwindSafe for TimeDecayedMean
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more