Mixture

class telescopic.kernels.Mixture(kernels, weights)

A mixture of kernels.

Each step draws one uniform and applies the component it selects. The coupled step gives both chains the same component and applies that component’s coupling. The choice never depends on the states, which keeps each marginal the mixture (Atchadé & Jacob 2024, section 4.2).

The field that component i names name in a State’s extra is named f"{i}/{name}" in the mixture’s states. Each component sees only its own fields, under their own names, and every other field is carried unchanged. A component with Markov fields (markov_fields) raises ValueError: the mixture leaves a joint law \(\pi(x)\psi(v)\) invariant only when \(\psi\) does not depend on \(x\), which cannot be checked.

Parameters:
kernelssequence of Kernel

Kernel instances. tune() tunes the untuned ones on one set of pilot chains; its report gives the largest weight ratio of any independence component.

weightsarray_like

Non-negative weights with a finite sum, normalized internally.

Examples

The sampler tunes the untuned components of a mixture on the same pilot chains, whose report gives the largest weight ratio of the independence proposal:

>>> from telescopic import examples, kernels, sample
>>> example = examples.gaussian(2)
>>> kernel = kernels.Mixture(
...     [kernels.RandomWalk(), kernels.Independence()], [0.8, 0.2]
... )
>>> result = sample(
...     example.log_prob, example.initial, kernel=kernel,
...     n_replicates=50, vectorize=True, rng=0,
... )
>>> result.kernel.is_tuned, result.tuning.max_weight_ratio is not None
(True, True)
check_ndim(ndim)

Raise ValueError if the kernel works in another dimension than ndim.

Parameters:
ndimint

The dimension of the initial points.

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
init_extra(state)

The initial values of the kernel’s own fields at the chains of state.

init_state() attaches them; none by default.

Parameters:
stateState

The chains, n of them, without fields of their own.

Returns:
dict of str to ndarray, each of shape (n, …)
property is_tuned

Whether every parameter is set, so the kernel can run.

Returns:
bool
property ndim

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

Returns:
int or None
property needs_grad

Whether the kernel evaluates the gradient of the log-density.

Returns:
bool
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.