Kernel

class telescopic.kernels.Kernel

A Markov kernel with its coupling.

Subclasses implement step() and coupled_step() on batches of chains. Each marginal of coupled_step() must have exactly the law of step(). The engine declares a meeting when a pair’s returned Markov states are equal under ==: the positions and the fields that markov_fields names. So a coupling that makes the two proposals identical must give the second chain a copy of the first chain’s proposal, never a recomputation. For a target evaluated point by point, a row’s arithmetic must not depend on the other rows of the batch; for a vectorized one it may, as BLAS products do, if a coupled step computes each chain’s products in a call of its own, of the same shape, so that equal chains stay equal.

check_ndim(ndim)

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

Parameters:
ndimint

The dimension of the initial points.

check_target(target)

Raise ValueError if target lacks a function that the kernel evaluates.

A target that is not a Target raises TypeError.

Parameters:
targetTarget

The target to check.

abstractmethod 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, …)
init_state(target, x)

The states at the rows of x: the target’s values, and the kernel’s fields.

Gradients are evaluated if the kernel needs them, and init_extra() gives the fields, which must include every Markov field.

A gradient that is not finite where the log-density is raises ValueError here, as chains cannot start there; elsewhere it is taken as 0.

Parameters:
targetTarget

The target to evaluate.

xndarray, shape (n, d)

Initial points, whose dimension must be the kernel’s.

Returns:
State

With the fields of init_extra().

property is_tuned

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

Returns:
bool
property markov_fields

The fields of a State’s extra that are part of the kernel’s Markov state.

A pair meets when its positions and these fields are equal; every other field of the kernel is a cache. None for every kernel of the package.

Returns:
tuple of str
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
abstractmethod 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.

tune(target, initial, rng=None, *, pilot=None)

A copy with the unset parameters tuned on pilot chains.

Each kernel’s documentation says which parameters it tunes and how.

Parameters:
targetTarget

Checked with check_target() before the pilot runs.

initial

An object with rvs(random_state=generator), a callable initial(rng) or an array of points of shape (n, d), as for telescopic.initial.as_initial().

rngNone, int, numpy.random.SeedSequence or numpy.random.Generator

Seed of the tuning, in the tuning namespace: the pilot chains, unless pilot is given, and dual averaging.

pilotPilotRun, optional

Adaptive Metropolis chains already run, from telescopic.tuning.adaptive_metropolis(), so that several kernels can be tuned on one pilot.

Returns:
kernelKernel
reportTuningReport or None

None, with the kernel itself, when there is nothing to tune.