Skip to contents

Computes \(\partial k(x_i, y_j) / \partial x_{i,d}\) for every input dimension \(d\): the covariance between the derivative of the process at \(x_i\) and its value at \(y_j\).

Usage

kernel_input_gradient(kernel, x, y = NULL)

Arguments

kernel

A Gaussian-process kernel specification.

x

Numeric vector or matrix of inputs; the derivatives are taken with respect to these.

y

Optional numeric vector or matrix. If NULL, x is used.

Value

A list with one numeric matrix per input dimension. Element d has nrow(x) rows and nrow(y) columns and holds \(\partial k(x_i, y_j) / \partial x_{i,d}\).

Details

Input derivatives exist only for mean-square differentiable kernels. The Matérn-1/2 and white-noise kernels, and any sum or product that contains one, are not, and raise an error of class gaussianprocesses_smoothness_error. The Matérn-3/2 process is differentiable once, the Matérn-5/2 process twice, and the RBF, rational-quadratic, periodic, and linear processes infinitely often.

Derivatives are closed-form expressions for every kernel, written so that coincident inputs need no limit; sums, products, and scaled kernels follow the sum and product rules. predict_gradient_gp() uses them to predict the gradient of the latent function. The derivations are in vignette("v07-kernel-derivatives", package = "gaussianprocesses").

Stability

Experimental: this interface may change in a minor release, and every change is listed in NEWS. See gaussianprocesses-package for the policy.

See also

kernel_gradient() for derivatives with respect to the hyperparameters.

Examples

kernel <- rbf_kernel(variance = 1, length_scale = 0.5)
x <- c(0, 0.25, 1)

gradient <- kernel_input_gradient(kernel, x)
gradient[[1]]
#>            [,1]       [,2]      [,3]
#> [1,]  0.0000000  0.8824969 0.5413411
#> [2,] -0.8824969  0.0000000 0.9739574
#> [3,] -0.5413411 -0.9739574 0.0000000

# Compare with a central difference in the first input.
h <- 1e-6
(evaluate_kernel(kernel, x + h, x) -
  evaluate_kernel(kernel, x - h, x)) / (2 * h)
#>            [,1]       [,2]      [,3]
#> [1,]  0.0000000  0.8824969 0.5413411
#> [2,] -0.8824969  0.0000000 0.9739574
#> [3,] -0.5413411 -0.9739574 0.0000000