Struct average::WeightedAverage
[−]
[src]
pub struct WeightedAverage { /* fields omitted */ }
Estimate the weighted arithmetic mean and the weighted variance of a sequence of numbers ("population").
This can be used to estimate the standard error of the weighted mean.
Example
use average::WeightedAverage; let a: WeightedAverage = (1..6).zip(1..6) .map(|(x, w)| (f64::from(x), f64::from(w))).collect(); println!("The weighted average is {} ± {}.", a.mean(), a.error());
Methods
impl WeightedAverage
[src]
fn new() -> WeightedAverage
Create a new weighted average estimator.
fn add(&mut self, sample: f64, weight: f64)
Add a weighted element sampled from the population.
fn is_empty(&self) -> bool
Determine whether the samples are empty.
fn sum_weights(&self) -> f64
Return the sum of the weights.
fn mean(&self) -> f64
Estimate the weighted mean of the population.
fn population_variance(&self) -> f64
Calculate the weighted population variance of the sample.
This is a biased estimator of the weighted variance of the population.
fn sample_variance(&self) -> f64
Calculate the weighted sample variance.
This is an unbiased estimator of the weighted variance of the population.
Note that this will return 0 if the sum of the weights is <= 1.
fn error(&self) -> f64
Estimate the standard error of the weighted mean of the population.
Note that this will return 0 if the sum of the weights is 0. For this estimator, the sum of weights should be larger than 1.
This biased estimator uses the weighted variance and the sum of weights. It considers the weights as (noninteger) counts of how often the sample has been observed, applying the standard formulas to calculate mean, variance and sample size across all "repeats".
fn merge(&mut self, other: &WeightedAverage)
Merge another sample into this one.
Example
use average::WeightedAverage; 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: WeightedAverage = weighted_sequence.iter().map(|&x| x).collect(); let mut avg_left: WeightedAverage = left.iter().map(|&x| x).collect(); let avg_right: WeightedAverage = right.iter().map(|&x| x).collect(); avg_left.merge(&avg_right); assert!((avg_total.mean() - avg_left.mean()).abs() < 1e-15); assert!((avg_total.error() - avg_left.error()).abs() < 1e-15);
Trait Implementations
impl Debug for WeightedAverage
[src]
impl Clone for WeightedAverage
[src]
fn clone(&self) -> WeightedAverage
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 WeightedAverage
[src]
fn default() -> WeightedAverage
Returns the "default value" for a type. Read more
impl FromIterator<(f64, f64)> for WeightedAverage
[src]
fn from_iter<T>(iter: T) -> WeightedAverage where
T: IntoIterator<Item = (f64, f64)>,
T: IntoIterator<Item = (f64, f64)>,
Creates a value from an iterator. Read more