Esempio n. 1
0
def standard_kernel_tests(k, shapes=None, dtype=np.float64):
    if shapes is None:
        shapes = [((10, 2), (5, 2)),
                  ((10, 1), (5, 1)),
                  ((10,), (5,)),
                  ((10,), ()),
                  ((), (5,)),
                  ((), ())]

    # Check various shapes of arguments.
    for shape1, shape2 in shapes:
        x1 = B.randn(dtype, *shape1)
        x2 = B.randn(dtype, *shape2)

        # Check that the kernel computes consistently.
        allclose(k(x1, x2), reversed(k)(x2, x1).T)

        # Check `elwise`.
        x2 = B.randn(dtype, *shape1)

        allclose(k.elwise(x1, x2)[:, 0], B.diag(k(x1, x2)))
        allclose(k.elwise(x1, x2), Kernel.elwise(k, x1, x2))
        # The element-wise computation is more accurate, which is why we allow
        # a discrepancy a bit larger than the square root of the machine
        # epsilon.
        allclose(k.elwise(x1)[:, 0], B.diag(k(x1)),
                 desc='', atol=1e-6, rtol=1e-6)
        allclose(k.elwise(x1), Kernel.elwise(k, x1))
Esempio n. 2
0
def test_fixed_delta():
    noises = B.rand(3)
    k = FixedDelta(noises)

    # Verify that the kernel has the right properties.
    assert k.stationary
    assert k.var == 1
    assert k.length_scale == 0
    assert k.period == np.inf
    assert str(k) == 'FixedDelta()'

    # Check equality.
    assert FixedDelta(noises) == FixedDelta(noises)
    assert FixedDelta(noises) != FixedDelta(2 * noises)
    assert FixedDelta(noises) != EQ()

    # Standard tests:
    standard_kernel_tests(k)

    # Check correctness.
    x1 = B.randn(5)
    x2 = B.randn(5)
    allclose(k(x1), B.zeros(5, 5))
    allclose(k.elwise(x1), B.zeros(5, 1))
    allclose(k(x1, x2), B.zeros(5, 5))
    allclose(k.elwise(x1, x2), B.zeros(5, 1))

    x1 = B.randn(3)
    x2 = B.randn(3)
    allclose(k(x1), B.diag(noises))
    allclose(k.elwise(x1), B.uprank(noises))
    allclose(k(x1, x2), B.zeros(3, 3))
    allclose(k.elwise(x1, x2), B.zeros(3, 1))
Esempio n. 3
0
def test_fixed_delta():
    noises = B.rand(3)
    k = FixedDelta(noises)

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

    # Check equality.
    assert FixedDelta(noises) == FixedDelta(noises)
    assert FixedDelta(noises) != FixedDelta(2 * noises)
    assert FixedDelta(noises) != EQ()

    # Standard tests:
    standard_kernel_tests(k)

    # Check correctness.
    x1 = B.randn(5)
    x2 = B.randn(5)
    approx(k(x1), B.zeros(5, 5))
    approx(k.elwise(x1), B.zeros(5, 1))
    approx(k(x1, x2), B.zeros(5, 5))
    approx(k.elwise(x1, x2), B.zeros(5, 1))

    x1 = B.randn(3)
    x2 = B.randn(3)
    approx(k(x1), B.diag(noises))
    approx(k.elwise(x1), B.uprank(noises))
    approx(k(x1, x2), B.zeros(3, 3))
    approx(k.elwise(x1, x2), B.zeros(3, 1))