Metropolis-Hastings Algorithm
Module containing Markov Chains Monte Carlo sampler utilizing Metropolis-Hastings algorithm with arbitrary proposal distribution. Because of the implementation of the Hastings ratio proposal distribution can be any arbitrary distribution including non-symetrical distributions.
- class monte.metropolis_hastings.MetropolisHastings(log_posterior)[source]
Bases:
BaseSampler- __init__(log_posterior) None[source]
Initializes the problem sampler object.
- Parameters:
log_posterior (callable) – Log-probability of the target distribution to be sampled from. This should either be posterior distribution of the model or a product of prior distribution and likelihood.
- sample(iter, warmup, theta, proposal_sampler=None, proposal_density=None, lag=1, **kwargs)[source]
Samples from the log_posterior distribution
- Parameters:
iter (int) – Number of iterations of the algorithm
warmup (int) – Number of warmup steps of the algorithm. These are discarded so that the only samples recorded are the ones obtained after the Markov chain has reached the stationary distribution
theta (ndarray) – Vector of initial values of parameter(s)
proposal_sampler (callable) – Function that returns a random value from a desired proposal distribution given current value of parameter. The only argument should be the sampling distribution location.
proposal_density (callable) – Function that returns probability density/mass of the proposal distribution. Must be the same distribution as in the sampler. First argument should be the value to be evaluated and second should be the sampling distribution location.
lag (int, optional) – Sampler lag. Parameter specifying every how many iterations will the sample be recorded. Used to limit autocorrelation of the samples. If lag=1, every sample is recorded, if lag=3 each third sample is recorded, etc. , defaults to 1
- Returns:
Numpy arrays of samples and acceptance information for every algorithm iteration.
- Return type:
ndarray, ndarray