Struct Ema

Source
pub struct Ema {
    alpha: f64,
    value: Option<f64>,
}
Expand description

Exponential moving average (EMA) over scalar samples.

Stores a single f64 state and updates it in O(1) time with O(1) memory. This is also called an exponentially weighted moving average; older samples are never dropped, but their influence on the average decays exponentially.

With a new sample x, the EMA is updated as: ema = alpha * x + (1.0 - alpha) * ema_prev

alpha ∈ (0, 1) is the weight of the newest sample, whereas 1 - alpha is the weight of the previous EMA value. alpha is called the smoothing factor: larger alpha reacts faster to sample changes, while smaller alpha smooths the average.

You can construct this type from a half-life integer by using Ema::from_half_life. After half_life updates, a sample contributes half as much as when it was added. With half-life H, the most recent N samples contribute 1 - 2^(-N/H) of the total weight (once warmed up). Example: if H = 50, newest 50 samples = 50%, 100 = 75%, 150 = 87.5%, etc.

Fields§

§alpha: f64

Smoothing factor: weight of the newest sample.

§value: Option<f64>

Current EMA value, if any samples have been added.

Implementations§

Source§

impl Ema

Source

pub fn with_half_life_50() -> Self

EMA preset: half-life 50 samples (good default for peer message latency).

Source

pub fn with_half_life_1000() -> Self

EMA preset: half-life 1000 samples (low noise, good for e.g., block validation time).

Source

pub fn from_half_life(half_life: NonZeroU16) -> Self

Constructs an EMA from a half-life measured in samples.

After half_life updates, a sample’s weight is halved. This chooses: alpha = 1 - 2^(-1/half_life).

Source

fn new(alpha: f64) -> Self

Constructs an EMA from alpha in (0, 1). Panics if alpha is outside this range.

Source

pub fn add(&mut self, x: f64)

Adds a sample to the EMA.

Source

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

Returns the current EMA value, or None if no samples have been added yet.

Source

pub fn alpha(&self) -> f64

Returns the alpha smoothing factor used by this EMA.

Trait Implementations§

Source§

impl Clone for Ema

Source§

fn clone(&self) -> Ema

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ema

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Ema

§

impl RefUnwindSafe for Ema

§

impl Send for Ema

§

impl Sync for Ema

§

impl Unpin for Ema

§

impl UnwindSafe for Ema

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> Same for T

Source§

type Output = T

Should always be Self
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.