Macro average::define_moments

source ·
macro_rules! define_moments {
    ($name:ident, $MAX_MOMENT:expr) => { ... };
}
Expand description

Define an estimator of all moments up to a number given at compile time.

This uses a general algorithm and is slightly less efficient than the specialized implementations (such as Mean, Variance, Skewness and Kurtosis), but it works for any number of moments >= 4.

(In practise, there is an upper limit due to integer overflow and possibly numerical issues.)

§Example

use average::{define_moments, assert_almost_eq};

define_moments!(Moments4, 4);

let mut a: Moments4 = (1..6).map(f64::from).collect();
assert_eq!(a.len(), 5);
assert_eq!(a.mean(), 3.0);
assert_eq!(a.central_moment(0), 1.0);
assert_eq!(a.central_moment(1), 0.0);
assert_eq!(a.central_moment(2), 2.0);
assert_eq!(a.standardized_moment(0), 5.0);
assert_eq!(a.standardized_moment(1), 0.0);
assert_eq!(a.standardized_moment(2), 1.0);
a.add(1.0);
// skewness
assert_almost_eq!(a.standardized_moment(3), 0.2795084971874741, 1e-15);
// kurtosis
assert_almost_eq!(a.standardized_moment(4), -1.365 + 3.0, 1e-14);