The OpenD Programming Language

zscore

Computes the Z-score of the input.

The Z-score is computed by first calculating the mean and standard deviation of the input, by default in one pass, and then scaling the input using those values.

Parameters

varianceAlgo

algorithm for calculating variance (default: VarianceAlgo.hybrid)

summation

algorithm for calculating sums (default: Summation.appropriate)

Return Value

The z-score of the input

Examples

zscore vector

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;

auto x = [1.0, 2, 3, 4, 5, 6].sliced;

assert(x.zscore.all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));
assert(x.zscore(true).all!approxEqual([-1.46385, -0.87831, -0.29277, 0.29277, 0.87831, 1.46385]));

zscore dynamic array

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;

auto x = [1.0, 2, 3, 4, 5, 6];
assert(x.zscore.all!approxEqual([-1.336306, -0.801784, -0.267261, 0.267261, 0.801784, 1.336306]));
assert(x.zscore(true).all!approxEqual([-1.46385, -0.87831, -0.29277, 0.29277, 0.87831, 1.46385]));

zscore matrix

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;

auto x = [
    [1.0, 2, 3], 
    [4.0, 5, 6]
].fuse;

assert(x.zscore.all!approxEqual([[-1.336306, -0.801784, -0.267261], [0.267261, 0.801784, 1.336306]]));
assert(x.zscore(true).all!approxEqual([[-1.46385, -0.87831, -0.29277], [0.29277, 0.87831, 1.46385]]));

Column zscore matrix

import mir.algorithm.iteration: all, equal;
import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;
import mir.ndslice.topology: alongDim, byDim, map;

auto x = [
    [20.0, 100.0, 2000.0],
    [10.0,   5.0,    2.0]
].fuse;

auto result = [
    [ 0.707107,  0.707107,  0.707107],
    [-0.707107, -0.707107, -0.707107]
].fuse;

// Use byDim with map to scale by row/column.
auto xZScoreByDim = x.byDim!1.map!zscore;
auto resultByDim = result.byDim!1;
assert(xZScoreByDim.equal!(equal!approxEqual)(resultByDim));

auto xZScoreAlongDim = x.alongDim!0.map!zscore;
auto resultAlongDim = result.alongDim!0;
assert(xZScoreAlongDim.equal!(equal!approxEqual)(resultAlongDim));

Can control how mean and standardDeviation are calculated and output type

import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
import mir.ndslice.topology: repeat;

//Set sum algorithm or output type
auto a = [1, 1e100, 1, -1e100];

auto x = a.sliced * 10_000;

auto result = [6.123724e-101, 1.224745, 6.123724e-101, -1.224745].sliced;

assert(x.zscore!("online", "kbn").all!approxEqual(result));
assert(x.zscore!("online", "kb2").all!approxEqual(result));
assert(x.zscore!("online", "precise").all!approxEqual(result));
assert(x.zscore!(double, "online", "precise").all!approxEqual(result));

auto y = [uint.max, uint.max / 2, uint.max / 3].sliced;
assert(y.zscore!ulong.all!approxEqual([1.120897, -0.320256, -0.800641]));

See Also

scale, $(MATHREF stat, mean), $(MATHREF stat, standardDeviation), $(MATHREF stat, variance)

Meta