Skip to main content

EwmaPredictor

Struct EwmaPredictor 

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

An exponentially weighted moving average (EWMA) predictor.

The first sample initialises the level exactly. Each later sample updates it in a fixed order with no fused multiply-add:

level = alpha * x + (1 - alpha) * level

alpha must be finite and in (0, 1]; a larger value reacts faster to new samples. alpha = 1 is equivalent to hold-last prediction. Predictor::predict returns the current level for any future tick, so this is a flat forecast. Unlike the polynomial predictors, EWMA needs no window: every accepted sample contributes to the level.

use predsync_core::predictor::{EwmaPredictor, Predictor};

let mut predictor = EwmaPredictor::new(0.25);
assert_eq!(predictor.predict(1), None);
predictor.update(0, 4.0).unwrap();
predictor.update(1, 8.0).unwrap();
assert_eq!(predictor.predict(9), Some(5.0));
predictor.update(2, 12.0).unwrap();
assert_eq!(predictor.predict(9), Some(6.75));

Implementations§

Source§

impl EwmaPredictor

Source

pub fn new(alpha: f64) -> Self

Create a predictor with smoothing factor alpha.

§Panics

Panics if alpha is not finite or is outside (0, 1].

Source

pub fn alpha(&self) -> f64

The smoothing factor supplied to Self::new.

Source

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

The current smoothed level, or None before the first sample or when the level is not finite.

Source

pub fn sample_count(&self) -> u64

Number of accepted samples.

Source

pub fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>

Incorporate a sample at strictly increasing tick t.

Source

pub fn reset(&mut self)

Clear the predictor to its initial state.

Source

pub fn state_hash(&self) -> u64

Deterministic hash of the smoothing factor and current state.

Source

pub fn predict(&self, _t: u64) -> Option<f64>

The current smoothed level, independent of the requested tick.

Trait Implementations§

Source§

impl Clone for EwmaPredictor

Source§

fn clone(&self) -> Self

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 EwmaPredictor

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Predictor for EwmaPredictor

Source§

fn reset(&mut self)

Reset predictor state to its initial value.
Source§

fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>

Update predictor with a sample at time t and value x.
Source§

fn predict(&self, t: u64) -> Option<f64>

Predict value at time t. Must not mutate predictor state.
Source§

fn state_hash(&self) -> u64

Deterministic hash of predictor state.

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

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

fn try_from(value: U) -> Result<T, !>

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.