Skip to main content

RunningStats

Struct RunningStats 

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

Numerically-stable online mean and variance using Welford’s algorithm.

Each call to push updates the running mean and the running sum-of-squared-deviations in O(1) time with no stored samples. The result is numerically more stable than the naive two-pass formula when values span several orders of magnitude.

§References

Welford, B. P. (1962). Note on a method for calculating corrected sums of squares and products. Technometrics, 4(3), 419–420.

Implementations§

Source§

impl RunningStats

Source

pub fn new() -> Self

Creates an empty accumulator.

Source

pub fn push(&mut self, value: f64)

Pushes a new sample and updates all running statistics.

Silently ignores NaN values to avoid poisoning the accumulator.

Source

pub fn count(&self) -> u64

Returns the number of samples pushed.

Source

pub fn is_empty(&self) -> bool

Returns true if no samples have been pushed.

Source

pub fn mean(&self) -> f64

Returns the current running mean, or 0.0 if empty.

Source

pub fn variance(&self) -> f64

Returns the sample variance (divides by n − 1), or 0.0 for < 2 samples.

Source

pub fn population_variance(&self) -> f64

Returns the population variance (divides by n), or 0.0 if empty.

Source

pub fn stddev(&self) -> f64

Returns the sample standard deviation, or 0.0 for < 2 samples.

Source

pub fn population_stddev(&self) -> f64

Returns the population standard deviation, or 0.0 if empty.

Source

pub fn min(&self) -> f64

Returns the minimum value seen, or f64::INFINITY if empty.

Source

pub fn max(&self) -> f64

Returns the maximum value seen, or f64::NEG_INFINITY if empty.

Source

pub fn range(&self) -> f64

Returns the range (max − min), or 0.0 if fewer than two samples.

Source

pub fn cv(&self) -> f64

Returns the coefficient of variation (stddev / |mean|) as a fraction.

Returns f64::NAN when the mean is zero.

Source

pub fn merge(&mut self, other: &Self)

Merges another RunningStats into self using Chan’s parallel algorithm.

After merging, self contains the combined statistics as though all samples from both accumulators had been pushed into a single one.

§References

Chan, T. F., Golub, G. H., & LeVeque, R. J. (1979). Updating Formulae and a Pairwise Algorithm for Computing Sample Variances.

Source

pub fn reset(&mut self)

Resets the accumulator to its initial empty state.

Trait Implementations§

Source§

impl Clone for RunningStats

Source§

fn clone(&self) -> RunningStats

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 RunningStats

Source§

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

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

impl Default for RunningStats

Source§

fn default() -> RunningStats

Returns the “default value” for a type. 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<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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more