Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/celerite2/jax/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion python/celerite2/pymc/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions python/test/jax/test_jax_distribution.py
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 20 additions & 0 deletions python/test/pymc/test_pymc_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)