Struct stats::OnlineStats

source ·
pub struct OnlineStats { /* private fields */ }
Expand description

Online state for computing mean, variance and standard deviation.

Implementations§

Create initial state.

Population size, variance and mean are set to 0.

Examples found in repository?
src/online.rs (line 165)
164
165
166
167
168
    fn from_iter<I: IntoIterator<Item = T>>(it: I) -> OnlineStats {
        let mut v = OnlineStats::new();
        v.extend(it);
        v
    }

Initializes variance from a sample.

Return the current mean.

Examples found in repository?
src/online.rs (line 158)
157
158
159
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:.10} +/- {:.10}", self.mean(), self.stddev())
    }

Return the current standard deviation.

Examples found in repository?
src/online.rs (line 158)
157
158
159
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:.10} +/- {:.10}", self.mean(), self.stddev())
    }

Return the current variance.

Examples found in repository?
src/online.rs (line 74)
73
74
75
    pub fn stddev(&self) -> f64 {
        self.variance().sqrt()
    }

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);
        }
    }

Add a new NULL value to the population.

This increases the population size by 1.

Returns the number of data points.

Returns if empty.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Merges the value other into self.
Merges the values in the iterator into self.
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.