This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Jupyter Book repository containing educational notebooks demonstrating nested sampling algorithms from the BlackJAX library. The book focuses on physics-motivated use cases and provides pedagogical examples of nested sampling implementations.
The nested sampling code now lives in main BlackJAX (https://github.com/blackjax-devs/blackjax), in the blackjax.ns subpackage, with top-level entry points blackjax.nss (Nested Slice Sampling) and blackjax.nsswig (slice-within-Gibbs). It is not yet in a tagged PyPI release, so it must be installed from the main branch with git (see Installation). The older handley-lab/blackjax fork (v0.1.0-beta) is deprecated — do not reference it in new content.
BlackJAX maintains a complementary Sampling Book whose Nested Sampling chapter is the upstream companion to this book; keep the API usage here consistent with it.
# Install dependencies
pip install -r requirements.txt
# Build the book locally
jupyter-book build .
# or shorthand
jb build .
# The built HTML will be in _build/html/# Install nested sampling from main BlackJAX (not yet on PyPI, so git is required)
pip install git+https://github.com/blackjax-devs/blackjax.git
# Install visualization dependencies (for examples)
pip install anesthetic-
Create a notebook in the appropriate directory:
basic/- Simple introductory examplesadvanced/- More involved implementations (posterior repartitioning, Random Walk NS from primitives)physics/- Physics-specific applications (supernovae, cosmology)scripts/- Python scripts for standalone examples
-
Add your notebook to
_toc.ymlunder the appropriate section -
Add yourself to
contributors.md -
Include citations in
references.biband use{cite}citation_key`` in markdown -
Test the build locally with
jb build .
-
Notebooks: Interactive examples demonstrating nested sampling usage
- Examples are pre-executed (notebooks run statically due to
execute_notebooks: 'off') - Visual state is preserved in notebooks for display in the book
- Examples are pre-executed (notebooks run statically due to
-
Scripts: Standalone Python implementations for specific physics problems
supernovae.py- SALT model fitting for supernovaeCMB.py,GW.py- Cosmological and gravitational wave examplesBF.py- Bayes factor computation
-
Configuration:
_config.yml- Jupyter Book configuration_toc.yml- Table of contents structure- GitHub Actions workflow in
.github/workflows/build_deploy.yamlfor automatic deployment
Standard workflow for nested sampling:
import blackjax
from blackjax.ns.utils import finalise
# Define likelihood and prior
loglikelihood_fn = lambda x: ...
logprior_fn = lambda x: ...
# Initialize algorithm
algo = blackjax.nss(
logprior_fn=logprior_fn,
loglikelihood_fn=loglikelihood_fn,
num_delete=50,
num_inner_steps=20,
)
# Run sampling loop
state = algo.init(initial_particles)
dead_points = []
while not state.integrator.logZ_live - state.integrator.logZ < -3:
rng_key, subkey = jax.random.split(rng_key)
state, info = algo.step(subkey, state) # step returns (new_state, NSInfo)
dead_points.append(info)
# Finalize results: returns an NSInfo whose `.particles` is a StateWithLogLikelihood
ns_run = finalise(state, dead_points)Key API notes for main BlackJAX (these changed from the old fork):
- Running evidence lives on
state.integrator: usestate.integrator.logZandstate.integrator.logZ_live(notstate.logZ/state.logZ_live). The standard stopping rule islogZ_live - logZ < -3. finalise(...)returns anNSInfo; the particles are under.particles, so usens_run.particles.position,ns_run.particles.loglikelihood,ns_run.particles.loglikelihood_birth.blackjax.ns.utils.sample(...)returns a particle state — take.positionfor the resampled positions.log_weights/ess/uniform_prioralso live inblackjax.ns.utils.- Custom inner kernels: use
blackjax.ns.from_mcmc.reject_constrained_step+blackjax.ns.from_mcmc.build_kernel(the oldrepeat_kernel/new_state_and_infohelpers no longer exist).
Convert results to anesthetic format for plotting:
import anesthetic
nested_samples = anesthetic.NestedSamples(
data=ns_run.particles.position,
logL=ns_run.particles.loglikelihood,
logL_birth=ns_run.particles.loglikelihood_birth,
)Core dependencies (from requirements.txt):
jupyter-book- For building the bookmatplotlib- For plottingnumpy- For numerical operations
Example notebooks may require additional packages:
blackjax(from main, via git — see Installation)anesthetic- For nested sampling visualizationjax,jax.numpy- JAX framework- Physics-specific packages (e.g.,
cosmopower-jaxfor the CMB example,jax-bandflux/jax_supernovaefor supernovae)
- The book is deployed automatically to GitHub Pages via GitHub Actions on push to main branch
- Notebooks are NOT re-executed during build (static display mode) to avoid dependency management issues
- Examples focus on JAX-based implementations for hardware acceleration
- Citations follow standard academic format using BibTeX references