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, ...)isCoupledSampler(log_prob, init, ...).run(n, ...): it tunes the kernel’s unset parameters, chooseskfrom 100 lag-1 pilot pairs when it is"auto", and runsn_replicatesreplicates of coupled chains.- Parameters:
- log_prob
callable()orNone \(\log\pi\) up to a constant,
log_prob(x, *args, **kwargs), forxof shape(d,), or(n, d)withvectorize=True;Nonefor a Gibbs kernel.- init
The initial distribution \(\pi_0\): an object with
rvs(random_state=rng), a callableinit(rng)or an array of points of shape(n, d).- n_replicates
int The number \(R\) of replicates, at least 2.
- args
tupleorlist Extra positional arguments of
log_prob,gradandlog_prob_and_grad, such as(data,).- kwargsmapping, optional
Extra keyword arguments of these functions.
- grad
callable(), 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_grad
callable(), optional In place of
grad: the pair \((\log\pi, \nabla\log\pi)\) from one call; seeCoupledSampler.- vectorizebool
Whether
log_prob,gradandlog_prob_and_gradtake 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,poolandn_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).- kernel
Kernelorstr A kernel, or one of
"auto"and"rwmh"(random-walk Metropolis with the reflection-maximal coupling),"mala"and"hmc"(which needgradorlog_prob_and_grad) and"independence".- k, m, lag
intor “auto” "auto"chooseskfrom the pilot’s meeting times, thenlag = max(1, k)andm = 10 k. A value given explicitly is kept.- thin
int, optional Average every
thin-th state fromk;Noneaverages every state.- max_iter
int, optional Censor pairs still unmet at this time;
Noneruns every pair until it meets.- keep
int, optional Store the signed measures of the replicates of index below
keeponly, and the moments of every replicate; seeCoupledSampler.run().Nonestores every replicate’s signed measure.- start
int, optional Run the replicates of indices
starttostart + n_replicates - 1, as a task of a run split into ranges; with acheckpoint, its plan must exist. SeeCoupledSampler.run().- checkpoint
stror 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; seeCoupledSampler.run().- namessequence of
str, optional Parameter names for summaries.
- pool
None,int, executor or pool Where the pilot pairs and the replicates run:
Nonein this process; an integer, that many worker processes; or an object withsubmit, such as aconcurrent.futures.Executor, or withmap, such as an emcee or schwimmbad pool. SeeCoupledSamplerfor how a pool runs them.- n_workers
int, optional The number of workers of an executor or a pool, which sets how many chunks it gets;
Nonereads the pool’s size, or else counts the CPUs this process may use. SeeCoupledSampler.- 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.- rng
None,int,numpy.random.SeedSequenceornumpy.random.Generator The root of every random stream.
- log_prob
- Returns:
ResultIts estimates are unbiased unless
max_itercensors a pair, which biases them and warns, or a Ctrl-C interrupts the run, which leaves them consistent and flags the resultinterrupted. 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 attributetelescopic_result, whichCoupledSampler.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,))