use core;
#[derive(Debug, Clone)]
pub struct WeightedAverage {
weight_sum: f64,
avg: f64,
v: f64,
}
impl WeightedAverage {
pub fn new() -> WeightedAverage {
WeightedAverage { weight_sum: 0., avg: 0., v: 0. }
}
pub fn add(&mut self, sample: f64, weight: f64) {
self.weight_sum += weight;
let prev_avg = self.avg;
self.avg = prev_avg + (weight / self.weight_sum) * (sample - prev_avg);
self.v += weight * (sample - prev_avg) * (sample - self.avg);
}
pub fn is_empty(&self) -> bool {
self.weight_sum == 0. && self.v == 0. && self.avg == 0.
}
pub fn sum_weights(&self) -> f64 {
self.weight_sum
}
pub fn mean(&self) -> f64 {
self.avg
}
pub fn population_variance(&self) -> f64 {
if self.is_empty() {
0.
} else {
self.v / self.weight_sum
}
}
pub fn sample_variance(&self) -> f64 {
if self.weight_sum <= 1. {
0.
} else {
self.v / (self.weight_sum - 1.0)
}
}
pub fn error(&self) -> f64 {
if self.weight_sum == 0. {
return 0.;
}
let variance = if self.weight_sum <= 1. {
self.population_variance()
} else {
self.sample_variance()
};
(variance / self.weight_sum).sqrt()
}
pub fn merge(&mut self, other: &WeightedAverage) {
let delta = other.avg - self.avg;
let total_weight_sum = self.weight_sum + other.weight_sum;
self.avg = (self.weight_sum * self.avg + other.weight_sum * other.avg)
/ (self.weight_sum + other.weight_sum);
self.v += other.v + delta*delta * self.weight_sum * other.weight_sum
/ total_weight_sum;
self.weight_sum = total_weight_sum;
}
}
impl core::default::Default for WeightedAverage {
fn default() -> WeightedAverage {
WeightedAverage::new()
}
}
impl core::iter::FromIterator<(f64, f64)> for WeightedAverage {
fn from_iter<T>(iter: T) -> WeightedAverage
where T: IntoIterator<Item=(f64, f64)>
{
let mut a = WeightedAverage::new();
for (i, w) in iter {
a.add(i, w);
}
a
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn merge_unweighted() {
let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];
for mid in 0..sequence.len() {
let (left, right) = sequence.split_at(mid);
let avg_total: WeightedAverage = sequence.iter().map(|x| (*x, 1.)).collect();
let mut avg_left: WeightedAverage = left.iter().map(|x| (*x, 1.)).collect();
let avg_right: WeightedAverage = right.iter().map(|x| (*x, 1.)).collect();
avg_left.merge(&avg_right);
assert_eq!(avg_total.weight_sum, avg_left.weight_sum);
assert_eq!(avg_total.avg, avg_left.avg);
assert_eq!(avg_total.v, avg_left.v);
}
}
#[test]
fn merge_weighted() {
let sequence: &[(f64, f64)] = &[
(1., 0.1), (2., 0.2), (3., 0.3), (4., 0.4), (5., 0.5),
(6., 0.6), (7., 0.7), (8., 0.8), (9., 0.)];
for mid in 0..sequence.len() {
let (left, right) = sequence.split_at(mid);
let avg_total: WeightedAverage = sequence.iter().map(|&(x, w)| (x, w)).collect();
let mut avg_left: WeightedAverage = left.iter().map(|&(x, w)| (x, w)).collect();
let avg_right: WeightedAverage = right.iter().map(|&(x, w)| (x, w)).collect();
avg_left.merge(&avg_right);
assert_almost_eq!(avg_total.weight_sum, avg_left.weight_sum, 1e-15);
assert_almost_eq!(avg_total.avg, avg_left.avg, 1e-15);
assert_almost_eq!(avg_total.v, avg_left.v, 1e-14);
}
}
}