def test_derivative(): m = TensorProductMean(lambda x: x**2) m2 = TensorProductMean(lambda x: x**3) x = B.randn(tf.float64, 10, 1) allclose(m.diff(0)(x), 2 * x) allclose(m2.diff(0)(x), 3 * x**2)
def test_basic_arithmetic(): dispatch = Dispatcher() @dispatch(Number) def f1(x): return np.array([[x**2]]) @dispatch(object) def f1(x): return np.sum(x**2, axis=1)[:, None] @dispatch(Number) def f2(x): return np.array([[x**3]]) @dispatch(object) def f2(x): return np.sum(x**3, axis=1)[:, None] m1 = TensorProductMean(f1) m2 = TensorProductMean(f2) m3 = ZeroMean() x1 = np.random.randn(10, 2) x2 = np.random.randn() yield ok, np.allclose((m1 * m2)(x1), m1(x1) * m2(x1)), 'prod' yield ok, np.allclose((m1 * m2)(x2), m1(x2) * m2(x2)), 'prod 2' yield ok, np.allclose((m1 + m3)(x1), m1(x1) + m3(x1)), 'sum' yield ok, np.allclose((m1 + m3)(x2), m1(x2) + m3(x2)), 'sum 2' yield ok, np.allclose((5. * m1)(x1), 5. * m1(x1)), 'prod 3' yield ok, np.allclose((5. * m1)(x2), 5. * m1(x2)), 'prod 4' yield ok, np.allclose((5. + m1)(x1), 5. + m1(x1)), 'sum 3' yield ok, np.allclose((5. + m1)(x2), 5. + m1(x2)), 'sum 4'
def test_sum(): m1 = TensorProductMean(f1) m2 = TensorProductMean(f2) # Test equality. assert m1 + m2 == m1 + m2 assert m1 + m2 == m2 + m1 assert m1 + m2 != ZeroMean() + m2 assert m1 + m2 != m1 + ZeroMean()
def test_product(): m1 = TensorProductMean(f1) m2 = TensorProductMean(f2) # Test equality. assert m1 * m2 == m1 * m2 assert m1 * m2 == m2 * m1 assert m1 * m2 != m2 * m2 assert m1 * m2 != m1 * m1
def test_posterior_mean(): z = np.linspace(0, 1, 10) pcm = PosteriorMean(TensorProductMean(lambda x: x), TensorProductMean(lambda x: x**2), EQ(), z, 2 * EQ()(z), np.random.randn(10)) # Check name. assert str(pcm) == 'PosteriorMean()' # Check that the mean computes. pcm(z)
def test_posterior_mean(): z = np.linspace(0, 1, 10) pcm = PosteriorMean(TensorProductMean(lambda x: x), TensorProductMean(lambda x: x**2), EQ(), z, matrix(2 * EQ()(z)), np.random.randn(10)) # Check name. yield eq, str(pcm), 'PosteriorMean()' # Check that the mean computes. yield lambda: pcm(z)
def test_derivative(): B.backend_to_tf() s = B.Session() m = TensorProductMean(lambda x: x**2) m2 = TensorProductMean(lambda x: x**3) x = B.array(np.random.randn(10, 1)) yield assert_allclose, s.run(m.diff(0)(x)), s.run(2 * x) yield assert_allclose, s.run(m2.diff(0)(x)), s.run(3 * x**2) s.close() B.backend_to_np()
def test_sum_other(): p = GP(TensorProductMean(lambda x: x**2), EQ()) def five(y): return 5 * B.ones(B.shape(y)[0], 1) x = B.randn(5, 1) for p_sum in [ # Add a numeric thing. p + 5.0, 5.0 + p, p.measure.sum(GP(), p, 5.0), p.measure.sum(GP(), 5.0, p), # Add a function. p + five, five + p, p.measure.sum(GP(), p, five), p.measure.sum(GP(), five, p), ]: approx(p.mean(x) + 5.0, p_sum.mean(x)) approx(p.mean(x) + 5.0, p_sum.mean(x)) approx(p.kernel(x), p_sum.kernel(x)) approx(p.kernel(x), p_sum.kernel(x)) # Check that a `GP` cannot be summed with a `Normal`. with pytest.raises(NotFoundLookupError): p + Normal(np.eye(3)) with pytest.raises(NotFoundLookupError): Normal(np.eye(3)) + p
def test_mul_other(): p = GP(TensorProductMean(lambda x: x**2), EQ()) def five(y): return 5 * B.ones(B.shape(y)[0], 1) x = B.randn(5, 1) for p_mul in [ # Multiply numeric thing. p * 5.0, 5.0 * p, p.measure.mul(GP(), p, 5.0), p.measure.mul(GP(), 5.0, p), # Multiply with a function. p * five, five * p, p.measure.mul(GP(), p, five), p.measure.mul(GP(), five, p), ]: approx(5.0 * p.mean(x), p_mul.mean(x)) approx(5.0 * p.mean(x), p_mul.mean(x)) approx(25.0 * p.kernel(x), p_mul.kernel(x)) approx(25.0 * p.kernel(x), p_mul.kernel(x)) # Check that a `GP` cannot be multiplied with a `Normal`. with pytest.raises(NotFoundLookupError): p * Normal(np.eye(3)) with pytest.raises(NotFoundLookupError): Normal(np.eye(3)) * p
def test_basic_arithmetic(): m1 = TensorProductMean(f1) m2 = TensorProductMean(f2) m3 = ZeroMean() x1 = B.randn(10, 2) x2 = B.randn() approx((m1 * m2)(x1), m1(x1) * m2(x1)) approx((m1 * m2)(x2), m1(x2) * m2(x2)) approx((m1 + m3)(x1), m1(x1) + m3(x1)) approx((m1 + m3)(x2), m1(x2) + m3(x2)) approx((5.0 * m1)(x1), 5.0 * m1(x1)) approx((5.0 * m1)(x2), 5.0 * m1(x2)) approx((5.0 + m1)(x1), 5.0 + m1(x1)) approx((5.0 + m1)(x2), 5.0 + m1(x2))
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
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()
def test_selection(): # Test construction: p = GP(TensorProductMean(lambda x: x**2), EQ()) assert str(p.select(1)) == "GP(<lambda> : [1], EQ() : [1])" assert str(p.select(1, 2)) == "GP(<lambda> : [1, 2], EQ() : [1, 2])" # Test case: p = GP(EQ()) # 1D p2 = p.select(0) # 2D x = B.linspace(0, 5, 10) x21 = B.stack(x, B.randn(10), axis=1) x22 = B.stack(x, B.randn(10), axis=1) y = p2(x).sample() post = p.measure | (p2(x21), y) approx(post(p(x)).mean, y) assert_equal_normals(post(p(x)), post(p2(x21))) post = p.measure | (p2(x22), y) approx(post(p(x)).mean, y) assert_equal_normals(post(p(x)), post(p2(x22))) post = p.measure | (p(x), y) approx(post(p2(x21)).mean, y) approx(post(p2(x22)).mean, y) assert_equal_normals(post(p2(x21)), post(p(x))) assert_equal_normals(post(p2(x22)), post(p(x)))
def test_construction(): model = Graph() p = GP(EQ(), graph=model) x = np.random.randn(10, 1) c = Cache() yield p.mean, x yield p.mean, p(x) yield p.mean, x, c yield p.mean, p(x), c yield p.kernel, x yield p.kernel, p(x) yield p.kernel, x, c yield p.kernel, p(x), c yield p.kernel, x, x yield p.kernel, p(x), x yield p.kernel, x, p(x) yield p.kernel, p(x), p(x) yield p.kernel, x, x, c yield p.kernel, p(x), x, c yield p.kernel, x, p(x), c yield p.kernel, p(x), p(x), c yield p.kernel.elwise, x yield p.kernel.elwise, p(x) yield p.kernel.elwise, x, c yield p.kernel.elwise, p(x), c yield p.kernel.elwise, x, x yield p.kernel.elwise, p(x), x yield p.kernel.elwise, x, p(x) yield p.kernel.elwise, p(x), p(x) yield p.kernel.elwise, x, x, c yield p.kernel.elwise, p(x), x, c yield p.kernel.elwise, x, p(x), c yield p.kernel.elwise, p(x), p(x), c # Test resolution of kernel and mean. k = EQ() m = TensorProductMean(lambda x: x ** 2) yield assert_instance, GP(k, graph=model).mean, ZeroMean yield assert_instance, GP(k, 5, graph=model).mean, ScaledMean yield assert_instance, GP(k, 1, graph=model).mean, OneMean yield assert_instance, GP(k, 0, graph=model).mean, ZeroMean yield assert_instance, GP(k, m, graph=model).mean, TensorProductMean yield assert_instance, GP(k, graph=model).kernel, EQ yield assert_instance, GP(5, graph=model).kernel, ScaledKernel yield assert_instance, GP(1, graph=model).kernel, OneKernel yield assert_instance, GP(0, graph=model).kernel, ZeroKernel # Test construction of finite-dimensional distribution. d = GP(k, m, graph=model)(x) yield assert_allclose, d.var, k(x) yield assert_allclose, d.mean, m(x)
def test_construction(): m = TensorProductMean(lambda x: x**2) x = np.random.randn(10, 1) c = Cache() yield m, x yield m, x, c yield m, Observed(x) yield m, Observed(x), c
def test_stretching(): # Test construction: p = GP(TensorProductMean(lambda x: x**2), EQ()) assert str(p.stretch(1)) == "GP(<lambda> > 1, EQ() > 1)" # Test case: p = GP(EQ()) p_stretched = p.stretch(5) x = B.linspace(0, 5, 10) y = p_stretched(x).sample() post = p.measure | (p_stretched(x), y) assert_equal_normals(post(p(x / 5)), post(p_stretched(x))) assert_equal_normals(post(p(x)), post(p_stretched(x * 5)))
def test_shifting(): # Test construction: p = GP(TensorProductMean(lambda x: x**2), Linear()) assert str(p.shift(1)) == "GP(<lambda> shift 1, Linear() shift 1)" # Test case: p = GP(EQ()) p_shifted = p.shift(5) x = B.linspace(0, 5, 10) y = p_shifted(x).sample() post = p.measure | (p_shifted(x), y) assert_equal_normals(post(p(x - 5)), post(p_shifted(x))) assert_equal_normals(post(p(x)), post(p_shifted(x + 5)))
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'
def test_input_transform(): # Test construction: p = GP(TensorProductMean(lambda x: x**2), EQ()) assert (str(p.transform(lambda x: x)) == "GP(<lambda> transform <lambda>, EQ() transform <lambda>)") # Test case: p = GP(EQ()) p_transformed = p.transform(lambda x: B.sqrt(x)) x = B.linspace(0, 5, 10) y = p_transformed(x).sample() post = p.measure | (p_transformed(x), y) assert_equal_normals(post(p(B.sqrt(x))), post(p_transformed(x))) assert_equal_normals(post(p(x)), post(p_transformed(x * x)))
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"
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
def test_shifting(): # Kernels: yield eq, str(ZeroKernel().shift(5)), '0' yield eq, str(EQ().shift(5)), 'EQ()' yield eq, str(Linear().shift(5)), 'Linear() shift 5' yield eq, str((5 * EQ()).shift(5)), '5 * EQ()' yield eq, str((5 * Linear()).shift(5)), '(5 * Linear()) shift 5' # Means: def mean(x): return x m = TensorProductMean(mean) yield eq, str(ZeroMean().shift(5)), '0' yield eq, str(m.shift(5)), 'mean shift 5' yield eq, str(m.shift(5).shift(5)), 'mean shift 10' yield eq, str((5 * m).shift(5)), '(5 * mean) shift 5'
def test_shifting(): # Kernels: assert str(ZeroKernel().shift(5)) == '0' assert str(EQ().shift(5)) == 'EQ()' assert str(Linear().shift(5)) == 'Linear() shift 5' assert str((5 * EQ()).shift(5)) == '5 * EQ()' assert str((5 * Linear()).shift(5)) == '(5 * Linear()) shift 5' # Means: def mean(x): return x m = TensorProductMean(mean) assert str(ZeroMean().shift(5)) == '0' assert str(m.shift(5)) == 'mean shift 5' assert str(m.shift(5).shift(5)) == 'mean shift 10' assert str((5 * m).shift(5)) == '(5 * mean) shift 5'
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
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
def test_construction(): model = Graph() p = GP(EQ(), graph=model) x = np.random.randn(10, 1) p.mean(x) p.mean(p(x)) p.kernel(x) p.kernel(p(x)) p.kernel(x, x) p.kernel(p(x), x) p.kernel(x, p(x)) p.kernel(p(x), p(x)) p.kernel.elwise(x) p.kernel.elwise(p(x)) p.kernel.elwise(x, x) p.kernel.elwise(p(x), x) p.kernel.elwise(x, p(x)) p.kernel.elwise(p(x), p(x)) # Test resolution of kernel and mean. k = EQ() m = TensorProductMean(lambda x: x ** 2) assert isinstance(GP(k, graph=model).mean, ZeroMean) assert isinstance(GP(k, 5, graph=model).mean, ScaledMean) assert isinstance(GP(k, 1, graph=model).mean, OneMean) assert isinstance(GP(k, 0, graph=model).mean, ZeroMean) assert isinstance(GP(k, m, graph=model).mean, TensorProductMean) assert isinstance(GP(k, graph=model).kernel, EQ) assert isinstance(GP(5, graph=model).kernel, ScaledKernel) assert isinstance(GP(1, graph=model).kernel, OneKernel) assert isinstance(GP(0, graph=model).kernel, ZeroKernel) # Test construction of finite-dimensional distribution. d = GP(k, m, graph=model)(x) allclose(d.var, k(x)) allclose(d.mean, m(x))
def test_marginals(): model = Graph() p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=model) x = np.linspace(0, 5, 10) # Check that `marginals` outputs the right thing. mean, lower, upper = p(x).marginals() var = B.diag(p.kernel(x)) yield assert_allclose, mean, p.mean(x)[:, 0] yield assert_allclose, lower, p.mean(x)[:, 0] - 2 * var ** .5 yield assert_allclose, upper, p.mean(x)[:, 0] + 2 * var ** .5 # Test correctness. y = p(x).sample() mean, lower, upper = (p | (x, y))(x).marginals() yield assert_allclose, mean, y[:, 0] yield le, B.mean(B.abs(upper - lower)), 1e-5 mean, lower, upper = (p | (x, y))(x + 100).marginals() yield assert_allclose, mean, p.mean(x + 100)[:, 0] yield assert_allclose, upper - lower, 4 * np.ones(10)
def test_construction(): p = GP(EQ()) x = B.randn(10, 1) p.mean(x) p.mean(Input(x)) p.kernel(x) p.kernel(Input(x)) p.kernel(x, x) p.kernel(Input(x), x) p.kernel(x, Input(x)) p.kernel(Input(x), Input(x)) p.kernel.elwise(x) p.kernel.elwise(Input(x)) p.kernel.elwise(x, x) p.kernel.elwise(Input(x), x) p.kernel.elwise(x, Input(x)) p.kernel.elwise(Input(x), Input(x)) # Test resolution of kernel and mean. k = EQ() m = TensorProductMean(lambda x: x**2) assert isinstance(GP(k).mean, ZeroMean) assert isinstance(GP(5, k).mean, ScaledMean) assert isinstance(GP(1, k).mean, OneMean) assert isinstance(GP(0, k).mean, ZeroMean) assert isinstance(GP(m, k).mean, TensorProductMean) assert isinstance(GP(k).kernel, EQ) assert isinstance(GP(5).kernel, ScaledKernel) assert isinstance(GP(1).kernel, OneKernel) assert isinstance(GP(0).kernel, ZeroKernel) # Test construction of finite-dimensional distribution. d = GP(m, k)(x) approx(d.var, k(x)) approx(d.mean, m(x))
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
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