How to implement the standard normal cumulative distribution function in C (or other language)

2.7k views Asked by At

First of all, for those of you who don't know this law, don't be afraid it's actually pretty simple. On this link http://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model you will see this law from a mathematical point of view. Go on the section notation and look at the function that starts by N(x)=1/sqrt(2*PI)... I am implementing the Black-Scholes model in C as you might have guess and I don't know how to implement this function, I have found an implementation online but I am not sure if I should be happy about it, it seems a little off. This is the code I am using.

double N(double z){
    const double b1=0.31938153;
    const double b2=-0.356563782;
    const double b3=1.781477937;
    const double b4=-1.821255978;
    const double b5=1.330274429;
    const double p=0.2316419;

    double a=fabs(z);
    double t=1.0/(1.0+a*p);
    double w=1.0-1.0/sqrt(2*M_PI)*exp(-a*a/2)*(b1*t+b2*t*t+b3*pow(t,3)+b4*pow(t,4)+b5*pow(t,5));
    if(z<0.0)
        w=1.0-w;
    return w;
}

What I would like is for you to tell me if this implementation of the law is correct and why it is. Thank you very much in advance.

1

There are 1 answers

3
Robert Dodier On BEST ANSWER

The standard normal cumulative distribution function is exactly (1/2)*(1 + erf(z/sqrt(2))) where erf is the Gaussian error function, which is found in many C programming libraries. Check the development environment you are using -- chances are good that erf is already in one of its libraries.