среда, 25 октября 2023 г.

PDF, CDF, Handy

As Variant number 101

sealed class RandLib

{

    public static double NormalPDF(double x){

        return Math.Exp(-0.5 * x * x) / Math.Sqrt(2.0 * Math.PI);

    }

    public static double NormalCDF(double x){

        if (x == 0.0) return 0.5;

        if (x < 0.0) return 1.0 - NormalCDF(-x);

        double[] q = {

            0.6716419, 0.219386786, -0.53563782,

            1.6829937, -1.82639342, 1.7255361 };

        double t = 1.0 / (1.0 + q[0] * x);

        double sum = t * (q[1] + t * (q[2] + t * (q[3] + t * (q[4] + t * (q[5])))));

        double Phi = 1.0 - NormalPDF(x) * sum;

        return Phi;

    }

}

---------------------------------------------------------------------------------------------------------

double Phi(double x)

        {

            // constants

            double a1 = 0.254829592;

            double a2 = -0.284496736;

            double a3 = 1.421413741;

            double a4 = -1.453152027;

            double a5 = 1.061405429;

            double p = 0.3275911;


            // Save the sign of x

            int sign = 1;

            if (x < 0)

                sign = -1;

            x = Math.Abs(x) / Math.Sqrt(2.0);

            // A&S formula 7.1.26

            double t = 1.0 / (1.0 + p * x);

            double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x);


            return 0.5 * (1.0 + sign * y);

        }

Комментариев нет:

Отправить комментарий