| """Kernels for the temporal Helmholtz GP of BALLAST (arXiv 2509.26005). |
| |
| The surrogate is a separable, vector-output, spatio-temporal GP |
| |
| k_tHelm((s,t),(s',t')) = k_Helm(s,s') * k_time(t,t') |
| |
| with k_Helm the Helmholtz kernel of Berlinghieri et al. (2023) (paper Sec. B.2) |
| built from two independent RBF kernels (potential Phi, stream Psi), and k_time a |
| Matern-3/2 kernel (paper Sec. 2.2). |
| |
| Section 4.1 of the paper additionally needs the *extended* GP f = [f, d_t f]^T, |
| whose kernel is the 2x2 block matrix of temporal derivatives of k_tHelm. Because |
| k_tHelm is separable, all t-derivatives act on the Matern-3/2 factor only. |
| |
| Everything is implemented **analytically**. The paper (Sec. H.2) warns that |
| autodiff through a Matern kernel written with a clipped distance |
| (`sqrt(max(sum((x-y)**2), 1e-36))`, as in GPJax) gives d^2_{tt'}k = 0 at t=t' |
| instead of the correct 3*sigma^2/l^2; the analytic form has no such problem. |
| `tests/test_kernels.py` checks these derivatives against finite differences. |
| |
| Index layout |
| ------------ |
| Spatial-velocity blocks are flattened point-major / component-minor: |
| row index of a velocity vector at point i, component c -> i*2 + c |
| The extended state adds the [f, d_t f] axis last: |
| (i, c, a) -> i*4 + c*2 + a with a=0 -> f, a=1 -> d_t f |
| """ |
|
|
| from __future__ import annotations |
|
|
| from typing import NamedTuple |
|
|
| import jax |
| import jax.numpy as jnp |
|
|
| SQRT3 = jnp.sqrt(3.0) |
|
|
|
|
| class HelmParams(NamedTuple): |
| """Hyperparameters of the temporal Helmholtz GP.""" |
|
|
| phi_ls: jnp.ndarray |
| phi_var: jnp.ndarray |
| psi_ls: jnp.ndarray |
| psi_var: jnp.ndarray |
| time_ls: jnp.ndarray |
| time_var: jnp.ndarray |
|
|
| def as_array(self) -> jnp.ndarray: |
| return jnp.stack( |
| [ |
| jnp.asarray(self.phi_ls), |
| jnp.asarray(self.phi_var), |
| jnp.asarray(self.psi_ls), |
| jnp.asarray(self.psi_var), |
| jnp.asarray(self.time_ls), |
| jnp.asarray(self.time_var), |
| ] |
| ) |
|
|
| @staticmethod |
| def from_array(a: jnp.ndarray) -> "HelmParams": |
| return HelmParams(a[0], a[1], a[2], a[3], a[4], a[5]) |
|
|
|
|
| |
| |
| |
|
|
|
|
| def _rbf_hess(S: jnp.ndarray, S2: jnp.ndarray, ls, var) -> jnp.ndarray: |
| """Mixed second derivatives of an RBF kernel. |
| |
| Returns H with H[i, j, a, b] = d^2 / (d x_a d x'_b) k(S_i, S2_j), which for |
| k = var * exp(-|d|^2 / (2 l^2)), d = x - x', equals |
| |
| k * (delta_ab / l^2 - d_a d_b / l^4). |
| """ |
| d = S[:, None, :] - S2[None, :, :] |
| sq = jnp.sum(d**2, axis=-1) |
| k = var * jnp.exp(-0.5 * sq / ls**2) |
| eye = jnp.eye(2) |
| outer = d[..., :, None] * d[..., None, :] |
| return k[..., None, None] * (eye / ls**2 - outer / ls**4) |
|
|
|
|
| def k_helm(S: jnp.ndarray, S2: jnp.ndarray, p: HelmParams) -> jnp.ndarray: |
| """Helmholtz kernel (paper Sec. B.2), returned as (N, M, 2, 2). |
| |
| F = grad(Phi) + rot(Psi) with rot(Psi) = (d_2 Psi, -d_1 Psi), so |
| |
| K[0,0] = d^2_{x1 x1'} k_Phi + d^2_{x2 x2'} k_Psi |
| K[0,1] = d^2_{x1 x2'} k_Phi - d^2_{x2 x1'} k_Psi |
| K[1,0] = d^2_{x2 x1'} k_Phi - d^2_{x1 x2'} k_Psi |
| K[1,1] = d^2_{x2 x2'} k_Phi + d^2_{x1 x1'} k_Psi |
| """ |
| A = _rbf_hess(S, S2, p.phi_ls, p.phi_var) |
| B = _rbf_hess(S, S2, p.psi_ls, p.psi_var) |
| k00 = A[..., 0, 0] + B[..., 1, 1] |
| k01 = A[..., 0, 1] - B[..., 1, 0] |
| k10 = A[..., 1, 0] - B[..., 0, 1] |
| k11 = A[..., 1, 1] + B[..., 0, 0] |
| return jnp.stack( |
| [jnp.stack([k00, k01], -1), jnp.stack([k10, k11], -1)], axis=-2 |
| ) |
|
|
|
|
| def k_helm_mat(S: jnp.ndarray, S2: jnp.ndarray, p: HelmParams) -> jnp.ndarray: |
| """Helmholtz Gram matrix flattened to (2N, 2M), point-major/component-minor.""" |
| K = k_helm(S, S2, p) |
| N, M = K.shape[0], K.shape[1] |
| return jnp.transpose(K, (0, 2, 1, 3)).reshape(2 * N, 2 * M) |
|
|
|
|
| |
| |
| |
|
|
|
|
| def matern32_blocks(t: jnp.ndarray, t2: jnp.ndarray, ls, var) -> jnp.ndarray: |
| """Matern-3/2 kernel and its t/t' derivatives, as (N, M, 2, 2). |
| |
| With lam = sqrt(3)/l, tau = t - t': |
| |
| M[0,0] = k = var (1 + lam|tau|) exp(-lam|tau|) |
| M[0,1] = d_{t'} k = var lam^2 tau exp(-lam|tau|) |
| M[1,0] = d_{t} k = -var lam^2 tau exp(-lam|tau|) |
| M[1,1] = d^2_{t t'} k = var lam^2 (1 - lam|tau|) exp(-lam|tau|) |
| |
| Note M[1,1] at tau=0 is var*lam^2 = 3 var / l^2 (= 3 for var=l=1), the value |
| the paper's Sec. H.2 flags as being silently zeroed by clipped-distance |
| autodiff implementations. It also equals P_inf[1,1] in the SPDE formulation |
| (spde.py), i.e. Var(d_t f) -- an internal consistency check of the two views. |
| """ |
| lam = SQRT3 / ls |
| tau = t[:, None] - t2[None, :] |
| a = jnp.abs(tau) |
| e = jnp.exp(-lam * a) |
| k = var * (1.0 + lam * a) * e |
| dk = var * lam**2 * tau * e |
| d2k = var * lam**2 * (1.0 - lam * a) * e |
| return jnp.stack( |
| [jnp.stack([k, dk], -1), jnp.stack([-dk, d2k], -1)], axis=-2 |
| ) |
|
|
|
|
| |
| |
| |
|
|
|
|
| def k_thelm_mat( |
| S: jnp.ndarray, t: jnp.ndarray, S2: jnp.ndarray, t2: jnp.ndarray, p: HelmParams |
| ) -> jnp.ndarray: |
| """Plain k_tHelm Gram matrix between (S,t) and (S2,t2). Shape (2N, 2M).""" |
| KS = k_helm(S, S2, p) |
| kt = matern32_blocks(t, t2, p.time_ls, p.time_var)[..., 0, 0] |
| K = KS * kt[..., None, None] |
| N, M = K.shape[0], K.shape[1] |
| return jnp.transpose(K, (0, 2, 1, 3)).reshape(2 * N, 2 * M) |
|
|
|
|
| def k_ext_cross( |
| S: jnp.ndarray, t: jnp.ndarray, S2: jnp.ndarray, t2: jnp.ndarray, p: HelmParams |
| ) -> jnp.ndarray: |
| """Cov between plain observations at (S,t) and the *extended* state at (S2,t2). |
| |
| Returns (2N, 4M): rows index (obs point, velocity component), columns index |
| (test point, velocity component, [f, d_t f]). |
| """ |
| KS = k_helm(S, S2, p) |
| Mt = matern32_blocks(t, t2, p.time_ls, p.time_var) |
| |
| K = KS[..., :, :, None] * Mt[:, :, None, None, 0, :] |
| N, M = K.shape[0], K.shape[1] |
| |
| return jnp.transpose(K, (0, 2, 1, 3, 4)).reshape(2 * N, 4 * M) |
|
|
|
|
| def k_ext_full(S: jnp.ndarray, t: jnp.ndarray, p: HelmParams) -> jnp.ndarray: |
| """Covariance of the extended state f = [f, d_t f]^T at (S,t). Shape (4N, 4N).""" |
| KS = k_helm(S, S, p) |
| Mt = matern32_blocks(t, t, p.time_ls, p.time_var) |
| K = KS[..., :, :, None, None] * Mt[:, :, None, None, :, :] |
| N = K.shape[0] |
| |
| return jnp.transpose(K, (0, 2, 4, 1, 3, 5)).reshape(4 * N, 4 * N) |
|
|