High-accuracy Normal cumulative distribution function. - CSharp System

CSharp examples for System:Math Number

Description

High-accuracy Normal cumulative distribution function.

Demo Code

// Copyright (C) 2013, Alan Genz,  All rights reserved.               
using System;/*from ww w .  j a  va  2 s . c  o  m*/

public class Main{
        /// <summary>
        ///     High-accuracy Normal cumulative distribution function.
        /// </summary>
        /// <remarks>
        ///     <para>
        ///         The following formula provide probabilities with an absolute error
        ///         less than 8e-16.
        ///     </para>
        ///     <para>
        ///         References:
        ///         - George Marsaglia, Evaluating the Normal Distribution, 2004.
        ///         Available in: http://www.jstatsoft.org/v11/a05/paper
        ///     </para>
        /// </remarks>
        public static double HighAccuracyFunction(double x)
        {
            double sum = x;
            double term = 0;

            double nextTerm = x;
            double pwr = x*x;
            double i = 1;

            // Iterate until adding next terms doesn't produce
            // any change within the current numerical accuracy.

            while (sum != term)
            {
                term = sum;

                // Next term
                nextTerm *= pwr/(i += 2);

                sum += nextTerm;
            }

            return 0.5 + sum*Math.Exp(-0.5*pwr - 0.5*Constants.Log2PI);
        }
}

Related Tutorials