I was wondering if there were statistics functions built into math libraries that are part of the standard C++ libraries like cmath. If not, can you guys recommend a good stats library that would have a cumulative normal distribution function? Thanks in advance.
More specifically, I am looking to use/create a cumulative distribution function.
I figured out how to do it using gsl, at the suggestion of the folks who answered before me, but then found a non-library solution (hopefully this helps many people out there who are looking for it like I was):
Boost is as good as the standard :D here you go: boost maths/statistical.
The implementations of the normal CDF given here are single precision approximations that have had
float
replaced withdouble
and hence are only accurate to 7 or 8 significant (decimal) figures.For a VB implementation of Hart's double precision approximation, see figure 2 of West's Better approximations to cumulative normal functions.
Edit: My translation of West's implementation into C++:
Note that I have rearranged expressions into the more familiar forms for series and continued fraction approximations. The last magic number in West's code is the square root of 2π, which I've deferred to the compiler on the first line by exploiting the identity acos(0) = ½ π.
I've triple checked the magic numbers, but there's always the chance that I've mistyped something. If you spot a typo, please comment!
The results for the test data John Cook used in his answer are
I take some small comfort from the fact that they agree to all of the digits given for the Mathematica results.
From NVIDIA CUDA samples:
Copyright 1993-2012 NVIDIA Corporation. All rights reserved.
Theres is no straight function. But since the gaussian error function and its complementary function is related to the normal cumulative distribution function (see here, or here) we can use the implemented c-function
erfc
(complementary error function):Which considers the relation of
erfc(x) = 1-erf(x)
withM_SQRT1_2
= √0,5.I use it for statistical calculations and it works great. No need for using coefficients.
Here's a stand-alone C++ implementation of the cumulative normal distribution in 14 lines of code.
http://www.johndcook.com/cpp_phi.html