The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

# NAME

Math::Gauss - Gaussian distribution function and its inverse

# SYNOPSIS

``````  use Math::Gauss ':all';

\$p = pdf( \$z );
\$p = pdf( \$x, \$m, \$s );

\$c = cdf( \$z );
\$c = cdf( \$x, \$m, \$s );

\$z = inv_cdf( \$z );``````

# DESCRIPTION

This module calculates the Gaussian probability density, the cumulative distribution function (Gaussian distribution function), and the inverse Gaussian distribution function.

## EXPORT

None by default. The `:all` tag is recognized, or import individual functions.

## FUNCTIONS

`pdf( \$x, \$m, \$s )`

The Gaussian probability density function (pdf)

``  exp( -0.5 ( (x-m)/s )**2 )/sqrt(2*pi*s**2)``

Only the first argument is mandatory, the other two are optional. If they are not supplied, they default as follows: `pdf( \$z ) == pdf( \$x, 0, 1 )`.

The parameter `\$s` must be strictly positive, `\$x` and `\$m` can be arbitrary.

If you choose to supply the z-score, `\$z = (\$x-\$m)/\$s` as argument to the pdf() instead of supplying the mean and the standard deviation as separate arguments, you must divide the return value by the standard deviation to obtain a properly normalized result. The following is true:

``  pdf( \$x, \$m, \$s ) == pdf( (\$x-\$m)/\$s )/\$s``

`cdf( \$x, \$m, \$s )`

The Gaussian cumulative distribution function (cdf). This is the integral of pdf(t,m,s) from -infinity to x over t:

``  int_{-infty}^x exp( -0.5 ( (t-m)/s )**2 )/sqrt(2*pi*s**2) dt``

Only the first argument is mandatory, the other two are optional. If they are not supplied, they default as follows: `cdf( \$z ) == cdf( \$x, 0, 1 )`.

The parameter `\$s` must be strictly positive, `\$x` and `\$m` can be arbitrary.

The implementation is guaranteed to have a maximum absolute error of less than 7.5e-8 for all x.

`inv_cdf( \$z )`

The inverse of the Gaussian cumulative distribution function. This function is only defined for arguments strictly greater than 0 and strictly less than 1: `0 < \$z < 1`.

The implementation is guaranteed to have a maximum absolute error of less than 4.5e-4 for all x.

# USAGE NOTES

This module favors simplicity and portability over accuracy. The accuracy should be good enough for most applications; if you need higher accuracy, see the resources below.

The cumulative distribution function and its inverse defined by this module are the so-called "probability function" versions. They are related to the "error function" `erf()` and its inverse `inverf()` by:

``````  erf(x) = 2 cdf( sqrt(2)*x ) - 1
inverf(x) = inv_cdf( (x+1)/2 )/sqrt(2)``````

The numerical algorithms are taken from:

Handbook of Mathematical Functions: with Formulas, Graphs, and Mathematical Tables by Milton Abramowitz and Irene A. Stegun; Dover (1965).

Alternative algorithms (including a high-accuracy algorithm for the inverse distribution functions) can be found here:

• http://home.online.no/~pjacklam/notes/invnorm/

• http://homepages.physik.uni-muenchen.de/~Winitzki/erf-approx.pdf

# AUTHOR

Philipp K. Janert, <janert at ieee dot org>, http://www.beyondcode.org