From 0f79508992b0df8988beb590406f75c201e6272f Mon Sep 17 00:00:00 2001 From: Ricardo Pinho Date: Wed, 18 Nov 2026 10:12:06 -0500 Subject: [PATCH] Include GP means in PyMC and NumPyro samples Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N --- python/celerite2/jax/distribution.py | 2 +- python/celerite2/pymc/distribution.py | 2 +- python/test/jax/test_jax_distribution.py | 26 ++++++++++++++++++++++ python/test/pymc/test_pymc_distribution.py | 20 +++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 python/test/jax/test_jax_distribution.py diff --git a/python/celerite2/jax/distribution.py b/python/celerite2/jax/distribution.py index 54b7bc25..ea8de8e4 100644 --- a/python/celerite2/jax/distribution.py +++ b/python/celerite2/jax/distribution.py @@ -24,4 +24,4 @@ def log_prob(self, value): def sample(self, key, sample_shape=()): eps = random.normal(key, shape=self.event_shape + sample_shape) - return jnp.moveaxis(self.gp.dot_tril(eps), 0, -1) + return jnp.moveaxis(self.gp.dot_tril(eps), 0, -1) + self.gp._mean_value diff --git a/python/celerite2/pymc/distribution.py b/python/celerite2/pymc/distribution.py index 629925ba..5f6784f2 100644 --- a/python/celerite2/pymc/distribution.py +++ b/python/celerite2/pymc/distribution.py @@ -26,7 +26,7 @@ def safe_celerite_normal(rng, mean, norm, t, c, U, W, d, size=None): out_shape = tuple(size) + (mean.shape[0],) n = rng.standard_normal(size=shape) * np.sqrt(d)[:, None] result = driver.matmul_lower(t, c, U, W, n, n) - return np.reshape(np.transpose(result), out_shape) + return np.reshape(np.transpose(result), out_shape) + mean class CeleriteNormalRV(RandomVariable): diff --git a/python/test/jax/test_jax_distribution.py b/python/test/jax/test_jax_distribution.py new file mode 100644 index 00000000..9d0bab33 --- /dev/null +++ b/python/test/jax/test_jax_distribution.py @@ -0,0 +1,26 @@ +import numpy as np +import pytest + +pytest.importorskip("celerite2.jax") +pytest.importorskip("numpyro") + +from jax import jit, random + +from celerite2.jax import GaussianProcess, terms + + +@pytest.mark.parametrize("mean", [0.0, 10.5, lambda t: 0.2 * (t - 5)]) +@pytest.mark.parametrize("sample_shape", [(), (4,)]) +def test_samples_include_mean(mean, sample_shape): + t = np.linspace(0.0, 10.0, 8) + kernel = terms.SHOTerm(sigma=1.0, rho=2.0, Q=1.5) + gp = GaussianProcess(kernel, t=t, diag=0.1, mean=mean) + zero_gp = GaussianProcess(kernel, t=t, diag=0.1) + key = random.PRNGKey(40582) + + sample = jit(lambda k: gp.numpyro_dist().sample(k, sample_shape))(key) + zero_sample = zero_gp.numpyro_dist().sample(key, sample_shape) + expected_mean = mean(t) if callable(mean) else mean + np.testing.assert_allclose( + sample, zero_sample + expected_mean, rtol=1e-12, atol=1e-12 + ) diff --git a/python/test/pymc/test_pymc_distribution.py b/python/test/pymc/test_pymc_distribution.py index 24a70f3a..e1f56bf7 100644 --- a/python/test/pymc/test_pymc_distribution.py +++ b/python/test/pymc/test_pymc_distribution.py @@ -42,3 +42,23 @@ def test_celerite_normal_support_point(t, mean, size, expected): # NOTE: Name must be "x" for assert function to work gp.marginal("x", size=size) assert_support_point_is_expected(model, expected) + + +@pytest.mark.parametrize("mean", [0.0, 10.5, lambda t: 0.2 * (t - 5)]) +def test_samples_include_mean(mean): + pm = pytest.importorskip("pymc") + t = np.linspace(0.0, 10.0, 8) + kernel = terms.SHOTerm(sigma=1.0, rho=2.0, Q=1.5) + + with pm.Model(): + gp = GaussianProcess(kernel, t=t, diag=0.1, mean=mean) + sample = pm.draw(gp.marginal("obs"), random_seed=40582) + + with pm.Model(): + zero_gp = GaussianProcess(kernel, t=t, diag=0.1) + zero_sample = pm.draw(zero_gp.marginal("obs"), random_seed=40582) + + expected_mean = mean(t) if callable(mean) else mean + np.testing.assert_allclose( + sample, zero_sample + expected_mean, rtol=1e-12, atol=1e-12 + )