views:

726

answers:

3

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.

+5  A: 

Boost is as good as the standard :D here you go: boost maths/statistical.

Hassan Syed
Is there a standard one built in?
Tyler Brock
No, the standard library does not yet have any.
dirkgently
normal distribution, yes I think so. Or are you talking about built into the standard library -- in the latter case, no.
Hassan Syed
Thanks, yes, the reason I'm asking is because I want to use as much of the standard stuff as possible. I guess there is no way getting around it in this case.
Tyler Brock
3 reasons for using boost: 1) You can copy out libraries of your choosing using a utility that comes with boost. 2) the libraries tend to be header only. 3) most libraries work very well and work everywhere with a C++ compiler.
Hassan Syed
+1  A: 

Here's a stand-alone C++ implementation of the cumulative normal distribution in 14 lines of code.

http://www.johndcook.com/cpp_phi.html

John D. Cook
Thank you for providing this as well.
Tyler Brock
+1  A: 

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

#ifndef Pi 
#define Pi 3.141592653589793238462643 
#endif 

double cnd_manual(double x)
{
  double L, K, w ;
  /* constants */
  double const a1 = 0.31938153, a2 = -0.356563782, a3 = 1.781477937;
  double const a4 = -1.821255978, a5 = 1.330274429;

  L = fabs(x);
  K = 1.0 / (1.0 + 0.2316419 * L);
  w = 1.0 - 1.0 / sqrt(2 * Pi) * exp(-L *L / 2) * (a1 * K + a2 * K *K + a3 * pow(K,3) + a4 * pow(K,4) + a5 * pow(K,5));

  if (x < 0 ){
    w= 1.0 - w;
  }
  return w;
}
Tyler Brock