Created
April 14, 2013 21:41
-
-
Save sl8r000/5384345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleMCMC(object): | |
def __init__(self, start_state, transition, goodness_measure): | |
self.present_state = start_state | |
self.transition = transition | |
self.goodness_measure = goodness_measure | |
self.present_goodness = self.goodness_measure(self.present_state) | |
def take_step(self): | |
# Use the transition function to find a candidate for the new state. | |
# Compute the ratio of the candidate goodness over the present-state | |
# goodness. If this ratio is >= 1: move to the candidate state. If it | |
# is < 1: Then move to the candidate state with probability that ratio. | |
candidate = self.transition(self.present_state) | |
candidate_goodness = self.goodness_measure(candidate) | |
if self.present_goodness == 0: | |
if candidate_goodness == 0: | |
relative_goodness = 0.5 | |
else: | |
relative_goodness = 1.0 | |
else: | |
relative_goodness = candidate_goodness/self.present_goodness | |
if relative_goodness >= 1.0: | |
self.present_state = candidate | |
self.present_goodness = candidate_goodness | |
else: | |
chance = random.random() | |
if chance <= relative_goodness: | |
self.present_state = candidate | |
self.present_goodness = candidate_goodness | |
def run_chain(self, num_iterations): | |
for k in range(num_iterations): | |
self.take_step() | |
return self.present_state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment