| """Correctness checks for the analytic kernels.""" |
|
|
| import jax |
| import jax.numpy as jnp |
| import numpy as np |
| import pytest |
|
|
| jax.config.update("jax_enable_x64", True) |
|
|
| from ballast.kernels import ( |
| HelmParams, |
| k_ext_full, |
| k_helm_mat, |
| k_thelm_mat, |
| matern32_blocks, |
| ) |
|
|
| P = HelmParams( |
| phi_ls=0.8, phi_var=0.5, psi_ls=0.5, psi_var=0.5, time_ls=2.5, time_var=1.0 |
| ) |
|
|
|
|
| def _m32(t, t2, ls, var): |
| """Reference Matern-3/2 written so autodiff is valid (no clipped distance).""" |
| r = jnp.abs(t - t2) |
| lam = jnp.sqrt(3.0) / ls |
| return var * (1 + lam * r) * jnp.exp(-lam * r) |
|
|
|
|
| def test_matern32_derivatives_match_autodiff(): |
| """Analytic d_t, d_t', d^2_tt' blocks agree with autodiff away from t=t'.""" |
| ts = jnp.array([0.0, 0.3, 1.7, 4.2]) |
| ts2 = jnp.array([0.5, 2.1, 3.3]) |
| M = matern32_blocks(ts, ts2, P.time_ls, P.time_var) |
|
|
| k = jax.vmap(lambda a: jax.vmap(lambda b: _m32(a, b, P.time_ls, P.time_var))(ts2))(ts) |
| dt = jax.grad(_m32, argnums=0) |
| dt2 = jax.grad(_m32, argnums=1) |
| d2 = jax.grad(jax.grad(_m32, argnums=0), argnums=1) |
|
|
| f = lambda g: jax.vmap(lambda a: jax.vmap(lambda b: g(a, b, P.time_ls, P.time_var))(ts2))(ts) |
| np.testing.assert_allclose(M[..., 0, 0], k, rtol=1e-10) |
| np.testing.assert_allclose(M[..., 1, 0], f(dt), rtol=1e-8) |
| np.testing.assert_allclose(M[..., 0, 1], f(dt2), rtol=1e-8) |
| np.testing.assert_allclose(M[..., 1, 1], f(d2), rtol=1e-8) |
|
|
|
|
| def test_matern32_second_derivative_at_zero(): |
| """Paper Sec. H.2: d^2_{tt'} k at t=t' must be 3 sigma^2/l^2, not 0. |
| |
| This is the value GPJax-style clipped-distance autodiff silently returns as |
| zero; it is also exactly P_inf[1,1] in the SPDE formulation, i.e. Var(d_t f). |
| """ |
| t = jnp.array([1.0]) |
| M = matern32_blocks(t, t, 1.0, 1.0) |
| assert np.isclose(M[0, 0, 1, 1], 3.0) |
| M2 = matern32_blocks(t, t, P.time_ls, P.time_var) |
| assert np.isclose(M2[0, 0, 1, 1], 3.0 * P.time_var / P.time_ls**2) |
|
|
|
|
| def test_matern32_finite_difference(): |
| """Independent finite-difference check of the mixed second derivative.""" |
| |
| |
| |
| h = 1e-3 |
| a, b = 1.0, 2.4 |
| num = ( |
| _m32(a + h, b + h, P.time_ls, P.time_var) |
| - _m32(a + h, b - h, P.time_ls, P.time_var) |
| - _m32(a - h, b + h, P.time_ls, P.time_var) |
| + _m32(a - h, b - h, P.time_ls, P.time_var) |
| ) / (4 * h * h) |
| ana = matern32_blocks(jnp.array([a]), jnp.array([b]), P.time_ls, P.time_var)[0, 0, 1, 1] |
| assert np.isclose(num, ana, rtol=1e-4) |
|
|
|
|
| def _helm_field_cov_reference(S, S2, p): |
| """Helmholtz kernel rebuilt by autodiff of the potential/stream construction. |
| |
| F_1 = d_1 Phi + d_2 Psi, F_2 = d_2 Phi - d_1 Psi, so |
| Cov(F_a(x), F_b(x')) is assembled from mixed second derivatives of k_Phi, k_Psi. |
| """ |
| kphi = lambda x, y: p.phi_var * jnp.exp(-0.5 * jnp.sum((x - y) ** 2) / p.phi_ls**2) |
| kpsi = lambda x, y: p.psi_var * jnp.exp(-0.5 * jnp.sum((x - y) ** 2) / p.psi_ls**2) |
| Hphi = jax.jacfwd(jax.jacrev(kphi, argnums=0), argnums=1) |
| Hpsi = jax.jacfwd(jax.jacrev(kpsi, argnums=0), argnums=1) |
|
|
| def cov(x, y): |
| A, B = Hphi(x, y), Hpsi(x, y) |
| return jnp.array( |
| [ |
| [A[0, 0] + B[1, 1], A[0, 1] - B[1, 0]], |
| [A[1, 0] - B[0, 1], A[1, 1] + B[0, 0]], |
| ] |
| ) |
|
|
| return jax.vmap(lambda x: jax.vmap(lambda y: cov(x, y))(S2))(S) |
|
|
|
|
| def test_helmholtz_matches_autodiff_construction(): |
| S = jnp.array([[0.0, 0.0], [0.4, -0.7], [1.2, 0.3]]) |
| S2 = jnp.array([[0.1, 0.2], [-0.5, 0.9]]) |
| ref = _helm_field_cov_reference(S, S2, P) |
| got = k_helm_mat(S, S2, P).reshape(3, 2, 2, 2).transpose(0, 2, 1, 3) |
| |
| np.testing.assert_allclose(got.transpose(0, 1, 2, 3), ref, rtol=1e-8, atol=1e-12) |
|
|
|
|
| def test_gram_matrices_are_psd(): |
| key = jax.random.PRNGKey(0) |
| S = jax.random.uniform(key, (12, 2), minval=-2, maxval=2) |
| t = jnp.linspace(0, 5, 12) |
| for K in (k_thelm_mat(S, t, S, t, P), k_ext_full(S, t, P), k_helm_mat(S, S, P)): |
| w = jnp.linalg.eigvalsh(0.5 * (K + K.T)) |
| assert w.min() > -1e-8, f"min eigenvalue {w.min()}" |
|
|
|
|
| def test_extended_kernel_blocks_are_consistent(): |
| """The f-block of the extended kernel is exactly the plain kernel.""" |
| key = jax.random.PRNGKey(1) |
| S = jax.random.uniform(key, (5, 2), minval=-1, maxval=1) |
| t = jnp.linspace(0, 3, 5) |
| E = k_ext_full(S, t, P).reshape(5, 2, 2, 5, 2, 2) |
| plain = k_thelm_mat(S, t, S, t, P).reshape(5, 2, 5, 2) |
| np.testing.assert_allclose(E[:, :, 0, :, :, 0], plain, rtol=1e-10) |
|
|