Create a Metropolis-Hastings sampler in Python using a normal distribution
The Metropolis-Hastings algorithm is a type of Markov Chain Monte Carlo (MCMC) method used to generate a sequence of samples from a probability distribution. This algorithm is particularly useful when direct sampling is difficult.
Applications of the Metropolis-Hastings sampler are:-
- Bayesian inference
- Statistical physics
- Econometrics and finance
Creating a Metropolis-Hastings sampler involves the following steps:-
- Specify the sampler: Set the number of iterations and burn-in draws
- Choose a starting value: Pick an arbitrary point as the first observation
- Propose a candidate: Draw a candidate from a distribution that’s easy to sample from
- Calculate the acceptance ratio: Determine the probability of accepting the candidate based on the current sample
- Accept or reject: Generate a random number and accept the candidate if it’s less than the acceptance ratio
- Repeat: Repeat steps 3–5 for the desired number of iterations
- Average the draws: Calculate the Metropolis-Hastings estimates for the desired posterior functions
In order to illustrate the effectiveness of the Metropolis-Hastings sampler, I have created a sampler using Python.
Get Crystal X’s stories in your inbox
Join Medium for free to get updates from this writer.
The first thing that I did was to import the libraries that I would need, being:-
- Numpy to carry out numerical compute numerical computations,
- Matplotlib to visualise the data, and
- Scipy to make scientific computations.
A function was written to sample the data, which accepts as input:-
- Target distribution,
- Proposal distribution,
- Proposal sampler,
- Start, and
- Iterations
The function returns the samples:-
The target distribution and proposal distribution are defined using scipy’s normal probability function.
The proposal sampler is defined using random numbers in the shape of the normal distribution.
The parameters are then defined.
Numpy is used to set a random seed.
Once the necessary variables had been defined, the metropolis hastings function is run and it returns the variable, samples:-
Python’s visualisation library, matplotlib, is used to visualise the data, samples:-
Once the sampler was created and visualised, some statistical metrics are calculated:-
- The mean of the sampler is 0.0565
- The variance is 1.065
A sampler can be created on distributions other than the normal distribution. The gamma distribution, for example, can also be used to create a Metropolis-Hastings sample.
I have created a code review to accompany this video, which can be viewed here:- https://youtu.be/yMsSlqceEos








