TRUNCATED_NORMAL
The Truncated Normal Distribution


TRUNCATED_NORMAL is a C++ library 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):

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):

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)

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):

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):

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".

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

TRUNCATED_NORMAL is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATHEMATICA version and a MATLAB version and a Python version.

Related Data and Programs:

NORMAL, a C++ library which samples the normal distribution.

PDFLIB, a C++ library 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, a C++ library 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, a C++ program 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].

UNIFORM, a C++ library which samples the uniform distribution.

Reference:

Source Code:

Examples and Tests:

List of Routines:

You can go up one level to the C++ source codes.


Last revised on 11 September 2013.