Magic numbers in C ++ implementation of Excel NORMDIST function

While searching for a C ++ Excel implementation NORMDIST(cumulative) I found this on a website :

static double normdist(double x, double mean, double standard_dev)
{
    double res;
    double x=(x - mean) / standard_dev;
    if (x == 0)
    {
        res=0.5;
    }
    else
    {
        double oor2pi = 1/(sqrt(double(2) * 3.14159265358979323846));
        double t = 1 / (double(1) + 0.2316419 * fabs(x));
        t *= oor2pi * exp(-0.5 * x * x) 
             * (0.31938153   + t 
             * (-0.356563782 + t
             * (1.781477937  + t 
             * (-1.821255978 + t * 1.330274429))));
        if (x >= 0)
        {
            res = double(1) - t;
        }
        else
        {
            res = t;
        }
    }
    return res;
}

My limited knowledge of mathematics made me think about the Taylor series , but I can’t determine where these numbers come from:

0.2316419 0.31938153 -0.356563782 1.781477937 -1.821255978, 1.330274429

Can anyone suggest where they came from and how they can be obtained?

+3
source share
1 answer

Check out numerical recipes, chapter 6.2.2. Approximation is standard. Recall that

NormCdf(x) = 0.5 * (1 + erf(x / sqrt(2)))
erf(x) = 2 / (sqrt(pi)) integral(e^(-t^2) dt, t = 0..x)

and write erf as

1 - erf x ~= t * exp(-x^2 + P(t))

for positive x, where

t = 2 / (2 + x)

t 0 1, P ( , 5.8), : , , . L ^ 2 norm, (= sup).

.

1 - erf x = t * exp(-x^2) * P(t)

, normCdf , erf.

"", , , , b*exp(-a*z^2)*y(t), . erfc (x), (1978) [http://www.ams.org/journals/mcom/1978-32-144/S0025-5718-1978-0494846-8/S0025-5718-1978-0494846-8.pdf]

Numerical Recipes 3- 6.2.2 C t*exp(-z^2 + c0 + c1*t+ c2t^2 + c3*t^3 + ... + c9t^9)

+9

Source: https://habr.com/ru/post/1790728/


All Articles