Search code examples
c++matlabcdf

Cumulative Normal Distribution Function in C/C++ with parameters


I want to implement equivalent of matlab normcdf function in C++, I have already found this useful post: Cumulative Normal Distribution Function in C/C++ pointing to this implementation http://www.johndcook.com/cpp_phi.html. But I want it with optional mu and sigma parameters as in matlab.

Is it ok, when I change this:

x = fabs(x)/sqrt(2.0);

to:

x = fabs(x - mu)/sqrt(2.0 * sigma * sigma);

or I am supposed to do something else?


Solution

  • Watch out - you want to save the sign of x-mu, not just of x:

    int sign = 1;
    if (x < mu)
        sign = -1;
    x = fabs(x-mu)/sqrt(2.0*sigma*sigma);
    

    Otherwise your scaling is correct.