Cumulative Normal Distribution Function in C/C++

后端 未结 7 2191
走了就别回头了
走了就别回头了 2020-12-07 14:15

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 st

7条回答
  •  庸人自扰
    2020-12-07 14:32

    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;
    }
    

提交回复
热议问题