emcee.ptsampler module¶
- class emcee.ptsampler.PTSampler(ntemps, nwalkers, dim, logl, logp, threads=1, pool=None, betas=None)[source]¶
Bases:
Sampler
A parallel-tempered ensemble sampler, using
EnsembleSampler
for sampling within each parallel chain.- Parameters:
ntemps – The number of temperatures.
nwalkers – The number of ensemble walkers at each temperature.
dim – The dimension of parameter space.
logl – The log-likelihood function.
logp – The log-prior function.
threads – (optional) The number of parallel threads to use in sampling.
pool – (optional) Alternative to
threads
. Any object that implements amap
method compatible with the built-inmap
will do here. For example,multi.Pool
will do.betas – (optional) Array giving the inverse temperatures,
, used in the ladder. The default is for an exponential ladder, with beta decreasing by a factor of each rung.
- property acceptance_fraction¶
Matrix of shape
(Ntemps, Nwalkers)
detailing the acceptance fraction for each walker.
- property acor¶
Returns a matrix of autocorrelation lengths for each parameter in each temperature of shape
(Ntemps, Ndim)
.
- property betas¶
Returns the sequence of inverse temperatures in the ladder.
- property chain¶
Returns the stored chain of samples; shape
(Ntemps, Nwalkers, Nsteps, Ndim)
.
- exponential_beta_ladder(ntemps)[source]¶
Exponential ladder in
, with increasing by each step, withntemps
in total.
- property lnlikelihood¶
Matrix of ln-likelihood values; shape
(Ntemps, Nwalkers, Nsteps)
.
- property lnprobability¶
Matrix of lnprobability values; shape
(Ntemps, Nwalkers, Nsteps)
.
- reset()[source]¶
Clear the
chain
,lnprobability
,lnlikelihood
,acceptance_fraction
,tswap_acceptance_fraction
stored properties.
- sample(p0, lnprob0=None, lnlike0=None, iterations=1, thin=1, storechain=True)[source]¶
Advance the chains
iterations
steps as a generator.- Parameters:
p0 – The initial positions of the walkers. Shape should be
(ntemps, nwalkers, dim)
.lnprob0 – (optional) The initial posterior values for the ensembles. Shape
(ntemps, nwalkers)
.lnlike0 – (optional) The initial likelihood values for the ensembles. Shape
(ntemps, nwalkers)
.iterations – (optional) The number of iterations to preform.
thin – (optional) The number of iterations to perform between saving the state to the internal chain.
storechain – (optional) If
True
store the iterations in thechain
property.
At each iteration, this generator yields
p
, the current position of the walkers.lnprob
the current posterior values for the walkers.lnlike
the current likelihood values for the walkers.
- thermodynamic_integration_log_evidence(logls=None, fburnin=0.1)[source]¶
Thermodynamic integration estimate of the evidence.
- Parameters:
logls – (optional) The log-likelihoods to use for computing the thermodynamic evidence. If
None
(the default), use the stored log-likelihoods in the sampler. Should be of shape(Ntemps, Nwalkers, Nsamples)
.fburnin – (optional) The fraction of the chain to discard as burnin samples; only the final
1-fburnin
fraction of the samples will be used to compute the evidence; the default isfburnin = 0.1
.
- Return (lnZ, dlnZ)
(lnZ, dlnZ)
: Returns an estimate of the log-evidence and the error associated with the finite number of temperatures at which the posterior has been sampled.
The evidence is the integral of the un-normalized posterior over all of parameter space:
Thermodymanic integration is a technique for estimating the evidence integral using information from the chains at various temperatures. Let
Then
so
By computing the average of the log-likelihood at the difference temperatures, the sampler can approximate the above integral.
- property tswap_acceptance_fraction¶
Returns an array of accepted temperature swap fractions for each temperature; shape
(ntemps, )
.