Created
August 14, 2013 03:38
-
-
Save lambda-fairy/6227808 to your computer and use it in GitHub Desktop.
Monte Carlo simulation of "Boy or Girl" paradox
This file contains hidden or 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/env python3 | |
""" | |
I go around, knocking on people's doors, until I find someone with two | |
children, at least one of which is a boy. | |
What is the probability that they are both boys? | |
(The answer should approximate 1/3.) | |
""" | |
from itertools import islice | |
from random import choice | |
N = 1000000 | |
SEXES = 'Boy Girl'.split() | |
def main(n_tries): | |
def pick_pair(): | |
return (choice(SEXES), choice(SEXES)) | |
def condition(children): | |
return any(child == 'Boy' for child in children) | |
def event(children): | |
return all(child == 'Boy' for child in children) | |
print(monte_carlo(pick_pair, condition, event, n_tries)) | |
def monte_carlo(sample, condition, event, n_tries): | |
space = filter(condition, infinitely(sample)) | |
n_success = 0 | |
for case in islice(space, n_tries): | |
if event(case): | |
n_success += 1 | |
return n_success / n_tries | |
def infinitely(f): | |
while True: | |
yield f() | |
if __name__ == '__main__': | |
main(N) |
This file contains hidden or 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/env python3 | |
""" | |
I go around, knocking on people's doors, until I find someone with two | |
children, at least one of which is a boy *who is born on Tuesday*. | |
What is the probability that they are both boys? | |
(The answer should approximate 13/27.) | |
""" | |
from collections import namedtuple | |
from itertools import islice | |
from random import choice | |
N = 1000000 | |
SEXES = 'Boy Girl'.split() | |
DAYS = 'Sun Mon Tue Wed Thu Fri Sat'.split() | |
Person = namedtuple('Person', 'sex day') | |
def main(n_tries): | |
def pick_child(): | |
return Person(choice(SEXES), choice(DAYS)) | |
def pick_pair(): | |
return (pick_child(), pick_child()) | |
test = Person('Boy', 'Tue') | |
def condition(children): | |
return any(child == test for child in children) | |
def event(children): | |
return all(child.sex == 'Boy' for child in children) | |
print(monte_carlo(pick_pair, condition, event, n_tries)) | |
def monte_carlo(sample, condition, event, n_tries): | |
space = filter(condition, infinitely(sample)) | |
n_success = 0 | |
for case in islice(space, n_tries): | |
if event(case): | |
n_success += 1 | |
return n_success / n_tries | |
def infinitely(f): | |
while True: | |
yield f() | |
if __name__ == '__main__': | |
main(N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment