blackjax.mcmc.slice#
Public API for the Slice sampling family.
Every slice update is univariate: a one-dimensional slice through the current
point. Multivariate behaviour is determined entirely by the proposal generator
that produces the line, proposal_generator(rng_key, position, logdensity_fn)
-> slice_fn with slice_fn(t) -> (state, is_valid). The candidate state is
threaded straight through the kernel, so a proposal can record extra quantities
on it.
Two samplers are built on this spine:
Multivariate slice: one univariate slice along a random direction. This is the top-level
blackjax.slice_sampling(as_top_level_api()), with the direction drawn bydirection_proposal()(ascale-shaped random direction, unit by default). Chaining such random-direction moves is the hit-and-run strategy.Coordinate-wise (slice-within-Gibbs,
coordinate_slice()): sweep the coordinate axes in turn, updating each full conditional with a univariate slice.
The one-dimensional interval is built by the stepping-out or doubling procedure
of Neal (2003), passed as a callable (interval=stepping_out or
interval=doubling), then narrowed by shrinkage to draw the new point.
Doubling additionally applies the Fig. 6 acceptance test. Additional
constraints are not built in but added downstream by overriding the proposal,
which gates on is_valid and may record extra quantities on the state.
References
Classes#
State of the Slice sampling chain. |
|
Additional information on a Slice sampling transition. |
Functions#
|
Create an initial state from a position and log-density function. |
|
Neal (2003) Fig. 3 stepping-out interval, in t-space (x0 at t=0). |
|
Neal (2003) Fig. 4 doubling interval, vectorized, in t-space. |
|
Build a slice kernel driven by a proposal generator. |
|
A fresh random permutation of the |
|
Sweep the coordinates in fixed natural order |
|
Default per-axis proposal for the coordinate sweep. |
|
Build a coordinate-wise (slice-within-Gibbs) kernel. |
|
A random slice direction shaped by |
|
Proposal-generator factory: slice along a random |
|
Multivariate slice sampler, |
|
Coordinate-wise (slice-within-Gibbs) slice sampler. |
Module Contents#
- class SliceState[source]#
State of the Slice sampling chain.
- position
Current position of the chain.
- logdensity
Log-density of the target at
position.
- class SliceInfo[source]#
Additional information on a Slice sampling transition.
- is_accepted
Whether shrinkage found a valid point within
max_shrinkagesteps. AlwaysTruefor an unconstrained target (the slice always contains the current point); can beFalsewhen the proposal gates a constraint intois_validand the budget is exhausted, leaving the chain in place. For the coordinate sweep it isTrueonly if every coordinate succeeded.- num_expansions
Number of interval expansions (stepping-out steps or doublings). Summed over coordinates for the sweep.
- num_shrink
Number of shrinkage evaluations taken to find the new point. Summed over coordinates for the sweep.
- bracket_left, bracket_right
The realized slice bracket in the 1-D slice coordinate
t, where the current point sits att = 0(so typicallybracket_left <= 0 <= bracket_right). For the multivariate slice these are scalars; for the coordinate sweep they are per-axistvalues, a PyTree aligned withposition. The bracket width isbracket_right - bracket_left.
- init(position: blackjax.types.ArrayLikeTree, logdensity_fn: Callable) SliceState[source]#
Create an initial state from a position and log-density function.
- stepping_out(rng_key: blackjax.types.PRNGKey, in_slice: Callable, width: float, max_expansions: int) tuple[blackjax.types.Array, blackjax.types.Array, blackjax.types.Array, AcceptFn][source]#
Neal (2003) Fig. 3 stepping-out interval, in t-space (x0 at t=0).
An interval procedure is a pluggable callable (pass it as
interval=stepping_out). It returns its own acceptance test so the kernel never branches on a name; stepping-out needs none, soaccept_fnalways returnsTrue.- Returns:
The tuple ``(left, right, num_expansions, accept_fn)`` (the bracket)
endpoints, the number of expansions, and the acceptance test.
- doubling(rng_key: blackjax.types.PRNGKey, in_slice: Callable, width: float, max_expansions: int) tuple[blackjax.types.Array, blackjax.types.Array, blackjax.types.Array, AcceptFn][source]#
Neal (2003) Fig. 4 doubling interval, vectorized, in t-space.
Expands one (randomly chosen) side at a time, doubling the bracket each step, until both ends are outside the slice or
max_expansionsis hit. A pluggable interval callable, likestepping_out(). Itsaccept_fnis Neal’s Fig. 6 acceptance test bound to this (original) bracket, which is required for doubling’s reversibility.- Returns:
The tuple ``(left, right, num_expansions, accept_fn)`` (the bracket)
endpoints, the number of expansions, and the acceptance test.
- build_kernel(interval: Callable = doubling, max_expansions: int = 10, max_shrinkage: int = 100) Callable[source]#
Build a slice kernel driven by a proposal generator.
The kernel performs one univariate slice using
proposal_generator, a callable(rng_key, position, logdensity_fn) -> slice_fnwhereslice_fn(t) -> (state, is_valid)builds the candidate state at coordinatetand reports whether it is admissible. Because the candidate state is threaded straight out, the proposal can record extra quantities on it and consume a constraint throughis_valid. To sample under a constraint, override the proposal generator rather than the kernel.- Parameters:
interval – Interval-finding procedure, passed directly as a callable. Use
doubling()(the default, Neal Fig. 4 with the Fig. 6 acceptance test) orstepping_out()(Neal Fig. 3).max_expansions – Cap on interval expansions (doublings or stepping-out steps).
max_shrinkage – Cap on shrinkage evaluations. Bounds the loop; on exhaustion the chain stays put.
- Returns:
A kernel that takes a rng_key, the current state, a log-density function, a
proposal generator and a bracket width, and returns a new state along with
information about the transition.
- random_order(rng_key: blackjax.types.PRNGKey, d: int) blackjax.types.Array[source]#
A fresh random permutation of the
dcoordinate indices (the default).
- fixed_order(rng_key: blackjax.types.PRNGKey, d: int) blackjax.types.Array[source]#
Sweep the coordinates in fixed natural order
0, 1, ..., d - 1.
- coordinate_proposal(rng_key: blackjax.types.PRNGKey, position: blackjax.types.ArrayLikeTree, logdensity_fn: Callable, i: int) Callable[source]#
Default per-axis proposal for the coordinate sweep.
The coordinate analogue of
direction_proposal(): a unit step along flattened axisi(the one-hot directione_i), sox(t)ispositionwithflat[i] += tand the current point sits att = 0. Shares theslice_fn(t) -> (state, is_valid)contract of the multivariate proposals.A constraint is added the same way as on the multivariate path – by overriding the proposal (
axis_proposal) to gateis_valid; there is no built-in constraint argument.
- build_coordinate_kernel(interval: Callable = doubling, axis_proposal: Callable = coordinate_proposal, coordinate_order: Callable = random_order, initial_widths: float | blackjax.types.Array = 1.0, max_expansions: int = 10, max_shrinkage: int = 100) Callable[source]#
Build a coordinate-wise (slice-within-Gibbs) kernel.
One step updates each scalar coordinate’s full conditional with a univariate slice, in the order given by
coordinate_order, the choice function(rng_key, d) -> indices(random_order(), the default, orfixed_order()). Each coordinate move is drawn byaxis_proposal, the per-axis analogue of the multivariateproposal_generator(coordinate_proposal()by default); override it to gate a constraint intois_valid.initial_widthsis a scalar (applied to every coordinate) or a length-Darray of per-coordinate bracket widths.- Returns:
A kernel that takes a rng_key, the current state and a log-density function,
and returns a new state along with information about the transition.
- sample_direction(rng_key: blackjax.types.PRNGKey, position: blackjax.types.ArrayLikeTree, scale: float | blackjax.types.Array = 1.0) blackjax.types.ArrayTree[source]#
A random slice direction shaped by
scaleand normalized to unit length.scaleis a scalar (isotropic), a vector (per-coordinate / diagonal) or a dense matrix (a full preconditioner, applied as a linear map to standard-normal noise, so its covariance isscale @ scale.T). Defaults to1.0(uniformly random unit directions).
- direction_proposal(scale: float | blackjax.types.Array = 1.0) Callable[source]#
Proposal-generator factory: slice along a random
scale-shaped direction.See
sample_direction()forscale(scalar / vector / dense, unit by default). Pass asslice_sampling(logp, proposal_generator=direction_proposal(scale)).
- as_top_level_api(logdensity_fn: Callable, *, proposal_generator: Callable = direction_proposal(), width: float = 1.0, interval: Callable = doubling, max_expansions: int = 10, max_shrinkage: int = 100) blackjax.base.SamplingAlgorithm[source]#
Multivariate slice sampler,
blackjax.slice_sampling.Each step takes one univariate slice along a random direction (chaining such moves is the hit-and-run strategy) drawn by
proposal_generator. The defaultdirection_proposal()draws a uniformly random direction; passdirection_proposal(scale)to precondition, or override with your own proposal to gate a constraint or record extra quantities on the state, as nested sampling does. For coordinate-wise slice-within-Gibbs, usecoordinate_slice().Examples
A new slice sampling kernel can be initialized and used with the following code:
slice_sampling = blackjax.slice_sampling(logdensity_fn) state = slice_sampling.init(position) new_state, info = slice_sampling.step(rng_key, state)
- Parameters:
logdensity_fn – Log-density of the distribution to sample from.
proposal_generator – Proposal generator
(rng_key, position, logdensity_fn) -> slice_fn, whereslice_fn(t) -> (state, is_valid). Defaults todirection_proposal()(isotropic unit directions).width – Initial bracket width along the direction (default 1.0).
interval – Interval procedure
doubling()(default) orstepping_out(), passed as a callable.max_expansions – Caps on interval expansion and shrinkage.
max_shrinkage – Caps on interval expansion and shrinkage.
- Return type:
A
SamplingAlgorithm.
- coordinate_slice(logdensity_fn: Callable, *, max_expansions: int = 10, initial_widths: float | blackjax.types.Array = 1.0, interval: Callable = doubling, coordinate_order: Callable = random_order, axis_proposal: Callable = coordinate_proposal, max_shrinkage: int = 100) blackjax.base.SamplingAlgorithm[source]#
Coordinate-wise (slice-within-Gibbs) slice sampler.
Updates each scalar coordinate’s full conditional with a univariate slice, swept in the order given by
coordinate_order. The single-variable counterpart to the multivariateas_top_level_api().- Parameters:
logdensity_fn – Log-density of the distribution to sample from.
max_expansions – Cap on interval expansions per coordinate (default 10).
initial_widths – Scalar or per-coordinate initial bracket width(s) (default 1.0).
interval – Interval procedure
doubling()(default) orstepping_out(), passed as a callable.coordinate_order – Choice function
(rng_key, d) -> indices, eitherrandom_order()(default) orfixed_order().axis_proposal – Per-axis proposal
(rng_key, position, logdensity_fn, i) -> slice_fn(coordinate_proposal()by default). Override to gate a constraint intois_valid, as a customproposal_generatordoes foras_top_level_api().max_shrinkage – Cap on shrinkage evaluations per coordinate.
- Return type:
A
SamplingAlgorithm.