Struct stats::OnlineStats
source · pub struct OnlineStats { /* private fields */ }
Expand description
Online state for computing mean, variance and standard deviation.
Implementations§
source§impl OnlineStats
impl OnlineStats
sourcepub fn new() -> OnlineStats
pub fn new() -> OnlineStats
Create initial state.
Population size, variance and mean are set to 0
.
sourcepub fn from_slice<T: ToPrimitive>(samples: &[T]) -> OnlineStats
pub fn from_slice<T: ToPrimitive>(samples: &[T]) -> OnlineStats
Initializes variance from a sample.
sourcepub fn add<T: ToPrimitive>(&mut self, sample: T)
pub fn add<T: ToPrimitive>(&mut self, sample: T)
Add a new sample.
Examples found in repository?
src/online.rs (line 103)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
pub fn add_null(&mut self) {
self.add(0usize);
}
/// Returns the number of data points.
#[inline]
#[must_use]
pub const fn len(&self) -> usize {
self.size as usize
}
/// Returns if empty.
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.size == 0
}
}
impl Commute for OnlineStats {
#[inline]
fn merge(&mut self, v: OnlineStats) {
// Taken from: https://en.wikipedia.org/wiki/Standard_deviation#Combining_standard_deviations
let (s1, s2) = (self.size as f64, v.size as f64);
let meandiffsq = (self.mean - v.mean) * (self.mean - v.mean);
self.size += v.size;
//self.mean = ((s1 * self.mean) + (s2 * v.mean)) / (s1 + s2);
/*
below is the fused multiply add version of the statement above
its more performant as we're taking advantage of a CPU instruction
Note that fma appears to have issues on macOS per the flaky CI tests
and it appears that clippy::suboptimal_flops lint that suggested
this made a false-positive recommendation
https://github.com/rust-lang/rust-clippy/issues/10003
leaving on for now, as qsv is primarily optimized for Linux targets */
self.mean = s1.mul_add(self.mean, s2 * v.mean) / (s1 + s2);
self.q += v.q + meandiffsq * s1 * s2 / (s1 + s2);
}
}
impl Default for OnlineStats {
fn default() -> OnlineStats {
OnlineStats {
size: 0,
mean: 0.0,
q: 0.0,
}
}
}
impl fmt::Debug for OnlineStats {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:.10} +/- {:.10}", self.mean(), self.stddev())
}
}
impl<T: ToPrimitive> FromIterator<T> for OnlineStats {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> OnlineStats {
let mut v = OnlineStats::new();
v.extend(it);
v
}
}
impl<T: ToPrimitive> Extend<T> for OnlineStats {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, it: I) {
for sample in it {
self.add(sample);
}
}
Trait Implementations§
source§impl Clone for OnlineStats
impl Clone for OnlineStats
source§fn clone(&self) -> OnlineStats
fn clone(&self) -> OnlineStats
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Commute for OnlineStats
impl Commute for OnlineStats
source§impl Debug for OnlineStats
impl Debug for OnlineStats
source§impl Default for OnlineStats
impl Default for OnlineStats
source§fn default() -> OnlineStats
fn default() -> OnlineStats
Returns the “default value” for a type. Read more
source§impl<T: ToPrimitive> Extend<T> for OnlineStats
impl<T: ToPrimitive> Extend<T> for OnlineStats
source§fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, it: I)
Extends a collection with the contents of an iterator. Read more
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
source§impl<T: ToPrimitive> FromIterator<T> for OnlineStats
impl<T: ToPrimitive> FromIterator<T> for OnlineStats
source§fn from_iter<I: IntoIterator<Item = T>>(it: I) -> OnlineStats
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> OnlineStats
Creates a value from an iterator. Read more