コード例 #1
0
ファイル: test_field.py プロジェクト: leekwoon/stheno
def test_cancellations_zero():
    # With constants:
    yield eq, str(1 * EQ()), 'EQ()'
    yield eq, str(EQ() * 1), 'EQ()'
    yield eq, str(0 * EQ()), '0'
    yield eq, str(EQ() * 0), '0'
    yield eq, str(0 + EQ()), 'EQ()'
    yield eq, str(EQ() + 0), 'EQ()'
    yield eq, str(0 + OneMean()), '1'
    yield eq, str(OneMean() + 0), '1'

    # Adding to zero:
    yield eq, str(0 + ZeroKernel()), '0'
    yield eq, str(ZeroKernel() + 0), '0'
    yield eq, str(1 + ZeroKernel()), '1'
    yield eq, str(ZeroKernel() + 1), '1'
    yield eq, str(2 + ZeroKernel()), '2 * 1'
    yield eq, str(ZeroKernel() + 2), '2 * 1'

    # Sums:
    yield eq, str(EQ() + EQ()), '2 * EQ()'
    yield eq, str(ZeroKernel() + EQ()), 'EQ()'
    yield eq, str(EQ() + ZeroKernel()), 'EQ()'
    yield eq, str(ZeroKernel() + ZeroKernel()), '0'

    # Products:
    yield eq, str(EQ() * EQ()), 'EQ() * EQ()'
    yield eq, str(ZeroKernel() * EQ()), '0'
    yield eq, str(EQ() * ZeroKernel()), '0'
    yield eq, str(ZeroKernel() * ZeroKernel()), '0'

    # Scales:
    yield eq, str(5 * ZeroKernel()), '0'
    yield eq, str(ZeroKernel() * 5), '0'
    yield eq, str(EQ() * 5), '5 * EQ()'
    yield eq, str(5 * EQ()), '5 * EQ()'

    # Stretches:
    yield eq, str(ZeroKernel().stretch(5)), '0'
    yield eq, str(EQ().stretch(5)), 'EQ() > 5'

    # Periodicisations:
    yield eq, str(ZeroKernel().periodic(5)), '0'
    yield eq, str(EQ().periodic(5)), 'EQ() per 5'

    # Reversals:
    yield eq, str(reversed(ZeroKernel())), '0'
    yield eq, str(reversed(EQ())), 'EQ()'
    yield eq, str(reversed(Linear())), 'Reversed(Linear())'

    # Integration:
    yield eq, str(EQ() * EQ() + ZeroKernel() * EQ()), 'EQ() * EQ()'
    yield eq, str(EQ() * ZeroKernel() + ZeroKernel() * EQ()), '0'
コード例 #2
0
def test_function_mean():
    m1 = 5 * OneMean() + (lambda x: x**2)
    m2 = (lambda x: x**2) + 5 * OneMean()
    m3 = (lambda x: x**2) + ZeroMean()
    m4 = ZeroMean() + (lambda x: x**2)
    x = np.random.randn(10, 1)

    yield ok, np.allclose(m1(x), 5 + x**2)
    yield ok, np.allclose(m2(x), 5 + x**2)
    yield ok, np.allclose(m3(x), x**2)
    yield ok, np.allclose(m4(x), x**2)

    def my_function(x):
        pass

    yield eq, str(TensorProductMean(my_function)), 'my_function'
コード例 #3
0
ファイル: test_field.py プロジェクト: leekwoon/stheno
def test_input_transform():
    yield eq, str(EQ().transform(lambda x: x)), 'EQ() transform <lambda>'
    yield eq, str(EQ().transform(lambda x: x, lambda x: x)), \
          'EQ() transform (<lambda>, <lambda>)'

    yield eq, str(ZeroKernel().transform(lambda x: x)), '0'
    yield eq, str(OneMean().transform(lambda x: x)), '1'
コード例 #4
0
def test_tensor_product():
    m1 = 5 * OneMean() + (lambda x: x ** 2)
    m2 = (lambda x: x ** 2) + 5 * OneMean()
    m3 = (lambda x: x ** 2) + ZeroMean()
    m4 = ZeroMean() + (lambda x: x ** 2)

    x = B.randn(10, 1)
    assert np.allclose(m1(x), 5 + x ** 2)
    assert np.allclose(m2(x), 5 + x ** 2)
    assert np.allclose(m3(x), x ** 2)
    assert np.allclose(m4(x), x ** 2)

    def my_function(x):
        pass

    assert str(TensorProductMean(my_function)) == "my_function"
コード例 #5
0
def test_input_transform():
    assert str(EQ().transform(lambda x: x)) == 'EQ() transform <lambda>'
    assert str(EQ().transform(lambda x: x, lambda x: x)) == \
           'EQ() transform (<lambda>, <lambda>)'

    assert str(ZeroKernel().transform(lambda x: x)) == '0'
    assert str(OneMean().transform(lambda x: x)) == '1'
コード例 #6
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'
コード例 #7
0
ファイル: test_field.py プロジェクト: leekwoon/stheno
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'
コード例 #8
0
def test_ones_zeros():
    c = Cache()

    # Nothing to check for kernels:  ones and zeros are represented in a
    # structured way.

    # Test that ones and zeros are cached and that all signatures work.
    m = ZeroMean()
    yield eq, id(m(np.random.randn(10, 10), c)), \
          id(m(np.random.randn(10, 10), c))
    yield neq, id(m(np.random.randn(10, 10), c)), \
          id(m(np.random.randn(5, 10), c))
    yield eq, id(m(1, c)), id(m(1, c))

    m = OneMean()
    yield eq, id(m(np.random.randn(10, 10), c)), \
          id(m(np.random.randn(10, 10), c))
    yield neq, id(m(np.random.randn(10, 10), c)), \
          id(m(np.random.randn(5, 10), c))
    yield eq, id(m(1, c)), id(m(1, c))
コード例 #9
0
ファイル: test_field.py プロジェクト: leekwoon/stheno
def test_selection():
    yield assert_allclose, _to_list((1, 2)), [1, 2]
    yield assert_allclose, _to_list([1, 2]), [1, 2]
    yield assert_allclose, _to_list(np.array([1, 2])), [1, 2]
    yield assert_allclose, _to_list(1), [1]
    yield raises, ValueError, lambda: _to_list(np.ones((1, 1)))

    yield eq, str(EQ().select(0)), 'EQ() : [0]'
    yield eq, str(EQ().select([0, 2])), 'EQ() : [0, 2]'
    yield eq, str(EQ().select(0, 2)), 'EQ() : ([0], [2])'
    yield eq, str(EQ().select([0], 2)), 'EQ() : ([0], [2])'
    yield eq, str(EQ().select(0, [2])), 'EQ() : ([0], [2])'
    yield eq, str(EQ().select([0, 1], [2])), 'EQ() : ([0, 1], [2])'
    yield eq, str(EQ().select(None, [2])), 'EQ() : (None, [2])'
    yield eq, str(EQ().select(None)), 'EQ() : None'
    yield eq, str(EQ().select(None, None)), 'EQ() : (None, None)'
    yield eq, str(EQ().select([1], None)), 'EQ() : ([1], None)'

    yield eq, str(ZeroKernel().select(0)), '0'
    yield eq, str(OneMean().select(0)), '1'
コード例 #10
0
def test_selection():
    allclose(_to_list((1, 2)), [1, 2])
    allclose(_to_list([1, 2]), [1, 2])
    allclose(_to_list(np.array([1, 2])), [1, 2])
    allclose(_to_list(1), [1])
    with pytest.raises(ValueError):
        _to_list(np.ones((1, 1)))

    assert str(EQ().select(0)) == 'EQ() : [0]'
    assert str(EQ().select([0, 2])) == 'EQ() : [0, 2]'
    assert str(EQ().select(0, 2)) == 'EQ() : ([0], [2])'
    assert str(EQ().select([0], 2)) == 'EQ() : ([0], [2])'
    assert str(EQ().select(0, [2])) == 'EQ() : ([0], [2])'
    assert str(EQ().select([0, 1], [2])) == 'EQ() : ([0, 1], [2])'
    assert str(EQ().select(None, [2])) == 'EQ() : (None, [2])'
    assert str(EQ().select(None)) == 'EQ() : None'
    assert str(EQ().select(None, None)) == 'EQ() : (None, None)'
    assert str(EQ().select([1], None)) == 'EQ() : ([1], None)'

    assert str(ZeroKernel().select(0)) == '0'
    assert str(OneMean().select(0)) == '1'
コード例 #11
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])
コード例 #12
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]
コード例 #13
0
ファイル: test_mean.py プロジェクト: wubizhi/stheno
def test_shifting():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    allclose(m.shift(5)(x), m(x - 5))
コード例 #14
0
def test_selected_mean():
    m = 5 * OneMean() + (lambda x: x ** 2)
    x = B.randn(10, 3)
    approx(m.select([1, 2])(x), m(x[:, [1, 2]]))
コード例 #15
0
def test_shifting():
    m = 5 * OneMean() + (lambda x: x ** 2)
    x = B.randn(10, 3)
    approx(m.shift(5)(x), m(x - 5))
コード例 #16
0
def test_stretching():
    m = 5 * OneMean() + (lambda x: x ** 2)
    x = B.randn(10, 3)
    approx(m.stretch(5)(x), m(x / 5))
コード例 #17
0
def test_input_transform():
    m = 5 * OneMean() + (lambda x: x ** 2)
    x = B.randn(10, 3)
    approx(m.transform(lambda x: x - 5)(x), m(x - 5))
コード例 #18
0
def test_input_transform():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    yield assert_allclose, m.transform(lambda x, c: x - 5)(x), m(x - 5)
コード例 #19
0
def test_stretching():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    yield assert_allclose, m.stretch(5)(x), m(x / 5)
コード例 #20
0
def test_shifting():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    yield assert_allclose, m.shift(5)(x), m(x - 5)
コード例 #21
0
def test_selected_mean():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    yield assert_allclose, m.select([1, 2])(x), m(x[:, [1, 2]])
コード例 #22
0
ファイル: test_mean.py プロジェクト: wubizhi/stheno
def test_input_transform():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    allclose(m.transform(lambda x: x - 5)(x), m(x - 5))
コード例 #23
0
ファイル: test_mean.py プロジェクト: wubizhi/stheno
def test_stretching():
    m = 5 * OneMean() + (lambda x: x**2)
    x = np.random.randn(10, 3)

    allclose(m.stretch(5)(x), m(x / 5))