emcee.mh module

A vanilla Metropolis-Hastings sampler

class emcee.mh.MHSampler(cov, *args, **kwargs)[source]

Bases: Sampler

The most basic possible Metropolis-Hastings style MCMC sampler

Parameters:
  • cov – The covariance matrix to use for the proposal distribution.

  • 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.

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

reset()[source]

Clear chain, lnprobability and the bookkeeping parameters.

sample(p0, lnprob=None, randomstate=None, thin=1, storechain=True, iterations=1)[source]

Advances the chain iterations steps as an iterator

Parameters:
  • p0 – The initial position vector.

  • lnprob0 – (optional) The log posterior probability at position p0. If lnprob is not provided, the initial value is calculated.

  • rstate0 – (optional) The state of the random number generator. See the 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.

At each iteration, this generator yields:

  • pos — The current positions of the chain in the parameter space.

  • lnprob — The value of the log posterior at pos .

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