Metropolis-Hastings Sampler

class sampyl.Metropolis(logp, start, tune_interval=100, **kwargs)

Metropolis-Hastings sampler for drawing from a distribution defined by a logp function.

Has automatic scaling such that acceptance rate stays around 50%

Parameters:
  • logp – function log P(X) function for sampling distribution.
  • start – Dictionary of starting state for the sampler. Should have one element for each argument of logp.
  • scalescalar or 1D array-like. initial scaling factor for proposal distribution.
  • tune_intervalint.
  • scalescalar or 1D array-like. initial scaling factor for proposal distribution.
  • tune_intervalint. number of samples between tunings of scale factor.

Example:

def logp(x, y):
    ...

start = {'x': x_start, 'y': y_start}
metro = sampyl.Metropolis(logp, start)
chain = metro.sample(20000, burn=5000, thin=4)
sample(num, burn=0, thin=1, n_chains=1, progress_bar=True)

Sample from \(P(X)\)

Parameters:
  • numint. Number of samples to draw from \(P(X)\).
  • burn – (optional) int. Number of samples to discard from the beginning of the chain.
  • thin – (optional) float. Thin the samples by this factor.
  • n_chains – (optional) int. Number of chains to return. Each chain is given its own process and the OS decides how to distribute the processes.
  • progress_bar – (optional) boolean. Show the progress bar, default = True.
Returns:

Record array with fields taken from arguments of logp function.

step()

Perform a Metropolis-Hastings step.