Rename avg to mean
This commit is contained in:
parent
a98e259f5f
commit
16b4f48338
20
src/lib.rs
20
src/lib.rs
@ -15,7 +15,7 @@ use conv::ApproxFrom;
|
|||||||
/// use average::Average;
|
/// use average::Average;
|
||||||
///
|
///
|
||||||
/// let a: Average = (1..6).map(Into::into).collect();
|
/// let a: Average = (1..6).map(Into::into).collect();
|
||||||
/// assert_eq!(a.avg(), 3.0);
|
/// assert_eq!(a.mean(), 3.0);
|
||||||
/// assert_eq!(a.var(), 2.5);
|
/// assert_eq!(a.var(), 2.5);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -42,8 +42,8 @@ impl Average {
|
|||||||
self.v += (x - prev_avg) * (x - self.avg);
|
self.v += (x - prev_avg) * (x - self.avg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the average of the sequence.
|
/// Return the mean of the sequence.
|
||||||
pub fn avg(&self) -> f64 {
|
pub fn mean(&self) -> f64 {
|
||||||
self.avg
|
self.avg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ impl Average {
|
|||||||
self.v / f64::approx_from(self.n - 1).unwrap()
|
self.v / f64::approx_from(self.n - 1).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate the standard error of the average of the sequence.
|
/// Calculate the standard error of the mean of the sequence.
|
||||||
pub fn err(&self) -> f64 {
|
pub fn err(&self) -> f64 {
|
||||||
if self.n == 0 {
|
if self.n == 0 {
|
||||||
return 0.;
|
return 0.;
|
||||||
@ -69,6 +69,12 @@ impl Average {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl core::default::Default for Average {
|
||||||
|
fn default() -> Average {
|
||||||
|
Average::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl core::iter::FromIterator<f64> for Average {
|
impl core::iter::FromIterator<f64> for Average {
|
||||||
fn from_iter<T>(iter: T) -> Average
|
fn from_iter<T>(iter: T) -> Average
|
||||||
where T: IntoIterator<Item=f64>
|
where T: IntoIterator<Item=f64>
|
||||||
@ -103,7 +109,7 @@ mod tests {
|
|||||||
let mut a = Average::new();
|
let mut a = Average::new();
|
||||||
assert_eq!(a.len(), 0);
|
assert_eq!(a.len(), 0);
|
||||||
a.add(1.0);
|
a.add(1.0);
|
||||||
assert_eq!(a.avg(), 1.0);
|
assert_eq!(a.mean(), 1.0);
|
||||||
assert_eq!(a.len(), 1);
|
assert_eq!(a.len(), 1);
|
||||||
assert_eq!(a.var(), 0.0);
|
assert_eq!(a.var(), 0.0);
|
||||||
assert_eq!(a.err(), 0.0);
|
assert_eq!(a.err(), 0.0);
|
||||||
@ -112,7 +118,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn average_simple() {
|
fn average_simple() {
|
||||||
let a: Average = (1..6).map(|x| x.approx().unwrap()).collect();
|
let a: Average = (1..6).map(|x| x.approx().unwrap()).collect();
|
||||||
assert_eq!(a.avg(), 3.0);
|
assert_eq!(a.mean(), 3.0);
|
||||||
assert_eq!(a.len(), 5);
|
assert_eq!(a.len(), 5);
|
||||||
assert_eq!(a.var(), 2.5);
|
assert_eq!(a.var(), 2.5);
|
||||||
assert_almost_eq!(a.err(), f64::sqrt(0.5), 1e-16);
|
assert_almost_eq!(a.err(), f64::sqrt(0.5), 1e-16);
|
||||||
@ -126,7 +132,7 @@ mod tests {
|
|||||||
for _ in 0..1000000 {
|
for _ in 0..1000000 {
|
||||||
a.add(normal.ind_sample(&mut ::rand::thread_rng()));
|
a.add(normal.ind_sample(&mut ::rand::thread_rng()));
|
||||||
}
|
}
|
||||||
assert_almost_eq!(a.avg(), 2.0, 1e-2);
|
assert_almost_eq!(a.mean(), 2.0, 1e-2);
|
||||||
assert_almost_eq!(a.var().sqrt(), 3.0, 1e-2);
|
assert_almost_eq!(a.var().sqrt(), 3.0, 1e-2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user