kernel_matrix {BKP} | R Documentation |
Compute Kernel Matrix Between Input Locations
Description
Computes the kernel matrix between two sets of input locations using a specified kernel function. Supports both isotropic and anisotropic lengthscales. Available kernels include the Gaussian, Matérn 5/2, and Matérn 3/2.
Usage
kernel_matrix(
X,
Xprime = NULL,
theta = 0.1,
kernel = c("gaussian", "matern52", "matern32"),
anisotropic = TRUE
)
Arguments
X |
A numeric matrix (or vector) of input locations with shape |
Xprime |
An optional numeric matrix of input locations with shape |
theta |
A positive numeric value or vector specifying the kernel
lengthscale(s). If a scalar, the same lengthscale is applied to all input
dimensions. If a vector, it must be of length |
kernel |
A character string specifying the kernel function. Must be one
of |
anisotropic |
Logical. If |
Details
Let \mathbf{x}
and \mathbf{x}'
denote two input points.
The scaled distance is defined as
r = \left\| \frac{\mathbf{x} - \mathbf{x}'}{\boldsymbol{\theta}} \right\|_2.
The available kernels are defined as:
-
Gaussian:
k(\mathbf{x}, \mathbf{x}') = \exp(-r^2)
-
Matérn 5/2:
k(\mathbf{x}, \mathbf{x}') = \left(1 + \sqrt{5} r + \frac{5}{3} r^2 \right) \exp(-\sqrt{5} r)
-
Matérn 3/2:
k(\mathbf{x}, \mathbf{x}') = \left(1 + \sqrt{3} r \right) \exp(-\sqrt{3} r)
The function performs consistency checks on input dimensions and
automatically broadcasts theta
when it is a scalar.
Value
A numeric matrix of size n \times m
, where each element
K_{ij}
gives the kernel similarity between input X_i
and
X'_j
.
References
Rasmussen, C. E., & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
Examples
# Basic usage with default Xprime = X
X <- matrix(runif(20), ncol = 2)
K1 <- kernel_matrix(X, theta = 0.2, kernel = "gaussian")
# Anisotropic lengthscales with Matérn 5/2
K2 <- kernel_matrix(X, theta = c(0.1, 0.3), kernel = "matern52")
# Isotropic Matérn 3/2
K3 <- kernel_matrix(X, theta = 1, kernel = "matern32", anisotropic = FALSE)
# Use Xprime different from X
Xprime <- matrix(runif(10), ncol = 2)
K4 <- kernel_matrix(X, Xprime, theta = 0.2, kernel = "gaussian")