The Metropolis–Hastings algorithm can be implemented in python as an Abstract Base Class using the code below. The code was designed to:
Example Usage
The code required to apply the Metropolis–Hastings algorithm to the Variational Monte Carlo problem is as follows
The code above defines
- The state space to be of type
float
. More complex examples might have states in R3 by passing in the type Tuple[float, float, float]
, etc
- The likelihood function to be e−p0(x−p1)2−p2(x−p3)2.
With the methodology defined, we can run an example
This starts the simulation at state x=100, runs 100 iterations, and then plots the result. As an example, we get the result