def test_mean_wrong_input_shape_raises_error(): with pytest.raises(ValueError): randprocs.GaussianProcess( mean=mean_fns.Zero(input_shape=(2, 2), output_shape=(1, )), cov=kernels.ExpQuad(input_shape=(2, )), ) with pytest.raises(ValueError): randprocs.GaussianProcess( mean=mean_fns.Zero(input_shape=(2, ), output_shape=(2, 1)), cov=kernels.ExpQuad(input_shape=(2, )), )
def test_nu_large_recovers_rbf_kernel(x0: np.ndarray, x1: np.ndarray, input_shape: ShapeType): """Test whether a Matern kernel with nu large is close to an RBF kernel.""" lengthscale = 1.25 rbf = kernels.ExpQuad(input_shape=input_shape, lengthscale=lengthscale) matern = kernels.Matern(input_shape=input_shape, lengthscale=lengthscale, nu=15) np.testing.assert_allclose( rbf.matrix(x0, x1), matern.matrix(x0, x1), err_msg= "RBF and Matern kernel are not sufficiently close for nu->infty.", rtol=0.05, atol=0.01, )
def get_kernel(kernel_name, input_shape): """Return a kernel for a given name.""" if kernel_name == "white_noise": kernel = kernels.WhiteNoise(input_shape=input_shape) elif kernel_name == "linear": kernel = kernels.Linear(input_shape=input_shape) elif kernel_name == "polynomial": kernel = kernels.Polynomial(input_shape=input_shape) elif kernel_name == "exp_quad": kernel = kernels.ExpQuad(input_shape=input_shape) elif kernel_name == "rat_quad": kernel = kernels.RatQuad(input_shape=input_shape) elif kernel_name == "matern12": kernel = kernels.Matern(input_shape=input_shape, nu=0.5) elif kernel_name == "matern32": kernel = kernels.Matern(input_shape=input_shape, nu=1.5) elif kernel_name == "matern52": kernel = kernels.Matern(input_shape=input_shape, nu=2.5) elif kernel_name == "matern72": kernel = kernels.Matern(input_shape=input_shape, nu=3.5) else: raise ValueError(f"Kernel name '{kernel_name}' not recognized.") return kernel
def case_kernel_matrix(n: int, rng: np.random.Generator) -> np.ndarray: """Kernel Gram matrix.""" x_min, x_max = (-4.0, 4.0) X = rng.uniform(x_min, x_max, (n, 1)) kern = kernels.ExpQuad(input_shape=1, lengthscale=1) return kern(X)
def test_mean_not_function_raises_error(): with pytest.raises(TypeError): randprocs.GaussianProcess( mean=np.zeros_like, cov=kernels.ExpQuad(input_shape=(1, )), )
def test_product_kernel_contracts(): input_shape = () k = kernels.ExpQuad(input_shape=input_shape) k_prod = ProductKernel(k, ProductKernel(k, k)) assert all(not isinstance(factor, ProductKernel) for factor in k_prod._factors)
def test_sum_kernel_contracts(): input_shape = () k = kernels.ExpQuad(input_shape=input_shape) k_sum = SumKernel(k, SumKernel(k, k)) assert all(not isinstance(summand, SumKernel) for summand in k_sum._summands)