template arguments to pass to mir.math.sum (to compute the mean & variance of the slice)
TODO: merge fast implementation in https://github.com/libmir/numir/pull/22#issuecomment-366526194
Variance of vector
import std.math : approxEqual; import mir.ndslice.slice : sliced; static immutable 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]; assert(approxEqual(x.sliced.var, 54.76563 / 12)); assert(approxEqual(x.sliced.var(false), 54.76563 / 11)); assert(approxEqual(x.sliced.var(true), 54.76563 / 12));
Variance of matrix
import mir.ndslice.slice : sliced; import std.math : approxEqual; static immutable 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]; assert(approxEqual(x.sliced(2, 6).var, 54.76563 / 12)); assert(approxEqual(x.sliced(2, 6).var(false), 54.76563 / 11)); assert(approxEqual(x.sliced(2, 6).var(true), 54.76563 / 12));
Row variance of matrix
import mir.ndslice.slice : sliced; import mir.ndslice.topology : alongDim, byDim, map; import numir : approxEqual; static immutable 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]; static immutable y = [2.092014, 6.722222]; // Use byDim or alongDim with map to compute variance of row/column. assert(approxEqual(x.sliced(2, 6).byDim!0.map!(a => a.var(true)), y.sliced)); assert(approxEqual(x.sliced(2, 6).alongDim!1.map!(a => a.var(true)), y.sliced));
Compute variance tensors along specified dimention of tensors with the negative dim support
import mir.ndslice : alongDim, iota, map, as; import numir : var; /* [[1, 2], [3, 4]] */ auto x = iota([2, 2], 1).as!double; static immutable v0 = [1.0, 1.0]; static immutable v1 = [0.25, 0.25]; /* [[1, 2, 3], [4, 5, 6]] */ auto y = iota([2, 3], 1).as!double; static immutable v2 = [2.25, 2.25, 2.25]; // static foreach (faster; [true, false]) { assert(x.alongDim!0.map!var == v0); assert(x.alongDim!(-2).map!var == v0); assert(x.alongDim!1.map!var == v1); assert(x.alongDim!(-1).map!var == v1); assert(y.alongDim!0.map!var == v2); assert(y.alongDim!(-2).map!var == v2); }
$(MATHREF sum, sum)
Computes the variance of a slice.