Returns the cumulative distribution function of a double value. - Java java.lang

Java examples for java.lang:Math Value

Description

Returns the cumulative distribution function of a double value.

Demo Code

/*//from   ww w  . j a v  a 2  s . c  om
 * ------------------------------------------------------------------------=
 * Copyright (C) 1997 - 1998 by Visual Numerics, Inc. All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software is freely
 * granted by Visual Numerics, Inc., provided that the copyright notice
 * above and the following warranty disclaimer are preserved in human
 * readable form.
 *
 * Because this software is licenses free of charge, it is provided
 * "AS IS", with NO WARRANTY.  TO THE EXTENT PERMITTED BY LAW, VNI
 * DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO ITS PERFORMANCE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * VNI WILL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING OUT OF THE USE
 * OF OR INABILITY TO USE THIS SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, PUNITIVE, AND EXEMPLARY DAMAGES, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * ------------------------------------------------------------------------=
 */

public class Main{
    private static final double ERFC_COEF[] = {
            -.490461212346918080399845440334e-1,
            -.142261205103713642378247418996e0,
            .100355821875997955757546767129e-1,
            -.576876469976748476508270255092e-3,
            .274199312521960610344221607915e-4,
            -.110431755073445076041353812959e-5,
            .384887554203450369499613114982e-7,
            -.118085825338754669696317518016e-8,
            .323342158260509096464029309534e-10,
            -.799101594700454875816073747086e-12,
            .179907251139614556119672454866e-13,
            -.371863548781869263823168282095e-15,
            .710359900371425297116899083947e-17,
            -.126124551191552258324954248533e-18 };
    private static final double ERFC2_COEF[] = {
            -.69601346602309501127391508262e-1,
            -.411013393626208934898221208467e-1,
            .391449586668962688156114370524e-2,
            -.490639565054897916128093545077e-3,
            .715747900137703638076089414183e-4,
            -.115307163413123283380823284791e-4,
            .199467059020199763505231486771e-5,
            -.364266647159922287393611843071e-6,
            .694437261000501258993127721463e-7,
            -.137122090210436601953460514121e-7,
            .278838966100713713196386034809e-8,
            -.581416472433116155186479105032e-9,
            .123892049175275318118016881795e-9,
            -.269063914530674343239042493789e-10,
            .594261435084791098244470968384e-11,
            -.133238673575811957928775442057e-11,
            .30280468061771320171736972433e-12,
            -.696664881494103258879586758895e-13,
            .162085454105392296981289322763e-13,
            -.380993446525049199987691305773e-14,
            .904048781597883114936897101298e-15,
            -.2164006195089607347809812047e-15,
            .522210223399585498460798024417e-16,
            -.126972960236455533637241552778e-16,
            .310914550427619758383622741295e-17,
            -.766376292032038552400956671481e-18,
            .190081925136274520253692973329e-18 };
    private static final double ERFCC_COEF[] = {
            .715179310202924774503697709496e-1,
            -.265324343376067157558893386681e-1,
            .171115397792085588332699194606e-2,
            -.163751663458517884163746404749e-3,
            .198712935005520364995974806758e-4,
            -.284371241276655508750175183152e-5,
            .460616130896313036969379968464e-6,
            -.822775302587920842057766536366e-7,
            .159214187277090112989358340826e-7,
            -.329507136225284321486631665072e-8,
            .72234397604005554658126115389e-9,
            -.166485581339872959344695966886e-9,
            .401039258823766482077671768814e-10,
            -.100481621442573113272170176283e-10,
            .260827591330033380859341009439e-11,
            -.699111056040402486557697812476e-12,
            .192949233326170708624205749803e-12,
            -.547013118875433106490125085271e-13,
            .158966330976269744839084032762e-13,
            -.47268939801975548392036958429e-14,
            .14358733767849847867287399784e-14,
            -.444951056181735839417250062829e-15,
            .140481088476823343737305537466e-15,
            -.451381838776421089625963281623e-16,
            .147452154104513307787018713262e-16,
            -.489262140694577615436841552532e-17,
            .164761214141064673895301522827e-17,
            -.562681717632940809299928521323e-18,
            .194744338223207851429197867821e-18 };
    /**
     * Returns the cumulative distribution function of a double value.
     * 
     * @param x
     *            A double value.
     * @return Returns the cumulative distribution function of a double value.
     */
    static public double cdf(double x) {
        // Phi(x) = 0.5 * erfc(-x/sqrt(2))
        double ans = 0.5 * MathUtils.erfc(-x / Math.sqrt(2));
        return ans;
    }
    /**
     * Returns the cumulative distribution function of a pair of double values.
     * 
     * @param x
     *            A double value.
     * @param y
     *            A double value.
     * @return Returns the cumulative distribution function of a pair of double
     *         values.
     */
    static public double cdf(double x, double y) {
        double ans;
        double a = x;
        double b = y;
        double temp = 0.0;

        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
        // Phi(x) = 0.5 * erfc(-x/sqrt(2))
        double sqrt2 = Math.sqrt(2);
        double ansA = 0.5 * MathUtils.erfc(-a / sqrt2);
        double ansB = 0.5 * MathUtils.erfc(-b / sqrt2);
        ans = ansB - ansA;
        return ans;
    }
    /**
     * Returns the cumulative distribution function of a pair of double values.
     * 
     * @param x
     *            A double value.
     * @param mean
     *            The mean of the gaussian distribution.
     * @param variance
     *            The variance of the gaussian distribution.
     * @return Returns the cumulative distribution function of a pair of double
     *         values.
     */
    static public double cdf(double x, double mean, double variance) {
        double a;
        double stdDev = Math.sqrt(variance);
        if (stdDev == 0.0) {
            stdDev = 1.0;
        }
        a = (x - mean) / stdDev;
        // Phi(x) = 0.5 * erfc(-x/sqrt(2))
        double ans = 0.5 * MathUtils.erfc(-a / Math.sqrt(2));
        return ans;
    }
    /**
     * Returns the cumulative distribution function of a pair of double values.
     * 
     * @param x
     *            A double value.
     * @param y
     *            A double value.
     * @param mean
     *            The mean of the gaussian distribution.
     * @param variance
     *            The variance of the gaussian distribution.
     * @return Returns the cumulative distribution function of a pair of double
     *         values.
     */
    static public double cdf(double x, double y, double mean,
            double variance) {
        double ans;
        double a = x;
        double b = y;
        double temp = 0.0;
        double stdDev = Math.sqrt(variance);
        if (a > b) {
            temp = a;
            a = b;
            b = temp;
        }
        if (stdDev == 0.0) {
            stdDev = 1.0;
        }
        a = (a - mean) / stdDev;
        b = (b - mean) / stdDev;
        // Phi(x) = 0.5 * erfc(-x/sqrt(2))
        double sqrt2 = Math.sqrt(2);
        double ansA = 0.5 * MathUtils.erfc(-a / sqrt2);
        double ansB = 0.5 * MathUtils.erfc(-b / sqrt2);
        ans = ansB - ansA;
        return ans;
    }
    /**
     * Returns the complementary error function of a double.
     * 
     * @param x
     *            A double value.
     * @return The complementary error function of x.
     */
    static public double erfc(double x) {
        double ans;
        double y = Math.abs(x);

        if (x <= -6.013687357) {
            // -6.013687357 =
            // -Math.sqrt(-Math.log(1.77245385090551602729816748334 *
            // EPSILON_SMALL))
            ans = 2;
        } else if (y < 1.49012e-08) {
            // 1.49012e-08 = Math.sqrt(2*EPSILON_SMALL)
            ans = 1 - 2 * x / 1.77245385090551602729816748334;
        } else {
            double ysq = y * y;
            if (y < 1) {
                ans = 1 - x * (1 + csevl(2 * ysq - 1, ERFC_COEF));
            } else if (y <= 4.0) {
                ans = Math.exp(-ysq)
                        / y
                        * (0.5 + csevl((8.0 / ysq - 5.0) / 3.0, ERFC2_COEF));
                if (x < 0)
                    ans = 2.0 - ans;
                if (x < 0)
                    ans = 2.0 - ans;
                if (x < 0)
                    ans = 2.0 - ans;
            } else {
                ans = Math.exp(-ysq) / y
                        * (0.5 + csevl(8.0 / ysq - 1, ERFCC_COEF));
                if (x < 0)
                    ans = 2.0 - ans;
            }
        }
        return ans;
    }
    static double csevl(double x, double coef[]) {
        double b0, b1, b2, twox;
        int i;
        b1 = 0.0;
        b0 = 0.0;
        b2 = 0.0;
        twox = 2.0 * x;
        for (i = coef.length - 1; i >= 0; i--) {
            b2 = b1;
            b1 = b0;
            b0 = twox * b1 - b2 + coef[i];
        }
        return 0.5 * (b0 - b2);
    }
}

Related Tutorials