The OpenD Programming Language

moment

Calculates the n-th moment of the input.

Members

Functions

moment
stdevType!Range moment(Range r)
moment
stdevType!T moment(T[] ar)

Parameters

N

controls n-th standardized moment

momentAlgo

type of moment to be calculated

summation

algorithm for calculating sums (default: Summation.appropriate)

Return Value

The n-th moment of the input, must be floating point or complex type

Examples

Basic implementation

import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;

assert(moment!(1, "raw")([1.0, 2, 3]).approxEqual(6.0 / 3));
assert(moment!(2, "raw")([1.0, 2, 3]).approxEqual(14.0 / 3));
assert(moment!(3, "raw")([1.0, 2, 3]).approxEqual(36.0 / 3));
assert(moment!(4, "raw")([1.0, 2, 3]).approxEqual(98.0 / 3));

assert(moment!(1, "central")([1.0, 2, 3]).approxEqual(0.0 / 3));
assert(moment!(2, "central")([1.0, 2, 3]).approxEqual(2.0 / 3));
assert(moment!(3, "central")([1.0, 2, 3]).approxEqual(0.0 / 3));
assert(moment!(4, "central")([1.0, 2, 3]).approxEqual(2.0 / 3));

assert(moment!(1, "standardized")([1.0, 2, 3]).approxEqual(0.0));
assert(moment!(2, "standardized")([1.0, 2, 3]).approxEqual(1.0));
assert(moment!(3, "standardized")([1.0, 2, 3]).approxEqual(0.0 / 3));
assert(moment!(4, "standardized")([1.0, 2, 3]).approxEqual(4.5 / 3));

assert(moment!(float, 2, "standardized")([0, 1, 2, 3, 4, 5].sliced(3, 2)).approxEqual(6f / 6));
static assert(is(typeof(moment!(float, 2, "standardized")([1, 2, 3])) == float));

Standardized Moment of vector

import mir.math.common: approxEqual;
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;

assert(x.moment!(3, "standardized").approxEqual(12.000999 / 12));

Standardized Moment of matrix

import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;

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]
].fuse;

assert(x.moment!(3, "standardized").approxEqual(12.000999 / 12));

For integral slices, can pass output type as template parameter to ensure output type is correct. By default, they get converted to double.

import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;

auto x = [0, 1, 1, 2, 4, 4,
          2, 7, 5, 1, 2, 0].sliced;

auto y = x.moment!(3, "standardized");
assert(y.approxEqual(9.666455 / 12));
static assert(is(typeof(y) == double));

assert(x.moment!(float, 3, "standardized").approxEqual(9.666455f / 12));

Arbitrary standardized moment

import mir.math.common: approxEqual;

assert(moment!(3, "standardized")(1.0, 2, 3).approxEqual(0.0 / 3));
assert(moment!(float, 3, "standardized")(1, 2, 3).approxEqual(0f / 3));

Meta