Skip to content

Probability Functions

David Wright edited this page Apr 28, 2018 · 1 revision

Probability distributions are used in modeling, Monte-Carlo methods, and other applications. Meta.Numerics has implementations of more than 30 of the most common probability distributions, both continuous and discrete.

How do I get the PDF of a continuous probability distribution?

Use the ProbabilityDensity method. Here's some code that uses the PDF of the Gumbel distribution to compute the mean absolute deviation of a Gumbel-distributed variable:

using System;
using Meta.Numerics.Analysis;
using Meta.Numerics.Statistics.Distributions;

ContinuousDistribution gumbel = new GumbelDistribution();

IntegrationResult r = FunctionMath.Integrate(
    z => gumbel.ProbabilityDensity(z) * Math.Abs(z - gumbel.Mean),
    gumbel.Support
);
Console.WriteLine($"mean absolute deviation = {r.Value}");

Even easier, if you use the ExpectationValue function, we'll even package up the integral for you:

double gumbelMad = gumbel.ExpectationValue(z => Math.Abs(z - gumbel.Mean));
Console.WriteLine($"mean absolute deviation = {gumbelMad}");

How do I get CDF of a continuous probability distribution?

Meta.Numerics uses the mnemonic names LeftProbability and RightProbability for the CDF and complementary CDF functions.

double x = 1.5;

// CDF, aka percentile
double P = gumbel.LeftProbability(x);
Console.WriteLine($"P({x}) = {P}");

// Right-tailed complementary CDF 
double Q = gumbel.RightProbability(x);
Console.WriteLine($"Q({x}) = {Q}");

Console.WriteLine($"P + Q = {P + Q}");

Our implementations are coded carefully so as not to loose accuracy in the tails. So, for example, even when Q < 10-16, we will return accurate values:

double xt = 100.0;
double qt = gumbel.RightProbability(xt);
Console.WriteLine($"Q({xt}) = {qt}");

How do I get the inverse CDF of a continuous probability distribution?

The inverse CDF is also called the quantile function. Here is some code that illustrates that our InverseLeftProbability and InverseRightProbability methods accurately invert the LeftProbability and RightProbability methods.

Console.WriteLine($"PI({P}) = {gumbel.InverseLeftProbability(P)}");
Console.WriteLine($"QI({qt} = {gumbel.InverseRightProbability(qt)}");

Notice that even our extremely tiny Q-value is accurately inverted.

How can I get the PMF of a discrete distribution?

Unlike the PDF of a continuous distribution, which isn't itself a probability but instead only a probability density, the PMF of a discrete distribution has a direct interpretation as the probability of obtaining that particular value:

DiscreteDistribution binomial = new BinomialDistribution(0.4, 8);

Console.WriteLine($"support {binomial.Support}");

int k = 4;
Console.WriteLine($"P({k}) = {binomial.ProbabilityMass(k)}");

As for continuous distributions, we offer an expectation function that uses the PMF to compute expectation values of arbitrary functions on the set:

double binomialMad = binomial.ExpectationValue(i => Math.Abs(i - binomial.Mean));
Console.WriteLine($"mean absolute deviation = {binomialMad}");

How do I get the CDF for a discrete distribution?

When dealing with cumulative distribution functions of discrete distributions, you have to be careful to specify whether you are dealing with inclusive probabilities (all the values up to and including the one specified) or exclusive probabilities (all the values strictly above or strictly below the one specified). Meta.Numerics supports you by defining both inclusive and exclusive CDF methods for discrete distributions:

Console.WriteLine($"P(k < {k}) = {binomial.LeftExclusiveProbability(k)}");
Console.WriteLine($"P(k <= {k}) = {binomial.LeftInclusiveProbability(k)}");
Console.WriteLine($"P(k > {k}) = {binomial.RightExclusiveProbability(k)}");

How do I get the inverse CDF for a discrete distribution?

The InverseLeftProbability method gives you the minimum integer value for the LeftInclusiveProbability to reach the specified cumulative probability.

int k0 = binomial.InverseLeftProbability(0.5);
Console.WriteLine($"min k0 to achieve P(k <= k0) > 0.5: {k0}");
Console.WriteLine($"P(k < {k0}) = {binomial.LeftExclusiveProbability(k0)}");
Console.WriteLine($"P(k <= {k0}) = {binomial.LeftInclusiveProbability(k0)}");

Home

Clone this wiki locally