Struct average::WeightedMeanWithError
source · pub struct WeightedMeanWithError { /* private fields */ }
Expand description
Estimate the weighted and unweighted arithmetic mean and the unweighted variance of a sequence of numbers (“population”).
This can be used to estimate the standard error of the weighted mean.
§Example
use average::WeightedMeanWithError;
let a: WeightedMeanWithError = (1..6).zip(1..6)
.map(|(x, w)| (f64::from(x), f64::from(w))).collect();
println!("The weighted mean is {} ± {}.", a.weighted_mean(), a.error());
Implementations§
source§impl WeightedMeanWithError
impl WeightedMeanWithError
sourcepub fn new() -> WeightedMeanWithError
pub fn new() -> WeightedMeanWithError
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 sum_weights(&self) -> f64
pub fn sum_weights(&self) -> f64
Return the sum of the weights.
Returns 0 for an empty sample.
sourcepub fn sum_weights_sq(&self) -> f64
pub fn sum_weights_sq(&self) -> f64
Return the sum of the squared weights.
Returns 0 for an empty sample.
sourcepub fn weighted_mean(&self) -> f64
pub fn weighted_mean(&self) -> f64
Estimate the weighted mean of the population.
Returns NaN for an empty sample, or if the sum of weights is zero.
sourcepub fn unweighted_mean(&self) -> f64
pub fn unweighted_mean(&self) -> f64
Estimate the unweighted mean of the population.
Returns NaN for an empty sample.
sourcepub fn effective_len(&self) -> f64
pub fn effective_len(&self) -> f64
Calculate the effective sample size.
sourcepub fn population_variance(&self) -> f64
pub fn population_variance(&self) -> f64
Calculate the unweighted population variance of the sample.
This is a biased estimator of the variance of the population.
Returns NaN for an empty sample.
sourcepub fn sample_variance(&self) -> f64
pub fn sample_variance(&self) -> f64
Calculate the unweighted sample variance.
This is an unbiased estimator of the variance of the population.
Returns NaN for samples of size 1 or less.
sourcepub fn variance_of_weighted_mean(&self) -> f64
pub fn variance_of_weighted_mean(&self) -> f64
Estimate the standard error of the weighted mean of the population.
Returns NaN if the sample is empty, or if the sum of weights is zero.
This unbiased estimator assumes that the samples were independently drawn from the same population with constant variance.
sourcepub fn error(&self) -> f64
Available on crate features std
or libm
only.
pub fn error(&self) -> f64
std
or libm
only.Estimate the standard error of the weighted mean of the population.
Returns NaN if the sample is empty, or if the sum of weights is zero.
This unbiased estimator assumes that the samples were independently drawn from the same population with constant variance.
Trait Implementations§
source§impl Clone for WeightedMeanWithError
impl Clone for WeightedMeanWithError
source§fn clone(&self) -> WeightedMeanWithError
fn clone(&self) -> WeightedMeanWithError
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for WeightedMeanWithError
impl Debug for WeightedMeanWithError
source§impl Default for WeightedMeanWithError
impl Default for WeightedMeanWithError
source§fn default() -> WeightedMeanWithError
fn default() -> WeightedMeanWithError
source§impl<'de> Deserialize<'de> for WeightedMeanWithError
impl<'de> Deserialize<'de> for WeightedMeanWithError
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>,
source§impl<'a> Extend<&'a (f64, f64)> for WeightedMeanWithError
impl<'a> Extend<&'a (f64, f64)> for WeightedMeanWithError
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)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl Extend<(f64, f64)> for WeightedMeanWithError
impl Extend<(f64, f64)> for WeightedMeanWithError
source§fn extend<T: IntoIterator<Item = (f64, f64)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (f64, f64)>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<'a> FromIterator<&'a (f64, f64)> for WeightedMeanWithError
impl<'a> FromIterator<&'a (f64, f64)> for WeightedMeanWithError
source§fn from_iter<T>(iter: T) -> WeightedMeanWithError
fn from_iter<T>(iter: T) -> WeightedMeanWithError
source§impl FromIterator<(f64, f64)> for WeightedMeanWithError
impl FromIterator<(f64, f64)> for WeightedMeanWithError
source§fn from_iter<T>(iter: T) -> WeightedMeanWithError
fn from_iter<T>(iter: T) -> WeightedMeanWithError
source§impl Merge for WeightedMeanWithError
impl Merge for WeightedMeanWithError
source§fn merge(&mut self, other: &WeightedMeanWithError)
fn merge(&mut self, other: &WeightedMeanWithError)
Merge another sample into this one.
§Example
use average::{WeightedMeanWithError, 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: WeightedMeanWithError = weighted_sequence.iter().collect();
let mut avg_left: WeightedMeanWithError = left.iter().collect();
let avg_right: WeightedMeanWithError = right.iter().collect();
avg_left.merge(&avg_right);
assert!((avg_total.weighted_mean() - avg_left.weighted_mean()).abs() < 1e-15);
assert!((avg_total.error() - avg_left.error()).abs() < 1e-15);
Auto Trait Implementations§
impl Freeze for WeightedMeanWithError
impl RefUnwindSafe for WeightedMeanWithError
impl Send for WeightedMeanWithError
impl Sync for WeightedMeanWithError
impl Unpin for WeightedMeanWithError
impl UnwindSafe for WeightedMeanWithError
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
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
source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
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>
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>
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