The OpenD Programming Language

interquartileRange

Computes the interquartile range of the input.

By default, this function computes the result using quantile, i.e. result = quantile(x, 0.75) - quantile(x, 0.25). There are also overloads for providing a low value, as in result = quantile(x, 1 - low) - quantile(x, low) and both a low and high value, as in result = quantile(x, high) - quantile(x, low).

For all QuantileAlgo except QuantileAlgo.type1 and QuantileAlgo.type3, 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.

For QuantileAlgo.type1 and QuantileAlgo.type3, the return type is the $(MATHREF sum, elementType) of the input.

Parameters

quantileAlgo

algorithm for calculating quantile (default: QuantileAlgo.type7)

allowModifySlice

controls whether the input is modified in place, default is false

Return Value

The interquartile range of the input.

Examples

Simple example

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

auto x = [3.0, 1.0, 4.0, 2.0, 0.0].sliced;

assert(x.interquartileRange.approxEqual(2.0));
assert(x.interquartileRange(0.25).approxEqual(2.0));
assert(x.interquartileRange(0.25, 0.75).approxEqual(2.0));

Interquartile Range of vector

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

auto x = [1.0, 9.8, 0.2, 8.5, 5.8, 3.5, 4.5, 8.2, 5.2, 5.2,
          2.5, 1.8, 2.2, 3.8, 5.2, 9.2, 6.2, 9.2, 9.2, 8.5].sliced;

assert(x.interquartileRange.approxEqual(5.25));

Interquartile Range of matrix

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

auto x = [
    [1.0, 9.8, 0.2, 8.5, 5.8, 3.5, 4.5, 8.2, 5.2, 5.2],
    [2.5, 1.8, 2.2, 3.8, 5.2, 9.2, 6.2, 9.2, 9.2, 8.5]
].fuse;

assert(x.interquartileRange.approxEqual(5.25));

Allow modification of input

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

auto x = [3.0, 1.0, 4.0, 2.0, 0.0].sliced;
auto x_copy = x.dup;

auto result = x.interquartileRange!(QuantileAlgo.type7, true);
assert(!x.all!approxEqual(x_copy));

Can also set algorithm type

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

auto x = [1.0, 9.8, 0.2, 8.5, 5.8, 3.5, 4.5, 8.2, 5.2, 5.2,
          2.5, 1.8, 2.2, 3.8, 5.2, 9.2, 6.2, 9.2, 9.2, 8.5].sliced;

assert(x.interquartileRange!"type1".approxEqual(6.0));
assert(x.interquartileRange!"type2".approxEqual(5.5));
assert(x.interquartileRange!"type3".approxEqual(6.0));
assert(x.interquartileRange!"type4".approxEqual(6.0));
assert(x.interquartileRange!"type5".approxEqual(5.5));
assert(x.interquartileRange!"type6".approxEqual(5.75));
assert(x.interquartileRange!"type7".approxEqual(5.25));
assert(x.interquartileRange!"type8".approxEqual(5.583333));
assert(x.interquartileRange!"type9".approxEqual(5.5625));

Can also set algorithm or output type

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

auto a = [1, 1e34, 1, -1e34, 0].sliced;

auto x = a * 10_000;

auto result0 = x.interquartileRange!float;
assert(result0.approxEqual(10_000));
static assert(is(typeof(result0) == float));

auto result1 = x.interquartileRange!(float, "type8");
assert(result1.approxEqual(6.666667e37));
static assert(is(typeof(result1) == float));

Support for array

import mir.math.common: approxEqual;

double[] x = [3.0, 1.0, 4.0, 2.0, 0.0];

assert(x.interquartileRange.approxEqual(2.0));

See Also

Meta