# Probability Distributions

In

*MathNet.Numerics.Distributions* we provide a wide range of probability distributions. Once parametrized, they can be used to sample non-uniform random numbers or investigate their statistical properties.

All the distributions implement a basic set of operations such as computing the mean, standard deviation, density, etc. Because it is often numerically more stable and faster to compute quantities in the log domain, we also provide a selection of them, including Density, in the logarithmic domain with the "Ln" suffix, .e.g. DensityLn.

using MathNet.Numerics.Distributions;
var gamma = new Gamma(2.0, 1.5);
var mean = gamma.Mean;
var variance = gamma.Variance;
var entropy = gamma.Entropy;
// ...
var a = gamma.Density(2.3); // pdf
var b = gamma.DensityLn(2.3); // ln(pdf)
var c = gamma.CumulativeDistribution(0.7); // cdf

### Parametrizing the Distributions

There are many ways to parameterize a distribution in the literature. When using the default constructor, study carefully which parameters it requires. If they do not suite your needs, there will likely be static method which can construct the distribution for you with the parameters of your choice. For example, to construct a normal distribution with mean 0.0 and standard deviation 2.0 you can use

var n = new Normal(0.0, 2.0); . However, if you'd rather parameterize the normal distribution using a mean and precision, one can use the following code instead:

var n = Normal.WithMeanPrecision(0.0,0.5); var gamma = Gamma.WithShapeScale(2.0, 0.75);

### Random Number Sampling

Each distribution provides methods to generate random numbers from that distribution. These random variate generators work by accessing the distribution's member

*RandomSource* (which is a subclass of

*System.Random*, see

Random Numbers for details) to provide uniform random numbers. By default, this member is an instance of

*System.Random* but one can easily replace this with more sophisticated random number generators from

*MathNet.Numerics.Random*. Each distribution class has two static and two class methods: for each pair (static and class), one of them generates a single sample, the other will generate an

*IEnumerable<T>* of samples. The static methods allow random number generation without instantiating the actual class.

using MathNet.Numerics.Random;
var gamma = new Gamma(2.0, 1.5);
gamma.RandomSource = new MersenneTwister();
double a = gamma.Sample();
double[] b = gamma.Samples().Take(100).ToArray();

Alternative using static methods (note that no intermediate value caching is possible this way and parameters must be validated on each call):

var rnd = new MersenneTwister();
double a = Gamma.Sample(rnd, 2.0, 1.5);
double[] b = Gamma.Samples(rnd, 2.0, 1.5).Take(10).ToArray();

### Probability Distributions in F#

The F# extensions provide a few simple helper functions in the MathNet.Numerics.Distributions namespace to assign a specific random source to a distribution:

*withRandom*,

*withSystemRandom*,

*withCryptoRandom* and

*withMersenneTwister*:

let rnd = Random.xorshift ()
let normal = Normal.WithMeanVariance(3.0, 1.5) |> withRandom rnd
let gamma = new Gamma(2.0, 1.5) |> withMersenneTwister
let cauchy = new Cauchy() |> withRandom (Random.mrg32k3aWith 10 false)
let samples = cauchy.Samples() |> Seq.take 100 |> List.ofSeq
let samples2 = Cauchy.Samples(rnd, 1.0, 3.0) |> Seq.take 20 |> List.ofSeq

## Discrete Distributions

All discrete distributions implement the

*IDiscreteDistribution* and

*IDistribution* intefaces.

## Continuous Distributions

All continuous distributions implement the

*IContinuousDistribution* and

*IDistribution* intefaces.

## Multivariate Distributions

There is no shared interface for the multivariate distributions: as their domains can be quite different it would be hard to come up with a simple and clean but still useful unifying interface.