Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.stat.distribution.categorical

This module contains algorithms for the Categorical Distribution.
License:
Authors:
John Michael Hall
pure nothrow @nogc @safe elementType!(Slice!(Iterator, 1, kind)) categoricalPMF(Iterator, SliceKind kind)(const size_t x, scope const Slice!(Iterator, 1, kind) p)
if (isFloatingPoint!(elementType!(Slice!(Iterator, 1, kind))));

pure nothrow @nogc @safe T categoricalPMF(T)(const size_t x, scope const T[] p...)
if (isFloatingPoint!T);
Computes the Categorical probability mass function (PMF).
Parameters:
size_t x value to evaluate PMF
Slice!(Iterator, 1, kind) p slice containing the probability associated with the Categorical Distribution
Examples:
import mir.ndslice.slice: sliced;
import mir.test: shouldApprox;

static immutable x = [0.1, 0.5, 0.4];
auto p = x.sliced;

0.categoricalPMF(p).shouldApprox == 0.1;
1.categoricalPMF(p).shouldApprox == 0.5;
2.categoricalPMF(p).shouldApprox == 0.4;
Examples:
Can also use dynamic array
import mir.test: shouldApprox;

double[] p = [0.1, 0.5, 0.4];

0.categoricalPMF(p).shouldApprox == 0.1;
1.categoricalPMF(p).shouldApprox == 0.5;
2.categoricalPMF(p).shouldApprox == 0.4;
Examples:
import mir.math.sum: sum;
import mir.ndslice.slice: sliced;
import mir.test: shouldApprox;

static immutable x = [1.0, 5, 4];
auto p = x.sliced;
auto q = p / sum(p);

0.categoricalPMF(q).shouldApprox == 0.1;
1.categoricalPMF(q).shouldApprox == 0.5;
2.categoricalPMF(q).shouldApprox == 0.4;
pure nothrow @nogc @safe sumType!(Slice!(Iterator, 1, kind)) categoricalCDF(Iterator, SliceKind kind)(const size_t x, scope const Slice!(Iterator, 1, kind) p)
if (isFloatingPoint!(elementType!(Slice!(Iterator, 1, kind))));

pure nothrow @nogc @safe T categoricalCDF(T)(const size_t x, scope const T[] p...)
if (isFloatingPoint!T);
Computes the Categorical cumulative distribution function (CDF).
Parameters:
size_t x value to evaluate CDF
Slice!(Iterator, 1, kind) p slice containing the probability associated with the Categorical Distribution
Examples:
import mir.ndslice.slice: sliced;
import mir.test: shouldApprox;

static immutable x = [0.1, 0.5, 0.4];
auto p = x.sliced;

0.categoricalCDF(p).shouldApprox == 0.1;
1.categoricalCDF(p).shouldApprox == 0.6;
2.categoricalCDF(p).shouldApprox == 1.0;
Examples:
Can also use dynamic array
import mir.test: shouldApprox;

double[] p = [0.1, 0.5, 0.4];

0.categoricalCDF(p).shouldApprox == 0.1;
1.categoricalCDF(p).shouldApprox == 0.6;
2.categoricalCDF(p).shouldApprox == 1.0;
pure nothrow @nogc @safe sumType!(Slice!(Iterator, 1, kind)) categoricalCCDF(Iterator, SliceKind kind)(const size_t x, scope const Slice!(Iterator, 1, kind) p)
if (isFloatingPoint!(elementType!(Slice!(Iterator, 1, kind))));

pure nothrow @nogc @safe T categoricalCCDF(T)(const size_t x, scope const T[] p...)
if (isFloatingPoint!T);
Computes the Categorical complementary cumulative distribution function (CCDF).
Parameters:
size_t x value to evaluate CCDF
Slice!(Iterator, 1, kind) p slice containing the probability associated with the Categorical Distribution
Examples:
import mir.ndslice.slice: sliced;
import mir.test: shouldApprox;

static immutable x = [0.1, 0.5, 0.4];
auto p = x.sliced;

0.categoricalCCDF(p).shouldApprox == 1.0;
1.categoricalCCDF(p).shouldApprox == 0.9;
2.categoricalCCDF(p).shouldApprox == 0.4;
Examples:
Can also use dynamic array
import mir.test: shouldApprox;

double[] p = [0.1, 0.5, 0.4];

0.categoricalCCDF(p).shouldApprox == 1.0;
1.categoricalCCDF(p).shouldApprox == 0.9;
2.categoricalCCDF(p).shouldApprox == 0.4;
pure nothrow @nogc @safe size_t categoricalInvCDF(T, Iterator, SliceKind kind)(const T q, scope const Slice!(Iterator, 1, kind) p)
if (isFloatingPoint!(CommonType!(T, elementType!(Slice!(Iterator, 1, kind)))));

pure nothrow @nogc @safe size_t categoricalInvCDF(T)(const T q, scope const T[] p...)
if (isFloatingPoint!T);
Computes the Categorical inverse cumulative distribution function (InvCDF).
Parameters:
T q value to evaluate InvCDF
Slice!(Iterator, 1, kind) p slice containing the probability associated with the Categorical Distribution
Examples:
import mir.ndslice.slice: sliced;
import mir.test: should;

static immutable x = [0.1, 0.5, 0.4];
auto p = x.sliced;

categoricalInvCDF(0.0, p).should == 0;
categoricalInvCDF(0.1, p).should == 0;
categoricalInvCDF(0.2, p).should == 1;
categoricalInvCDF(0.3, p).should == 1;
categoricalInvCDF(0.4, p).should == 1;
categoricalInvCDF(0.5, p).should == 1;
categoricalInvCDF(0.6, p).should == 1;
categoricalInvCDF(0.7, p).should == 2;
categoricalInvCDF(0.8, p).should == 2;
categoricalInvCDF(0.9, p).should == 2;
categoricalInvCDF(1.0, p).should == 2;
Examples:
Can also use dynamic array
import mir.test: should;

double[] p = [0.1, 0.5, 0.4];

categoricalInvCDF(0.5, p).should == 1;
pure nothrow @nogc @safe elementType!(Slice!(Iterator, 1, kind)) categoricalLPMF(Iterator, SliceKind kind)(const size_t x, scope const Slice!(Iterator, 1, kind) p)
if (isFloatingPoint!(elementType!(Slice!(Iterator, 1, kind))));

pure nothrow @nogc @safe T categoricalLPMF(T)(const size_t x, scope const T[] p...)
if (isFloatingPoint!T);
Computes the Categorical log probability mass function (LPMF).
Parameters:
size_t x value to evaluate LPMF
Slice!(Iterator, 1, kind) p slice containing the probability associated with the Categorical Distribution
Examples:
import mir.math.common: log;
import mir.ndslice.slice: sliced;
import mir.test: shouldApprox;

static immutable x = [0.1, 0.5, 0.4];
auto p = x.sliced;

0.categoricalLPMF(p).shouldApprox == log(0.1);
1.categoricalLPMF(p).shouldApprox == log(0.5);
2.categoricalLPMF(p).shouldApprox == log(0.4);
Examples:
Can also use dynamic array
import mir.math.common: log;
import mir.test: shouldApprox;

double[] p = [0.1, 0.5, 0.4];

0.categoricalLPMF(p).shouldApprox == log(0.1);
1.categoricalLPMF(p).shouldApprox == log(0.5);
2.categoricalLPMF(p).shouldApprox == log(0.4);