Struct average::Covariance
source · 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());