gensbi.inference.samplers#
blackjax samplers consumed by NLEPosterior. blackjax imported lazily in run().
Classes#
Microcanonical Langevin Monte Carlo sampler. |
|
Tuning parameters and diagnostics from an MCLMC run. |
|
Nested slice sampling (blackjax |
|
Diagnostics from a nested sampling run. |
|
Abstract base class for posterior samplers. |
|
Diagnostics from an adaptive tempered SMC run. |
|
Adaptive tempered Sequential Monte Carlo for (possibly multimodal) posteriors. |
Functions#
Raise if the tuned |
|
|
Run a blackjax SamplingAlgorithm.step for num_samples via lax.scan. |
|
Map a mean trajectory length to a uniform-integer draw scale. |
Module Contents#
- class gensbi.inference.samplers.MCLMC(*, adjusted=True, num_samples=1000, num_tuning_steps=5000, num_chains=1, target_acceptance=0.9, diagonal_preconditioning=True)[source]#
Bases:
SamplerMicrocanonical Langevin Monte Carlo sampler.
adjusted=True(the default) applies an MH correction for asymptotically exact sampling.adjusted=Falseuses the faster unadjusted variant, which is biased by the discretization error.- Parameters:
adjusted (bool, optional) – Whether to apply an MH correction. Default is
True.num_samples (int, optional) – Number of posterior samples to collect per chain. Default is 1000.
num_tuning_steps (int, optional) – Number of warmup steps for the L / step-size tuning loop. Default is 5000.
num_chains (int, optional) – Number of independent MCLMC chains. Default is 1.
target_acceptance (float, optional) – Target Metropolis acceptance rate for the adjusted variant. Default is 0.9.
diagonal_preconditioning (bool, optional) – Whether to use diagonal preconditioning during tuning. Default is
True.
- run(key, target)[source]#
Draw posterior samples using MCLMC.
- Parameters:
key (jax.random.PRNGKey) – Random key.
target (PosteriorTarget) – Posterior target produced by
build_target().
- Returns:
samples (Array) – Posterior samples of shape
(num_chains * num_samples, dim).info (MclmcInfo) – Tuning parameters and diagnostic information.
- class gensbi.inference.samplers.MclmcInfo[source]#
Tuning parameters and diagnostics from an MCLMC run.
- Parameters:
L (float) – Tuned trajectory length.
step_size (float) – Tuned integrator step size.
acceptance_rate (float) – Mean Metropolis acceptance rate over the sampling run.
float('nan')for the unadjusted variant.num_samples (int) – Number of samples drawn per chain.
num_chains (int) – Number of independent chains.
- class gensbi.inference.samplers.NestedSampler(*, num_live=500, num_delete=None, num_inner_steps=None, num_samples=1000, dlogz=-3.0, max_iterations=100000, num_rejuvenation_steps=0)[source]#
Bases:
SamplerNested slice sampling (blackjax
nss) posterior sampler.Runs blackjax’s Nested Slice Sampling from prior-drawn live points, accumulating dead points until the live set’s evidence share is negligible, then resamples the dead-point history into equal-weight posterior draws. Unlike the MCMC samplers this also estimates the log evidence, and handles multimodal posteriors without tempering.
- Parameters:
num_live (int, optional) – Number of live points. Default is 500.
num_delete (int or None, optional) – Number of lowest-likelihood points replaced per step (device batching). If
None, defaults tomax(1, num_live // 10).num_inner_steps (int or None, optional) – Constrained slice moves per replacement. If
None, resolved at run time tomax(5, 2 * target.dim)(blackjax’s rule of thumb for reliable mixing). Default isNone.num_samples (int, optional) – Number of equal-weight posterior draws returned. Default is 1000.
dlogz (float, optional) – Termination threshold (blackjax convention): stop once
logZ_live - logZ < dlogz. Default is -3.0; use e.g. -10.0 near phase transitions.max_iterations (int, optional) – Safety cap on the number of outer NS steps. Default is 100_000.
num_rejuvenation_steps (int, optional) – Posterior-invariant hit-and-run slice moves applied to each equal-weight draw after resampling. The with-replacement resampling duplicates draws whenever
num_samplesis comparable to the run’s ESS; a few slice moves break the duplicated atoms without changing the sampled distribution. Default is 0 (no rejuvenation).
- _rejuvenate(key, positions, target)[source]#
Break duplicated equal-weight draws with posterior-invariant moves.
Runs
num_rejuvenation_stepshit-and-run slice moves on every resampled draw (one vmapped chain per draw), targeting the unconstrained log-posterior. Directions are shaped by the empirical covariance of the resampled cloud and scaled to Mahalanobis norm 2, the same proposal the NS run’s inner kernel uses – so the moves are local decorrelation only; mode coverage and weights stay as the NS run left them.
- run(key, target)[source]#
Draw posterior samples using nested slice sampling.
- Parameters:
key (jax.random.PRNGKey) – Random key.
target (PosteriorTarget) – Posterior target produced by
build_target().
- Returns:
samples (Array) – Equal-weight posterior samples of shape
(num_samples, dim).info (NestedSamplerInfo) – Evidence estimate, ESS and the raw finalised run.
- class gensbi.inference.samplers.NestedSamplerInfo[source]#
Diagnostics from a nested sampling run.
- Parameters:
log_evidence (float) – Log marginal likelihood estimate (mean over stochastic prior-volume draws).
log_evidence_err (float) – Standard deviation of the log-evidence estimate over the stochastic prior-volume draws.
ess (float) – Effective sample size of the weighted dead-point set.
num_dead (int) – Total number of points in the finalised run (dead points plus the final live set).
dead (object) – Raw finalised
blackjax.ns.base.NSInfocarrying the full point history (positions, log-likelihoods, birth contours). Kept for downstream re-weighting or anesthetic-style analysis.
- class gensbi.inference.samplers.Sampler[source]#
Bases:
abc.ABCAbstract base class for posterior samplers.
Subclasses consume a
PosteriorTargetand return an array of posterior samples together with a sampler-specific info object.- abstractmethod run(key, target)[source]#
Draw posterior samples from a log-density target.
- Parameters:
key (jax.random.PRNGKey) – Random key.
target (PosteriorTarget) – Posterior target produced by
build_target().
- Returns:
samples (Array) – Posterior samples of shape
(n, dim).info (object) – Sampler-specific diagnostics.
- Raises:
NotImplementedError – This is an abstract method; subclasses must override it.
- class gensbi.inference.samplers.SmcInfo[source]#
Diagnostics from an adaptive tempered SMC run.
- Parameters:
log_evidence (float) – Log marginal likelihood estimate accumulated over tempering steps.
num_temperature_steps (int) – Number of temperature increments taken from beta=0 to beta=1.
final_tempering_param (float) – Final value of the tempering parameter beta (should be 1.0).
- class gensbi.inference.samplers.TemperedSMC(*, num_particles=1000, target_ess=0.9, num_mcmc_steps=10, inner_kernel='mclmc', inner_step_size=0.1, inner_num_integration_steps=5, inner_inverse_mass_matrix=None)[source]#
Bases:
SamplerAdaptive tempered Sequential Monte Carlo for (possibly multimodal) posteriors.
Walks particles along
p(theta) * q(x_o | theta)^betafor beta from 0 to 1, choosing the beta ladder adaptively to maintaintarget_ess. The inner rejuvenation kernel is adjusted MCLMC by default; fixed-trajectory HMC is available as an alternative. (NUTS is deliberately not offered: its data-dependent trajectory length does not vectorize cleanly across SMC particles, and rejuvenation does not need NUTS’s full-mixing guarantee.)target_essandnum_mcmc_stepsdefault to values calibrated for blackjax >= 1.6, whose adaptive-tempering ESS solver was corrected (upstream #914 fixed a sign bug in the bisection target). The fix changes how many temperature steps are needed to anneal from beta=0 to 1: with the pre-1.6-eratarget_ess=0.5default, the corrected solver can collapse the schedule to a single step, leaving too few temperature increments for rejuvenation to move particles onto the posterior.- Parameters:
num_particles (int, optional) – Number of SMC particles. Default is 1000.
target_ess (float, optional) – Target effective sample size ratio in
(0, 1), used to adapt the temperature increments. Default is 0.9.num_mcmc_steps (int, optional) – Number of inner MCMC rejuvenation steps per temperature. Default is 10.
inner_kernel (str, optional) – Inner MCMC kernel:
"mclmc"(adjusted MCLMC, default) or"hmc"(fixed-trajectory HMC).inner_step_size (float, optional) – Step size for the inner MCMC kernel. Default is 0.1.
inner_num_integration_steps (int, optional) – Number of integration steps for the inner MCLMC or HMC kernel. Default is 5.
inner_inverse_mass_matrix (Array or None, optional) – Inverse mass matrix for the inner kernel. If
None, defaults to a vector of ones of lengthtarget.dim. Default isNone.
- run(key, target)[source]#
Draw posterior samples using adaptive tempered SMC.
- Parameters:
key (jax.random.PRNGKey) – Random key.
target (PosteriorTarget) – Posterior target produced by
build_target().
- Returns:
samples (Array) – Posterior samples of shape
(num_particles, dim).info (SmcInfo) – SMC diagnostics including log evidence and tempering statistics.
- gensbi.inference.samplers._check_rescale_domain(mu)[source]#
Raise if the tuned
mu = L / step_sizeis outside_rescale’s domain.For
mu < 1,floor(2 * mu - 1) == 0and_rescalereturns 0, so the integration-step drawceil(U(0,1) * 0)is 0 — a chain that never moves. A host-side check on the tuned value turns that silent failure into an explicit error. (The in-tuning average is left to blackjax; this is a convenience sampler, not a fully hardened MCMC engine.)
- gensbi.inference.samplers._inference_loop(rng_key, step_fn, initial_state, num_samples)[source]#
Run a blackjax SamplingAlgorithm.step for num_samples via lax.scan.
- gensbi.inference.samplers._rescale(mu)[source]#
Map a mean trajectory length to a uniform-integer draw scale.
From blackjax’s
adjusted_mclmc_dynamic: drawing the number of integration steps asceil(U(0,1) * _rescale(L/step_size))makes the average number of steps exactlymu = L / step_size.mumust satisfymu >= 1; see_check_rescale_domain()for the host-side guard applied to the tuned value.