Reproducibility#

Record both the random seed and the computational environment when saving an analysis. A seed controls random draws; it does not by itself guarantee identical floating-point results across environments.

import importlib.metadata
import platform

import jax
import jax.numpy as jnp
import numpy as np
import pandas as pd
import tensorflow_probability.substrates.jax.bijectors as tfb
import tensorflow_probability.substrates.jax.distributions as tfd

import liesel.goose as gs
import liesel.model as lsl

PRNG seeding#

This example uses the regression model from Sample your first posterior, including its kernel assignments and jitter settings. It starts from that model’s initial state, independently of earlier notebook sessions.

results = gs.LieselMCMC(model).run_for_epochs(
    seed=1,
    num_chains=4,
    adaptation=500,
    posterior=500,
    show_progress=False,
)
samples = results.get_posterior_samples()
liesel.goose.engine - INFO - Initializing kernels...
liesel.goose.engine - INFO - Done
liesel.goose.engine - INFO - Finished warmup
{name: value.shape for name, value in samples.items()}
{'beta': (4, 500, 2), 'log_sigma_sq': (4, 500)}

The seed configures Goose’s random-key stream, including chain initialization. Keep data, initial values, jitter, kernel ordering, and iteration counts with it. The lower-level EngineBuilder also takes a seed.

JAX uses explicit random keys. When simulating data yourself, split a key for separate draws. Reusing a key repeats its random stream; it does not produce an independent replicate.

x_key, noise_key = jax.random.split(jax.random.key(42))
x = jax.random.normal(x_key, (3,))
noise = jax.random.normal(noise_key, (3,))
{"x": x, "noise": noise}
{'x': Array([ 0.07592554, -0.48634264,  1.2903206 ], dtype=float32),
 'noise': Array([ 0.60576403,  0.7990441 , -0.908927  ], dtype=float32)}

Record the environment#

Capture versions and numerical settings from the environment actually running the analysis. Keep the source code and input data alongside this information.

packages = ["liesel", "jax", "jaxlib", "tfp-nightly", "blackjax"]
environment = {
    "Python": platform.python_version(),
    "Platform": platform.platform(),
    "Backend": jax.default_backend(),
    "64-bit enabled": jax.config.jax_enable_x64,
    "PRNG implementation": jax.config.jax_default_prng_impl,
    **{name: importlib.metadata.version(name) for name in packages},
}
pd.Series(environment, name="Value").to_frame()
Value
Python 3.13.17
Platform Linux-7.0.0-1017-azure-x86_64-with-glibc2.43
Backend cpu
64-bit enabled False
PRNG implementation threefry2x32
liesel 0.6.0.dev0+optim.1
jax 0.10.1
jaxlib 0.10.1
tfp-nightly 0.26.0.dev20260528
blackjax 1.5

Also record the exact source revision if using development code, package lock file, model and sampler settings, and data preparation. Save posterior draws and diagnostics for a published analysis; rerunning code is not a substitute for preserving the results being reported.

Understand the limits#

Changing numerical libraries, hardware, compiler settings, or evaluation order can change floating-point calculations. In MCMC, small differences can affect adaptation or acceptance decisions and lead to different trajectories. CPU execution is not a promise of bitwise equality across systems.

The Stan reproducibility discussion also distinguishes repeating a program in a fixed computational environment from statistical reproducibility. When comparing runs in different environments, assess posterior quantities relative to their Monte Carlo uncertainty and check Check diagnostics; do not require identical individual draws.

For this documentation, the repository lockfile and build instructions specify the tested environment. See the JAX random configuration reference for PRNG options.