pub struct RunningMoments { /* private fields */ }Expand description
Welford’s online accumulator for the running mean and variance of a stream.
Maintains the count, running mean, and the sum of squared deviations (M2) so
that the mean and the Bessel-corrected sample variance are available after any
number of updates without storing the values themselves. Numerically stable:
it avoids the catastrophic cancellation of the naive “sum of squares minus
square of sum” formula.
§Invariants
The struct holds exactly three scalar fields, so size_of::<RunningMoments>()
is constant regardless of stream length — the bounded-memory guarantee.
§Examples
use stats_claw::streaming::RunningMoments;
let mut m = RunningMoments::new();
for x in [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0] {
m.update(x);
}
// Mean of the eight values is 5.0.
assert!((m.mean() - 5.0).abs() < 1e-12);Implementations§
Source§impl RunningMoments
impl RunningMoments
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an empty accumulator that has consumed no values.
§Returns
A RunningMoments with zero count; Self::mean is 0.0 until the first
Self::update.
Sourcepub fn update(&mut self, x: f64)
pub fn update(&mut self, x: f64)
Folds one observation into the running summary using Welford’s update.
§Arguments
x— the next value of the stream. Any finitef64;NaN/±∞propagate into the running statistics unchanged.
Sourcepub const fn mean(&self) -> f64
pub const fn mean(&self) -> f64
Returns the running arithmetic mean of all values consumed so far.
§Returns
The mean, or 0.0 if no values have been consumed yet.
Sourcepub fn variance(&self) -> f64
pub fn variance(&self) -> f64
Returns the running Bessel-corrected sample variance.
§Returns
The sample variance M2 / (n - 1), or 0.0 when fewer than two values
have been consumed (variance is undefined for a single observation, and
0.0 is the natural, panic-free convention here).
Sourcepub fn std_dev(&self) -> f64
pub fn std_dev(&self) -> f64
Returns the running sample standard deviation (the square root of
Self::variance).
Trait Implementations§
Source§impl Clone for RunningMoments
impl Clone for RunningMoments
Source§fn clone(&self) -> RunningMoments
fn clone(&self) -> RunningMoments
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for RunningMoments
Source§impl Debug for RunningMoments
impl Debug for RunningMoments
Source§impl Default for RunningMoments
impl Default for RunningMoments
Source§fn default() -> Self
fn default() -> Self
Returns an empty accumulator, equivalent to RunningMoments::new.
Source§impl PartialEq for RunningMoments
impl PartialEq for RunningMoments
Source§fn eq(&self, other: &RunningMoments) -> bool
fn eq(&self, other: &RunningMoments) -> bool
self and other values to be equal, and is used by ==.