pub struct Covariance { /* private fields */ }
Expand description
Estimate the arithmetic means and the covariance of a sequence of number pairs (“population”).
Because the variances are calculated as well, this can be used to calculate the Pearson correlation coefficient.
§Example
use average::Covariance;
let a: Covariance = [(1., 5.), (2., 4.), (3., 3.), (4., 2.), (5., 1.)].iter().cloned().collect();
assert_eq!(a.mean_x(), 3.);
assert_eq!(a.mean_y(), 3.);
assert_eq!(a.population_covariance(), -2.0);
assert_eq!(a.sample_covariance(), -2.5);
Implementations§
Source§impl Covariance
impl Covariance
Sourcepub fn new() -> Covariance
pub fn new() -> Covariance
Create a new covariance estimator.
Sourcepub fn population_covariance(&self) -> f64
pub fn population_covariance(&self) -> f64
Calculate the population covariance of the sample.
This is a biased estimator of the covariance of the population.
Returns NaN for an empty sample.
Sourcepub fn sample_covariance(&self) -> f64
pub fn sample_covariance(&self) -> f64
Calculate the sample covariance.
This is an unbiased estimator of the covariance of the population.
Returns NaN for samples of size 1 or less.
Sourcepub fn pearson(&self) -> f64
Available on crate features std
or libm
only.
pub fn pearson(&self) -> f64
std
or libm
only.Calculate the population Pearson correlation coefficient.
Returns NaN for an empty sample.
Sourcepub fn mean_x(&self) -> f64
pub fn mean_x(&self) -> f64
Estimate the mean of the x
population.
Returns NaN for an empty sample.
Sourcepub fn mean_y(&self) -> f64
pub fn mean_y(&self) -> f64
Estimate the mean of the y
population.
Returns NaN for an empty sample.
Sourcepub fn sample_variance_x(&self) -> f64
pub fn sample_variance_x(&self) -> f64
Calculate the sample variance of x
.
This is an unbiased estimator of the variance of the population.
Returns NaN for samples of size 1 or less.
Sourcepub fn population_variance_x(&self) -> f64
pub fn population_variance_x(&self) -> f64
Calculate the population variance of the sample for x
.
This is a biased estimator of the variance of the population.
Returns NaN for an empty sample.
Sourcepub fn sample_variance_y(&self) -> f64
pub fn sample_variance_y(&self) -> f64
Calculate the sample variance of y
.
This is an unbiased estimator of the variance of the population.
Returns NaN for samples of size 1 or less.
Sourcepub fn population_variance_y(&self) -> f64
pub fn population_variance_y(&self) -> f64
Calculate the population variance of the sample for y
.
This is a biased estimator of the variance of the population.
Returns NaN for an empty sample.
Trait Implementations§
Source§impl Clone for Covariance
impl Clone for Covariance
Source§fn clone(&self) -> Covariance
fn clone(&self) -> Covariance
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Covariance
impl Debug for Covariance
Source§impl Default for Covariance
impl Default for Covariance
Source§fn default() -> Covariance
fn default() -> Covariance
Source§impl<'de> Deserialize<'de> for Covariance
impl<'de> Deserialize<'de> for Covariance
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 Covariance
impl<'a> Extend<&'a (f64, f64)> for Covariance
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 Covariance
impl Extend<(f64, f64)> for Covariance
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 Covariance
impl<'a> FromIterator<&'a (f64, f64)> for Covariance
Source§fn from_iter<T>(iter: T) -> Covariance
fn from_iter<T>(iter: T) -> Covariance
Source§impl FromIterator<(f64, f64)> for Covariance
impl FromIterator<(f64, f64)> for Covariance
Source§fn from_iter<T>(iter: T) -> Covariance
fn from_iter<T>(iter: T) -> Covariance
Source§impl Merge for Covariance
impl Merge for Covariance
Source§fn merge(&mut self, other: &Covariance)
fn merge(&mut self, other: &Covariance)
Merge another sample into this one.
§Example
use average::{Covariance, Merge};
let sequence: &[(f64, f64)] = &[(1., 2.), (3., 4.), (5., 6.), (7., 8.), (9., 10.)];
let (left, right) = sequence.split_at(3);
let cov_total: Covariance = sequence.iter().collect();
let mut cov_left: Covariance = left.iter().collect();
let cov_right: Covariance = right.iter().collect();
cov_left.merge(&cov_right);
assert_eq!(cov_total.population_covariance(), cov_left.population_covariance());
Auto Trait Implementations§
impl Freeze for Covariance
impl RefUnwindSafe for Covariance
impl Send for Covariance
impl Sync for Covariance
impl Unpin for Covariance
impl UnwindSafe for Covariance
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> 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>
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