telescopic.testing

Helpers for testing kernels and couplings.

Classes

CouplingReport

The checks of check_coupling().

telescopic.testing.check_coupling(kernel, target, x, y, n=10000, rng=None)

Check a kernel’s coupling as the engine uses it.

From n pairs, (x, y) or (y, x) in random rows, one coupled step is compared with one step of the kernel alone from the first points and one from the second: how often each chain stays put, and where it moves to. Swapped pairs in one batch catch a coupling that reads another row’s state. It also counts the pairs that meet, the states whose cached log-density or gradient is stale, the states whose caches change the next step, and the pairs that nearly meet. Custom kernels should pass it before use.

Parameters:
kernelKernel

A tuned kernel.

targetTarget

The target of the steps.

x, yarray_like, shape (d,)

The starting points, close enough for the coupling to meet them in one step sometimes: for HMC, within about its meeting_scale.

nint

Pairs; at least 2. More detect smaller differences.

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

The root of the check’s random streams.

Returns:
CouplingReport
Raises:
ValueError

When a returned state lacks one of the kernel’s Markov fields.

Notes

Costs one coupled step and two steps of n chains, and an evaluation of the target at each of their 4n new positions. A kernel whose states carry caches costs eight more steps of n chains, two from each returned state.

Examples

A random walk whose proposal has the target’s covariance passes from two points close enough for the pair to meet in one step:

>>> from telescopic import Target, examples, kernels
>>> from telescopic.testing import check_coupling
>>> example = examples.gaussian(2)
>>> kernel = kernels.RandomWalk(example.cov)
>>> target = Target(example.log_prob, vectorize=True)
>>> check_coupling(kernel, target, [0.0, 0.0], [0.5, -0.5], rng=0).passed()
True
telescopic.testing.fixed_draws(normals=None, uniforms=None, generators=None)

Draws that replay prescribed random numbers, for tests that inject randomness.

Parameters:
normals, uniformsarray_like, shape (n, k), optional

Row i is consumed left to right by row i of every request.

generatorssequence of numpy.random.Generator, length n, optional

Served by telescopic.kernels.Draws.generator(). A Gibbs kernel’s common random numbers spawn from them with telescopic.kernels.Draws.child(), which needs generators built from a seed sequence, as numpy.random.default_rng() builds them.

Returns:
Draws

Draws for n rows; a request beyond the prescribed numbers raises IndexError.

telescopic.testing.random_draws(n_rows, rng=None)

Draws whose rows read fresh numbers from one generator, for statistical tests.

The rows are independent, but a row’s numbers depend on the other rows’ requests, unlike the per-replicate streams of a run.

Parameters:
n_rowsint

The number of rows, at least 1.

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

The seed of the one generator, or the generator itself. A Gibbs kernel’s common random numbers spawn from it with telescopic.kernels.Draws.child(), which needs a generator built from a seed sequence, as numpy.random.default_rng() builds them.

Returns:
Draws