Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing nested sampling #1871

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ jobs:
- name: Test custom prng
run: |
JAX_ENABLE_CUSTOM_PRNG=1 pytest -vs test/infer/test_mcmc.py
- name: Test nested sampling
run: |
JAX_ENABLE_X64=1 pytest -vs test/contrib/test_nested_sampling.py
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved


examples:
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ funsor
ipython
jax
jaxlib
jaxns==2.4.8
jaxns==2.6.2
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved
Jinja2
matplotlib
multipledispatch
Expand Down
5 changes: 5 additions & 0 deletions examples/gaussian_shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
https://github.com/joshspeagle/dynesty/blob/master/demos/Examples%20--%20Gaussian%20Shells.ipynb
"""

# ruff: noqa: E402
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved

import argparse

import matplotlib.pyplot as plt
Expand All @@ -30,6 +32,9 @@
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS, DiscreteHMCGibbs

numpyro.enable_x64()
numpyro.set_host_device_count(2)
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved


class GaussianShell(dist.Distribution):
support = dist.constraints.real_vector
Expand Down
9 changes: 6 additions & 3 deletions numpyro/contrib/nested_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

from functools import singledispatch

import jax
from jax import random
import jax.numpy as jnp

try:
import jaxns # noqa: F401
from jaxns import (
DefaultNestedSampler,
Model,
Prior,
TerminationCondition,
Expand All @@ -17,11 +18,13 @@
resample,
summary,
)
from jaxns.public import DefaultNestedSampler
from jaxns.utils import NestedSamplerResults

except ImportError as e:
raise ImportError(
"To use this module, please install `jaxns` package. It can be"
f"{e} \n "
f"To use this module, please install `jaxns>2.5` package. It can be"
" installed with `pip install jaxns` with python>=3.8"
) from e

Expand Down Expand Up @@ -258,7 +261,7 @@ def prior_model():

default_constructor_kwargs = dict(
num_live_points=model.U_ndims * 25,
num_parallel_workers=1,
devices=jax.devices(),
max_samples=1e4,
)
default_termination_kwargs = dict(dlogZ=1e-4)
Expand Down
2 changes: 1 addition & 1 deletion numpyro/distributions/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ class LowerCholeskyAffine(Transform):
>>> scale_tril = jnp.array([[0.3, 0.0], [1.0, 0.5]])
>>> affine = LowerCholeskyAffine(loc=loc, scale_tril=scale_tril)
>>> affine(base)
Array([0.3, 1.5], dtype=float32)
Array([0.3, 1.5], dtype=float64)
"""

domain = constraints.real_vector
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"flax",
"funsor>=0.4.1",
"graphviz",
"jaxns==2.4.8",
"jaxns==2.6.2",
"matplotlib",
"optax>=0.0.6",
"pylab-sdk", # jaxns dependency
Expand Down
12 changes: 9 additions & 3 deletions test/contrib/test_nested_sampling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0

import os

import numpy as np
from numpy.testing import assert_allclose
import pytest
Expand All @@ -17,9 +19,13 @@
import numpyro.distributions as dist
from numpyro.distributions.transforms import AffineTransform, ExpTransform

pytestmark = pytest.mark.filterwarnings(
"ignore:jax.tree_.+ is deprecated:FutureWarning"
)
pytestmark = [
pytest.mark.filterwarnings("ignore:jax.tree_.+ is deprecated:FutureWarning"),
renecotyfanboy marked this conversation as resolved.
Show resolved Hide resolved
pytest.mark.skipif(
not os.environ.get("JAX_ENABLE_X64"),
reason="test suite for jaxns requires double precision",
),
]


# Test helper to extract a few central moments from samples.
Expand Down
Loading