use serde_derive::Serialize;
use std::ops::AddAssign;
#[derive(Debug, Serialize, Clone)]
pub struct Stats {
max: f64,
min: f64,
sum: f64,
sum_2: f64,
count: f64,
}
impl Default for Stats {
fn default() -> Self {
use std::f64::*;
Stats {
max: NEG_INFINITY,
min: INFINITY,
sum: 0.,
sum_2: 0.,
count: 0.,
}
}
}
impl AddAssign<(f64, f64)> for Stats {
fn add_assign(&mut self, other: (f64, f64)) {
self.max = self.max.max(other.0);
self.min = self.min.min(other.0);
self.sum += other.0;
self.sum_2 += other.0 * other.0;
self.count += other.1;
}
}
impl AddAssign<f64> for Stats {
fn add_assign(&mut self, other: f64) {
*self += (other, 1.);
}
}
impl AddAssign<&Stats> for Stats {
fn add_assign(&mut self, other: &Stats) {
self.max = self.max.max(other.max);
self.min = self.min.min(other.min);
self.sum += other.sum;
self.sum_2 += other.sum_2;
self.count += other.count;
}
}
impl Stats {
#[inline]
pub fn max(&self) -> f64 {
self.max
}
#[inline]
pub fn min(&self) -> f64 {
self.min
}
#[inline]
pub fn sum(&self) -> f64 {
self.sum
}
#[inline]
pub fn sum_2(&self) -> f64 {
self.sum_2
}
#[inline]
pub fn count(&self) -> f64 {
self.count
}
#[inline]
pub fn mean(&self) -> f64 {
self.sum / self.count
}
#[inline]
pub fn variance(&self) -> f64 {
self.sum_2 / self.count - self.mean().powi(2)
}
#[inline]
pub fn std_deviation(&self) -> f64 {
self.variance().sqrt()
}
}