telescopic.testing¶
Helpers for testing kernels and couplings.
Classes
The checks of |
- telescopic.testing.check_coupling(kernel, target, x, y, n=10000, rng=None)¶
Check a kernel’s coupling as the engine uses it.
From
npairs,(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:
- kernel
Kernel A tuned kernel.
- target
Target 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.- n
int Pairs; at least 2. More detect smaller differences.
- rng
None,int,numpy.random.SeedSequenceornumpy.random.Generator The root of the check’s random streams.
- kernel
- Returns:
- Raises:
- ValueError
When a returned state lacks one of the kernel’s Markov fields.
Notes
Costs one coupled step and two steps of
nchains, and an evaluation of the target at each of their4nnew positions. A kernel whose states carry caches costs eight more steps ofnchains, 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
iis consumed left to right by rowiof 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 withtelescopic.kernels.Draws.child(), which needs generators built from a seed sequence, asnumpy.random.default_rng()builds them.
- Returns:
DrawsDraws for
nrows; a request beyond the prescribed numbers raisesIndexError.
- 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_rows
int The number of rows, at least 1.
- rng
None,int,numpy.random.SeedSequenceornumpy.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, asnumpy.random.default_rng()builds them.
- n_rows
- Returns: