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) * levelalpha 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
impl EwmaPredictor
Sourcepub fn new(alpha: f64) -> Self
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].
Sourcepub fn level(&self) -> Option<f64>
pub fn level(&self) -> Option<f64>
The current smoothed level, or None before the first sample or when
the level is not finite.
Sourcepub fn sample_count(&self) -> u64
pub fn sample_count(&self) -> u64
Number of accepted samples.
Sourcepub fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>
pub fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>
Incorporate a sample at strictly increasing tick t.
Sourcepub fn state_hash(&self) -> u64
pub fn state_hash(&self) -> u64
Deterministic hash of the smoothing factor and current state.
Trait Implementations§
Source§impl Clone for EwmaPredictor
impl Clone for EwmaPredictor
Source§impl Debug for EwmaPredictor
impl Debug for EwmaPredictor
Source§impl Predictor for EwmaPredictor
impl Predictor for EwmaPredictor
Source§fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>
fn update(&mut self, t: u64, x: f64) -> Result<(), PredictorError>
t and value x.Source§fn predict(&self, t: u64) -> Option<f64>
fn predict(&self, t: u64) -> Option<f64>
t. Must not mutate predictor state.