sample

telescopic.sample(
log_prob,
init,
n_replicates=100,
*,
args=(),
kwargs=None,
grad=None,
log_prob_and_grad=None,
vectorize=False,
kernel='auto',
k='auto',
m='auto',
lag='auto',
thin=None,
max_iter=None,
keep=None,
start=None,
checkpoint=None,
names=None,
pool=None,
n_workers=None,
progress=False,
check_grad=True,
rng=None,
)

Unbiased estimates of expectations under a target, in one call.

sample(log_prob, init, n, ...) is CoupledSampler(log_prob, init, ...).run(n, ...): it tunes the kernel’s unset parameters, chooses k from 100 lag-1 pilot pairs when it is "auto", and runs n_replicates replicates of coupled chains.

Parameters:
log_probcallable() or None

\(\log\pi\) up to a constant, log_prob(x, *args, **kwargs), for x of shape (d,), or (n, d) with vectorize=True; None for a Gibbs kernel.

init

The initial distribution \(\pi_0\): an object with rvs(random_state=rng), a callable init(rng) or an array of points of shape (n, d).

n_replicatesint

The number \(R\) of replicates, at least 2.

argstuple or list

Extra positional arguments of log_prob, grad and log_prob_and_grad, such as (data,).

kwargsmapping, optional

Extra keyword arguments of these functions.

gradcallable(), optional

\(\nabla\log\pi\) with the signature of log_prob. Only the kernels that need it, MALA and HMC, evaluate it; "auto" is random-walk Metropolis.

log_prob_and_gradcallable(), optional

In place of grad: the pair \((\log\pi, \nabla\log\pi)\) from one call; see CoupledSampler.

vectorizebool

Whether log_prob, grad and log_prob_and_grad take a batch of points. The kernels then also compute their matrix products for a batch of chains at once, with BLAS, so a run repeats bitwise only with the same seed and settings, pool and n_workers, BLAS build and number of BLAS threads. Otherwise a replicate can differ by rounding, which can move its meeting time and its whole output, and the estimates agree within their error bars (Reproducibility, in Run in parallel).

kernelKernel or str

A kernel, or one of "auto" and "rwmh" (random-walk Metropolis with the reflection-maximal coupling), "mala" and "hmc" (which need grad or log_prob_and_grad) and "independence".

k, m, lagint or “auto”

"auto" chooses k from the pilot’s meeting times, then lag = max(1, k) and m = 10 k. A value given explicitly is kept.

thinint, optional

Average every thin-th state from k; None averages every state.

max_iterint, optional

Censor pairs still unmet at this time; None runs every pair until it meets.

keepint, optional

Store the signed measures of the replicates of index below keep only, and the moments of every replicate; see CoupledSampler.run(). None stores every replicate’s signed measure.

startint, optional

Run the replicates of indices start to start + n_replicates - 1, as a task of a run split into ranges; with a checkpoint, its plan must exist. See CoupledSampler.run().

checkpointstr or path-like, optional

A directory to which the run writes its plan and its replicates as they finish, so that the same call, with the same rng, completes a run whose process was killed; see CoupledSampler.run().

namessequence of str, optional

Parameter names for summaries.

poolNone, int, executor or pool

Where the pilot pairs and the replicates run: None in this process; an integer, that many worker processes; or an object with submit, such as a concurrent.futures.Executor, or with map, such as an emcee or schwimmbad pool. See CoupledSampler for how a pool runs them.

n_workersint, optional

The number of workers of an executor or a pool, which sets how many chunks it gets; None reads the pool’s size, or else counts the CPUs this process may use. See CoupledSampler.

progressbool

Show the progress of the pilot and of the run: a tqdm bar when tqdm is installed, lines on stderr otherwise.

check_gradbool

Compare the gradient with differences of the log-density before tuning, if the kernel evaluates a gradient, and warn where they disagree; see CoupledSampler.

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

The root of every random stream.

Returns:
Result

Its estimates are unbiased unless max_iter censors a pair, which biases them and warns, or a Ctrl-C interrupts the run, which leaves them consistent and flags the result interrupted. It warns about the conditions listed under Warnings, in Warnings and errors. An error during the replicates is raised with the result of those finished before it as its attribute telescopic_result, which CoupledSampler.resume() continues.

Examples

One call runs 50 replicates on a correlated Gaussian in two dimensions and estimates the mean of each coordinate:

>>> from telescopic import examples, sample
>>> example = examples.gaussian(2)
>>> result = sample(
...     example.log_prob, example.initial,
...     n_replicates=50, vectorize=True, rng=0,
... )
>>> result.n_replicates, result.mean().value.shape
(50, (2,))