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

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

normal_cdf(): returns CDF, given X.

normal_cdf_inv(): returns X, given CDF.

normal_mean(): returns mean (which will be MU).

normal_moment(): returns moments.

normal_moment_central(): returns central moments.

normal_pdf(): returns PDF.

normal_sample(): randomly samples.

normal_variance(): returns variance (which will be S^2).
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)

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

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

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;
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 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:
LOG_NORMAL_TRUNCATED_AB,
a C library which
returns quantities associated with the log normal Probability
Distribution Function (PDF) truncated to the interval [A,B].
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:

Norman Johnson, Samuel Kotz, Narayanaswamy Balakrishnan,
Continuous Univariate Distributions,
Second edition,
Wiley, 1994,
ISBN: 0471584940,
LC: QA273.6.J6.
Source Code:
Examples and Tests:
List of Routines:

NORMAL_01_CDF evaluates the Normal 01 CDF.

NORMAL_01_CDF_INV inverts the standard normal CDF.

NORMAL_01_MEAN returns the mean of the Normal 01 PDF.

NORMAL_01_MOMENT evaluates moments of the Normal 01 PDF.

NORMAL_01_PDF evaluates the Normal 01 PDF.

NORMAL_01_SAMPLE samples the standard normal probability distribution.

NORMAL_01_VARIANCE returns the variance of the Normal 01 PDF.

NORMAL_CDF evaluates the Normal CDF.

NORMAL_CDF_INV inverts the Normal CDF.

NORMAL_MEAN returns the mean of the Normal PDF.

NORMAL_MOMENT evaluates moments of the Normal PDF.

NORMAL_MOMENT_CENTRAL evaluates central moments of the Normal PDF.

NORMAL_MOMENT_CENTRAL_VALUES: moments 0 through 10 of the Normal PDF.

NORMAL_MOMENT_VALUES evaluates moments 0 through 8 of the Normal PDF.

NORMAL_PDF evaluates the Normal PDF.

NORMAL_SAMPLE samples the Normal PDF.

NORMAL_VARIANCE returns the variance of the Normal PDF.

R8_ABS returns the absolute value of an R8.

R8_CHOOSE computes the binomial coefficient C(N,K) as an R8.

R8_FACTORIAL2 computes the double factorial function.

R8_HUGE returns a "huge" R8.

R8_LOG_2 returns the logarithm base 2 of an R8.

R8_MOP returns the Ith power of 1 as an R8 value.

R8_UNIFORM_01 returns a unit pseudorandom R8.

R8POLY_VALUE evaluates a double precision polynomial.

R8VEC_MAX returns the value of the maximum element in an R8VEC.

R8VEC_MEAN returns the mean of an R8VEC.

R8VEC_MIN returns the value of the minimum element in an R8VEC.

R8VEC_VARIANCE returns the variance of an R8VEC.

TIMESTAMP prints the current YMDHMS date as a time stamp.

TRUNCATED_NORMAL_AB_CDF evaluates the truncated Normal CDF.

TRUNCATED_NORMAL_AB_CDF_VALUES: values of the Truncated Normal CDF.

TRUNCATED_NORMAL_AB_CDF_INV inverts the truncated Normal CDF.

TRUNCATED_NORMAL_AB_MEAN returns the mean of the truncated Normal PDF.

TRUNCATED_NORMAL_AB_MOMENT: moments of the truncated Normal PDF.

TRUNCATED_NORMAL_AB_PDF evaluates the truncated Normal PDF.

TRUNCATED_NORMAL_AB_PDF_VALUES: values of the Truncated Normal PDF.

TRUNCATED_NORMAL_AB_SAMPLE samples the truncated Normal PDF.

TRUNCATED_NORMAL_AB_VARIANCE returns the variance of the truncated Normal PDF.

TRUNCATED_NORMAL_A_CDF evaluates the lower truncated Normal CDF.

TRUNCATED_NORMAL_A_CDF_VALUES: values of the Lower Truncated Normal CDF.

TRUNCATED_NORMAL_A_CDF_INV inverts the lower truncated Normal CDF.

TRUNCATED_NORMAL_A_MEAN returns the mean of the lower truncated Normal PDF.

TRUNCATED_NORMAL_A_MOMENT: moments of the lower truncated Normal PDF.

TRUNCATED_NORMAL_A_PDF evaluates the lower truncated Normal PDF.

TRUNCATED_NORMAL_A_PDF_VALUES: values of the Lower Truncated Normal PDF.

TRUNCATED_NORMAL_A_SAMPLE samples the lower truncated Normal PDF.

TRUNCATED_NORMAL_A_VARIANCE: variance of the lower truncated Normal PDF.

TRUNCATED_NORMAL_B_CDF evaluates the upper truncated Normal CDF.

TRUNCATED_NORMAL_B_CDF_VALUES: values of the Upper Truncated Normal CDF.

TRUNCATED_NORMAL_B_CDF_INV inverts the upper truncated Normal CDF.

TRUNCATED_NORMAL_B_MEAN returns the mean of the upper truncated Normal PDF.

TRUNCATED_NORMAL_B_MOMENT: moments of the upper truncated Normal PDF.

TRUNCATED_NORMAL_B_PDF evaluates the upper truncated Normal PDF.

TRUNCATED_NORMAL_B_PDF_VALUES: values of the Upper Truncated Normal PDF.

TRUNCATED_NORMAL_B_SAMPLE samples the upper truncated Normal PDF.

TRUNCATED_NORMAL_B_VARIANCE: variance of the upper truncated Normal PDF.
You can go up one level to
the C source codes.
Last revised on 16 September 2013.