Struct 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

Source

pub fn new() -> WeightedMean

Create a new weighted and unweighted mean estimator.

Source

pub fn add(&mut self, sample: f64, weight: f64)

Add an observation sampled from the population.

Source

pub fn is_empty(&self) -> bool

Determine whether the sample is empty.

Might be a false positive if the sum of weights is zero.

Source

pub fn sum_weights(&self) -> f64

Return the sum of the weights.

Returns 0 for an empty sample.

Source

pub fn mean(&self) -> f64

Estimate the weighted mean of the population.

Returns NaN for an empty sample, or if the sum of weights is zero.

Trait Implementations§

Source§

impl Clone for WeightedMean

Source§

fn clone(&self) -> WeightedMean

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WeightedMean

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WeightedMean

Source§

fn default() -> WeightedMean

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for WeightedMean

Source§

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

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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

Source§

fn from_iter<T>(iter: T) -> WeightedMean
where T: IntoIterator<Item = &'a (f64, f64)>,

Creates a value from an iterator. Read more
Source§

impl FromIterator<(f64, f64)> for WeightedMean

Source§

fn from_iter<T>(iter: T) -> WeightedMean
where T: IntoIterator<Item = (f64, f64)>,

Creates a value from an iterator. Read more
Source§

impl Merge for WeightedMean

Source§

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);
Source§

impl Serialize for WeightedMean

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<S, T> Cast<T> for S
where T: Conv<S>,

Source§

fn cast(self) -> T

Cast from Self to T Read more
Source§

fn try_cast(self) -> Result<T, Error>

Try converting from Self to T Read more
Source§

impl<S, T> CastApprox<T> for S
where T: ConvApprox<S>,

Source§

fn try_cast_approx(self) -> Result<T, Error>

Try approximate conversion from Self to T Read more
Source§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
Source§

impl<S, T> CastFloat<T> for S
where T: ConvFloat<S>,

Source§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
Source§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
Source§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
Source§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
Source§

fn try_cast_trunc(self) -> Result<T, Error>

Try converting to integer with truncation Read more
Source§

fn try_cast_nearest(self) -> Result<T, Error>

Try converting to the nearest integer Read more
Source§

fn try_cast_floor(self) -> Result<T, Error>

Try converting the floor to an integer Read more
Source§

fn try_cast_ceil(self) -> Result<T, Error>

Try convert the ceiling to an integer Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,