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 a map method compatible with the built-in map will do here. For example, multi.Pool will do.

  • betas – (optional) Array giving the inverse temperatures, \(\beta=1/T\), used in the ladder. The default is for an exponential ladder, with beta decreasing by a factor of \(1/\sqrt{2}\) 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 \(1/T\), with \(T\) increasing by \(\sqrt{2}\) each step, with ntemps 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 the chain 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 is fburnin = 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:

\[Z \equiv \int d\theta \, l(\theta) p(\theta)\]

Thermodymanic integration is a technique for estimating the evidence integral using information from the chains at various temperatures. Let

\[Z(\beta) = \int d\theta \, l^\beta(\theta) p(\theta)\]

Then

\[\frac{d \ln Z}{d \beta} = \frac{1}{Z(\beta)} \int d\theta l^\beta p \ln l = \left \langle \ln l \right \rangle_\beta\]

so

\[\ln Z(\beta = 1) = \int_0^1 d\beta \left \langle \ln l \right\rangle_\beta\]

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, ).