4.1 Distribution Classes (.NET, C#, CSharp, VB, Visual Basic, F#)
The NMath Stats probability distribution classes are listed in Table 5.
Class 
Distribution 
Beta distribution 

Binomial distribution 

ChiSquare () distribution 

Exponential distribution 

F distribution 

Gamma distribution 

Geometric distribution 

Johnson distribution 

Logistic distribution 

Lognormal distribution 

Negative Binomial distribution 

Normal (Gaussian) distribution 

Poisson distribution 

Student's t distribution 

Triangular distribution 

Uniform distribution 

Weibull distribution 
All distribution classes share a common interface. Class ProbabilityDistribution is the abstract base class for the distribution classes, and provides the following abstract methods implemented by the derived classes:
● PDF() computes the probability density function at a given x.
● CDF() computes the cumulative distribution function at a given x.
● InverseCDF() computes the inverse cumulative distribution function for a given probability p—that is, it returns x such that CDF( x ) = p.
In addition, all NMath Stats distribution classes implement the IRandomVariableMoments interface, which provides the following readonly properties:
● Mean gets the mean of the distribution.
● Variance gets the variance of the distribution.
● Kurtosis gets the kurtosis of the distribution.
● Skewness gets the skewness of the distribution.
Variance is the square of the standard deviation. Kurtosis is a measure of the degree of peakednesss of a distribution; skewness is a measure of the degree of asymmetry.
Once you have constructed a derived distribution type, you can query it for the PDF, CDF, inverse CDF, and random variable moments. For example, this code constructs a NormalDistribution with mean 0 and variance 1, then queries it:
Code Example – C# normal distribution
var dist = new NormalDistribution( 0, 1 ); double pdf = dist.PDF( 0 ); double cdf = dist.CDF( 0 ); double invCdf = dist.InverseCDF( .5 ); double mean = dist.Mean; double var = dist.Variance; double kurt = dist.Kurtosis; double skew = dist.Skewness;
Class BetaDistribution represents the beta probability distribution. The beta distribution is a family of curves with two free parameters, usually labelled and . Beta distributions are nonzero only on the interval (0 1).
The distribution function for the beta distribution is:
where is the beta function. The beta CDF is the same as the incomplete beta function.
For example, this code constructs a BetaDistribution:
Code Example – C# beta distribution
double alpha = 3; double beta = 7; var dist = new BetaDistribution( alpha, beta );
The default constructor creates a BetaDistribution with and equal to 1:
Code Example – C# beta distribution
var dist = new BetaDistribution();
The provided Alpha and Beta properties can be used to get and set the shape parameters after construction:
Code Example – C# beta distribution
dist.Alpha = 4; dist.Beta = 10;
Once you have constructed a BetaDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class BinomialDistribution represents the discrete probability distribution of obtaining exactly n successes in N trials where the probability of success on each trial is p. For example, this code constructs an BinomialDistribution:
Code Example – C# binomial distribution
int n = 20; double p = 0.25; var bin = new BinomialDistribution( n, p );
The default constructor creates an BinomialDistribution with and :
Code Example – C# binomial distribution
var bin = new BinomialDistribution();
The provided N and P properties can be used to get and set the number of trials and the probability of success on each trial after construction:
Code Example – C# binomial distribution
bin.N = 75; bin.P = 0.02;
Once you have constructed an BinomialDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class ChiSquareDistribution represents the chisquare () probability distribution. The chisquare distribution is a special case of the gamma distribution with and , where df is the degrees of freedom.
For example, this code constructs a ChiSquareDistribution:
Code Example – C# chisquare distribution
double df = 16; var chiSq = new ChiSquareDistribution( df );
The default constructor creates a ChiSquareDistribution with 1 degree of freedom:
Code Example – C# chisquare distribution
var chiSq = new ChiSquareDistribution();
The provided DegreesOfFreedom property can be used to get and set the degrees of freedom of the distribution after construction:
Code Example – C# chisquare distribution
chiSq.DegreesOfFreedom = 10;
Once you have constructed a ChiSquareDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class ExponentialDistribution represents the exponential distribution. A random variable w is said to have an exponential distribution if it has a probability density function
where is often called the rate parameter. The mean of an exponential distribution is , and the variance is . For example, this code constructs an ExponentialDistribution:
Code Example – C# exponential distribution
double lambda = 22; ExponentialDistribution exp = new ExponentialDistribution( lambda );
The provided Lambda property can be used to get and set the rate after construction:
Code Example – C# exponential distribution
exp.Lambda = 15;
Once you have constructed an ExponentialDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class FDistribution represents the F probability distribution. The F distribution is the ratio of two chisquare distributions with degrees of freedom df1 and df2, respectively, where each chisquare has first been divided by its degrees of freedom. For example, this code constructs an FDistribution:
Code Example – C# F distribution
double df1 = 11; double df2 = 19; var f = new FDistribution( df1, df2 );
The default constructor creates an FDistribution with both degrees of freedom equal to 1:
Code Example – C# F distribution
var f = new FDistribution();
The provided DegreesOfFreedom1 and DegreesOfFreedom2 properties can be used to get and set the degrees of freedom after construction:
Code Example – C# F distribution
f.DegreesOfFreedom1 = 15; f.DegreesOfFreedom2 = 23;
Once you have constructed an FDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class GammaDistribution represents the gamma probability distribution. The gamma distribution is a family of curves with two free parameters, usually labelled and . The mean of the distribution is ; the variance is . When is large, the gamma distribution closely approximates a normal distribution.
The distribution function for the gamma distribution is:
where is the Gamma function.
For example, this code constructs a GammaDistribution:
Code Example – C# gamma distribution
double alpha = 7; double beta = 12; var gamma = new GammaDistribution( alpha, beta );
The default constructor creates a GammaDistribution with and equal to 1:
Code Example – C# gamma distribution
var gamma = new GammaDistribution();
The provided Alpha and Beta properties can be used to get and set the shape parameters after construction:
Code Example – C# gamma distribution
gamma.Alpha = 10; gamma.Beta = 15;
Once you have constructed a GammaDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class GeometricDistribution represents the geometric distribution. The geometric distribution is the probability distribution of the number of failures before the first success. It is supported on the set .
A GeometricDistribution is constructed from a given probability of success p, where . For example:
Code Example – C# geometric distribution
double p = .25; var geo = new GeometricDistribution( p );
Class GeometricDistribution provides property P that gets and sets the probability for success for the distribution.
Code Example – C# geometric distribution
geo.P = .5;
Once you have constructed a GeometricDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class JohnsonDistribution represents the Johnson system of distributions. The Johnson system is based on three possible transformations of a normal random variable—exponential, logistic, and hyperbolic sine—plus the identity transformation:
where the transformation f() has four possible forms based on the distribution type:
● Normal (SN): f(u) = exp(u)
● Log Normal (SL): f(u) = u
● Unbounded (SU): f(u) = u + sqrt(1+u^2)
● Bounded (SB): f(u) = u/(1u)
A JohnsonDistribution instance is constructed from a set of distribution parameter values, and a JohnsonTransformationType enumerated value specifying the transformation type. For instance:
Code Example – C# Johnson distribution
double gamma = 0.18; double delta = 2.55; double xi = 0.14; double lambda = 2.35; JohnsonTransformationType type = JohnsonTransformationType.Normal; JohnsonDistribution dist = new JohnsonDistribution( gamma, delta, xi, lambda, type );
Once you have constructed a JohnsonDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class JohnsonDistribution also provides a static Fit() method for fitting a Johnson distribution to a data set. Estimation of the Johnson parameters is done from quantiles that correspond to the cumulative probabilities [0.05, 0.206, 0.5, 0.794, 0.95] using the method of Wheeler (1980).^{1} For example:
Code Example – C# Johnson distribution
var data = new DoubleVector(0.09736927, 0.21615254, 0.88246516, 0.20559750, 0.61643584, 0.73479925, 0.13180279, 0.31001699, 1.03968035, 0.18430887, 0.96726726, 0.10828009,  0.69842067, 0.27594517, 1.11464855, 0.55004396, 1.23667580, 0.13909786, 0.41027510, 0.55845691); var dist = JohnsonDistribution.Fit(data);
The Transform() method transforms data using a JohnsonDistribution object.
Class LogisticDistribution represents the logistic probability distribution with a specified location (mean) and scale. The logistic distribution with location m and scale b has distribution function:
and density:
For example, this code constructs a LogisticDistribution:
Code Example – C# logistic distribution
double loc = 2.0; double scale = 1.5; LogisticDistribution logistic = new LogisticDistribution( loc, scale );
The provided Location and Scale properties can be used to get and set distribution parameters after construction:
Code Example – C# logistic distribution
logistic.Location = 7.123; logistic.Scale = 4.5;
Once you have constructed a LogisticDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class LognormalDistribution represents the lognormal distribution. A lognormal distribution has a normal distribution as its logarithm:
For example, this code constructs an LognormalDistribution whose associated normal distribution has the specified mean and standard deviation:
Code Example – C# lognormal distribution
double mu = 99; double sigma = 6; var ln = new LognormalDistribution( mu, sigma );
The default constructor creates a LognormalDistribution whose associated normal distribution has mean 0 and standard deviation 1:
Code Example – C# lognormal distribution
var ln = new LognormalDistribution();
The Mu and Sigma properties can be used to get and set the mean and standard deviation after construction:
Code Example – C# lognormal distribution
ln.Mu = 2.25; ln.Sigma = .75;
Once you have constructed a LognormalDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Negative Binomial Distribution
Class NegativeBinomialDistribution represents the discrete probability distribution of obtaining N successes in a series of x trials, where the probability of success on each trial is P.
For example, this code constructs an NegativeBinomialDistribution:
Code Example – C# negative binomial distribution
int n = 5; double p = 0.25; var negBin = new NegativeBinomialDistribution( n, p );
The default constructor creates an NegativeBinomialDistribution with and :
Code Example – C# negative binomial distribution
var negBin = new BinomialDistribution();
The provided N and P properties can be used to get and set the number of successes and the probability of success on each trial after construction:
Code Example – C# negative binomial distribution
negBin.N = 75; negBin.P = 0.02;
Once you have constructed an NegativeBinomialDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class NormalDistribution represents the normal (Gaussian) probability distribution. with a specified mean and variance. For example, this code creates a normal distribution with a mean of 1 and variance of 2.5:
Code Example – C# normal distribution
var norm = new NormalDistribution( 1, 2.5 );
The default constructor creates a NormalDistribution with mean 0 and variance 1:
Code Example – C# normal distribution
var norm = new NormalDistribution();
The Mean and Variance properties inherited from IRandomVariableMoments can be used to get and set the mean and variance after construction:
Code Example – C# normal distribution
norm.Mean = 2.25; norm.Variance = .75;
Once you have constructed a NormalDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class PoissonDistribution represents a poisson distribution with a specified parameter, which is both the mean and the variance of the distribution. The poisson distribution is the probability of obtaining exactly n successes in N trials. It is often used as a model for the number of events in a specific time period. Poisson (1837) showed that the Poisson distribution is the limiting case of a binomial distribution where N approaches infinity and p goes to zero while . The distribution function for the Poisson distribution is:
For example, this code constructs a PoissonDistribution:
Code Example – C# poisson distribution
double lambda = 150; var poisson = new PoissonDistribution( lambda );
The Mean and Variance properties inherited from IRandomVariableMoments can also be used to get and set after construction:
Code Example – C# poisson distribution
poisson.Mean = 3;
Once you have constructed a PoissonDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class TDistribution represents Student's t distribution with specified degrees of freedom. As the number of degrees of freedom grows, the t distribution approaches the normal distribution with mean 0 and variance 1.
For example, this code constructs a TDistribution:
Code Example – C# t distribution
double df = 53; var t = new TDistribution( df );
The default constructor creates a TDistribution with 1 degree of freedom:
Code Example – C# t distribution
var t = new TDistribution();
The provided DegreesOfFreedom property can be used to get and set the degrees of freedom of the distribution after construction:
Code Example – C# t distribution
t.DegreesOfFreedom = 54;
Once you have constructed a TDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class TriangularDistribution represents the triangular distribution. The triangular distribution is defined by three parameters, a lower limit a, an upper limit b, and number c, between a and b, called the mode. The probability density function has the shape of a triangle in the X/Y plane with vertices (a, 0), (b, 0), and (c, y), where y is chosen so that the area of the triangle is 1.
For example, this code constructs an TriangularDistribution with the given parameters:
Code Example – C# triangular distribution
double lower = 3; double upper = 10; double mode = 8; TriangularDistribution td = new TriangularDistribution( lower, upper, mode );
If you don't specify the mode, the midpoint of the lower and upper limits is used.
The default constructor creates a TriangularDistribution with lower limit 0, upper limit 1, and mode 0.5:
Code Example – C# triangular distribution
var td = new TriangularDistribution();
The LowerLimit, UpperLimit, and Mode properties can be used to get and set the distribution parameters after construction:
Code Example – C# triangular distribution
td.LowerLimit = 1.5; td.UpperLimit = 3.5; td.Mode = 2.75;
Once you have constructed a TriangularDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class UniformDistribution represents the uniform distribution. For example, this code constructs an UniformDistribution with the specified lower and upper limits:
Code Example – C# uniform distribution
double lower = .77; double upper = 1.22; var uni = new UniformDistribution( lower, upper );
The default constructor creates a UniformDistribution with lower limit 0 and upper limit 1:
Code Example – C# uniform distribution
var uni = new UniformDistribution();
The LowerLimit and UpperLimit properties can be used to get and set the lower and upper limits after construction:
Code Example – C# uniform distribution
uni.LowerLimit = 0; uni.UpperLimit = 2.0;
Once you have constructed a UniformDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.
Class WeibullDistribution represents the Weibull distribution. The probability density function of the Weibull distribution is given by:
where is the shape parameter and is the scale parameter of the distribution.
For example, this code constructs an WeibullDistribution with the specified distribution parameters:
Code Example – C# Weibull distribution
double scale = 1.5; double shape = 3; var wb = new WeibullDistribution( scale, shape );
The Scale and Shape properties can be used to get and set the distribution parameters after construction:
Code Example – C# Weibull distribution
wb.Scale = .5; wb.Shape = 2;
Once you have constructed a WeibullDistribution object, you can query it for the PDF, CDF, inverse CDF, and random variable moments, as described in Section 4.1.