Created
August 22, 2013 08:28
-
-
Save orian/6304550 to your computer and use it in GitHub Desktop.
Let's say that you have an event which happen at each second with probability p, each second is independent on previous and next one. Instead of tossing a coin every second, one can try to toss a coin once and estimate when a next event is going to happen, and then call the function again at a moment of event to get next event time and so on.
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
#!/usr/bin/python | |
import random | |
class BernoulliPbb(object): | |
def __init__(self, p): | |
self.p = p | |
def Estimate(self): | |
r = random.random() | |
i = 0 | |
pbb = self.p | |
pbb_sum = self.p | |
while r >= pbb_sum: | |
pbb *= self.p | |
pbb_sum += pbb | |
print pbb, pbb_sum | |
i += 1 | |
return i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment