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>)'
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>)'
def test_compare_noisy_kernel_and_additive_component_kernel(): def NoisyKernelCopy(k1, k2): return AdditiveComponentKernel({ Component('wiggly'): k1, Component('noise'): k2 }, latent=[Component('wiggly')]) x = np.random.randn(10, 2) k1 = EQ() k2 = RQ(1e-2) kn1 = NoisyKernel(k1, k2) kn2 = NoisyKernelCopy(k1, k2) yield ok, allclose(kn1(Latent(x)), k1(x)), 'noisy kernel 1' yield ok, allclose(kn1(Latent(x), Observed(x)), k1(x)), 'noisy kernel 2' yield ok, allclose(kn1(Observed(x), Latent(x)), k1(x)), 'noisy kernel 3' yield ok, allclose(kn1(Observed(x)), (k1 + k2)(x)), 'noisy kernel 4' yield ok, allclose(kn2(Latent(x)), k1(x)), 'noisy copy 1' yield ok, allclose(kn2(Latent(x), Observed(x)), k1(x)), 'noisy copy 2' yield ok, allclose(kn2(Observed(x), Latent(x)), k1(x)), 'noisy copy 3' yield ok, allclose(kn2(Observed(x)), (k1 + k2)(x)), 'noisy copy 4' yield ok, allclose(kn2(Latent(x), Component('noise')(x)), ZeroKernel()(x)), 'additive 1' yield ok, allclose(kn2(Component('noise')(x), Latent(x)), ZeroKernel()(x)), 'additive 2' yield ok, allclose(kn2(Observed(x), Component('noise')(x)), k2(x)), 'additive 3' yield ok, allclose(kn2(Component('noise')(x), Observed(x)), k2(x)), 'additive 4' yield eq, str(NoisyKernel(EQ(), RQ(1))), \ 'NoisyKernel(EQ(), RQ(1))'
def test_grouping(): # Scales: assert str(5 * EQ()) == '5 * EQ()' assert str(5 * (5 * EQ())) == '25 * EQ()' # Stretches: assert str(EQ().stretch(5)) == 'EQ() > 5' assert str(EQ().stretch(5).stretch(5)) == 'EQ() > 25' # Shifts: assert str(Linear().shift(5)) == 'Linear() shift 5' assert str(Linear().shift(5).shift(5)) == 'Linear() shift 10' # Products: assert str((5 * EQ()) * (5 * EQ())) == '25 * EQ()' assert str((5 * (EQ() * EQ())) * (5 * EQ() * EQ())) == '25 * EQ() * EQ()' assert str((5 * RQ(1)) * (5 * RQ(2))) == '25 * RQ(1) * RQ(2)' # Sums: assert str((5 * EQ()) + (5 * EQ())) == '10 * EQ()' assert str(EQ() + (5 * EQ())) == '6 * EQ()' assert str((5 * EQ()) + EQ()) == '6 * EQ()' assert str((EQ() + EQ())) == '2 * EQ()' assert str((5 * (EQ() * EQ())) + (5 * (EQ() * EQ()))) == '10 * EQ() * EQ()' assert str((5 * RQ(1)) + (5 * RQ(2))) == '5 * RQ(1) + 5 * RQ(2)' # Reversal: assert str(reversed(Linear() + EQ())) == 'Reversed(Linear()) + EQ()' assert str(reversed(Linear() * EQ())) == 'Reversed(Linear()) * EQ()'
def test_grouping(): # Scales: yield eq, str(5 * EQ()), '5 * EQ()' yield eq, str(5 * (5 * EQ())), '25 * EQ()' # Stretches: yield eq, str(EQ().stretch(5)), 'EQ() > 5' yield eq, str(EQ().stretch(5).stretch(5)), 'EQ() > 25' # Shifts: yield eq, str(Linear().shift(5)), 'Linear() shift 5' yield eq, str(Linear().shift(5).shift(5)), 'Linear() shift 10' # Products: yield eq, str((5 * EQ()) * (5 * EQ())), '25 * EQ()' yield eq, str((5 * (EQ() * EQ())) * (5 * EQ() * EQ())), \ '25 * EQ() * EQ()' yield eq, str((5 * RQ(1)) * (5 * RQ(2))), '25 * RQ(1) * RQ(2)' # Sums: yield eq, str((5 * EQ()) + (5 * EQ())), '10 * EQ()' yield eq, str(EQ() + (5 * EQ())), '6 * EQ()' yield eq, str((5 * EQ()) + EQ()), '6 * EQ()' yield eq, str((EQ() + EQ())), '2 * EQ()' yield eq, str((5 * (EQ() * EQ())) + (5 * (EQ() * EQ()))), \ '10 * EQ() * EQ()' yield eq, str((5 * RQ(1)) + (5 * RQ(2))), '5 * RQ(1) + 5 * RQ(2)' # Reversal: yield eq, str(reversed(Linear() + EQ())), 'Reversed(Linear()) + EQ()' yield eq, str(reversed(Linear() * EQ())), 'Reversed(Linear()) * EQ()'
def test_terms(): k = EQ() + EQ() * Linear() + RQ(1) * RQ(2) + Delta() yield eq, k.num_terms, 4 yield eq, str(k.term(0)), 'EQ()' yield eq, str(k.term(1)), 'EQ() * Linear()' yield eq, str(k.term(2)), 'RQ(1) * RQ(2)' yield eq, str(k.term(3)), 'Delta()' yield raises, IndexError, lambda: k.term(4) yield raises, IndexError, lambda: EQ().term(1)
def test_terms(): k = EQ() + EQ() * Linear() + RQ(1) * RQ(2) + Delta() assert k.num_terms == 4 assert str(k.term(0)) == 'EQ()' assert str(k.term(1)) == 'EQ() * Linear()' assert str(k.term(2)) == 'RQ(1) * RQ(2)' assert str(k.term(3)) == 'Delta()' with pytest.raises(IndexError): k.term(4) with pytest.raises(IndexError): EQ().term(1)
def test_product(): k = (2 * EQ().stretch(10)) * (3 * RQ(1e-2).stretch(20)) assert k.stationary # Test equality. assert EQ() * Linear() == EQ() * Linear() assert EQ() * Linear() == Linear() * EQ() assert EQ() * Linear() != EQ() * RQ(1e-1) assert EQ() * Linear() != RQ(1e-1) * Linear() # Standard tests: standard_kernel_tests(k)
def test_product(): k = (2 * EQ().stretch(10)) * (3 * RQ(1e-2).stretch(20)) # Verify that the kernel has the right properties. assert k.stationary # Test equality. assert EQ() * Linear() == EQ() * Linear() assert EQ() * Linear() == Linear() * EQ() assert EQ() * Linear() != EQ() * RQ(1e-1) assert EQ() * Linear() != RQ(1e-1) * Linear() # Standard tests: standard_kernel_tests(k)
def test_sum(): k1 = EQ().stretch(2) k2 = 3 * RQ(1e-2).stretch(5) k = k1 + k2 assert k.stationary assert EQ() + Linear() == EQ() + Linear() assert EQ() + Linear() == Linear() + EQ() assert EQ() + Linear() != EQ() + RQ(1e-1) assert EQ() + Linear() != RQ(1e-1) + Linear() # Standard tests: standard_kernel_tests(k)
def test_parentheses(): yield eq, str((reversed(Linear() * Linear() + 2 * EQ().stretch(1).periodic(2) + RQ(3).periodic(4))) * (EQ().stretch(1) + EQ())), \ '(Reversed(Linear()) * Reversed(Linear()) + ' \ '2 * ((EQ() > 1) per 2) + RQ(3) per 4) * (EQ() > 1 + EQ())'
def test_sum(): k1 = EQ().stretch(2) k2 = 3 * RQ(1e-2).stretch(5) k = k1 + k2 # Verify that the kernel has the right properties. assert k.stationary # Test equality. assert EQ() + Linear() == EQ() + Linear() assert EQ() + Linear() == Linear() + EQ() assert EQ() + Linear() != EQ() + RQ(1e-1) assert EQ() + Linear() != RQ(1e-1) + Linear() # Standard tests: standard_kernel_tests(k)
def test_product(): k = (2 * EQ().stretch(10)) * (3 * RQ(1e-2).stretch(20)) assert k.stationary assert k.length_scale == 10 assert k.period == np.inf assert k.var == 6 # Test equality. assert EQ() * Linear() == EQ() * Linear() assert EQ() * Linear() == Linear() * EQ() assert EQ() * Linear() != EQ() * RQ(1e-1) assert EQ() * Linear() != RQ(1e-1) * Linear() # Standard tests: standard_kernel_tests(k)
def test_basic_arithmetic(): k1 = EQ() k2 = RQ(1e-1) k3 = Matern12() k4 = Matern32() k5 = Matern52() k6 = Delta() k7 = Linear() xs1 = np.random.randn(10, 2), np.random.randn(20, 2) xs2 = np.random.randn(), np.random.randn() yield ok, allclose(k6(xs1[0]), k6(xs1[0], xs1[0])), 'dispatch' yield ok, allclose((k1 * k2)(*xs1), k1(*xs1) * k2(*xs1)), 'prod' yield ok, allclose((k1 * k2)(*xs2), k1(*xs2) * k2(*xs2)), 'prod 2' yield ok, allclose((k3 + k4)(*xs1), k3(*xs1) + k4(*xs1)), 'sum' yield ok, allclose((k3 + k4)(*xs2), k3(*xs2) + k4(*xs2)), 'sum 2' yield ok, allclose((5. * k5)(*xs1), 5. * k5(*xs1)), 'prod 3' yield ok, allclose((5. * k5)(*xs2), 5. * k5(*xs2)), 'prod 4' yield ok, allclose((5. + k7)(*xs1), 5. + k7(*xs1)), 'sum 3' yield ok, allclose((5. + k7)(*xs2), 5. + k7(*xs2)), 'sum 4' yield ok, allclose(k1.stretch(2.)(*xs1), k1(xs1[0] / 2., xs1[1] / 2.)), 'stretch' yield ok, allclose(k1.stretch(2.)(*xs2), k1(xs2[0] / 2., xs2[1] / 2.)), 'stretch 2' yield ok, allclose( k1.periodic(1.)(*xs1), k1.periodic(1.)(xs1[0], xs1[1] + 5.)), 'periodic' yield ok, allclose( k1.periodic(1.)(*xs2), k1.periodic(1.)(xs2[0], xs2[1] + 5.)), 'periodic 2'
def test_corner_cases(): with pytest.raises(IndexError): EQ().stretch(1)[1] with pytest.raises(IndexError): (EQ() + RQ(1))[2] with pytest.raises(RuntimeError): mul(1, 1) with pytest.raises(RuntimeError): add(1, 1) with pytest.raises(RuntimeError): stretch(1, 1) with pytest.raises(RuntimeError): transform(1, 1) with pytest.raises(RuntimeError): differentiate(1, 1) with pytest.raises(RuntimeError): select(1, 1) with pytest.raises(RuntimeError): shift(1, 1) assert repr(EQ()) == str(EQ()) assert EQ().__name__ == 'EQ' with pytest.raises(NotImplementedError): WrappedElement(1).display(1, lambda x: x) with pytest.raises(NotImplementedError): JoinElement(1, 2).display(1, 2, lambda x: x)
def test_product(): k = (2 * EQ().stretch(10)) * (3 * RQ(1e-2).stretch(20)) yield eq, k.stationary, True yield eq, k.length_scale, 10 yield eq, k.period, np.inf yield eq, k.var, 6 # Test equality. yield eq, EQ() * Linear(), EQ() * Linear() yield eq, EQ() * Linear(), Linear() * EQ() yield neq, EQ() * Linear(), EQ() * RQ(1e-1) yield neq, EQ() * Linear(), RQ(1e-1) * Linear() # Standard tests: for x in kernel_generator(k): yield x
def test_sum(): k1 = EQ().stretch(2) k2 = 3 * RQ(1e-2).stretch(5) k = k1 + k2 assert k.stationary allclose(k.length_scale, (1 * 2 + 3 * 5) / 4) assert k.period == np.inf assert k.var == 4 allclose((EQ() + EQ()).length_scale, 1) allclose((EQ().stretch(2) + EQ().stretch(2)).length_scale, 2) assert EQ() + Linear() == EQ() + Linear() assert EQ() + Linear() == Linear() + EQ() assert EQ() + Linear() != EQ() + RQ(1e-1) assert EQ() + Linear() != RQ(1e-1) + Linear() # Standard tests: standard_kernel_tests(k)
def test_sum(): k1 = EQ().stretch(2) k2 = 3 * RQ(1e-2).stretch(5) k = k1 + k2 yield eq, k.stationary, True yield assert_allclose, k.length_scale, (1 * 2 + 3 * 5) / 4 yield eq, k.period, np.inf yield eq, k.var, 4 yield assert_allclose, (EQ() + EQ()).length_scale, 1 yield assert_allclose, (EQ().stretch(2) + EQ().stretch(2)).length_scale, 2 yield eq, EQ() + Linear(), EQ() + Linear() yield eq, EQ() + Linear(), Linear() + EQ() yield neq, EQ() + Linear(), EQ() + RQ(1e-1) yield neq, EQ() + Linear(), RQ(1e-1) + Linear() # Standard tests: for x in kernel_generator(k): yield x
def test_factors(): k = EQ() * Linear() yield eq, k.num_factors, 2 yield eq, str(k.factor(0)), 'EQ()' yield eq, str(k.factor(1)), 'Linear()' yield raises, IndexError, lambda: k.factor(2) k = (EQ() + EQ()) * Delta() * (RQ(1) + Linear()) yield eq, k.num_factors, 4 yield eq, str(k.factor(0)), '2' yield eq, str(k.factor(1)), 'EQ()' yield eq, str(k.factor(2)), 'Delta()' yield eq, str(k.factor(3)), 'RQ(1) + Linear()' yield raises, IndexError, lambda: k.factor(4) yield raises, IndexError, lambda: EQ().factor(1)
def test_corner_cases(): yield raises, IndexError, lambda: EQ().stretch(1)[1] yield raises, IndexError, lambda: (EQ() + RQ(1))[2] yield raises, RuntimeError, lambda: mul(1, 1) yield raises, RuntimeError, lambda: add(1, 1) yield raises, RuntimeError, lambda: stretch(1, 1) yield raises, RuntimeError, lambda: transform(1, 1) yield raises, RuntimeError, lambda: differentiate(1, 1) yield raises, RuntimeError, lambda: select(1, 1) yield raises, RuntimeError, lambda: shift(1, 1) yield eq, repr(EQ()), str(EQ()) yield eq, EQ().__name__, 'EQ' yield raises, NotImplementedError, \ lambda: WrappedElement(1).display(1, lambda x: x) yield raises, NotImplementedError, \ lambda: JoinElement(1, 2).display(1, 2, lambda x: x)
def test_factors(): k = EQ() * Linear() assert k.num_factors == 2 assert str(k.factor(0)) == 'EQ()' assert str(k.factor(1)) == 'Linear()' with pytest.raises(IndexError): k.factor(2) k = (EQ() + EQ()) * Delta() * (RQ(1) + Linear()) assert k.num_factors == 4 assert str(k.factor(0)) == '2' assert str(k.factor(1)) == 'EQ()' assert str(k.factor(2)) == 'Delta()' assert str(k.factor(3)) == 'RQ(1) + Linear()' with pytest.raises(IndexError): k.factor(4) with pytest.raises(IndexError): EQ().factor(1)
def test_component_kernel(): x = np.random.randn(10, 2) k1 = EQ() k2 = RQ(1e-1) kzero = ZeroKernel() kc = ComponentKernel({ (Component(1), Component(1)): k1, (Component(1), Component(2)): kzero, (Component(2), Component(1)): kzero, (Component(2), Component(2)): k2 }) yield ok, allclose(kc(Component(1)(x)), k1(x)) yield ok, allclose(kc(Component(2)(x)), k2(x)) yield ok, allclose(kc(Component(1)(x), Component(2)(x)), kzero(x)) yield ok, allclose(kc(Component(2)(x), Component(1)(x)), kzero(x)) yield eq, str(ComponentKernel({Component(1): EQ(), Component(2): EQ()})), \ 'ComponentKernel(EQ(), EQ())'
def test_rq(): k = RQ(1e-1) # Verify that the kernel has the right properties. assert k.alpha == 1e-1 assert k.stationary assert str(k) == 'RQ(0.1)' # Test equality. assert RQ(1e-1) == RQ(1e-1) assert RQ(1e-1) != RQ(2e-1) assert RQ(1e-1) != Linear() # Standard tests: standard_kernel_tests(k)
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
def test_basic_arithmetic(): k1 = EQ() k2 = RQ(1e-1) k3 = Matern12() k4 = Matern32() k5 = Matern52() k6 = Delta() k7 = Linear() xs1 = B.randn(10, 2), B.randn(20, 2) xs2 = B.randn(), B.randn() approx(k6(xs1[0]), k6(xs1[0], xs1[0])) approx((k1 * k2)(*xs1), k1(*xs1) * k2(*xs1)) approx((k1 * k2)(*xs2), k1(*xs2) * k2(*xs2)) approx((k3 + k4)(*xs1), k3(*xs1) + k4(*xs1)) approx((k3 + k4)(*xs2), k3(*xs2) + k4(*xs2)) approx((5.0 * k5)(*xs1), 5.0 * k5(*xs1)) approx((5.0 * k5)(*xs2), 5.0 * k5(*xs2)) approx((5.0 + k7)(*xs1), 5.0 + k7(*xs1)) approx((5.0 + k7)(*xs2), 5.0 + k7(*xs2)) approx(k1.stretch(2.0)(*xs1), k1(xs1[0] / 2.0, xs1[1] / 2.0)) approx(k1.stretch(2.0)(*xs2), k1(xs2[0] / 2.0, xs2[1] / 2.0)) approx(k1.periodic(1.0)(*xs1), k1.periodic(1.0)(xs1[0], xs1[1] + 5.0)) approx(k1.periodic(1.0)(*xs2), k1.periodic(1.0)(xs2[0], xs2[1] + 5.0))
def test_rq(): k = RQ(1e-1) # Verify that the kernel has the right properties. yield eq, k.alpha, 1e-1 yield eq, k.stationary, True yield eq, k.var, 1 yield eq, k.length_scale, 1 yield eq, k.period, np.inf yield eq, str(k), 'RQ(0.1)' # Test equality. yield eq, RQ(1e-1), RQ(1e-1) yield neq, RQ(1e-1), RQ(2e-1) yield neq, RQ(1e-1), Linear() # Standard tests: for x in kernel_generator(k): yield x
def test_indexing(): yield eq, str((5 * EQ())[0]), 'EQ()' yield eq, str((EQ() + RQ(1.0))[0]), 'EQ()' yield eq, str((EQ() + RQ(1.0))[1]), 'RQ(1.0)' yield eq, str((EQ() * RQ(1.0))[0]), 'EQ()' yield eq, str((EQ() * RQ(1.0))[1]), 'RQ(1.0)'
def test_subtraction_and_negation(): assert str(-EQ()) == '-1 * EQ()' assert str(EQ() - EQ()) == '0' assert str(RQ(1) - EQ()) == 'RQ(1) + -1 * EQ()' assert str(1 - EQ()) == '1 + -1 * EQ()' assert str(EQ() - 1) == 'EQ() + -1 * 1'
def test_indexing(): assert str((5 * EQ())[0]) == 'EQ()' assert str((EQ() + RQ(1.0))[0]) == 'EQ()' assert str((EQ() + RQ(1.0))[1]) == 'RQ(1.0)' assert str((EQ() * RQ(1.0))[0]) == 'EQ()' assert str((EQ() * RQ(1.0))[1]) == 'RQ(1.0)'
def test_subtraction_and_negation(): yield eq, str(-EQ()), '-1 * EQ()' yield eq, str(EQ() - EQ()), '0' yield eq, str(RQ(1) - EQ()), 'RQ(1) + -1 * EQ()' yield eq, str(1 - EQ()), '1 + -1 * EQ()' yield eq, str(EQ() - 1), 'EQ() + -1 * 1'