Struct average::WeightedMean
source · pub struct WeightedMean { /* private fields */ }Expand description
Estimate the weighted and unweighted arithmetic mean of a sequence of numbers (“population”).
Example
use average::WeightedMean;
let a: WeightedMean = (1..6).zip(1..6)
.map(|(x, w)| (f64::from(x), f64::from(w))).collect();
println!("The weighted mean is {}.", a.mean());Implementations§
source§impl WeightedMean
impl WeightedMean
sourcepub fn new() -> WeightedMean
pub fn new() -> WeightedMean
Create a new weighted and unweighted mean estimator.
sourcepub fn add(&mut self, sample: f64, weight: f64)
pub fn add(&mut self, sample: f64, weight: f64)
Add an observation sampled from the population.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Determine whether the sample is empty.
Might be a false positive if the sum of weights is zero.
sourcepub fn sum_weights(&self) -> f64
pub fn sum_weights(&self) -> f64
Return the sum of the weights.
Returns 0 for an empty sample.
Trait Implementations§
source§impl Clone for WeightedMean
impl Clone for WeightedMean
source§fn clone(&self) -> WeightedMean
fn clone(&self) -> WeightedMean
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 Debug for WeightedMean
impl Debug for WeightedMean
source§impl Default for WeightedMean
impl Default for WeightedMean
source§fn default() -> WeightedMean
fn default() -> WeightedMean
Returns the “default value” for a type. Read more
source§impl<'de> Deserialize<'de> for WeightedMean
impl<'de> Deserialize<'de> for WeightedMean
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<'a> Extend<&'a (f64, f64)> for WeightedMean
impl<'a> Extend<&'a (f64, f64)> for WeightedMean
source§fn extend<T: IntoIterator<Item = &'a (f64, f64)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a (f64, f64)>>(&mut self, iter: T)
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 Extend<(f64, f64)> for WeightedMean
impl Extend<(f64, f64)> for WeightedMean
source§fn extend<T: IntoIterator<Item = (f64, f64)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (f64, f64)>>(&mut self, iter: T)
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<'a> FromIterator<&'a (f64, f64)> for WeightedMean
impl<'a> FromIterator<&'a (f64, f64)> for WeightedMean
source§fn from_iter<T>(iter: T) -> WeightedMeanwhere
T: IntoIterator<Item = &'a (f64, f64)>,
fn from_iter<T>(iter: T) -> WeightedMeanwhere T: IntoIterator<Item = &'a (f64, f64)>,
Creates a value from an iterator. Read more
source§impl FromIterator<(f64, f64)> for WeightedMean
impl FromIterator<(f64, f64)> for WeightedMean
source§fn from_iter<T>(iter: T) -> WeightedMeanwhere
T: IntoIterator<Item = (f64, f64)>,
fn from_iter<T>(iter: T) -> WeightedMeanwhere T: IntoIterator<Item = (f64, f64)>,
Creates a value from an iterator. Read more
source§impl Merge for WeightedMean
impl Merge for WeightedMean
source§fn merge(&mut self, other: &WeightedMean)
fn merge(&mut self, other: &WeightedMean)
Merge another sample into this one.
Example
use average::{WeightedMean, Merge};
let weighted_sequence: &[(f64, f64)] = &[
(1., 0.1), (2., 0.2), (3., 0.3), (4., 0.4), (5., 0.5),
(6., 0.6), (7., 0.7), (8., 0.8), (9., 0.9)];
let (left, right) = weighted_sequence.split_at(3);
let avg_total: WeightedMean = weighted_sequence.iter().collect();
let mut avg_left: WeightedMean = left.iter().collect();
let avg_right: WeightedMean = right.iter().collect();
avg_left.merge(&avg_right);
assert!((avg_total.mean() - avg_left.mean()).abs() < 1e-15);Auto Trait Implementations§
impl RefUnwindSafe for WeightedMean
impl Send for WeightedMean
impl Sync for WeightedMean
impl Unpin for WeightedMean
impl UnwindSafe for WeightedMean
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere T: ConvApprox<S>,
source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere T: ConvFloat<S>,
source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Cast to integer, truncating Read more
source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Cast to the nearest integer Read more
source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
Cast the floor to an integer Read more