pub struct RunningStats { /* private fields */ }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
impl RunningStats
Sourcepub fn add(&mut self, value: f64)
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.
Sourcepub fn variance(&self) -> f64
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.
Sourcepub fn sample_variance(&self) -> f64
pub fn sample_variance(&self) -> f64
Get the sample variance
This is the unbiased variance estimator (Bessel’s correction).
Use variance() for population variance.
Sourcepub fn sample_stddev(&self) -> f64
pub fn sample_stddev(&self) -> f64
Get the sample standard deviation
Sourcepub fn merge_stats(&mut self, other: &Self)
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
impl Clone for RunningStats
Source§fn clone(&self) -> RunningStats
fn clone(&self) -> RunningStats
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more