Macro average::concatenate [] [src]

macro_rules! concatenate {
    ( $name:ident, $([$estimator:ident, $statistic:ident]),+ ) => { ... };
    ( $name:ident, $( [$estimator:ident, $field:ident, $($statistic:ident),+] ),+ ) => { ... };
}

Concatenate several iterative estimators into one.

$name is the name of the new struct. $statistic is the name of a statistic and must exist as a method of the corresponding type $estimator. $estimator must have an add method for adding new observations to the sample (taking an f64 as an argument). It must also implement Default.

If the short syntax is used, the fields will be named $statistic. Use the long syntax and $field to give them explicit names. The long syntax also supports calculating several statistics from one estimator.

For moments, only an estimator for the highest moment should be used and reused for the lower moments (see the example below).

The following methods will be implemented: new, add, $statistic.

The following traits will be implemented: Default, FromIterator<f64>.

Examples

use average::{Min, Max, Estimate};

concatenate!(MinMax, [Min, min], [Max, max]);

let s: MinMax = (1..6).map(Into::into).collect();

assert_eq!(s.min(), 1.0);
assert_eq!(s.max(), 5.0);

The generated code looks roughly like this:

struct MinMax {
    min: Min,
    max: Max,
}

impl MinMax {
    pub fn new() -> MinMax {
        MinMax { min: Min::default(), max: Max::default() }
    }

    pub fn add(&mut self, x: f64) {
        self.min.add(x);
        self.max.add(x);
    }

    pub fn min(&self) -> f64 {
        self.min.min()
    }

    pub fn max(&self) -> f64 {
        self.max.max()
    }
}

If you want to calculate the mean, variance and the median in one pass, you can do the following:

use average::{Variance, Quantile, Estimate};

concatenate!(Estimator,
    [Variance, variance, mean, sample_variance],
    [Quantile, quantile, quantile]);