Skip to contents

Computes common kernel functions elementwise for two equal-length numeric vectors. Each function is vectorized: the result at position i depends only on y1[i] and y2[i].

Usage

linear_kernel(y1, y2)

polynomial_kernel(y1, y2, intercept, degree)

gaussian_kernel(y1, y2, bandwidth)

matern_kernel(y1, y2, lengthscale)

Arguments

y1

A numeric vector.

y2

A numeric vector of the same length as y1.

intercept

Numeric intercept \(c\).

degree

Non-negative integer degree \(d\).

bandwidth

Positive numeric bandwidth \(\sigma\).

lengthscale

Positive numeric lengthscale \(\ell\).

Value

A numeric vector of length length(y1) giving the kernel values for each pair of corresponding elements in y1 and y2.

Details

The following kernels are provided:

  • Linear: $$K(y_1, y_2) = y_1 y_2$$

  • Polynomial: $$K(y_1, y_2) = (y_1 y_2 + c)^{d},$$ with intercept \(c \in \mathbb{R}\) and degree \(d \in \mathbb{N}_0\).

  • Gaussian (RBF): $$K(y_1, y_2) = \exp\left(-\frac{(y_1 - y_2)^2}{2 \sigma^2}\right),$$ where \(\sigma\) is the bandwidth.

  • Matern (\(\nu = 3/2\)): $$K(y_1, y_2) = \left(1 + \frac{\sqrt{3} |y_1 - y_2|}{\ell}\right) \exp\left(-\frac{\sqrt{3} |y_1 - y_2|}{\ell}\right),$$ with lengthscale \(\ell > 0\).

Inputs must have the same length; kernel-specific parameters must be valid (e.g., positive bandwidth/lengthscale, non-negative integer degree). Inputs containing NA will yield NA in the corresponding positions.

Examples

linear_kernel(1:3, 4:6)
#> [1]  4 10 18
polynomial_kernel(1:3, 4:6, intercept = 1, degree = 2)
#> [1]  25 121 361
gaussian_kernel(1:3, 4:6, bandwidth = 1)
#> [1] 0.011109 0.011109 0.011109
matern_kernel(1:3, 4:6, lengthscale = 0.5)
#> [1] 0.0003493743 0.0003493743 0.0003493743