Struct rolling_stats::Stats

source ·
pub struct Stats<T: Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug> {
    pub min: T,
    pub max: T,
    pub mean: T,
    pub std_dev: T,
    pub count: usize,
    /* private fields */
}
Expand description

A statistics object that continuously calculates min, max, mean, and deviation for tracking time-varying statistics. Utilizes Welford’s Online algorithm. More details on the algorithm can be found at: “https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford’s_online_algorithm”

Example

use rolling_stats::Stats;
use rand_distr::{Distribution, Normal};
use rand::SeedableRng;

type T = f64;

const MEAN: T = 0.0;
const STD_DEV: T = 1.0;
const NUM_SAMPLES: usize = 10_000;
const SEED: u64 = 42;

let mut stats: Stats<T> = Stats::new();
let mut rng = rand::rngs::StdRng::seed_from_u64(SEED); // Seed the RNG for reproducibility
let normal = Normal::<T>::new(MEAN, STD_DEV).unwrap();

// Generate random data
let random_data: Vec<T> = (0..NUM_SAMPLES).map(|_x| normal.sample(&mut rng)).collect();

// Update the stats one by one
random_data.iter().for_each(|v| stats.update(*v));

// Print the stats
println!("{}", stats);
// Output: (avg: 0.00, std_dev: 1.00, min: -3.53, max: 4.11, count: 10000)

Fields§

§min: T

The smallest value seen so far.

§max: T

The largest value seen so far.

§mean: T

The calculated mean (average) of all the values seen so far.

§std_dev: T

The calculated standard deviation of all the values seen so far.

§count: usize

The count of the total values seen.

Implementations§

source§

impl<T> Stats<T>where T: Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug,

source

pub fn new() -> Stats<T>

Creates a new stats object with all values set to their initial states.

source

pub fn update(&mut self, value: T)

Updates the stats object with a new value. The statistics are recalculated using the new value.

source

pub fn merge(&self, other: &Self) -> Self

Merges another stats object into new one. This is done by combining the statistics of the two objects in accordance with the formula provided at: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm

This is useful for combining statistics from multiple threads or processes.

Example
use rolling_stats::Stats;
use rand_distr::{Distribution, Normal};
use rand::SeedableRng;
use rayon::prelude::*;

type T = f64;

const MEAN: T = 0.0;
const STD_DEV: T = 1.0;
const NUM_SAMPLES: usize = 500_000;
const SEED: u64 = 42;
const CHUNK_SIZE: usize = 1000;

let mut stats: Stats<T> = Stats::new();
let mut rng = rand::rngs::StdRng::seed_from_u64(SEED); // Seed the RNG for reproducibility
let normal = Normal::<T>::new(MEAN, STD_DEV).unwrap();

// Generate random data
let random_data: Vec<T> = (0..NUM_SAMPLES).map(|_x| normal.sample(&mut rng)).collect();

// Update the stats in parallel. New stats objects are created for each chunk of data.
let stats: Vec<Stats<T>> = random_data
    .par_chunks(CHUNK_SIZE) // Multi-threaded parallelization via Rayon
    .map(|chunk| {
            let mut s: Stats<T> = Stats::new();
            chunk.iter().for_each(|v| s.update(*v));
            s
     })
    .collect();

// Check if there's more than one stat object
assert!(stats.len() > 1);

// Accumulate the stats using the reduce method. The last stats object is returned.
let merged_stats = stats.into_iter().reduce(|acc, s| acc.merge(&s)).unwrap();

// Print the stats
println!("{}", merged_stats);

// Output: (avg: -0.00, std_dev: 1.00, min: -4.53, max: 4.57, count: 500000)

Trait Implementations§

source§

impl<T: Clone + Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug> Clone for Stats<T>

source§

fn clone(&self) -> Stats<T>

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<T: Debug + Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug> Debug for Stats<T>

source§

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

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

impl<T: Default + Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug> Default for Stats<T>

source§

fn default() -> Stats<T>

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

impl<T> Display for Stats<T>where T: Display + Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug,

Implementing the Display trait for the Stats struct to present the statistics in a readable format.

source§

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

Formats the output of the statistics.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Stats<T>where T: RefUnwindSafe,

§

impl<T> Send for Stats<T>where T: Send,

§

impl<T> Sync for Stats<T>where T: Sync,

§

impl<T> Unpin for Stats<T>where T: Unpin,

§

impl<T> UnwindSafe for Stats<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.