rgpycrumbs.surfaces._kernels¶
Kernel functions for Gaussian process surface fitting.
Provides element-wise and matrix kernel functions for Matern 5/2, inverse
multiquadric (IMQ), squared exponential (SE), rational quadratic (RQ), and
thin-plate spline (TPS) kernels. Each kernel family includes a full_covariance_*
function that builds the gradient-enhanced covariance block
[[k, dk/dx2], [dk/dx1, d2k/dx1dx2]]. Matern 5/2 uses a
cancellation-free closed form. The other families use JAX automatic
differentiation.
Added in version 1.0.0.
Attributes¶
Functions¶
Compute the thin-plate spline kernel matrix. |
|
|
Compute the Matern 5/2 kernel matrix. |
|
Evaluate the Matern 5/2 kernel for a single pair of points. |
|
Cancellation-free Matern 5/2 value and |
|
Value and |
|
Build the gradient-enhanced covariance block for the Matern 5/2 kernel. |
|
Compute the inverse multiquadric (IMQ) kernel matrix. |
|
Evaluate the IMQ kernel for a single pair of points. |
|
Build the gradient-enhanced covariance block for the IMQ kernel. |
|
Evaluate the squared exponential (SE) kernel for a single pair of points. |
|
Build the gradient-enhanced covariance block for the SE kernel. |
|
Evaluate the rational quadratic (RQ) base kernel. |
|
Evaluate the RQ kernel with mirror symmetry for a single pair. |
|
Build the gradient-enhanced covariance block for the RQ kernel. |
|
Arrange |
|
Return the gradient-enhanced covariance of |
Module Contents¶
- rgpycrumbs.surfaces._kernels._tps_kernel_matrix(x)[source]¶
Compute the thin-plate spline kernel matrix.
Evaluates
K_{ij} = r_{ij}^2 * log(r_{ij})wherer_{ij}is the Euclidean distance between pointsx_iandx_j.- Parameters:
x – Input points, shape
(N, D).- Returns:
Kernel matrix, shape
(N, N).
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels._matern_kernel_matrix(x, length_scale)[source]¶
Compute the Matern 5/2 kernel matrix.
Evaluates k(r) = (1 + sqrt(5)*r/l + 5*r^2/(3*l^2)) * exp(-sqrt(5)*r/l).
- Parameters:
x – Input points, shape
(N, D).length_scale – Kernel length scale parameter.
- Returns:
Kernel matrix, shape
(N, N).
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels.matern_kernel_elem(x1, x2, length_scale=1.0)[source]¶
Evaluate the Matern 5/2 kernel for a single pair of points.
- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).length_scale – Kernel length scale parameter.
- Returns:
Scalar kernel value.
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels._matern_radial(x1, x2, length_scale)[source]¶
Cancellation-free Matern 5/2 value and
dk/dx2.With
diff = x1 - x2andu = sqrt(5) ||diff|| / l,k = (1 + u + u**2 / 3) exp(-u)anddk/dx2 = (5 / (3 l**2)) exp(-u) (1 + u) diff,which is the identity
-dk/dr / r = (5 / (3 l**2)) (1 + u) exp(-u). At a repeated point this is(1, 0)with no0/0quotient. Automatic differentiation ofmatern_kernel_elem()subtracts twoO(1/l)terms there and is wrong by several percent in float32.
- rgpycrumbs.surfaces._kernels._matern_query_row(x1, x2, length_scale)[source]¶
Value and
dk/dx2for a query, finite underjax.gradatr = 0.The train-train block keeps
_matern_radial(). A baresqrtof the squared distance has an infinite derivative at a repeated point, so differentiating a prediction or a variance with respect to the query is NaN exactly at a training point. The doublewhereis the identity forr > 0and returns0atr = 0, so the forward values match the closed form.
- rgpycrumbs.surfaces._kernels.full_covariance_matern(x1, x2, length_scale)[source]¶
Build the gradient-enhanced covariance block for the Matern 5/2 kernel.
Constructs a
(D+1, D+1)matrix containing the energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries from the closed form in_matern_radial(). The Hessian block isd**2 k / (dx1 dx2) = c ((1 + u) I - (5 / l**2) outer(diff, diff))with
c = (5 / (3 l**2)) exp(-u), so a repeated point has diagonal5 / (3 l**2)and the matrix stays positive definite in float32.- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).length_scale – Kernel length scale parameter.
- Returns:
Covariance block, shape
(D+1, D+1).
Added in version 1.1.0.
- rgpycrumbs.surfaces._kernels._imq_kernel_matrix(x, epsilon)[source]¶
Compute the inverse multiquadric (IMQ) kernel matrix.
Evaluates k(r) = 1 / sqrt(r^2 + epsilon^2).
- Parameters:
x – Input points, shape
(N, D).epsilon – Shape parameter controlling kernel width.
- Returns:
Kernel matrix, shape
(N, N).
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels.imq_kernel_elem(x1, x2, epsilon=1.0)[source]¶
Evaluate the IMQ kernel for a single pair of points.
- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).epsilon – Shape parameter controlling kernel width.
- Returns:
Scalar kernel value.
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels.full_covariance_imq(x1, x2, epsilon)[source]¶
Build the gradient-enhanced covariance block for the IMQ kernel.
Constructs a
(D+1, D+1)matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries.- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).epsilon – Shape parameter controlling kernel width.
- Returns:
Covariance block, shape
(D+1, D+1).
Added in version 1.1.0.
- rgpycrumbs.surfaces._kernels.se_kernel_elem(x1, x2, length_scale=1.0)[source]¶
Evaluate the squared exponential (SE) kernel for a single pair of points.
Computes k(r) = exp(-r^2 / (2 * l^2)).
- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).length_scale – Kernel length scale parameter.
- Returns:
Scalar kernel value.
Added in version 1.0.0.
- rgpycrumbs.surfaces._kernels.full_covariance_se(x1, x2, length_scale)[source]¶
Build the gradient-enhanced covariance block for the SE kernel.
Constructs a
(D+1, D+1)matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries.- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).length_scale – Kernel length scale parameter.
- Returns:
Covariance block, shape
(D+1, D+1).
Added in version 1.1.0.
- rgpycrumbs.surfaces._kernels.rq_kernel_base(x1, x2, length_scale, alpha)[source]¶
Evaluate the rational quadratic (RQ) base kernel.
Computes k(r) = (1 + r^2 / (2 * alpha * l^2))^(-alpha).
- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).length_scale – Kernel length scale parameter.
alpha – Shape parameter controlling the mixture of length scales.
- Returns:
Scalar kernel value.
Added in version 1.1.0.
- rgpycrumbs.surfaces._kernels.rq_kernel_elem(x1, x2, params)[source]¶
Evaluate the RQ kernel with mirror symmetry for a single pair.
Computes k(x1, x2) + k(flip(x1), x2) to enforce symmetry under coordinate reversal.
- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).params – Array of
[length_scale, alpha].
- Returns:
Scalar kernel value (sum of direct and mirrored terms).
Added in version 1.1.0.
- rgpycrumbs.surfaces._kernels.full_covariance_rq(x1, x2, params)[source]¶
Build the gradient-enhanced covariance block for the RQ kernel.
Constructs a
(D+1, D+1)matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries. Row1 + iand column1 + jhold the derivative inx1[i]andx2[j].- Parameters:
x1 – First point, shape
(D,).x2 – Second point, shape
(D,).params – Array of
[length_scale, alpha].
- Returns:
Covariance block, shape
(D+1, D+1).
Added in version 1.1.0.
Changed in version 1.14.0: The gradient-gradient block is no longer transposed.
- rgpycrumbs.surfaces._kernels.gradient_covariance(K_blocks)[source]¶
Arrange
(N, M, D+1, D+1)pair blocks as one(N(D+1), M(D+1))matrix.- Parameters:
K_blocks – Pair blocks from a
k_matrix_*_grad_mapfunction.- Returns:
Covariance matrix ordered by point, then by value and gradient component.
Added in version 1.14.0.
- rgpycrumbs.surfaces._kernels.gradient_kernel_matrix(k_matrix_grad_map, x, params)[source]¶
Return the gradient-enhanced covariance of
xwith itself.Compiles once per kernel map and input shape, as the model solves do.
- Parameters:
k_matrix_grad_map – A
k_matrix_*_grad_mapfunction.x – Inputs (N, D).
params – Kernel parameters of that map.
- Returns:
The
(N(D+1), N(D+1))matrix fromgradient_covariance.
Added in version 1.14.0.