emcee.ensemble module

An affine invariant Markov chain Monte Carlo (MCMC) sampler.

Goodman & Weare, Ensemble Samplers With Affine Invariance

Comm. App. Math. Comp. Sci., Vol. 5 (2010), No. 1, 65–80

class emcee.ensemble.EnsembleSampler(nwalkers, dim, lnpostfn, a=2.0, args=[], postargs=None, threads=1, pool=None, live_dangerously=False)[source]

Bases: Sampler

A generalized Ensemble sampler that uses 2 ensembles for parallelization. The __init__ function will raise an AssertionError if k < 2 * dim (and you haven’t set the live_dangerously parameter) or if k is odd.

Warning: The chain member of this object has the shape: (nwalkers, nlinks, dim) where nlinks is the number of steps taken by the chain and k is the number of walkers. Use the flatchain property to get the chain flattened to (nlinks, dim). For users of pre-1.0 versions, this shape is different so be careful!

Parameters:
  • nwalkers – The number of Goodman & Weare “walkers”.

  • dim – Number of dimensions in the parameter space.

  • lnpostfn – A function that takes a vector in the parameter space as input and returns the natural logarithm of the posterior probability for that position.

  • a – (optional) The proposal scale parameter. (default: 2.0)

  • args – (optional) A list of extra arguments for lnpostfn. lnpostfn will be called with the sequence lnpostfn(p, *args).

  • postargs – (optional) Alias of args for backwards compatibility.

  • threads – (optional) The number of threads to use for parallelization. If threads == 1, then the multiprocessing module is not used but if threads > 1, then a Pool object is created and calls to lnpostfn are run in parallel.

  • pool – (optional) An alternative method of using the parallelized algorithm. If provided, the value of threads is ignored and the object provided by pool is used for all parallelization. It can be any object with a map method that follows the same calling sequence as the built-in map function.

property acceptance_fraction

An array (length: k) of the fraction of steps accepted for each walker.

property acor

The autocorrelation time of each parameter in the chain (length: dim) as estimated by the acor module.

property blobs

Get the list of “blobs” produced by sampling. The result is a list (of length iterations) of list s (of length nwalkers) of arbitrary objects. Note: this will actually be an empty list if your lnpostfn doesn’t return any metadata.

property chain

A pointer to the Markov chain itself. The shape of this array is (k, iterations, dim).

property flatchain

A shortcut for accessing chain flattened along the zeroth (walker) axis.

property flatlnprobability

A shortcut to return the equivalent of lnprobability but aligned to flatchain rather than chain.

property lnprobability

A pointer to the matrix of the value of lnprobfn produced at each step for each walker. The shape is (k, iterations).

reset()[source]

Clear the chain and lnprobability array. Also reset the bookkeeping parameters.

sample(p0, lnprob0=None, rstate0=None, blobs0=None, iterations=1, thin=1, storechain=True, mh_proposal=None)[source]

Advance the chain iterations steps as a generator.

Parameters:
  • p0 – A list of the initial positions of the walkers in the parameter space. It should have the shape (nwalkers, dim).

  • lnprob0 – (optional) The list of log posterior probabilities for the walkers at positions given by p0. If lnprob is None, the initial values are calculated. It should have the shape (k, dim).

  • rstate0 – (optional) The state of the random number generator. See the Sampler.random_state property for details.

  • iterations – (optional) The number of steps to run.

  • thin – (optional) If you only want to store and yield every thin samples in the chain, set thin to an integer greater than 1.

  • storechain – (optional) By default, the sampler stores (in memory) the positions and log-probabilities of the samples in the chain. If you are using another method to store the samples to a file or if you don’t need to analyse the samples after the fact (for burn-in for example) set storechain to False.

  • mh_proposal – (optional) A function that returns a list of positions for nwalkers walkers given a current list of positions of the same size. See utils.MH_proposal_axisaligned for an example.

At each iteration, this generator yields:

  • pos — A list of the current positions of the walkers in the parameter space. The shape of this object will be (nwalkers, dim).

  • lnprob — The list of log posterior probabilities for the walkers at positions given by pos . The shape of this object is (nwalkers, dim).

  • rstate — The current state of the random number generator.

  • blobs — (optional) The metadata “blobs” associated with the current position. The value is only returned if lnpostfn returns blobs too.