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:
SamplerA generalized Ensemble sampler that uses 2 ensembles for parallelization. The
__init__function will raise anAssertionErrorifk < 2 * dim(and you haven’t set thelive_dangerouslyparameter) or ifkis odd.Warning: The
chainmember of this object has the shape:(nwalkers, nlinks, dim)wherenlinksis the number of steps taken by the chain andkis the number of walkers. Use theflatchainproperty 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.lnpostfnwill be called with the sequencelnpostfn(p, *args).postargs – (optional) Alias of
argsfor backwards compatibility.threads – (optional) The number of threads to use for parallelization. If
threads == 1, then themultiprocessingmodule is not used but ifthreads > 1, then aPoolobject is created and calls tolnpostfnare run in parallel.pool – (optional) An alternative method of using the parallelized algorithm. If provided, the value of
threadsis ignored and the object provided bypoolis used for all parallelization. It can be any object with amapmethod that follows the same calling sequence as the built-inmapfunction.
- 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 theacormodule.
- property blobs¶
Get the list of “blobs” produced by sampling. The result is a list (of length
iterations) oflists (of lengthnwalkers) of arbitrary objects. Note: this will actually be an empty list if yourlnpostfndoesn’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
lnprobabilitybut aligned toflatchainrather thanchain.
- property lnprobability¶
A pointer to the matrix of the value of
lnprobfnproduced at each step for each walker. The shape is(k, iterations).
- sample(p0, lnprob0=None, rstate0=None, blobs0=None, iterations=1, thin=1, storechain=True, mh_proposal=None)[source]¶
Advance the chain
iterationssteps 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. Iflnprob 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_stateproperty for details.iterations – (optional) The number of steps to run.
thin – (optional) If you only want to store and yield every
thinsamples 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
storechaintoFalse.mh_proposal – (optional) A function that returns a list of positions for
nwalkerswalkers given a current list of positions of the same size. Seeutils.MH_proposal_axisalignedfor 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 bypos. 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 iflnpostfnreturns blobs too.