Skip to main content

RunningStats

Struct RunningStats 

Source
pub struct RunningStats { /* private fields */ }
Available on crate feature statistics only.
Expand description

Running statistics calculator using Welford’s algorithm

Computes mean, variance, standard deviation, min, and max in a single pass with O(1) memory. Uses Welford’s numerically stable algorithm to avoid catastrophic cancellation.

§Example

use flowstats::statistics::RunningStats;

let mut stats = RunningStats::new();

for value in [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0] {
    stats.add(value);
}

assert!((stats.mean() - 5.0).abs() < 0.001);
assert!((stats.variance() - 4.0).abs() < 0.001);
assert!((stats.stddev() - 2.0).abs() < 0.001);
assert_eq!(stats.min(), Some(2.0));
assert_eq!(stats.max(), Some(9.0));

§Distributed Usage

use flowstats::statistics::RunningStats;
use flowstats::traits::Sketch;

let mut stats1 = RunningStats::new();
let mut stats2 = RunningStats::new();

// Worker 1
for v in [1.0, 2.0, 3.0] {
    stats1.add(v);
}

// Worker 2
for v in [4.0, 5.0, 6.0] {
    stats2.add(v);
}

// Merge
stats1.merge(&stats2).unwrap();
assert!((stats1.mean() - 3.5).abs() < 0.001);

Implementations§

Source§

impl RunningStats

Source

pub fn new() -> Self

Create a new empty statistics accumulator

Source

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

Add a value to the statistics

Uses Welford’s online algorithm for numerical stability. NaN values are ignored to prevent poisoning the statistics.

Source

pub fn len(&self) -> u64

Get the number of values

Source

pub fn is_empty(&self) -> bool

Check if empty

Source

pub fn mean(&self) -> f64

Get the mean (average)

Source

pub fn variance(&self) -> f64

Get the population variance

This is the variance assuming the data represents the entire population. Use sample_variance() if the data is a sample.

Source

pub fn sample_variance(&self) -> f64

Get the sample variance

This is the unbiased variance estimator (Bessel’s correction). Use variance() for population variance.

Source

pub fn stddev(&self) -> f64

Get the population standard deviation

Source

pub fn sample_stddev(&self) -> f64

Get the sample standard deviation

Source

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

Get the minimum value

Source

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

Get the maximum value

Source

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

Get the range (max - min)

Source

pub fn sum(&self) -> f64

Get the sum of all values

Source

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

Merge with another RunningStats using parallel algorithm

Uses Chan et al.’s parallel algorithm for combining statistics.

Trait Implementations§

Source§

impl Clone for RunningStats

Source§

fn clone(&self) -> RunningStats

Returns a duplicate 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 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() -> Self

Returns the “default value” for a type. Read more
Source§

impl Sketch for RunningStats

Source§

type Item = f64

The type of item this sketch processes
Source§

fn update(&mut self, item: &Self::Item)

Add an item to the sketch
Source§

fn merge(&mut self, other: &Self) -> Result<(), MergeError>

Merge another sketch into this one Read more
Source§

fn clear(&mut self)

Reset sketch to empty state
Source§

fn size_bytes(&self) -> usize

Memory usage in bytes
Source§

fn count(&self) -> u64

Number of items processed
Source§

fn is_empty(&self) -> bool

Check if sketch is empty

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