Tuesday, 17 September 2013

Numerical Estimate of the Inverse Error Function

The function that numerically approximates the inverse error (erf) function $[\large \frac{2}{\sqrt{\pi}}\int_0^xe^{-t^2}dt]^{-1}$ is captured in the C function below - it's a fragment of the source code of my (now free) iOS App SciStatCalc. Note the one iteration of Halley's method to refine the result.

Feel free to use it, if you wish to do so.

double inverf(double x)
    double w,p;
    w = -log((1.00-x)*(1.00+x));
    if ( w < 5.000000 ) {
        w = w - 2.500000;
        p= 2.81022636e-08;
        p= 3.43273939e-07 + p*w;
        p= -3.5233877e-06 + p*w;
        p = -4.39150654e-06 + p*w;
        p = 0.00021858087 + p*w;
        p = -0.00125372503 + p*w;
        p = -0.00417768164 + p*w;
        p = 0.246640727 + p*w;
        p = 1.50140941 + p*w;
    } else
        w = sqrtf(w) - 3.000000;
        p =  -0.000200214257;
        p = 0.000100950558 + p*w;
        p = 0.00134934322 + p*w;
        p = -0.00367342844 + p*w;
        p = 0.00573950773 + p*w;
        p = -0.0076224613 + p*w;
        p = 0.00943887047 + p*w;
        p = 1.00167406 + p*w;
        p = 2.83297682 + p*w;
    double res_ra = p*x; // assign to rational estimate variable
    // Halley's method to refine estimate of inverse erf
    double res_hm = 0;
    double fx = (erf(res_ra) - x);
    double df = (2.0/sqrt(M_PI))*exp(-(res_ra * res_ra));
    double d2f = -2 * res_ra * df;
    res_hm = res_ra - (2*fx*df)/((2*df*df) - (fx*d2f));
    return res_hm;

The above function can be used to estimate the inverse cumulative distribution function (quantile) of the Gaussian distribution (mu is the mean, and sig the standard deviation of the distribution), as below:-

double gauss_icdf(double p, double mu, double sig)
    double res;
    res = (inverf((2*p) - 1) *sig*sqrt(2.0)) + mu;
    return res;


  1. Hello Everyone out there,I am here to give my testimony about a Herbalist doctor who helped me . I was infected with HERPES SIMPLEX VIRUS in 2011, i went to many hospitals for cure but there was no solution, so I was thinking how can I get a solution out so that my body can be okay. One day I was in the river side thinking where I can go to get solution. so a lady walked to me telling me why am I so sad and i open up all to her telling her my problem, she told me that she can help me out, she introduce me to a doctor who uses herbal medication to cure HERPES SIMPLEX VIRUS and gave me his email, so i mail him. He told me all the things I need to do and also give me instructions to take, which I followed properly. Before I knew what is happening after two weeks the HERPES SIMPLEX VIRUS that was in my body got vanished . so if you are also heart broken and also need a help, you can also email him at {oliha.miraclemedicine@gmail.com}
    or whatsapp him number: +2349038382931. or website is https://olihamiraclemedicine.webs.com/
    Contact him today and you will have a testimony...Good luck!

    Dr. OLIHA Also Cures:
    1. HIV/AIDS
    2. HERPES 1/2
    3. CANCER
    4. ALS (Lou Gehrig’s disease)
    5. Hepatitis B
    6. chronic pancreatic
    7. Emphysema
    8. COPD (Chronic Obstructive Pulmonary Disease

  2. Am really happy that i have been cured from HSV1&2 DISEASE with the herbal medicine of Dr otaigbe have been suffering from this disease for a long time now without solution until i came across the email of this doctor who have cure so many people with his herbal medicine, i also choose to give him a chance to help me, he told me what to do and i kindly did it, and he gave me his herbal medicine and direct me on how to use it, i also follow his instructions for use and he ask me to go for a check up after 3 months which i did, to my greatest surprise my result came out as negative, i am really happy that there is someone like this doctor who is ready to help anytime any day. To all the readers and viewers that is doubting this testimony stop doubting it and contact this doctor if you have any disease and see if he will not actually help you. I know that there are some people out there who are really suffering and hurting their family just because of these diseases. you can get to him through email:DROTAIGBESPELLHOME@GMAIL. COM or WhatsApp him +2349066103332 .