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


6 comments:

  1. 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 .

    ReplyDelete
  2. All thanks to Dr Raymond for helping me to get my penis longer and bigger with his cream in just two week his cream is very active and also it work fast for everyone who have used it, the best thing about his herbal cream is that it does not have side effects and the results is permanent. Note he can also cure the below sicknesses and disease PREMATURE EJACULATIONLOW SPERM COUNTERECTILE DYSFUNCTIONHIV/AIDS CUREHERPES CUREINFECTION CUREPILESTROKEHEART FAILUREYou can email him if you need his help via: drraymondherbalcenter@gmail.com or WhatsApp him via: +2348116744524

    ReplyDelete

  3. I'm 40 years old female I tested genital herpes (HSV1-2) positive in 2016. I was having bad outbreaks. EXTREMELY PAINFUL. I have try different kinds of drugs and treatment by the medical doctors all to know was avail. Six months ago I was desperately online searching for a helpful remedies for genital herpes (HSV1-2) cure, which I come across some helpful remedies on how Dr OYAGU have help so many people in curing genital herpes (HSV1-2) with the help of herbal treatment because I too believe there is someone somewhere in the world who can cure herpes completely. At of the past 2 months, however, I've been following his herpes protocol Via oyaguherbalhome@gmail.com or +2348101755322 and it stopped all outbreaks completely! To my greatest surprise I was cured completely by following the protocol of his herbal medicine . Don't be discouraged by the medical doctors. There is a cure for HSV with the help of herbs and roots by a herbalist Dr call Dr OYAGU he is so kind and truthful with his herbal treatment, kindly contact him for more information Via oyaguherbalhome@gmail.com or WhatsApp DR on +2348101755322 or visit his website https://oyaguspellcaster.wixsite.com/oyaguherbalhome you will be lucky as I am today

    ReplyDelete
  4. I'm so excited to share my testimonies about the Good Work of Dr odoma who cured my HIV/AIDS with his herbs, I never thought that I would live on earth before the year runs out. I have been suffering from a deadly disease (HIV) for the past (2)years now; I have spent a lot of money going from one Hospital to another looking for ways to get rid of this disease, the hospital has been my home everyday. Constant checks up have been my hobby not until this fateful day, I was searching
    through the internet, I saw a testimony on how Dr Odoma helped someone in curing his HIV disease using his healing
    Herbs, quickly I copied his email just to give him a test I spoke to him, he asked me to a preparation for the herbs items, of which I did exactly what he instructed me to do, he told me that he is going to provide the herbal cure to me, which he
    did, few days after i went back to the same hospital that previously confirmed me as HIV/AIDS positive and this time the result declared me negative to the deadly disease of HIV/AIDS....so I decided to share my testimony, that nothing is impossible with God, God use a man to healed me. No matter what you are passing through, no matter how deadly the sickness is and no matter what the situation is, God that did mine is still going to do yours, suffering from AIDS, ALS, asthma.. Cancer, any kind of disease, you can reach him now via ? Gmail address: (DR odomasolutionhome20@gmail. com) WhatsApp number (+2348100649947)

    ReplyDelete
  5. I got diagnose of herpes virus last year, and i was taking some drugs prescribed for me by my family doc the drugs could not work and the herpes in my system was very terrifying that i was so depressed the good news is that i never gave up in searching for natural cure for herpes virus ,cause i believe so much in herbal medication.. One faithful morning i read a comment from a lady called Destiny Hudson on how she was cured with natural herbs made by DR.OYAGU from somewhere is West Africa, i immediately copied out his contact email via oyaguherbalhome@gmail.com and explain all my herpes problem to him via his email. the big truth is that it took just two week for his herbal medication to cure me completely without side affect. Till date am herpes virus free and all thanks to Dr OYAGU for his good deeds for me, Once again am very happy to share this great testimony of DR OYAGU cure hurry up now and contact him via his email address (oyaguherbalhome@gmail.com Call & WhatsApp him on (+2348101755322 ) and see what he can do

    ReplyDelete
  6. Life is always beautiful when you have good health.i have be in pain for almost 1 year had HSV 1&2 and I was lonely and sad, luckily I was directed to a very kind and Great Dr Oliha who helped me cure my HSV 1&2 and today I am free from the virus and very healthy thank you so much Dr Oliha .Email
    oliha.miraclemedicine@gmail.com
    you can also whatsapp/Call him; +2349038382931

    ReplyDelete

Logistic Regression Calculator and ROC Curve Plotter

This blog post implements a Logistic Regression calculator for a binary output. Consider a binary outcome response variable \(Y\...