truncated_normal

truncated_normal, an Octave code which computes quantities associated with the truncated normal distribution.

In statistics and probability, many quantities are well modeled by the normal distribution, often called the "bell curve". The main features of the normal distribution are that it has an average value or mean, whose probability exceeds that of all other values, and that on either side of the mean, the density function smoothly decreases, without every becoming zero.

For various reasons, it may be preferable to work with a truncated normal distribution. This may be because the normal distribution is a good fit for our data, but for physical reasons we know our data can never be negative, or we only wish to consider data within a particular range of interest to us, which we might symbolize as [A,B], or [A,+oo), or (-oo,B), depending on the truncation we apply.

It is possible to define a truncated normal distribution by first assuming the existence of a "parent" normal distribution, with mean MU and standard deviation SIGMA. We may then derive a modified distribution which is zero outside the region of interest, and inside the region, has the same "shape" as the parent normal distribution, although scaled by a constant so that its integral is 1.

Note that, although we define the truncated normal distribution function in terms of a parent normal distribution with mean MU and standard deviation SIGMA, in general, the mean and standard deviation of the truncated normal distribution are different values entirely; however, their values can be worked out from the parent values MU and SIGMA, and the truncation limits. That is what is done in the "_mean()" and "_variance()" functions.

Details

Define the unit normal distribution probability density function (PDF) for any -oo < x < +oo:

```        N(0,1)(x) = 1/sqrt(2*pi) * exp ( - x^2 / 2 )
```
This library includes the following functions for N(0,1)(x):
• normal_01_cdf(): returns CDF, given X.
• normal_01_cdf_inv(): returns X, given CDF.
• normal_01_mean(): returns the mean (which will be 0).
• normal_01_moment(): returns moments.
• normal_01_pdf(): returns PDF.
• normal_01_sample(): randomly samples.
• normal_01_variance(): returns variance (which will be 1).

For a normal distribution with mean MU and standard deviation SIGMA, the formula for the PDF is:

```        N(MU,S)(x) = 1 / sqrt(2*pi) / sigma * exp ( - ( ( x - mu ) / sigma )^2 )
```
This library includes the following functions for N(MU,SIGMA)(x):
• normal_ms_cdf(): returns CDF, given X.
• normal_ms_cdf_inv(): returns X, given CDF.
• normal_ms_mean(): returns mean (which will be MU).
• normal_ms_moment(): returns moments.
• normal_ms_moment_central(): returns central moments.
• normal_ms_pdf(): returns PDF.
• normal_ms_sample(): randomly samples.
• normal_ms_variance(): returns variance (which will be SIGMA^2).

Define the truncated normal distribution PDF with parent normal N(MU,SIGMA)(x), for a < x < b:

```        NAB(MU,SIGMA)(x) = N(MU,SIGMA)(x) / ( cdf(N(MU,SIGMA))(b) - cdf(N(MU,SIGMA))(a) )
```
This library includes the following functions for NAB(MU,SIGMA)(x)
• truncated_normal_ab_cdf(): returns CDF, given X.
• truncated_normal_ab_cdf_inv(): returns X, given CDF.
• truncated_normal_ab_mean(): returns mean.
• truncated_normal_ab_moment(): returns moments.
• truncated_normal_ab_pdf(): returns PDF.
• truncated_normal_ab_sample(): randomly samples.
• truncated_normal_ab_variance(): returns variance.

Define the lower truncated normal distribution PDF with parent normal N(MU,SIGMA)(x), for a < x < +oo:

```        NA(MU,SIGMA)(x) = N(MU,SIGMA)(x) / ( 1 - cdf(N(MU,SIGMA))(a) )
```
This library includes the following functions for NA(MU,SIGMA)(x):
• truncated_normal_a_cdf(): returns CDF, given X.
• truncated_normal_a_cdf_inv(): returns X, given CDF.
• truncated_normal_a_mean(): returns mean.
• truncated_normal_a_moment(): returns moments.
• truncated_normal_a_pdf(): returns PDF.
• truncated_normal_a_sample(): randomly samples.
• truncated_normal_a_variance(): returns variance.

Define the upper truncated normal distribution PDF with parent normal N(MU,SIGMA), for -oo < x < b:

```        NB(MU,SIGMA)(x) = N(MU,SIGMA)(x) / cdf(N(MU,SIGMA))(b)
```
This library includes the following functions for NB(MU,SIGMA)(x):
• truncated_normal_b_cdf(): returns CDF, given X.
• truncated_normal_b_cdf_inv(): returns X, given CDF.
• truncated_normal_b_mean(): returns mean.
• truncated_normal_b_moment(): returns moments.
• truncated_normal_b_pdf(): returns PDF.
• truncated_normal_b_sample(): randomly samples.
• truncated_normal_b_variance(): returns variance.

Demonstrations

The CDF and CDF_INV functions should be inverses of each other. A simple test of the truncated AB normal functions would be

```        mu = 100.0;
sigma = 25.0;
a = 50.0;
b = 120.0;
seed = 123456789;

[ x, seed ] = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
cdf = truncated_normal_ab_cdf ( x, mu, sigma, a, b );
x2 = truncated_normal_ab_cdf_inv ( cdf, mu, sigma, a, b );
```
and compare x and x2, which should be quite close if the inverse function is working correctly.

A simple test of the mean and variance functions might be to compare the theoretical mean and variance to the sample mean and variance of a sample of 1,000 values:

```        sample_num = 1000;
mu = 100.0;
sigma = 25.0;
a = 50.0;
b = 120.0;
seed = 123456789;

for i = 1 : sample_num
[ x(i), seed ] = truncated_normal_ab_sample ( mu, sigma, a, b, seed );
end

m = truncated_normal_ab_mean ( mu, sigma, a, b );
v = truncated_normal_ab_variance ( mu, sigma, a, b );
ms = mean ( x );
vs = var ( x );
```
Typically, the values of m and ms, of v and vs should be "reasonably close".

Languages:

truncated_normal is available in a C version and a C++ version and a Fortran7 version and a Fortran90 version and a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

log_normal_truncated_ab, an Octave code which returns quantities associated with the log normal probability distribution function (pdf) truncated to the interval [a,b].

normal, an Octave code which samples the normal distribution.

pdflib, an Octave code which evaluates probability density functions (pdf's) and produces random samples from them, including beta, binomial, chi, exponential, gamma, inverse chi, inverse gamma, multinomial, normal, scaled inverse chi, and uniform.

prob, an Octave code which evaluates probability density functions (pdf's) and cumulative density functions (cdf's), means, variances, and samples for a variety of standard probability distributions.

truncated_normal_rule, an Octave code which computes a quadrature rule for a normal probability density function (PDF), also called a Gaussian distribution, that has been truncated to [a,+oo), (-oo,b] or [a,b].

truncated_normal_sparse_grid, an Octave code which computes a sparse grid based on a normal probability density function (PDF), also called a Gaussian distribution, that has been truncated to [a,+oo), (-oo,b] or [a,b].

uniform, an Octave code which samples the uniform distribution.

Reference:

• Norman Johnson, Samuel Kotz, Narayanaswamy Balakrishnan,
Continuous Univariate Distributions,
Second edition,
Wiley, 1994,
ISBN: 0471584940,
LC: QA273.6.J6.

Source Code:

Last revised on 02 April 2024.