From 5c9c2ddc21d600b4c38fec60f8da35c718bc313d Mon Sep 17 00:00:00 2001 From: Zhe Wang <53119724+flyingdoubleG@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:27:47 +0800 Subject: [PATCH] Pass full inputs through quasisep Scale Scale inherits Wrapper's observation_model and transition_matrix, which reduce the input with coord_to_sortable before calling the wrapped kernel. That suits wrappers that add structure, like Multiband in the custom kernel tutorial, but Scale doesn't change the coordinates. Scaling a kernel on structured inputs, e.g. 2.0 * Multiband(...), handed it only the time and failed with an IndexError. Pass the inputs through unchanged. --- src/tinygp/kernels/quasisep.py | 9 +++++++++ tests/test_kernels/test_quasisep.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/tinygp/kernels/quasisep.py b/src/tinygp/kernels/quasisep.py index f28ca81b..5a9a2e38 100644 --- a/src/tinygp/kernels/quasisep.py +++ b/src/tinygp/kernels/quasisep.py @@ -336,6 +336,15 @@ class Scale(Wrapper): scale: JAXArray | float + # Scaling doesn't change the coordinates, so pass them through unchanged + # instead of reducing them with ``coord_to_sortable`` as ``Wrapper`` does. + # The wrapped kernel may need the full input, e.g. a band index. + def observation_model(self, X: JAXArray) -> JAXArray: + return self.kernel.observation_model(X) + + def transition_matrix(self, X1: JAXArray, X2: JAXArray) -> JAXArray: + return self.kernel.transition_matrix(X1, X2) + def stationary_covariance(self) -> JAXArray: return self.scale * self.kernel.stationary_covariance() diff --git a/tests/test_kernels/test_quasisep.py b/tests/test_kernels/test_quasisep.py index c585f989..ac1e1b2f 100644 --- a/tests/test_kernels/test_quasisep.py +++ b/tests/test_kernels/test_quasisep.py @@ -7,6 +7,7 @@ from tinygp import GaussianProcess from tinygp.kernels import quasisep from tinygp.noise import Banded +from tinygp.solvers import DirectSolver from tinygp.test_utils import assert_allclose @@ -196,3 +197,32 @@ def test_sum_kernel_use_block_false(data): gp_block = GaussianProcess(k_block, x, diag=0.1 * jnp.ones(N)) gp_dense = GaussianProcess(k_dense, x, diag=0.1 * jnp.ones(N)) assert_allclose(gp_block.log_probability(y), gp_dense.log_probability(y)) + + +class _Multiband(quasisep.Wrapper): + """A kernel on (time, band) inputs, as in the custom quasisep tutorial.""" + + amplitudes: jnp.ndarray + + def coord_to_sortable(self, X): + return X[0] + + def observation_model(self, X): + return self.amplitudes[X[1]] * self.kernel.observation_model(X[0]) + + +def test_scale_of_kernel_with_structured_inputs(random): + t = jnp.sort(random.uniform(0, 10, 12)) + band = jnp.asarray(random.integers(0, 3, len(t))) + X = (t, band) + kernel = _Multiband( + kernel=quasisep.Matern52(scale=1.5), amplitudes=jnp.array([1.0, 0.5, 2.0]) + ) + K = kernel.to_symm_qsm(X).to_dense() + for scaled in (2.0 * kernel, kernel * 2.0): + assert_allclose(scaled.to_symm_qsm(X).to_dense(), 2.0 * K) + assert_allclose(scaled(X, X), 2.0 * K) + gp = GaussianProcess(kernel + 2.0 * kernel, X, diag=0.1) + gp_dense = GaussianProcess(kernel + 2.0 * kernel, X, diag=0.1, solver=DirectSolver) + y = random.normal(size=len(t)) + assert_allclose(gp.log_probability(y), gp_dense.log_probability(y))