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) -> WeightedMean
fn from_iter<T>(iter: T) -> WeightedMean
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) -> WeightedMean
fn from_iter<T>(iter: T) -> WeightedMean
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 Freeze for WeightedMean
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more