Last active
September 29, 2015 15:37
-
-
Save beltiras/228c4e77b1e2aa6d33f1 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
from random import random | |
from collections import Counter | |
def randbits(n, p): | |
i = 0 | |
while i<n: | |
yield random() < p | |
i += 1 | |
def mc_hothand(trials, p): | |
d={1:{0:0,1:0},0:{0:0,1:0}} | |
lastbit=0 | |
for nextbit in randbits(trials, p): | |
d[lastbit][nextbit] += 1 | |
lastbit = nextbit | |
if d[1][0]+d[1][1]: | |
return float(d[1][1])/float(d[1][0]+d[1][1]) | |
return 0.0 # short runs will sometimes yield a division by zero. | |
def hothand_asymptote(): | |
return [sum([mc_hothand(n,0.5) for i in xrange(1000)])/1000.0 for n in xrange(4,100)] | |
def hothand(p=0.5, trials=3): | |
lastbit = random() < p | |
wins = 0 | |
for i in xrange(trials): | |
nextbit = random() < p | |
if nextbit != lastbit: | |
wins += 1 | |
lastbit = nextbit | |
return wins | |
def test_hothand(p,n): | |
a = Counter([hothand(p,n) for i in xrange(1000)]) | |
return (a[0]*-3)+(a[1]*-1)+a[2]+(3*a[3]) |
Author
beltiras
commented
Sep 29, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment