Package mystic :: Module metropolis

Source Code for Module mystic.metropolis

 1  #!/usr/bin/env python 
 2   
 3  """ 
 4  Implements a simple version of the Metropolis-Hastings algorithm 
 5  """ 
 6   
 7   
8 -def metropolis_hastings(proposal, target, x):
9 """ 10 Proposal(x) -> next point. Must be symmetric. 11 This is because otherwise the PDF of the proposal density is needed 12 (not just a way to draw from the proposal) 13 """ 14 from numpy import random 15 Yt = proposal(x) 16 r = min(target(Yt)/(target(x) + 1e-100), 1) 17 if random.random() <= r: 18 return Yt 19 else: 20 return x
21 22 23 if __name__=='__main__': 24 pass 25 26 # end of file 27