Gibbs

class telescopic.kernels.Gibbs(conditional, blocks, *, coupling='maximal', eta=1.0, threshold=None)

A Gibbs sampler that draws blocks of coordinates from their full conditionals.

A step sweeps the blocks in order and draws each from its full conditional given the current values of the other coordinates. Coupled chains draw each block from a coupling of their two conditionals:

  • "maximal": the rejection maximal coupling, with eta;

  • "crn": common random numbers, which contract the chains but make them equal only by rounding. For a block of one coordinate whose two conditionals have ppf, one uniform goes through each ppf. For a block of several coordinates, or of one without ppf, each chain draws from its own conditional with rvs, from generators in the same state, so each keeps its conditional’s law whatever its type. The two draws are then close when rvs reads a fixed count of numbers from the generator and maps them smoothly in the conditional’s parameters, as a normal block drawn as mean + cholesky(cov) @ random_state.standard_normal(k), or with random_state.multivariate_normal(mean, cov, method="cholesky"), does. NumPy’s default method, a singular value decomposition, which SciPy’s multivariate_normal calls, can change signs and order between nearby covariances, and then the two draws are as far apart as independent ones;

  • "two-scale": common random numbers for pairs farther apart than threshold at the start of the sweep, the maximal coupling for the others (Biswas, Bhattacharya, Jacob & Johndrow 2022).

Where the two chains agree on every coordinate outside a block, their conditionals are the same, and both chains get the same draw.

Parameters:
conditionalcallable()

conditional(x, b) returns the full conditional of block b given the other coordinates of x, a read-only point of shape (d,): a frozen distribution with rvs(random_state=...) and a normalized logpdf or logpmf, such as scipy.stats.norm(mean, sd). One value of logpdf per coordinate, as with array parameters, is summed: the joint log-density of independent coordinates.

blockssequence of array_like of int

Index arrays that partition the coordinates 0, ..., d - 1.

coupling{“maximal”, “crn”, “two-scale”}
etafloat

Cap on the maximal coupling’s meeting probability, in (0, 1]; 1 with "crn".

thresholdfloat, optional

The distance at which "two-scale" switches couplings; required by it, and refused by the others.

Notes

Sweeps loop over rows in Python, and build a conditional per row and block. A frozen SciPy distribution takes 250 to 300 µs to build, which can dominate a step; a small class with the methods used is faster, as in telescopic.examples. A step draws each block from the row’s generator. A coupled step does the same for the maximal coupling and for identical conditionals. For common random numbers it reads 1 uniform per row and block through ppf, or else draws from a new child of the row’s generator, child(), which leaves the row’s own stream where it was. A conditional that draws a point that is not finite, or not of its block’s size, raises ValueError. With log_prob=None the target has no log-density, and states carry 0.

Examples

The pump-failure model has full conditionals, so a Gibbs kernel samples it without a log-density, which is never evaluated:

>>> from telescopic import examples, kernels, sample
>>> example = examples.pumps()
>>> kernel = kernels.Gibbs(example.conditional, example.blocks)
>>> result = sample(
...     None, example.initial, kernel=kernel, n_replicates=50, rng=0
... )
>>> result.n_log_prob, result.mean().value.shape
(0, (11,))
coupled_step(target, state_x, state_y, draws)

One coupled transition of each pair.

Parameters:
targetTarget

The target that the chains sample.

state_x, state_yState

The first and the second chains of the n pairs.

drawsDraws

Random numbers for the n pairs.

Returns:
state_x, state_yState
property ndim

The dimension that the kernel’s parameters fix, or None.

Returns:
int or None
property needs_log_prob

Whether the kernel evaluates the log-density; Gibbs kernels do not.

Returns:
bool
step(target, state, draws)

One transition of each chain.

Parameters:
targetTarget

The target that the chains sample.

stateState

The chains, n of them.

drawsDraws

Random numbers for the n chains.

Returns:
stateState
acceptancendarray, shape (n,)

Each chain’s probability of accepting its proposal, 1 for a kernel that always moves; dual averaging adapts step sizes to its mean.