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 anAssertionError
ifk < 2 * dim
(and you haven’t set thelive_dangerously
parameter) or ifk
is odd.Warning: The
chain
member of this object has the shape:(nwalkers, nlinks, dim)
wherenlinks
is the number of steps taken by the chain andk
is the number of walkers. Use theflatchain
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 sequencelnpostfn(p, *args)
.postargs – (optional) Alias of
args
for backwards compatibility.threads – (optional) The number of threads to use for parallelization. If
threads == 1
, then themultiprocessing
module is not used but ifthreads > 1
, then aPool
object is created and calls tolnpostfn
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 bypool
is used for all parallelization. It can be any object with amap
method that follows the same calling sequence as the built-inmap
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 theacor
module.
- property blobs¶
Get the list of “blobs” produced by sampling. The result is a list (of length
iterations
) oflist
s (of lengthnwalkers
) of arbitrary objects. Note: this will actually be an empty list if yourlnpostfn
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 toflatchain
rather thanchain
.
- property lnprobability¶
A pointer to the matrix of the value of
lnprobfn
produced 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
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
. 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_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
toFalse
.mh_proposal – (optional) A function that returns a list of positions for
nwalkers
walkers given a current list of positions of the same size. Seeutils.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 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 iflnpostfn
returns blobs too.