The OpenD Programming Language

wmean

Computes the weighted mean of the input.

By default, if F is not floating point type or complex type, then the result will have a double type if F is implicitly convertible to a floating point type or a type for which isComplex!F is true.

Parameters

F

controls type of output

summation

algorithm for calculating sums (default: Summation.appropriate)

assumeWeights

true if weights are assumed to add to 1 (default = AssumeWeights.primary)

G

controls the type of weights

Return Value

The weighted mean of all the elements in the input, must be floating point or complex type

Examples

import mir.complex;
import mir.ndslice.slice: sliced;
import mir.test: should, shouldApprox;
alias C = Complex!double;

wmean([1.0, 2, 3], [1, 2, 3]).shouldApprox == (1.0 + 4.0 + 9.0) / 6;
wmean!true([1.0, 2, 3], [1.0 / 6, 2.0 / 6, 3.0 / 6]).shouldApprox == (1.0 + 4.0 + 9.0) / 6;
wmean([C(1, 3), C(2), C(3)], [1, 2, 3]).should == C(14.0 / 6, 3.0 / 6);

wmean!float([0, 1, 2, 3, 4, 5].sliced(3, 2), [1, 2, 3, 4, 5, 6].sliced(3, 2)).shouldApprox == 70.0 / 21;

static assert(is(typeof(wmean!float([1, 2, 3], [1, 2, 3])) == float));

If weights are not provided, then behaves like mean

import mir.complex;
import mir.ndslice.slice: sliced;
import mir.test: should;
alias C = Complex!double;

wmean([1.0, 2, 3]).should == 2;
wmean([C(1, 3), C(2), C(3)]).should == C(2, 1);

wmean!float([0, 1, 2, 3, 4, 5].sliced(3, 2)).should == 2.5;

static assert(is(typeof(wmean!float([1, 2, 3])) == float));

Weighted mean of vector

import mir.ndslice.slice: sliced;
import mir.ndslice.topology: iota, map;
import mir.test: shouldApprox;

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 w = iota([12], 1);
auto w_SumToOne = w.map!(a => a / 78.0);

x.wmean.shouldApprox == 29.25 / 12;
x.wmean(w).shouldApprox == 203.0 / 78;
x.wmean!true(w_SumToOne).shouldApprox == 203.0 / 78;

Weighted mean of matrix

import mir.ndslice.fuse: fuse;
import mir.ndslice.topology: iota, map;
import mir.test: shouldApprox;

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;
auto w = iota([2, 6], 1);
auto w_SumToOne = w.map!(a => a / 78.0);

x.wmean.shouldApprox == 29.25 / 12;
x.wmean(w).shouldApprox == 203.0 / 78;
x.wmean!true(w_SumToOne).shouldApprox == 203.0 / 78;

Column mean of matrix

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

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;
auto w = iota([2], 1).universal;
auto result = [4.0 / 3, 16.0 / 3, 11.5 / 3, 4.0 / 3, 6.5 / 3, 4.25 / 3];

// Use byDim or alongDim with map to compute mean of row/column.
assert(x.byDim!1.map!(a => a.wmean(w)).all!approxEqual(result));
assert(x.alongDim!0.map!(a => a.wmean(w)).all!approxEqual(result));

// FIXME
// Without using map, computes the mean of the whole slice
// assert(x.byDim!1.wmean(w) == x.sliced.wmean);
// assert(x.alongDim!0.wmean(w) == x.sliced.wmean);

Can also set algorithm or output type

import mir.ndslice.slice: sliced;
import mir.ndslice.topology: repeat, universal;
import mir.test: shouldApprox;

//Set sum algorithm (also for weights) or output type

auto a = [1, 1e100, 1, -1e100].sliced;

auto x = a * 10_000;
auto w1 = [1, 1, 1, 1].sliced;
auto w2 = [0.25, 0.25, 0.25, 0.25].sliced;

x.wmean!"kbn"(w1).shouldApprox == 20_000 / 4;
x.wmean!(true, "kbn")(w2).shouldApprox == 20_000 / 4;
x.wmean!("kbn", true)(w2).shouldApprox == 20_000 / 4;
x.wmean!("kbn", true, "pairwise")(w2).shouldApprox == 20_000 / 4;
x.wmean!(true, "kbn", "pairwise")(w2).shouldApprox == 20_000 / 4;
x.wmean!"kb2"(w1).shouldApprox == 20_000 / 4;
x.wmean!"precise"(w1).shouldApprox == 20_000 / 4;
x.wmean!(double, "precise")(w1).shouldApprox == 20_000.0 / 4;

auto y = uint.max.repeat(3);
y.wmean!ulong([1, 1, 1].sliced.universal).shouldApprox == 12884901885 / 3;

For integral slices, can pass output type as template parameter to ensure output type is correct.

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

auto x = [0, 1, 1, 2, 4, 4,
          2, 7, 5, 1, 2, 0].sliced;
auto w = [1, 2, 3,  4,  5,  6,
          7, 8, 9, 10, 11, 12].sliced;

auto y = x.wmean(w);
y.shouldApprox(1.0e-10) == 204.0 / 78;
static assert(is(typeof(y) == double));

x.wmean!float(w).shouldApprox(1.0e-10) == 204f / 78;

Mean works for complex numbers and other user-defined types (provided they can be converted to a floating point or complex type)

import mir.complex;
import mir.ndslice.slice: sliced;
import mir.test: should;
alias C = Complex!double;

auto x = [C(1.0, 2), C(2, 3), C(3, 4), C(4, 5)].sliced;
auto w = [1, 2, 3, 4].sliced;
x.wmean(w).should == C(3, 4);

Compute weighted mean tensors along specified dimention of tensors

import mir.ndslice.fuse: fuse;
import mir.ndslice.slice: sliced;
import mir.ndslice.topology: alongDim, as, iota, map, universal;
/++
  [[0,1,2],
   [3,4,5]]
 +/
auto x = [
    [0, 1, 2],
    [3, 4, 5]
].fuse.as!double;
auto w = [
    [1, 2, 3],
    [4, 5, 6]
].fuse;
auto w1 = [1, 2].sliced.universal;
auto w2 = [1, 2, 3].sliced;

assert(x.wmean(w) == (70.0 / 21));

auto m0 = [(0.0 + 6.0) / 3, (1.0 + 8.0) / 3, (2.0 + 10.0) / 3];
assert(x.alongDim!0.map!(a => a.wmean(w1)) == m0);
assert(x.alongDim!(-2).map!(a => a.wmean(w1)) == m0);

auto m1 = [(0.0 + 2.0 + 6.0) / 6, (3.0 + 8.0 + 15.0) / 6];
assert(x.alongDim!1.map!(a => a.wmean(w2)) == m1);
assert(x.alongDim!(-1).map!(a => a.wmean(w2)) == m1);

assert(iota(2, 3, 4, 5).as!double.alongDim!0.map!wmean == iota([3, 4, 5], 3 * 4 * 5 / 2));

See Also

$(MATHREF sum, Summation), $(SUB2REF univariate, mean), $(SUB2REF univariate, meanType)

Meta