コード例 #1
0
ファイル: test_random.py プロジェクト: cj401/stheno
def test_normal_sampling():
    # Test sampling and dtype conversion.
    dist = Normal(3 * np.eye(200, dtype=np.integer))
    assert np.abs(np.std(dist.sample(1000))**2 - 3) <= 5e-2, 'full'
    assert np.abs(np.std(dist.sample(1000, noise=2))**2 - 5) <= 5e-2, 'full 2'

    dist = Normal(Diagonal(3 * np.ones(200, dtype=np.integer)))
    assert np.abs(np.std(dist.sample(1000))**2 - 3) <= 5e-2, 'diag'
    assert np.abs(np.std(dist.sample(1000, noise=2))**2 - 5) <= 5e-2, 'diag 2'

    dist = Normal(UniformlyDiagonal(3, 200))
    assert np.abs(np.std(dist.sample(1000))**2 - 3) <= 5e-2, 'unif'
    assert np.abs(np.std(dist.sample(1000, noise=2))**2 - 5) <= 5e-2, 'unif 2'

    # Test `__str__` and `__repr__`.
    assert str(dist) == RandomVector.__str__(dist)
    assert repr(dist) == RandomVector.__repr__(dist)

    # Test zero mean determination.
    assert Normal(np.eye(3))._zero_mean
    assert not Normal(np.eye(3), np.random.randn(3, 1))._zero_mean

    x = np.random.randn(3)
    assert GP(1)(x)._zero_mean
    assert not GP(1, 1)(x)._zero_mean
    assert GP(1, 0)(x)._zero_mean
コード例 #2
0
ファイル: test_graph.py プロジェクト: cj401/stheno
def test_properties():
    model = Graph()

    p1 = GP(EQ(), graph=model)
    p2 = GP(EQ().stretch(2), graph=model)
    p3 = GP(EQ().periodic(10), graph=model)

    p = p1 + 2 * p2

    assert p.stationary == True, 'stationary'
    assert p.var == 1 + 2**2, 'var'
    allclose(p.length_scale, (1 + 2 * 2**2) / (1 + 2**2))
    assert p.period == np.inf, 'period'

    assert p3.period == 10, 'period'

    p = p3 + p

    assert p.stationary == True, 'stationary 2'
    assert p.var == 1 + 2**2 + 1, 'var 2'
    assert p.period == np.inf, 'period 2'

    p = p + GP(Linear(), graph=model)

    assert p.stationary == False, 'stationary 3'
コード例 #3
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_multi_logpdf():
    model = Graph()

    p1 = GP(EQ(), graph=model)
    p2 = GP(2 * Exp(), graph=model)
    p3 = p1 + p2

    x1 = np.linspace(0, 2, 5)
    x2 = np.linspace(1, 3, 6)
    x3 = np.linspace(2, 4, 7)

    y1, y2, y3 = model.sample(p1(x1), p2(x2), p3(x3))

    # Test case that only one process is fed.
    yield assert_allclose, p1(x1).logpdf(y1), model.logpdf(p1(x1), y1)
    yield assert_allclose, p1(x1).logpdf(y1), model.logpdf((p1(x1), y1))

    # Test that all inputs must be specified.
    yield raises, ValueError, lambda: model.logpdf((x1, y1), (p2(x2), y2))
    yield raises, ValueError, lambda: model.logpdf((p1(x1), y1), (x2, y2))

    # Compute the logpdf with the product rule.
    logpdf1 = p1(x1).logpdf(y1) + \
              p2(x2).logpdf(y2) + \
              (p3 | ((p1(x1), y1), (p2(x2), y2)))(x3).logpdf(y3)
    logpdf2 = model.logpdf((p1(x1), y1), (p2(x2), y2), (p3(x3), y3))
    yield assert_allclose, logpdf1, logpdf2
コード例 #4
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_properties():
    model = Graph()

    p1 = GP(EQ(), graph=model)
    p2 = GP(EQ().stretch(2), graph=model)
    p3 = GP(EQ().periodic(10), graph=model)

    p = p1 + 2 * p2

    yield eq, p.stationary, True, 'stationary'
    yield eq, p.var, 1 + 2 ** 2, 'var'
    yield assert_allclose, p.length_scale, \
          (1 + 2 * 2 ** 2) / (1 + 2 ** 2)
    yield eq, p.period, np.inf, 'period'

    yield eq, p3.period, 10, 'period'

    p = p3 + p

    yield eq, p.stationary, True, 'stationary 2'
    yield eq, p.var, 1 + 2 ** 2 + 1, 'var 2'
    yield eq, p.period, np.inf, 'period 2'

    p = p + GP(Linear(), graph=model)

    yield eq, p.stationary, False, 'stationary 3'
コード例 #5
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_approximate_multiplication():
    model = Graph()

    # Construct model.
    p1 = GP(EQ(), 20, graph=model)
    p2 = GP(EQ(), 20, graph=model)
    p_prod = p1 * p2
    x = np.linspace(0, 10, 50)

    # Sample functions.
    s1, s2 = model.sample(p1(x), p2(x))

    # Infer product.
    post = p_prod.condition((p1(x), s1), (p2(x), s2))
    yield le, rel_err(post(x).mean, s1 * s2), 1e-2

    # Perform division.
    cur_epsilon = B.epsilon
    B.epsilon = 1e-8
    post = p2.condition((p1(x), s1), (p_prod(x), s1 * s2))
    yield le, rel_err(post(x).mean, s2), 1e-2
    B.epsilon = cur_epsilon

    # Check graph check.
    model2 = Graph()
    p3 = GP(EQ(), graph=model2)
    yield raises, RuntimeError, lambda: p3 * p1
コード例 #6
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_case_fd_derivative():
    x = np.linspace(0, 10, 50)[:, None]
    y = np.sin(x)

    model = Graph()
    p = GP(.7 * EQ().stretch(1.), graph=model)
    dp = (p.shift(-1e-3) - p.shift(1e-3)) / 2e-3

    yield le, abs_err(np.cos(x) - dp.condition(p(x), y)(x).mean), 1e-4
コード例 #7
0
ファイル: test_graph.py プロジェクト: mhavasi/stheno
def test_observations_and_conditioning():
    model = Graph()
    p1 = GP(EQ(), graph=model)
    p2 = GP(EQ(), graph=model)
    p = p1 + p2
    x = np.linspace(0, 5, 10)
    y = p(x).sample()
    y1 = p1(x).sample()

    # Test all ways of conditioning, including shorthands.
    obs1 = Obs(p(x), y)
    obs2 = Obs(x, y, ref=p)
    yield assert_allclose, obs1.y, obs2.y
    yield assert_allclose, obs1.K_x, obs2.K_x

    obs3 = Obs((p(x), y), (p1(x), y1))
    obs4 = Obs((x, y), (p1(x), y1), ref=p)
    yield assert_allclose, obs3.y, obs4.y
    yield assert_allclose, obs3.K_x, obs4.K_x

    def assert_equal_mean_var(x, *ys):
        for y in ys:
            yield assert_allclose, x.mean, y.mean
            yield assert_allclose, x.var, y.var

    for test in assert_equal_mean_var(
            p.condition(x, y)(x),
            p.condition(p(x), y)(x), (p | (x, y))(x), (p | (p(x), y))(x),
            p.condition(obs1)(x),
            p.condition(obs2)(x), (p | obs1)(x), (p | obs2)(x)):
        yield test

    for test in assert_equal_mean_var(
            p.condition((x, y), (p1(x), y1))(x),
            p.condition((p(x), y), (p1(x), y1))(x),
        (p | [(x, y), (p1(x), y1)])(x), (p | [(p(x), y), (p1(x), y1)])(x),
            p.condition(obs3)(x),
            p.condition(obs4)(x), (p | obs3)(x), (p | obs4)(x)):
        yield test

    # Check conditioning multiple processes at once.
    p1_post, p2_post, p_post = (p1, p2, p) | obs1
    p1_post, p2_post, p_post = p1_post(x), p2_post(x), p_post(x)
    p1_post2, p2_post2, p_post2 = (p1 | obs1)(x), (p2 | obs1)(x), (p | obs1)(x)

    yield assert_allclose, p1_post.mean, p1_post2.mean
    yield assert_allclose, p1_post.var, p1_post2.var
    yield assert_allclose, p2_post.mean, p2_post2.mean
    yield assert_allclose, p2_post.var, p2_post2.var
    yield assert_allclose, p_post.mean, p_post2.mean
    yield assert_allclose, p_post.var, p_post2.var

    # Test `At` check.
    yield raises, ValueError, lambda: Obs(0, 0)
    yield raises, ValueError, lambda: Obs((0, 0), (0, 0))
    yield raises, ValueError, lambda: SparseObs(0, p, (0, 0))
コード例 #8
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_corner_cases():
    m1 = Graph()
    m2 = Graph()
    p1 = GP(EQ(), graph=m1)
    p2 = GP(EQ(), graph=m2)
    x = np.random.randn(10, 2)
    yield raises, RuntimeError, lambda: p1 + p2
    yield raises, NotImplementedError, lambda: p1 + p1(x)
    yield raises, NotImplementedError, lambda: p1 * p1(x)
    yield eq, str(GP(EQ(), graph=m1)), 'GP(EQ(), 0)'
コード例 #9
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_selection():
    model = Graph()

    # Test construction:
    p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=model)
    yield eq, str(p.select(1)), 'GP(EQ() : [1], <lambda> : [1])'
    yield eq, str(p.select(1, 2)), 'GP(EQ() : [1, 2], <lambda> : [1, 2])'

    # Test case:
    p = GP(EQ(), graph=model)  # 1D
    p2 = p.select(0)  # 2D

    n = 5
    x = np.linspace(0, 10, n)[:, None]
    x1 = np.concatenate((x, np.random.randn(n, 1)), axis=1)
    x2 = np.concatenate((x, np.random.randn(n, 1)), axis=1)
    y = p2(x).sample()

    post = p.condition(p2(x1), y)
    yield assert_allclose, post(x).mean, y
    yield le, abs_err(B.diag(post(x).var)), 1e-10

    post = p.condition(p2(x2), y)
    yield assert_allclose, post(x).mean, y
    yield le, abs_err(B.diag(post(x).var)), 1e-10

    post = p2.condition(p(x), y)
    yield assert_allclose, post(x1).mean, y
    yield assert_allclose, post(x2).mean, y
    yield le, abs_err(B.diag(post(x1).var)), 1e-10
    yield le, abs_err(B.diag(post(x2).var)), 1e-10
コード例 #10
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_corner_cases():
    m1 = Graph()
    m2 = Graph()
    p1 = GP(EQ(), graph=m1)
    p2 = GP(EQ(), graph=m2)
    x = np.random.randn(10, 2)
    with pytest.raises(RuntimeError):
        p1 + p2
    with pytest.raises(NotImplementedError):
        p1 + p1(x)
    with pytest.raises(NotImplementedError):
        p1 * p1(x)
    assert str(GP(EQ(), graph=m1)) == 'GP(EQ(), 0)'
コード例 #11
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_mul_other():
    model = Graph()
    p1 = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=model)
    p2 = 5. * p1
    p3 = p1 * 5.

    x = np.random.randn(5, 1)
    yield assert_allclose, 5. * p1.mean(x), p2.mean(x)
    yield assert_allclose, 5. * p1.mean(x), p3.mean(x)
    yield assert_allclose, 25. * p1.kernel(x), p2.kernel(x)
    yield assert_allclose, 25. * p1.kernel(x), p3.kernel(x)
    yield assert_allclose, model.kernels[p2, p3](x, x), 25. * p1.kernel(x)

    # Check that a `GP` cannot be multiplied with a `Normal`.
    yield raises, NotImplementedError, lambda: p1 * Normal(np.eye(3))
    yield raises, NotImplementedError, lambda: Normal(np.eye(3)) * p1
コード例 #12
0
ファイル: test_momean.py プロジェクト: cj401/stheno
def test_momean():
    m = Graph()
    p1 = GP(1 * EQ(), lambda x: 2 * x, graph=m)
    p2 = GP(2 * EQ().stretch(2), 1, graph=m)

    mom = MultiOutputMean(p1, p2)
    ms = m.means

    x = np.linspace(0, 1, 10)

    assert str(mom) == 'MultiOutputMean(<lambda>, 1)'

    allclose(mom(x), np.concatenate([ms[p1](x), ms[p2](x)], axis=0))
    allclose(mom(p1(x)), ms[p1](x))
    allclose(mom(p2(x)), ms[p2](x))
    allclose(mom(MultiInput(p2(x), p1(x))),
             np.concatenate([ms[p2](x), ms[p1](x)], axis=0))
コード例 #13
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_derivative():
    # Test construction:
    p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=Graph())
    yield eq, str(p.diff(1)), 'GP(d(1) EQ(), d(1) <lambda>)'

    # Test case:
    B.backend_to_tf()
    s = B.Session()

    model = Graph()
    x = np.linspace(0, 1, 100)[:, None]
    y = 2 * x

    p = GP(EQ(), graph=model)
    dp = p.diff()

    # Test conditioning on function.
    yield le, abs_err(s.run(dp.condition(p(x), y)(x).mean - 2)), 1e-3

    # Test conditioning on derivative.
    post = p.condition((B.cast(0., np.float64), B.cast(0., np.float64)),
                       (dp(x), y))
    yield le, abs_err(s.run(post(x).mean - x ** 2)), 1e-3

    s.close()
    B.backend_to_np()
コード例 #14
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_properties():
    model = Graph()

    p1 = GP(EQ(), graph=model)
    p2 = GP(EQ().stretch(2), graph=model)
    p3 = GP(EQ().periodic(10), graph=model)

    p = p1 + 2 * p2

    assert p.stationary == True

    p = p3 + p

    assert p.stationary == True

    p = p + GP(Linear(), graph=model)

    assert p.stationary == False
コード例 #15
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_mul_other():
    model = Graph()
    p1 = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=model)
    p2 = 5. * p1
    p3 = p1 * 5.

    x = np.random.randn(5, 1)
    allclose(5. * p1.mean(x), p2.mean(x))
    allclose(5. * p1.mean(x), p3.mean(x))
    allclose(25. * p1.kernel(x), p2.kernel(x))
    allclose(25. * p1.kernel(x), p3.kernel(x))
    allclose(model.kernels[p2, p3](x, x), 25. * p1.kernel(x))

    # Check that a `GP` cannot be multiplied with a `Normal`.
    with pytest.raises(NotImplementedError):
        p1 * Normal(np.eye(3))
    with pytest.raises(NotImplementedError):
        Normal(np.eye(3)) * p1
コード例 #16
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_case_blr():
    model = Graph()
    x = np.linspace(0, 10, 100)

    slope = GP(1, graph=model)
    intercept = GP(1, graph=model)
    f = slope * (lambda x: x) + intercept
    y = f + 1e-2 * GP(Delta(), graph=model)

    # Sample observations, true slope, and intercept.
    y_obs, true_slope, true_intercept = \
        model.sample(y(x), slope(0), intercept(0))

    # Predict.
    post_slope, post_intercept = (slope, intercept) | Obs(y(x), y_obs)
    mean_slope, mean_intercept = post_slope(0).mean, post_intercept(0).mean

    yield le, np.abs(true_slope[0, 0] - mean_slope[0, 0]), 5e-2
    yield le, np.abs(true_intercept[0, 0] - mean_intercept[0, 0]), 5e-2
コード例 #17
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_multi_sample():
    model = Graph()
    p1 = GP(0, 1, graph=model)
    p2 = GP(0, 2, graph=model)
    p3 = GP(0, 3, graph=model)

    x1 = np.linspace(0, 1, 10)
    x2 = np.linspace(0, 1, 20)
    x3 = np.linspace(0, 1, 30)

    s1, s2, s3 = model.sample(p1(x1), p2(x2), p3(x3))

    yield eq, s1.shape, (10, 1)
    yield eq, s2.shape, (20, 1)
    yield eq, s3.shape, (30, 1)
    yield eq, np.shape(model.sample(p1(x1))), (10, 1)

    yield le, abs_err(s1 - 1), 1e-4
    yield le, abs_err(s2 - 2), 1e-4
    yield le, abs_err(s3 - 3), 1e-4
コード例 #18
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_multi_sample():
    model = Graph()
    p1 = GP(0, 1, graph=model)
    p2 = GP(0, 2, graph=model)
    p3 = GP(0, 3, graph=model)

    x1 = np.linspace(0, 1, 10)
    x2 = np.linspace(0, 1, 20)
    x3 = np.linspace(0, 1, 30)

    s1, s2, s3 = model.sample(p1(x1), p2(x2), p3(x3))

    assert s1.shape == (10, 1)
    assert s2.shape == (20, 1)
    assert s3.shape == (30, 1)
    assert np.shape(model.sample(p1(x1))) == (10, 1)

    assert abs_err(s1 - 1) <= 1e-4
    assert abs_err(s2 - 2) <= 1e-4
    assert abs_err(s3 - 3) <= 1e-4
コード例 #19
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_case_summation_with_itself():
    # Test summing the same GP with itself.
    model = Graph()
    p1 = GP(EQ(), graph=model)
    p2 = p1 + p1 + p1 + p1 + p1
    x = np.linspace(0, 10, 5)[:, None]

    yield assert_allclose, p2(x).var, 25 * p1(x).var
    yield assert_allclose, p2(x).mean, np.zeros((5, 1))

    y = np.random.randn(5, 1)
    yield assert_allclose, p2.condition(p1(x), y)(x).mean, 5 * y
コード例 #20
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_case_approximate_derivative():
    model = Graph()
    x = np.linspace(0, 1, 100)[:, None]
    y = 2 * x

    p = GP(EQ().stretch(1.), graph=model)
    dp = p.diff_approx()

    # Test conditioning on function.
    yield le, abs_err(dp.condition(p(x), y)(x).mean - 2), 1e-3

    # Add some regularisation for this test case.
    orig_epsilon = B.epsilon
    B.epsilon = 1e-10

    # Test conditioning on derivative.
    post = p.condition((0, 0), (dp(x), y))
    yield le, abs_err(post(x).mean - x ** 2), 1e-3

    # Set regularisation back.
    B.epsilon = orig_epsilon
コード例 #21
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_shorthands():
    model = Graph()
    p = GP(EQ(), graph=model)

    # Construct a normal distribution that serves as in input.
    x = p(1)
    assert isinstance(x, At)
    assert type_parameter(x) is p
    assert x.get() == 1
    assert str(p(x)) == '{}({})'.format(str(p), str(x))
    assert repr(p(x)) == '{}({})'.format(repr(p), repr(x))

    # Construct a normal distribution that does not serve as an input.
    x = Normal(np.ones((1, 1)))
    with pytest.raises(RuntimeError):
        type_parameter(x)
    with pytest.raises(RuntimeError):
        x.get()
    with pytest.raises(RuntimeError):
        p | (x, 1)

    # Test shorthands for stretching and selection.
    p = GP(EQ(), graph=Graph())
    assert str(p > 2) == str(p.stretch(2))
    assert str(p[0]) == str(p.select(0))
コード例 #22
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_sum_other():
    model = Graph()
    p1 = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=model)
    p2 = p1 + 5.
    p3 = 5. + p1
    p4 = model.sum(5., p1)

    x = np.random.randn(5, 1)
    yield assert_allclose, p1.mean(x) + 5., p2.mean(x)
    yield assert_allclose, p1.mean(x) + 5., p3.mean(x)
    yield assert_allclose, p1.mean(x) + 5., p4.mean(x)
    yield assert_allclose, p1.kernel(x), p2.kernel(x)
    yield assert_allclose, p1.kernel(x), p3.kernel(x)
    yield assert_allclose, p1.kernel(x), p4.kernel(x)
    yield assert_allclose, p1.kernel(p2(x), p3(x)), \
          p1.kernel(x)
    yield assert_allclose, p1.kernel(p2(x), p4(x)), \
          p1.kernel(x)

    # Check that a `GP` cannot be summed with a `Normal`.
    yield raises, NotImplementedError, lambda: p1 + Normal(np.eye(3))
    yield raises, NotImplementedError, lambda: Normal(np.eye(3)) + p1
コード例 #23
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_multi_conditioning():
    model = Graph()

    p1 = GP(EQ(), graph=model)
    p2 = GP(2 * Exp().stretch(2), graph=model)
    p3 = GP(.5 * RQ(1e-1).stretch(.5), graph=model)

    p = p1 + p2 + p3

    x1 = np.linspace(0, 2, 10)
    x2 = np.linspace(1, 3, 10)
    x3 = np.linspace(0, 3, 10)

    s1, s2 = model.sample(p1(x1), p2(x2))

    post1 = ((p | (p1(x1), s1)) | ((p2 | (p1(x1), s1))(x2), s2))(x3)
    post2 = (p | ((p1(x1), s1), (p2(x2), s2)))(x3)
    post3 = (p | ((p2(x2), s2), (p1(x1), s1)))(x3)

    yield assert_allclose, post1.mean, post2.mean, 'means 1', 1e-6, 1e-6
    yield assert_allclose, post1.mean, post3.mean, 'means 2', 1e-6, 1e-6
    yield assert_allclose, post1.var, post2.var
    yield assert_allclose, post1.var, post3.var
コード例 #24
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])
コード例 #25
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]
コード例 #26
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_case_additive_model():
    # Test some additive model.

    model = Graph()
    p1 = GP(EQ(), graph=model)
    p2 = GP(EQ(), graph=model)
    p3 = p1 + p2

    n = 5
    x = np.linspace(0, 10, n)[:, None]
    y1 = p1(x).sample()
    y2 = p2(x).sample()

    # First, test independence:
    yield assert_allclose, model.kernels[p2, p1](x), np.zeros((n, n))
    yield assert_allclose, model.kernels[p1, p2](x), np.zeros((n, n))

    # Now run through some test cases:

    obs = Obs(p1(x), y1)
    post = (p3 | obs) | ((p2 | obs)(x), y2)
    yield assert_allclose, post(x).mean, y1 + y2

    obs = Obs(p2(x), y2)
    post = (p3 | obs) | ((p1 | obs)(x), y1)
    yield assert_allclose, post(x).mean, y1 + y2

    obs = Obs(p1(x), y1)
    post = (p2 | obs) | ((p3 | obs)(x), y1 + y2)
    yield assert_allclose, post(x).mean, y2

    obs = Obs(p3(x), y1 + y2)
    post = (p2 | obs) | ((p1 | obs)(x), y1)
    yield assert_allclose, post(x).mean, y2

    yield assert_allclose, p3.condition(x, y1 + y2)(x).mean, y1 + y2
コード例 #27
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_naming():
    model = Graph()

    p1 = GP(EQ(), 1, graph=model)
    p2 = GP(EQ(), 2, graph=model)

    # Test setting and getting names.
    p1.name = 'name'

    assert model['name'] is p1
    assert p1.name == 'name'
    assert model[p1] == 'name'
    with pytest.raises(KeyError):
        model['other_name']
    with pytest.raises(KeyError):
        model[p2]

    # Check that names can not be doubly assigned.
    def doubly_assign():
        p2.name = 'name'

    with pytest.raises(RuntimeError):
        doubly_assign()

    # Move name to other GP.
    p1.name = 'other_name'
    p2.name = 'name'

    # Check that everything has been properly assigned.
    assert model['name'] is p2
    assert p2.name == 'name'
    assert model[p2] == 'name'
    assert model['other_name'] is p1
    assert p1.name == 'other_name'
    assert model[p1] == 'other_name'

    # Test giving a name to the constructor.
    p3 = GP(EQ(), name='yet_another_name', graph=model)
    assert model['yet_another_name'] is p3
    assert p3.name == 'yet_another_name'
    assert model[p3] == 'yet_another_name'
コード例 #28
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_naming():
    model = Graph()

    p1 = GP(EQ(), 1, graph=model)
    p2 = GP(EQ(), 2, graph=model)

    # Test setting and getting names.
    p1.name = 'name'

    yield ok, model['name'] is p1
    yield eq, p1.name, 'name'
    yield eq, model[p1], 'name'
    yield raises, KeyError, lambda: model['other_name']
    yield raises, KeyError, lambda: model[p2]

    # Check that names can not be doubly assigned.
    def doubly_assign():
        p2.name = 'name'

    yield raises, RuntimeError, doubly_assign

    # Move name to other GP.
    p1.name = 'other_name'
    p2.name = 'name'

    # Check that everything has been properly assigned.
    yield ok, model['name'] is p2
    yield eq, p2.name, 'name'
    yield eq, model[p2], 'name'
    yield ok, model['other_name'] is p1
    yield eq, p1.name, 'other_name'
    yield eq, model[p1], 'other_name'

    # Test giving a name to the constructor.
    p3 = GP(EQ(), name='yet_another_name', graph=model)
    yield ok, model['yet_another_name'] is p3
    yield eq, p3.name, 'yet_another_name'
    yield eq, model[p3], 'yet_another_name'
コード例 #29
0
ファイル: test_graph.py プロジェクト: wangwanwang56/stheno
def test_derivative():
    # Test construction:
    p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=Graph())
    assert str(p.diff(1)) == 'GP(d(1) EQ(), d(1) <lambda>)'

    # Test case:
    model = Graph()
    x = B.linspace(tf.float64, 0, 1, 100)[:, None]
    y = 2 * x

    p = GP(EQ(), graph=model)
    dp = p.diff()

    # Test conditioning on function.
    assert abs_err(dp.condition(p(x), y)(x).mean, 2) <= 1e-3

    # Test conditioning on derivative.
    post = p.condition((B.cast(tf.float64, 0),
                        B.cast(tf.float64, 0)), (dp(x), y))
    assert abs_err(post(x).mean, x ** 2) <= 1e-3
コード例 #30
0
ファイル: test_graph.py プロジェクト: leekwoon/stheno
def test_shifting():
    model = Graph()

    # Test construction:
    p = GP(Linear(), TensorProductMean(lambda x: x ** 2), graph=model)
    yield eq, str(p.shift(1)), 'GP(Linear() shift 1, <lambda> shift 1)'

    # Test case:
    p = GP(EQ(), graph=model)
    p2 = p.shift(5)

    n = 5
    x = np.linspace(0, 10, n)[:, None]
    y = p2(x).sample()

    post = p.condition(p2(x), y)
    yield assert_allclose, post(x - 5).mean, y
    yield le, abs_err(B.diag(post(x - 5).var)), 1e-10

    post = p2.condition(p(x), y)
    yield assert_allclose, post(x + 5).mean, y
    yield le, abs_err(B.diag(post(x + 5).var)), 1e-10