Ejemplo n.º 1
0
def test_common_shape():
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert_array_equal(common_shape(A, B, C), (4, 5))
    assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert_array_equal(common_shape(A, B, C), (4, 5))
    assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
Ejemplo n.º 2
0
def test_common_shape():
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert common_shape(A, B, C) == (4, 5)
    assert common_shape(A, B, C, shape=(3, 4, 1)) == (3, 4, 5)
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert common_shape(A, B, C) == (4, 5)
    assert common_shape(A, B, C, shape=(3, 4, 1)) == (3, 4, 5)
Ejemplo n.º 3
0
def test_common_shape():
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert_array_equal(common_shape(A, B, C), (4, 5))
    assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
    A = np.ones((4, 1))
    B = 2
    C = np.ones((1, 5)) * 5
    assert_array_equal(common_shape(A, B, C), (4, 5))
    assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
Ejemplo n.º 4
0
def cdfnorm2d(b1, b2, r):
    '''
    Returnc Bivariate Normal cumulative distribution function

    Parameters
    ----------

    b1, b2 : array-like
        upper integration limits
    r : real scalar
        correlation coefficient  (-1 <= r <= 1).

    Returns
    -------
    bvn : ndarray
        distribution function evaluated at b1, b2.

    Notes
    -----
    CDFNORM2D computes bivariate normal probabilities, i.e., the probability
    Prob(X1 <= B1 and X2 <= B2) with an absolute error less than 1e-15.

    This function is based on the method described by Drezner, z and
    G.O. Wesolowsky, (1989), with major modifications for double precision,
    and for |r| close to 1.

    Example
    -------
    >>> import wafo.gaussian as wg
    >>> x = np.linspace(-5,5,20)
    >>> [B1,B2] = np.meshgrid(x, x)
    >>> r  = 0.3;
    >>> F = wg.cdfnorm2d(B1,B2,r)

    surf(x,x,F)

    See also
    --------
    cdfnorm

    Reference
    ---------
    Drezner, z and g.o. Wesolowsky, (1989),
    "On the computation of the bivariate normal integral",
    Journal of statist. comput. simul. 35, pp. 101-107,
    '''
    # Translated into Python
    # Per A. Brodtkorb
    #
    # Original code
    # by  alan genz
    #     department of mathematics
    #     washington state university
    #     pullman, wa 99164-3113
    #     email : [email protected]

    cshape = common_shape(b1, b2, r, shape=[1, ])
    one = ones(cshape)

    h, k, r = (-b1 * one).ravel(), (-b2 * one).ravel(), (r * one).ravel()

    bvn = where(abs(r) > 1, nan, 0.0)

    two = 2.e0
    twopi = 6.283185307179586e0

    hk = h * k

    k0, = nonzero(abs(r) < 0.925e0)
    if len(k0) > 0:
        hs = (h[k0] ** 2 + k[k0] ** 2) / two
        asr = arcsin(r[k0])
        k1, = nonzero(r[k0] >= 0.75)
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(10):
                for sign in - 1, 1:
                    sn = sin(asr[k1] * (sign * _X20[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W20[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        k1, = nonzero((0.3 <= r[k0]) & (r[k0] < 0.75))
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(6):
                for sign in - 1, 1:
                    sn = sin(asr[k1] * (sign * _X12[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W12[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        k1, = nonzero(r[k0] < 0.3)
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(3):
                for sign in - 1, 1:
                    sn = sin(asr[k1] * (sign * _X6[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W6[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        bvn[k0] *= asr / (two * twopi)
        bvn[k0] += fi(-h[k0]) * fi(-k[k0])

    k1, = nonzero((0.925 <= abs(r)) & (abs(r) <= 1))
    if len(k1) > 0:
        k2, = nonzero(r[k1] < 0)
        if len(k2) > 0:
            k12 = k1[k2]
            k[k12] = -k[k12]
            hk[k12] = -hk[k12]

        k3, = nonzero(abs(r[k1]) < 1)
        if len(k3) > 0:
            k13 = k1[k3]
            a2 = (1 - r[k13]) * (1 + r[k13])
            a = sqrt(a2)
            b = abs(h[k13] - k[k13])
            bs = b * b
            c = (4.e0 - hk[k13]) / 8.e0
            d = (12.e0 - hk[k13]) / 16.e0
            asr = -(bs / a2 + hk[k13]) / 2.e0
            k4, = nonzero(asr > -100.e0)
            if len(k4) > 0:
                bvn[k13[k4]] = (a[k4] * exp(asr[k4]) *
                                (1 - c[k4] * (bs[k4] - a2[k4]) *
                                 (1 - d[k4] * bs[k4] / 5) / 3 +
                                 c[k4] * d[k4] * a2[k4] ** 2 / 5))

            k5, = nonzero(hk[k13] < 100.e0)
            if len(k5) > 0:
                #               b = sqrt(bs);
                k135 = k13[k5]
                bvn[k135] = bvn[k135] - exp(-hk[k135] / 2) * sqrt(twopi) * fi(-b[k5] / a[k5]) * \
                    b[k5] * (1 - c[k5] * bs[k5] * (1 - d[k5] * bs[k5] / 5) / 3)

            a /= two
            for i in range(10):
                for sign in - 1, 1:
                    xs = (a * (sign * _X20[i] + 1)) ** 2
                    rs = sqrt(1 - xs)
                    asr = -(bs / xs + hk[k13]) / 2
                    k6, = nonzero(asr > -100.e0)
                    if len(k6) > 0:
                        k136 = k13[k6]
                        bvn[k136] += (a[k6] * _W20[i] * exp(asr[k6]) *
                                      (exp(-hk[k136] * (1 - rs[k6]) /
                                           (2 * (1 + rs[k6]))) / rs[k6] -
                                       (1 + c[k6] * xs[k6] *
                                        (1 + d[k6] * xs[k6]))))

            bvn[k3] = -bvn[k3] / twopi

        k7, = nonzero(r[k1] > 0)
        if len(k7):
            k17 = k1[k7]
            bvn[k17] += fi(-np.maximum(h[k17], k[k17]))

        k8, = nonzero(r[k1] < 0)
        if len(k8) > 0:
            k18 = k1[k8]
            bvn[k18] = -bvn[k18] + np.maximum(0, fi(-h[k18]) - fi(-k[k18]))

    bvn.shape = cshape
    return bvn
Ejemplo n.º 5
0
def cdfnorm2d(b1, b2, r):
    '''
    Returnc Bivariate Normal cumulative distribution function

    Parameters
    ----------

    b1, b2 : array-like
        upper integration limits
    r : real scalar
        correlation coefficient  (-1 <= r <= 1).

    Returns
    -------
    bvn : ndarray
        distribution function evaluated at b1, b2.

    Notes
    -----
    CDFNORM2D computes bivariate normal probabilities, i.e., the probability
    Prob(X1 <= B1 and X2 <= B2) with an absolute error less than 1e-15.

    This function is based on the method described by Drezner, z and
    G.O. Wesolowsky, (1989), with major modifications for double precision,
    and for |r| close to 1.

    Example
    -------
    >>> import wafo.gaussian as wg
    >>> x = np.linspace(-5,5,20)
    >>> [B1,B2] = np.meshgrid(x, x)
    >>> r  = 0.3;
    >>> F = wg.cdfnorm2d(B1,B2,r)

    surf(x,x,F)

    See also
    --------
    cdfnorm

    Reference
    ---------
    Drezner, z and g.o. Wesolowsky, (1989),
    "On the computation of the bivariate normal integral",
    Journal of statist. comput. simul. 35, pp. 101-107,
    '''
    # Translated into Python
    # Per A. Brodtkorb
    #
    # Original code
    # by  alan genz
    #     department of mathematics
    #     washington state university
    #     pullman, wa 99164-3113
    #     email : [email protected]

    cshape = common_shape(b1, b2, r, shape=[
        1,
    ])
    one = ones(cshape)

    h, k, r = (-b1 * one).ravel(), (-b2 * one).ravel(), (r * one).ravel()

    bvn = where(abs(r) > 1, nan, 0.0)

    two = 2.e0
    twopi = 6.283185307179586e0

    hk = h * k

    k0, = nonzero(abs(r) < 0.925e0)
    if len(k0) > 0:
        hs = (h[k0]**2 + k[k0]**2) / two
        asr = arcsin(r[k0])
        k1, = nonzero(r[k0] >= 0.75)
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(10):
                for sign in -1, 1:
                    sn = sin(asr[k1] * (sign * _X20[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W20[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        k1, = nonzero((0.3 <= r[k0]) & (r[k0] < 0.75))
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(6):
                for sign in -1, 1:
                    sn = sin(asr[k1] * (sign * _X12[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W12[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        k1, = nonzero(r[k0] < 0.3)
        if len(k1) > 0:
            k01 = k0[k1]
            for i in range(3):
                for sign in -1, 1:
                    sn = sin(asr[k1] * (sign * _X6[i] + 1) / 2)
                    bvn[k01] = bvn[k01] + _W6[i] * \
                        exp((sn * hk[k01] - hs[k1]) / (1 - sn * sn))

        bvn[k0] *= asr / (two * twopi)
        bvn[k0] += fi(-h[k0]) * fi(-k[k0])

    k1, = nonzero((0.925 <= abs(r)) & (abs(r) <= 1))
    if len(k1) > 0:
        k2, = nonzero(r[k1] < 0)
        if len(k2) > 0:
            k12 = k1[k2]
            k[k12] = -k[k12]
            hk[k12] = -hk[k12]

        k3, = nonzero(abs(r[k1]) < 1)
        if len(k3) > 0:
            k13 = k1[k3]
            a2 = (1 - r[k13]) * (1 + r[k13])
            a = sqrt(a2)
            b = abs(h[k13] - k[k13])
            bs = b * b
            c = (4.e0 - hk[k13]) / 8.e0
            d = (12.e0 - hk[k13]) / 16.e0
            asr = -(bs / a2 + hk[k13]) / 2.e0
            k4, = nonzero(asr > -100.e0)
            if len(k4) > 0:
                bvn[k13[k4]] = (a[k4] * exp(asr[k4]) *
                                (1 - c[k4] * (bs[k4] - a2[k4]) *
                                 (1 - d[k4] * bs[k4] / 5) / 3 +
                                 c[k4] * d[k4] * a2[k4]**2 / 5))

            k5, = nonzero(hk[k13] < 100.e0)
            if len(k5) > 0:
                #               b = sqrt(bs);
                k135 = k13[k5]
                bvn[k135] = bvn[k135] - exp(-hk[k135] / 2) * sqrt(twopi) * fi(-b[k5] / a[k5]) * \
                    b[k5] * (1 - c[k5] * bs[k5] * (1 - d[k5] * bs[k5] / 5) / 3)

            a /= two
            for i in range(10):
                for sign in -1, 1:
                    xs = (a * (sign * _X20[i] + 1))**2
                    rs = sqrt(1 - xs)
                    asr = -(bs / xs + hk[k13]) / 2
                    k6, = nonzero(asr > -100.e0)
                    if len(k6) > 0:
                        k136 = k13[k6]
                        bvn[k136] += (a[k6] * _W20[i] * exp(asr[k6]) *
                                      (exp(-hk[k136] * (1 - rs[k6]) /
                                           (2 * (1 + rs[k6]))) / rs[k6] -
                                       (1 + c[k6] * xs[k6] *
                                        (1 + d[k6] * xs[k6]))))

            bvn[k3] = -bvn[k3] / twopi

        k7, = nonzero(r[k1] > 0)
        if len(k7):
            k17 = k1[k7]
            bvn[k17] += fi(-np.maximum(h[k17], k[k17]))

        k8, = nonzero(r[k1] < 0)
        if len(k8) > 0:
            k18 = k1[k8]
            bvn[k18] = -bvn[k18] + np.maximum(0, fi(-h[k18]) - fi(-k[k18]))

    bvn.shape = cshape
    return bvn