Esempio n. 1
0
    def test_math(self):
        if self.isWindows:
            skip('windows does not support c99 complex')
        import sys
        import numpypy as np
        rAlmostEqual = self.rAlmostEqual

        for t, testcases in (
                 ('complex128', self.testcases128),
                 #('complex64', self.testcases64),
                ):
            complex_ = np.dtype(t).type
            for id, fn, ar, ai, er, ei, flags in testcases:
                arg = complex_(complex(ar, ai))
                expected = (er, ei)
                if fn.startswith('acos'):
                    fn = 'arc' + fn[1:]
                elif fn.startswith('asin'):
                    fn = 'arc' + fn[1:]
                elif fn.startswith('atan'):
                    fn = 'arc' + fn[1:]
                elif fn in ('rect', 'polar'):
                    continue
                function = getattr(np, fn)
                _actual = function(arg)
                actual = (_actual.real, _actual.imag)

                if 'ignore-real-sign' in flags:
                    actual = (abs(actual[0]), actual[1])
                    expected = (abs(expected[0]), expected[1])
                if 'ignore-imag-sign' in flags:
                    actual = (actual[0], abs(actual[1]))
                    expected = (expected[0], abs(expected[1]))

                # for the real part of the log function, we allow an
                # absolute error of up to 2e-15.
                if fn in ('log', 'log10'):
                    real_abs_err = 2e-15
                else:
                    real_abs_err = 5e-323

                error_message = (
                    '%s: %s(%r(%r, %r))\n'
                    'Expected: complex(%r, %r)\n'
                    'Received: complex(%r, %r)\n'
                    ) % (id, fn, complex_, ar, ai,
                         expected[0], expected[1],
                         actual[0], actual[1])

                # since rAlmostEqual is a wrapped function,
                # convert arguments to avoid boxed values
                rAlmostEqual(float(expected[0]), float(actual[0]),
                             abs_err=real_abs_err, msg=error_message)
                rAlmostEqual(float(expected[1]), float(actual[1]),
                             msg=error_message)
                sys.stderr.write('.')
            sys.stderr.write('\n')
Esempio n. 2
0
def rAlmostEqual(a, b, rel_err=2e-15, abs_err=5e-323, msg=''):
    """Fail if the two floating-point numbers are not almost equal.

    Determine whether floating-point values a and b are equal to within
    a (small) rounding error.  The default values for rel_err and
    abs_err are chosen to be suitable for platforms where a float is
    represented by an IEEE 754 double.  They allow an error of between
    9 and 19 ulps.
    """

    # special values testing
    if isnan(a):
        if isnan(b):
            return True,''
        raise AssertionError(msg + '%r should be nan' % (b,))

    if isinf(a):
        if a == b:
            return True,''
        raise AssertionError(msg + 'finite result where infinity expected: '+ \
                          'expected %r, got %r' % (a, b))

    # if both a and b are zero, check whether they have the same sign
    # (in theory there are examples where it would be legitimate for a
    # and b to have opposite signs; in practice these hardly ever
    # occur).
    if not a and not b:
        # only check it if we are running on top of CPython >= 2.6
        if sys.version_info >= (2, 6) and copysign(1., a) != copysign(1., b):
            raise AssertionError( msg + \
                    'zero has wrong sign: expected %r, got %r' % (a, b))

    # if a-b overflows, or b is infinite, return False.  Again, in
    # theory there are examples where a is within a few ulps of the
    # max representable float, and then b could legitimately be
    # infinite.  In practice these examples are rare.
    try:
        absolute_error = abs(b-a)
    except OverflowError:
        pass
    else:
        # test passes if either the absolute error or the relative
        # error is sufficiently small.  The defaults amount to an
        # error of between 9 ulps and 19 ulps on an IEEE-754 compliant
        # machine.
        if absolute_error <= max(abs_err, rel_err * abs(a)):
            return True,''
    raise AssertionError(msg + \
            '%r and %r are not sufficiently close, %g > %g' %\
            (a, b, absolute_error, max(abs_err, rel_err*abs(a))))
Esempio n. 3
0
def elbo_did_not_converge(elbo, last_elbo, num_iter=0, 
                          criterion=0.001, min_iter=0, max_iter=20):
    """Accepts two elbo doubles.  
        Also accepts the number of iterations already performed in this loop.
        Also accepts convergence criterion: 
            (elbo - last_elbo) < criterion # True to stop
        Finally, accepts 
        Returns boolean.
        Figures out whether the elbo is sufficiently smaller than
            last_elbo.
    """
    if num_iter < min_iter:
        return True

    if num_iter >= max_iter:
        return False

    if elbo == INITIAL_ELBO or last_elbo == INITIAL_ELBO:
        return True
    else:
        # todo: do a criterion convergence test
        if np.abs(elbo - last_elbo) < criterion:
            return False
        else:
            return True
Esempio n. 4
0
def lm_recalculate_eta_sigma(eta, y, phi1, phi2):
    """
        Accepts eta (K+J)-size vector,
            also y (a D-size vector of reals),
            also two phi D-size vectors of NxK matrices.
        Returns new sigma squared update (a double).

        ηnew ← (E[ATA])-1 E[A]Ty
        σ2new ← (1/D) {yTy - yTE[A]ηnew}

        (Note that A is the D X (K + J) matrix whose rows are the vectors ZdT for document and comment concatenated.)
        (Also note that the dth row of E[A] is φd, and E[ATA] = Σd E[ZdZdT] .)
        (Also, note that E[Z] = φ := (1/N)Σnφn, and E[ZdZdT] = (1/N2)(ΣnΣm!=nφd,nφd,mT  +  Σndiag{φd,n})
    """
    ensure(len(phi1) == len(phi2))
    D = len(phi1)

    Nd,K = phi1[0].shape
    Nc,J = phi2[0].shape
    Ndc, KJ = (Nd+Nc,K+J)

    #print 'e_a...'
    E_A = np.zeros((D, KJ))
    for d in xrange(D):
        E_A[d,:] = calculate_EZ_from_small_phis(phi1[d], phi2[d])
  
    #print 'inverse...'
    E_ATA_inverse = calculate_E_ATA_inverse_from_small_phis(phi1, phi2)

    #print 'new eta...'
    #new_eta = matrix_multiply(matrix_multiply(E_ATA_inverse, E_A.T), y)
    new_eta = np.dot(np.dot(E_ATA_inverse, E_A.T), y)
    if np.sum(np.abs(new_eta)) > (KJ * KJ * 5):
        print 'ETA is GOING CRAZY {0}'.format(eta)
        print 'aborting the update!!!'
    else:
        eta[:] = new_eta
    
    # todo: don't do this later
    # keep sigma squared fix
    #import pdb; pdb.set_trace()
    #new_sigma_squared = (1.0 / D) * (np.dot(y, y) - np.dot(np.dot(np.dot(np.dot(y, E_A), E_ATA_inverse), E_A.T), y))
    new_sigma_squared = 1.0
    return new_sigma_squared
Esempio n. 5
0
def mean_absolute_error(y_true, y_pred):
    return np.mean(np.abs(y_pred - y_true))
Esempio n. 6
0
    def test_basic(self):
        import sys
        from numpypy import (dtype, add, array, dtype,
            subtract as sub, multiply, divide, negative, absolute as abs,
            floor_divide, real, imag, sign)
        from numpypy import (equal, not_equal, greater, greater_equal, less,
                less_equal, isnan)
        assert real(4.0) == 4.0
        assert imag(0.0) == 0.0
        a = array([complex(3.0, 4.0)])
        b = a.real
        b[0] = 1024
        assert a[0].real == 1024
        assert b.dtype == dtype(float)
        a = array(complex(3.0, 4.0))
        b = a.real
        assert b == array(3)
        assert a.imag == array(4)
        a.real = 1024
        a.imag = 2048
        assert a.real == 1024 and a.imag == 2048
        assert b.dtype == dtype(float)
        a = array(4.0)
        b = a.imag
        assert b == 0
        assert b.dtype == dtype(float)
        exc = raises(TypeError, 'a.imag = 1024')
        assert str(exc.value).startswith("array does not have imaginary")
        exc = raises(ValueError, 'a.real = [1, 3]')
        assert str(exc.value) == \
            "could not broadcast input array from shape (2) into shape ()"
        a = array('abc')
        assert str(a.real) == 'abc'
        assert str(a.imag) == ''
        for t in 'complex64', 'complex128', 'clongdouble':
            complex_ = dtype(t).type
            O = complex(0, 0)
            c0 = complex_(complex(2.5, 0))
            c1 = complex_(complex(1, 2))
            c2 = complex_(complex(3, 4))
            c3 = complex_(complex(-3, -3))

            assert equal(c0, 2.5)
            assert equal(c1, complex_(complex(1, 2)))
            assert equal(c1, complex(1, 2))
            assert equal(c1, c1)
            assert not_equal(c1, c2)
            assert not equal(c1, c2)

            assert less(c1, c2)
            assert less_equal(c1, c2)
            assert less_equal(c1, c1)
            assert not less(c1, c1)

            assert greater(c2, c1)
            assert greater_equal(c2, c1)
            assert not greater(c1, c2)

            assert add(c1, c2) == complex_(complex(4, 6))
            assert add(c1, c2) == complex(4, 6)

            assert sub(c0, c0) == sub(c1, c1) == 0
            assert sub(c1, c2) == complex(-2, -2)
            assert negative(complex(1,1)) == complex(-1, -1)
            assert negative(complex(0, 0)) == 0

            assert multiply(1, c1) == c1
            assert multiply(2, c2) == complex(6, 8)
            assert multiply(c1, c2) == complex(-5, 10)

            assert divide(c0, 1) == c0
            assert divide(c2, -1) == negative(c2)
            assert divide(c1, complex(0, 1)) == complex(2, -1)
            n = divide(c1, O)
            assert repr(n.real) == 'inf'
            assert repr(n.imag).startswith('inf') #can be inf*j or infj
            assert divide(c0, c0) == 1
            res = divide(c2, c1)
            assert abs(res.real-2.2) < 0.001
            assert abs(res.imag+0.4) < 0.001

            assert floor_divide(c0, c0) == complex(1, 0)
            assert isnan(floor_divide(c0, complex(0, 0)).real)
            assert floor_divide(c0, complex(0, 0)).imag == 0.0

            assert abs(c0) == 2.5
            assert abs(c2) == 5
            assert sign(complex(0, 0)) == 0
            assert sign(complex(-42, 0)) == -1
            assert sign(complex(42, 0)) == 1
            assert sign(complex(-42, 2)) == -1
            assert sign(complex(42, 2)) == 1
            assert sign(complex(-42, -3)) == -1
            assert sign(complex(42, -3)) == 1
            assert sign(complex(0, -42)) == -1
            assert sign(complex(0, 42)) == 1

            inf_c = complex_(complex(float('inf'), 0.))
            assert repr(abs(inf_c)) == 'inf'
            assert repr(abs(complex(float('nan'), float('nan')))) == 'nan'
            # numpy actually raises an AttributeError,
            # but numpypy raises a TypeError
            if '__pypy__' in sys.builtin_module_names:
                exct, excm = TypeError, 'readonly attribute'
            else:
                exct, excm = AttributeError, 'is not writable'
            exc = raises(exct, 'c2.real = 10.')
            assert excm in exc.value[0]
            exc = raises(exct, 'c2.imag = 10.')
            assert excm in exc.value[0]
            assert(real(c2) == 3.0)
            assert(imag(c2) == 4.0)
Esempio n. 7
0
    def test_basic(self):
        from numpypy import (complex128, complex64, add, array, dtype,
            subtract as sub, multiply, divide, negative, absolute as abs,
            floor_divide, real, imag, sign)
        from numpypy import (equal, not_equal, greater, greater_equal, less,
                less_equal, isnan)
        complex_dtypes = [complex64, complex128]
        try:
            from numpypy import clongfloat
            complex_dtypes.append(clongfloat)
        except:
            pass
        assert real(4.0) == 4.0
        assert imag(0.0) == 0.0
        a = array([complex(3.0, 4.0)])
        b = a.real
        b[0] = 1024
        assert a[0].real == 1024
        assert b.dtype == dtype(float)
        a = array(complex(3.0, 4.0))
        b = a.real
        assert b == array(3)
        assert a.imag == array(4)
        a.real = 1024
        a.imag = 2048
        assert a.real == 1024 and a.imag == 2048
        assert b.dtype == dtype(float)
        a = array(4.0)
        b = a.imag
        assert b == 0
        assert b.dtype == dtype(float)
        exc = raises(TypeError, 'a.imag = 1024')
        assert str(exc.value).startswith("array does not have imaginary")
        exc = raises(ValueError, 'a.real = [1, 3]')
        assert str(exc.value) == \
            "could not broadcast input array from shape (2) into shape ()"
        a = array('abc')
        assert str(a.real) == 'abc'
        # numpy imag for flexible types returns self
        assert str(a.imag) == 'abc'
        for complex_ in complex_dtypes:

            O = complex(0, 0)
            c0 = complex_(complex(2.5, 0))
            c1 = complex_(complex(1, 2))
            c2 = complex_(complex(3, 4))
            c3 = complex_(complex(-3, -3))

            assert equal(c0, 2.5)
            assert equal(c1, complex_(complex(1, 2)))
            assert equal(c1, complex(1, 2))
            assert equal(c1, c1)
            assert not_equal(c1, c2)
            assert not equal(c1, c2)

            assert less(c1, c2)
            assert less_equal(c1, c2)
            assert less_equal(c1, c1)
            assert not less(c1, c1)

            assert greater(c2, c1)
            assert greater_equal(c2, c1)
            assert not greater(c1, c2)

            assert add(c1, c2) == complex_(complex(4, 6))
            assert add(c1, c2) == complex(4, 6)

            assert sub(c0, c0) == sub(c1, c1) == 0
            assert sub(c1, c2) == complex(-2, -2)
            assert negative(complex(1,1)) == complex(-1, -1)
            assert negative(complex(0, 0)) == 0


            assert multiply(1, c1) == c1
            assert multiply(2, c2) == complex(6, 8)
            assert multiply(c1, c2) == complex(-5, 10)

            assert divide(c0, 1) == c0
            assert divide(c2, -1) == negative(c2)
            assert divide(c1, complex(0, 1)) == complex(2, -1)
            n = divide(c1, O)
            assert repr(n.real) == 'inf'
            assert repr(n.imag).startswith('inf') #can be inf*j or infj
            assert divide(c0, c0) == 1
            res = divide(c2, c1)
            assert abs(res.real-2.2) < 0.001
            assert abs(res.imag+0.4) < 0.001

            assert floor_divide(c0, c0) == complex(1, 0)
            assert isnan(floor_divide(c0, complex(0, 0)).real)
            assert floor_divide(c0, complex(0, 0)).imag == 0.0

            assert abs(c0) == 2.5
            assert abs(c2) == 5
            assert sign(complex(0, 0)) == 0
            assert sign(complex(-42, 0)) == -1
            assert sign(complex(42, 0)) == 1
            assert sign(complex(-42, 2)) == -1
            assert sign(complex(42, 2)) == 1
            assert sign(complex(-42, -3)) == -1
            assert sign(complex(42, -3)) == 1
            assert sign(complex(0, -42)) == -1
            assert sign(complex(0, 42)) == 1

            inf_c = complex_(complex(float('inf'), 0.))
            assert repr(abs(inf_c)) == 'inf'
            assert repr(abs(complex(float('nan'), float('nan')))) == 'nan'
            # numpy actually raises an AttributeError,
            # but numpypy raises a TypeError
            exc = raises((TypeError, AttributeError), 'c2.real = 10.')
            assert str(exc.value) == "readonly attribute"
            exc = raises((TypeError, AttributeError), 'c2.imag = 10.')
            assert str(exc.value) == "readonly attribute"
            assert(real(c2) == 3.0)
            assert(imag(c2) == 4.0)