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 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 test_kernel_matrix(input_dim, nu): """Check that the product Matérn kernel matrix is an elementwise product of 1D Matérn kernel matrices.""" lengthscale = 1.25 matern = kernels.Matern(input_shape=(1,), lengthscale=lengthscale, nu=nu) product_matern = kernels.ProductMatern( input_shape=(input_dim,), lengthscales=lengthscale, nus=nu ) rng = np.random.default_rng(42) num_xs = 15 xs = rng.random(size=(num_xs, input_dim)) kernel_matrix1 = product_matern.matrix(xs) kernel_matrix2 = np.ones(shape=(num_xs, num_xs)) for dim in range(input_dim): kernel_matrix2 *= matern.matrix(_utils.as_colvec(xs[:, dim])) np.testing.assert_allclose( kernel_matrix1, kernel_matrix2, )
def test_nonpositive_nu_raises_exception(nu): """Check whether a non-positive nu parameter raises a ValueError.""" with pytest.raises(ValueError): kernels.Matern(input_shape=(), nu=nu)
], name="cov", ) def fixture_cov(request, input_dim: int) -> kernels.Kernel: """Covariance function.""" return request.param[0](**request.param[1], input_dim=input_dim) @pytest.fixture( params=[ pytest.param(randprocdef, id=randprocdef[0]) for randprocdef in [ ( "gp", randprocs.GaussianProcess( mean=lambda x: np.zeros(x.shape[0]), cov=kernels.Matern(input_dim=1), ), ), ] ], name="random_process", ) def fixture_random_process(request) -> randprocs.RandomProcess: """Random process.""" return request.param[1] @pytest.fixture(name="gaussian_process") def fixture_gaussian_process(mean, cov) -> randprocs.GaussianProcess: """Gaussian process.""" return randprocs.GaussianProcess(mean=mean, cov=cov)
name="cov", ) def fixture_cov(request, input_dim: int) -> kernels.Kernel: """Covariance function.""" return request.param[0](**request.param[1], input_shape=(input_dim,)) @pytest.fixture( params=[ pytest.param(randprocdef, id=randprocdef[0]) for randprocdef in [ ( "gp", randprocs.GaussianProcess( mean=mean_fns.Zero(input_shape=(1,)), cov=kernels.Matern(input_shape=(1,)), ), ), ] ], name="random_process", ) def fixture_random_process(request) -> randprocs.RandomProcess: """Random process.""" return request.param[1] @pytest.fixture(name="gaussian_process") def fixture_gaussian_process(mean, cov) -> randprocs.GaussianProcess: """Gaussian process.""" return randprocs.GaussianProcess(mean=mean, cov=cov)