Accord.Math.Normal.HighAccuracyComplemented C# (CSharp) Method

HighAccuracyComplemented() public static method

High-accuracy Complementary normal distribution function.

This function uses 9 tabled values to provide tail values of the normal distribution, also known as complementary Phi, with an absolute error of 1e-14 ~ 1e-16.

References: - George Marsaglia, Evaluating the Normal Distribution, 2004. Available in: http://www.jstatsoft.org/v11/a05/paper
public static HighAccuracyComplemented ( double x ) : double
x double
return double
        public static double HighAccuracyComplemented(double x)
        {
            int j = (int)(0.5 * (Math.Abs(x) + 1));

            if (j >= high_R.Length)
                return x > 0 ? 0 : 1;

            double a = high_R[j];
            double z = 2 * j;
            double b = a * z - 1.0;

            double h = Math.Abs(x) - z;
            double q = h * h;
            double pwr = 1;

            double sum = a + h * b;
            double term = a;


            for (int i = 2; sum != term; i += 2)
            {
                term = sum;

                a = (a + z * b) / (i);
                b = (b + z * a) / (i + 1);
                pwr *= q;

                sum = term + pwr * (a + h * b);
            }

            sum *= Math.Exp(-0.5 * x * x - 0.91893853320467274178);

            return (x >= 0) ? sum : (1.0 - sum);
        }