Raw moment
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; import mir.stat.transform: center; auto a = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced; auto x = a.center; MomentAccumulator!(double, 2, Summation.naive) v; v.put(x); assert(v.moment.approxEqual(54.76562 / 12)); v.put(4.0); assert(v.moment.approxEqual(70.76562 / 13));
Central moment
import mir.math.common: approxEqual; import mir.ndslice.slice: sliced; import mir.stat.transform: center; auto x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced; MomentAccumulator!(double, 2, Summation.naive) v; auto m = mean(x); v.put(x, m); assert(v.moment.approxEqual(54.76562 / 12));
Standardized moment with scaled calculation
import mir.math.common: approxEqual, sqrt; import mir.ndslice.slice: sliced; auto x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25, 2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced; auto u = VarianceAccumulator!(double, VarianceAlgo.twoPass, Summation.naive)(x); MomentAccumulator!(double, 3, Summation.naive) v; v.put(x, u.mean, u.variance(true).sqrt); assert(v.moment.approxEqual(12.000999 / 12)); assert(v.count == 12);