Struct average::WeightedMean
[−]
[src]
pub struct WeightedMean { /* fields omitted */ }
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());
Methods
impl WeightedMean
[src]
fn new() -> WeightedMean
Create a new weighted and unweighted mean estimator.
fn add(&mut self, sample: f64, weight: f64)
Add a weighted observation sampled from the population.
fn is_empty(&self) -> bool
Determine whether the sample is empty.
Might be a false positive if the sum of weights is zero.
fn sum_weights(&self) -> f64
Return the sum of the weights.
Returns 0 for an empty sample.
fn mean(&self) -> f64
Estimate the weighted mean of the population.
Returns 0 for an empty sample.
fn merge(&mut self, other: &WeightedMean)
Merge another sample into this one.
Example
use average::WeightedMean; 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().map(|&x| x).collect(); let mut avg_left: WeightedMean = left.iter().map(|&x| x).collect(); let avg_right: WeightedMean = right.iter().map(|&x| x).collect(); avg_left.merge(&avg_right); assert!((avg_total.mean() - avg_left.mean()).abs() < 1e-15);
Trait Implementations
impl Debug for WeightedMean
[src]
impl Clone for WeightedMean
[src]
fn clone(&self) -> WeightedMean
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl Default for WeightedMean
[src]
fn default() -> WeightedMean
Returns the "default value" for a type. Read more
impl FromIterator<(f64, f64)> for WeightedMean
[src]
fn from_iter<T>(iter: T) -> WeightedMean where
T: IntoIterator<Item = (f64, f64)>,
T: IntoIterator<Item = (f64, f64)>,
Creates a value from an iterator. Read more