Skip to contents

Computes the posterior mean and variance of each partial derivative \(\partial f(x_*) / \partial x_{*,d}\) of the latent function of an exact Gaussian-process model. Differentiation is linear, so these derivatives are jointly Gaussian with the observations and the posterior is exact.

Usage

predict_gradient_gp(
  model,
  x,
  interval_level = 0.95,
  variance_tolerance = sqrt(.Machine$double.eps),
  block_size = NULL
)

Arguments

model

A fitted gaussianprocesses_model.

x

Numeric vector or matrix of prediction inputs.

interval_level

Probability level for the credible intervals.

variance_tolerance

Relative tolerance used when deciding whether a small negative posterior variance is floating-point error.

block_size

Number of prediction inputs processed together; NULL chooses it from a memory budget, as in predict_gp().

Value

An object of class gaussianprocesses_gradient_prediction with the prediction inputs x and, as matrices with one row per input and one column per input dimension, the posterior mean, variance, and sd of the partial derivatives and the credible-interval bounds lower and upper at interval_level.

Details

With \(C = R^\top R\) the factorized training covariance and \(\alpha = C^{-1}(y - m(X))\), $$\mathrm{E}[\partial_d f(x_*) \mid y] = \partial_d m(x_*) + \partial_{x_*,d} k(x_*, X)\,\alpha,$$ $$\mathrm{Var}[\partial_d f(x_*) \mid y] = \partial_d \partial'_d k(x_*, x_*) - v_d^\top v_d, \qquad v_d = R^{-\top} \partial_{x_*,d} k(X, x_*).$$ The kernel derivatives come from kernel_input_gradient() and its second-order counterpart. Only mean-square differentiable kernels have a gradient process; others raise an error of class gaussianprocesses_smoothness_error.

The variances are marginal: covariances between partial derivatives, or between points, are not returned.

Parametric means add \(\partial h(x_*)^\top \beta / \partial x_{*,d}\) to the mean. Marginalized coefficients add their uncertainty to the variance, as in predict_gp(), with \(H_*\) and \(K(x_*, X)\) replaced by their derivatives. basis_mean() has no input derivatives and raises an error.

Stability

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

Examples

x <- seq(0, 2 * pi, length.out = 25)
model <- fit_gp(
  x,
  sin(x),
  rbf_kernel(length_scale = 1),
  noise_variance = 1e-4
)

gradient <- predict_gradient_gp(model, c(0, pi / 2, pi))

# The derivative of sin is cos.
cbind(estimate = gradient$mean[, 1], truth = cos(c(0, pi / 2, pi)))
#>          estimate         truth
#> [1,]  0.969992695  1.000000e+00
#> [2,]  0.001655899  6.123234e-17
#> [3,] -0.999392258 -1.000000e+00
gradient$sd
#>              x1
#> [1,] 0.08710634
#> [2,] 0.01851901
#> [3,] 0.01781490