...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/distributions/normal.hpp>
namespace boost{ namespace math{ template <class RealType = double, class Policy = policies::policy<> > class normal_distribution; typedef normal_distribution<> normal; template <class RealType, class Policy> class normal_distribution { public: typedef RealType value_type; typedef Policy policy_type; // Construct: BOOST_MATH_GPU_ENABLED normal_distribution(RealType mean = 0, RealType sd = 1); // Accessors: BOOST_MATH_GPU_ENABLED RealType mean()const; // location. BOOST_MATH_GPU_ENABLED RealType standard_deviation()const; // scale. // Synonyms, provided to allow generic use of find_location and find_scale. BOOST_MATH_GPU_ENABLED RealType location()const; BOOST_MATH_GPU_ENABLED RealType scale()const; }; }} // namespaces
The normal distribution is probably the most well known statistical distribution: it is also known as the Gaussian Distribution. A normal distribution with mean zero and standard deviation one is known as the Standard Normal Distribution.
Given mean μ and standard deviation σ it has the PDF:
The variation the PDF with its parameters is illustrated in the following graph:
The cumulative distribution function is given by
and illustrated by this graph
BOOST_MATH_GPU_ENABLED normal_distribution(RealType mean = 0, RealType sd = 1);
Constructs a normal distribution with mean mean and standard deviation sd.
Requires sd > 0, otherwise domain_error is called.
BOOST_MATH_GPU_ENABLED RealType mean()const; BOOST_MATH_GPU_ENABLED RealType location()const;
both return the mean of this distribution.
BOOST_MATH_GPU_ENABLED RealType standard_deviation()const; BOOST_MATH_GPU_ENABLED RealType scale()const;
both return the standard deviation of this distribution. (Redundant location and scale function are provided to match other similar distributions, allowing the functions find_location and find_scale to be used generically).
All the usual non-member accessor
functions that are generic to all distributions are supported:
Cumulative Distribution Function,
Probability Density Function,
Quantile, Hazard Function, Cumulative Hazard Function,
__logcdf, __logpdf, mean,
median, mode,
variance, standard deviation, skewness, kurtosis,
kurtosis_excess,
range and support. For this distribution
all non-member accessor functions are marked with BOOST_MATH_GPU_ENABLED
and can be run on both host and device.
The domain of the random variable is [-[max_value], +[min_value]]. However, the pdf of +∞ and -∞ = 0 is also supported, and cdf at -∞ = 0, cdf at +∞ = 1, and complement cdf -∞ = 1 and +∞ = 0, if RealType permits.
The normal distribution is implemented in terms of the error function, and as such should have very low error rates.
In the following table m is the mean of the distribution, and s is its standard deviation.
Function |
Implementation Notes |
---|---|
|
Using the relation: pdf = e-(x-m)2/(2s2) / (s * sqrt(2*pi)) |
logpdf |
log(pdf) = -log(s) - log(2*π)/2 - (x-mean)2/(2*s2) |
cdf |
Using the relation: p = 0.5 * erfc(-(x-m)/(s*sqrt(2))) |
cdf complement |
Using the relation: q = 0.5 * erfc((x-m)/(s*sqrt(2))) |
quantile |
Using the relation: x = m - s * sqrt(2) * erfc_inv(2*p) |
quantile from the complement |
Using the relation: x = m + s * sqrt(2) * erfc_inv(2*p) |
mean and standard deviation |
The same as |
mode |
The same as the mean. |
median |
The same as the mean. |
skewness |
0 |
kurtosis |
3 |
kurtosis excess |
0 |