truncated_normal, a MATLAB 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.
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):
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".
The computer code and data files made available on this web page are distributed under the GNU LGPL license.
truncated_normal is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.
log_normal_truncated_ab, a MATLAB code which returns quantities associated with the log normal probability distribution function (pdf) truncated to the interval [a,b].
normal, a MATLAB code which samples the normal distribution.
pdflib, a MATLAB 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 MATLAB 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, a MATLAB 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, a MATLAB 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, a MATLAB code which samples the uniform distribution.