truncated_normal


truncated_normal, a C 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 S. 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 S, 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 S, 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 S, the formula for the PDF is:

        N(MU,S)(x) = 1 / sqrt(2*pi) / s * exp ( - ( ( x - mu ) / s )^2 )
      
This library includes the following functions for N(MU,S)(x):

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

        NAB(MU,S)(x) = N(MU,S)(x) / ( cdf(N(MU,S))(b) - cdf(N(MU,S))(a) )
      
This library includes the following functions for NAB(MU,S)(x)

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

        NA(MU,S)(x) = N(MU,S)(x) / ( 1 - cdf(N(MU,S))(a) )
      
This library includes the following functions for NA(MU,S)(x):

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

        NB(MU,S)(x) = N(MU,S)(x) / cdf(N(MU,S))(b)
      
This library includes the following functions for NB(MU,S)(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;
        s = 25.0;
        a = 50.0;
        b = 120.0;
        seed = 123456789;

        [ x, seed ] = truncated_normal_ab_sample ( mu, s, a, b, seed );
        cdf = truncated_normal_ab_cdf ( x, mu, s, a, b );
        x2 = truncated_normal_ab_cdf_inv ( cdf, mu, s, 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;
        s = 25.0;
        a = 50.0;
        b = 120.0;
        seed = 123456789;

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

        m = truncated_normal_ab_mean ( mu, s, a, b );
        v = truncated_normal_ab_variance ( mu, s, 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 MIT license

Languages:

truncated_normal is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.

Related Data and Programs:

log_normal_truncated_ab, a C code which returns quantities associated with the log normal Probability Distribution Function (PDF) truncated to the interval [A,B].

NORMAL, a C code which samples the normal distribution.

PDFLIB, a C 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, a C 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_test

TRUNCATED_NORMAL_RULE, a C 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].

UNIFORM, a C code which samples the uniform distribution.

Reference:

Source Code:


Last revised on 23 August 2019.