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.negative_binomial

This module contains algorithms for the Negative Binomial Distribution.
There are multiple alternative formulations of the Negative Binomial Distribution. The formulation in this module uses the number of Bernoulli trials until r successes.
License:
Authors:
John Michael Hall
pure nothrow @nogc @safe T negativeBinomialPMF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T);
Computes the negative binomial probability mass function (PMF).
Parameters:
size_t k value to evaluate PMF (e.g. number of "heads")
size_t r number of successes until stopping
T p true probability
Examples:
import mir.test: shouldApprox;

4.negativeBinomialPMF(6, 3.0 / 4).shouldApprox == 0.0875988;
pure nothrow @nogc @safe T fp_negativeBinomialPMF(T)(const size_t k, const size_t r, const T p)
if (is(T == Fp!size, size_t size));
Computes the negative binomial probability mass function (PMF) directly with extended floating point types (e.g. Fp!128), which provides additional accuracy for extreme values of k, r, or p.
Parameters:
size_t k value to evaluate PMF (e.g. number of "heads")
size_t r number of successes until stopping
T p true probability
Examples:
fp_binomialPMF provides accurate values for large values of n
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;

1.fp_negativeBinomialPMF(1_000_000, Fp!128(0.75)).fp_log!double.shouldApprox == negativeBinomialLPMF(1, 1_000_000, 0.75);
pure nothrow @nogc @safe T negativeBinomialCDF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T);
Computes the negative binomial cumulative distribution function (CDF).
Parameters:
size_t k value to evaluate CDF (e.g. number of "heads")
size_t r number of successes until stopping
T p true probability
Examples:
import mir.test: shouldApprox;

4.negativeBinomialCDF(6, 3.0 / 4).shouldApprox == 0.9218731;
pure nothrow @nogc @safe T negativeBinomialCCDF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T);
Computes the negative binomial complementary cumulative distribution function (CCDF).
Parameters:
size_t k value to evaluate CCDF (e.g. number of "heads")
size_t r number of successes until stopping
T p true probability
Examples:
import mir.test: shouldApprox;

4.negativeBinomialCCDF(6, 3.0 / 4).shouldApprox == 0.07812691;
pure nothrow @nogc @safe size_t negativeBinomialInvCDF(T)(const T q, const size_t r, const T p)
if (isFloatingPoint!T);
Computes the negative binomial inverse cumulative distribution function (InvCDF).
Parameters:
T q value to evaluate InvCDF
size_t r number of successes until stopping
T p true probability
Examples:
import mir.test: should;
0.9.negativeBinomialInvCDF(6, 3.0 / 4).should == 4;
pure nothrow @nogc @safe T negativeBinomialLPMF(T)(const size_t k, const size_t r, const T p)
if (isFloatingPoint!T);
Computes the negative binomial log probability mass function (LPMF).
Parameters:
size_t k value to evaluate PMF (e.g. number of "heads")
size_t r number of successes until stopping
T p true probability
Examples:
import mir.math.common: exp;
import mir.test: shouldApprox;

4.negativeBinomialLPMF(6, 3.0 / 4).exp.shouldApprox == 4.negativeBinomialPMF(6, 3.0 / 4);
Examples:
Accurate values for large values of n
import mir.bignum.fp: Fp, fp_log;
import mir.test: shouldApprox;

enum size_t val = 1_000_000;

1.negativeBinomialLPMF(1_000_000, 0.75).shouldApprox == fp_negativeBinomialPMF(1, 1_000_000, Fp!128(0.75)).fp_log!double;