This commit is contained in:
Andrea Cavalli 2019-12-02 13:03:20 +01:00
parent da03f66ecd
commit 0d2c1ccee7
2 changed files with 10 additions and 10 deletions

View File

@ -1,4 +1,4 @@
const ddof: f64 = 0.; const ddof0: f64 = 0.;
/// Estimate the arithmetic mean and the variance of a sequence of numbers /// Estimate the arithmetic mean and the variance of a sequence of numbers
/// ("population"). /// ("population").
@ -51,7 +51,7 @@ impl Variance0 {
// See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance. // See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
let n = f64::approx_from(self.avg.len()).unwrap(); let n = f64::approx_from(self.avg.len()).unwrap();
self.avg.add_inner(delta_n); self.avg.add_inner(delta_n);
self.sum_2 += delta_n * delta_n * n * (n - ddof); self.sum_2 += delta_n * delta_n * n * (n - ddof0);
} }
/// Determine whether the sample is empty. /// Determine whether the sample is empty.
@ -79,10 +79,10 @@ impl Variance0 {
/// This is an unbiased estimator of the variance of the population. /// This is an unbiased estimator of the variance of the population.
#[inline] #[inline]
pub fn sample_variance(&self) -> f64 { pub fn sample_variance(&self) -> f64 {
if self.avg.len() < 1 + ddof as u64 { if self.avg.len() < 1 + ddof0 as u64 {
return 0.; return 0.;
} }
self.sum_2 / f64::approx_from(self.avg.len() - ddof as u64).unwrap() self.sum_2 / f64::approx_from(self.avg.len() - ddof0 as u64).unwrap()
} }
/// Calculate the population variance of the sample. /// Calculate the population variance of the sample.
@ -91,7 +91,7 @@ impl Variance0 {
#[inline] #[inline]
pub fn population_variance(&self) -> f64 { pub fn population_variance(&self) -> f64 {
let n = self.avg.len(); let n = self.avg.len();
if n < 1 + ddof as u64 { if n < 1 + ddof0 as u64 {
return 0.; return 0.;
} }
self.sum_2 / f64::approx_from(n).unwrap() self.sum_2 / f64::approx_from(n).unwrap()

View File

@ -1,4 +1,4 @@
const ddof: f64 = 1.; const ddof1: f64 = 1.;
/// Estimate the arithmetic mean and the variance of a sequence of numbers /// Estimate the arithmetic mean and the variance of a sequence of numbers
/// ("population"). /// ("population").
@ -51,7 +51,7 @@ impl Variance1 {
// See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance. // See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.
let n = f64::approx_from(self.avg.len()).unwrap(); let n = f64::approx_from(self.avg.len()).unwrap();
self.avg.add_inner(delta_n); self.avg.add_inner(delta_n);
self.sum_2 += delta_n * delta_n * n * (n - ddof); self.sum_2 += delta_n * delta_n * n * (n - ddof1);
} }
/// Determine whether the sample is empty. /// Determine whether the sample is empty.
@ -79,10 +79,10 @@ impl Variance1 {
/// This is an unbiased estimator of the variance of the population. /// This is an unbiased estimator of the variance of the population.
#[inline] #[inline]
pub fn sample_variance(&self) -> f64 { pub fn sample_variance(&self) -> f64 {
if self.avg.len() < 1 + ddof as u64 { if self.avg.len() < 1 + ddof1 as u64 {
return 0.; return 0.;
} }
self.sum_2 / f64::approx_from(self.avg.len() - ddof as u64).unwrap() self.sum_2 / f64::approx_from(self.avg.len() - ddof1 as u64).unwrap()
} }
/// Calculate the population variance of the sample. /// Calculate the population variance of the sample.
@ -91,7 +91,7 @@ impl Variance1 {
#[inline] #[inline]
pub fn population_variance(&self) -> f64 { pub fn population_variance(&self) -> f64 {
let n = self.avg.len(); let n = self.avg.len();
if n < 1 + ddof as u64 { if n < 1 + ddof1 as u64 {
return 0.; return 0.;
} }
self.sum_2 / f64::approx_from(n).unwrap() self.sum_2 / f64::approx_from(n).unwrap()