Esempio n. 1
0
def test_function():
    def f():
        pass

    yield eq, str(ZeroKernel() * f), '0'
    yield eq, str(f * ZeroKernel()), '0'
    yield eq, str(OneKernel() * f), 'f'
    yield eq, str(f * OneKernel()), 'f'
    yield eq, str(EQ() * f), 'EQ() * f'
    yield eq, str(f * EQ()), 'f * EQ()'
    yield eq, str((EQ() * EQ()) * f), 'EQ() * EQ() * f'
    yield eq, str(f * (EQ() * EQ())), 'f * EQ() * EQ()'
    yield eq, str((5 * EQ()) * f), '5 * EQ() * f'
    yield eq, str(f * (5 * EQ())), '5 * f * EQ()'

    yield eq, str(ZeroKernel() + f), 'f'
    yield eq, str(f + ZeroKernel()), 'f'
    yield eq, str(OneKernel() + f), '1 + f'
    yield eq, str(f + OneKernel()), 'f + 1'
    yield eq, str(EQ() + f), 'EQ() + f'
    yield eq, str(f + EQ()), 'f + EQ()'
    yield eq, str((EQ() + RQ(1)) + f), 'EQ() + RQ(1) + f'
    yield eq, str(f + (EQ() + RQ(1))), 'f + EQ() + RQ(1)'
    yield eq, str((5 + EQ()) + f), '5 * 1 + EQ() + f'
    yield eq, str(f + (5 + EQ())), 'f + 5 * 1 + EQ()'

    yield eq, str(OneKernel() * f), 'f'
    yield eq, str(OneKernel() * TensorProductKernel((lambda x, c: x), f)), \
          '(<lambda> x f)'
    yield eq, str(OneKernel() * TensorProductKernel(f, (lambda x, c: x))), \
          '(f x <lambda>)'
Esempio n. 2
0
def test_function():
    def f():
        pass

    assert str(ZeroKernel() * f) == '0'
    assert str(f * ZeroKernel()) == '0'
    assert str(OneKernel() * f) == 'f'
    assert str(f * OneKernel()) == 'f'
    assert str(EQ() * f) == 'EQ() * f'
    assert str(f * EQ()) == 'f * EQ()'
    assert str((EQ() * EQ()) * f) == 'EQ() * EQ() * f'
    assert str(f * (EQ() * EQ())) == 'f * EQ() * EQ()'
    assert str((5 * EQ()) * f) == '5 * EQ() * f'
    assert str(f * (5 * EQ())) == '5 * f * EQ()'

    assert str(ZeroKernel() + f) == 'f'
    assert str(f + ZeroKernel()) == 'f'
    assert str(OneKernel() + f) == '1 + f'
    assert str(f + OneKernel()) == 'f + 1'
    assert str(EQ() + f) == 'EQ() + f'
    assert str(f + EQ()) == 'f + EQ()'
    assert str((EQ() + RQ(1)) + f) == 'EQ() + RQ(1) + f'
    assert str(f + (EQ() + RQ(1))) == 'f + EQ() + RQ(1)'
    assert str((5 + EQ()) + f) == '5 * 1 + EQ() + f'
    assert str(f + (5 + EQ())) == 'f + 5 * 1 + EQ()'

    assert str(OneKernel() * f) == 'f'
    assert str(OneKernel() * TensorProductKernel((lambda x, c: x), f)) == \
           '(<lambda> x f)'
    assert str(OneKernel() *
               TensorProductKernel(f, (lambda x, c: x))) == '(f x <lambda>)'
Esempio n. 3
0
def test_derivative():
    yield eq, str(EQ().diff(0)), 'd(0) EQ()'
    yield eq, str(EQ().diff(0, 1)), 'd(0, 1) EQ()'

    yield eq, str(ZeroKernel().diff(0)), '0'
    yield eq, str(OneKernel().diff(0)), '0'

    yield eq, str(ZeroMean().diff(0)), '0'
    yield eq, str(OneMean().diff(0)), '0'
Esempio n. 4
0
def test_derivative():
    assert str(EQ().diff(0)) == 'd(0) EQ()'
    assert str(EQ().diff(0, 1)) == 'd(0, 1) EQ()'

    assert str(ZeroKernel().diff(0)) == '0'
    assert str(OneKernel().diff(0)) == '0'

    assert str(ZeroMean().diff(0)) == '0'
    assert str(OneMean().diff(0)) == '0'
Esempio n. 5
0
def test_one():
    k = OneKernel()

    x1 = np.random.randn(10, 2)
    x2 = np.random.randn(5, 2)

    # Test that the kernel computes correctly.
    allclose(k(x1, x2), np.ones((10, 5)))

    # Verify that the kernel has the right properties.
    assert k.stationary
    assert str(k) == '1'

    # Test equality.
    assert OneKernel() == OneKernel()
    assert OneKernel() != Linear()

    # Standard tests:
    standard_kernel_tests(k)
Esempio n. 6
0
def test_one():
    k = OneKernel()

    x1 = np.random.randn(10, 2)
    x2 = np.random.randn(5, 2)

    # Test that the kernel computes correctly.
    yield assert_allclose, k(x1, x2), np.ones((10, 5))

    # Verify that the kernel has the right properties.
    yield eq, k.stationary, True
    yield eq, k.var, 1
    yield eq, k.length_scale, 0
    yield eq, k.period, 0
    yield eq, str(k), '1'

    # Test equality.
    yield eq, OneKernel(), OneKernel()
    yield neq, OneKernel(), Linear()

    # Standard tests:
    for x in kernel_generator(k):
        yield x
Esempio n. 7
0
def test_uprank():
    allclose(uprank(0), [[0]])
    allclose(uprank(np.array([0])), [[0]])
    allclose(uprank(np.array([[0]])), [[0]])
    assert type(uprank(Component('test')(0))) == Component('test')

    k = OneKernel()

    assert B.shape(k(0, 0)) == (1, 1)
    assert B.shape(k(0, np.ones(5))) == (1, 5)
    assert B.shape(k(0, np.ones((5, 2)))) == (1, 5)

    assert B.shape(k(np.ones(5), 0)) == (5, 1)
    assert B.shape(k(np.ones(5), np.ones(5))) == (5, 5)
    assert B.shape(k(np.ones(5), np.ones((5, 2)))) == (5, 5)

    assert B.shape(k(np.ones((5, 2)), 0)) == (5, 1)
    assert B.shape(k(np.ones((5, 2)), np.ones(5))) == (5, 5)
    assert B.shape(k(np.ones((5, 2)), np.ones((5, 2)))) == (5, 5)

    with pytest.raises(ValueError):
        k(0, np.ones((5, 2, 1)))
    with pytest.raises(ValueError):
        k(np.ones((5, 2, 1)))

    m = OneMean()

    assert B.shape(m(0)) == (1, 1)
    assert B.shape(m(np.ones(5))) == (5, 1)
    assert B.shape(m(np.ones((5, 2)))) == (5, 1)

    p = GP(EQ(), graph=Graph())
    x = np.linspace(0, 10, 10)

    approx(p.condition(1, 1)(1).mean, np.array([[1]]))
    approx(p.condition(x, x)(x).mean, x[:, None])
    approx(p.condition(x, x[:, None])(x).mean, x[:, None])
Esempio n. 8
0
def test_uprank():
    yield assert_allclose, uprank(0), [[0]]
    yield assert_allclose, uprank(np.array([0])), [[0]]
    yield assert_allclose, uprank(np.array([[0]])), [[0]]
    yield eq, type(uprank(Component('test')(0))), Component('test')

    k = OneKernel()

    yield eq, B.shape(k(0, 0)), (1, 1)
    yield eq, B.shape(k(0, np.ones(5))), (1, 5)
    yield eq, B.shape(k(0, np.ones((5, 2)))), (1, 5)

    yield eq, B.shape(k(np.ones(5), 0)), (5, 1)
    yield eq, B.shape(k(np.ones(5), np.ones(5))), (5, 5)
    yield eq, B.shape(k(np.ones(5), np.ones((5, 2)))), (5, 5)

    yield eq, B.shape(k(np.ones((5, 2)), 0)), (5, 1)
    yield eq, B.shape(k(np.ones((5, 2)), np.ones(5))), (5, 5)
    yield eq, B.shape(k(np.ones((5, 2)), np.ones((5, 2)))), (5, 5)

    yield raises, ValueError, lambda: k(0, np.ones((5, 2, 1)))
    yield raises, ValueError, lambda: k(np.ones((5, 2, 1)))

    m = OneMean()

    yield eq, B.shape(m(0)), (1, 1)
    yield eq, B.shape(m(np.ones(5))), (5, 1)
    yield eq, B.shape(m(np.ones((5, 2)))), (5, 1)

    p = GP(EQ(), graph=Graph())
    x = np.linspace(0, 10, 10)

    yield assert_approx_equal, p.condition(1, 1)(1).mean, np.array([[1]])
    yield assert_array_almost_equal, p.condition(x, x)(x).mean, x[:, None]
    yield assert_array_almost_equal, p.condition(x, x[:, None])(x).mean, \
          x[:, None]
Esempio n. 9
0
def test_cancellations_one():
    # Products:
    yield eq, str(EQ() * EQ()), 'EQ() * EQ()'
    yield eq, str(OneKernel() * EQ()), 'EQ()'
    yield eq, str(EQ() * OneKernel()), 'EQ()'
    yield eq, str(OneKernel() * OneKernel()), '1'
Esempio n. 10
0
def test_cancellations_one():
    # Products:
    assert str(EQ() * EQ()) == 'EQ() * EQ()'
    assert str(OneKernel() * EQ()) == 'EQ()'
    assert str(EQ() * OneKernel()) == 'EQ()'
    assert str(OneKernel() * OneKernel()) == '1'