-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboy-or-girl-1.py
37 lines (27 loc) · 891 Bytes
/
boy-or-girl-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''
Boy or girl paradox (version 1).
Mr. Jones has two children. The older child is a girl.
What is the probability that both children are girls?
Answer: 1/2
'''
import numpy as np
NUMBER_OF_SIMULATIONS = 1_000_000
number_of_favorable_outcomes = 0
np.random.seed(0)
i = 0
while i < NUMBER_OF_SIMULATIONS:
# Pick a random family
sorted_children = [
'boy' if np.random.choice([0, 1]) else 'girl',
'boy' if np.random.choice([0, 1]) else 'girl'
]
# Make sure the older sibling is a girl
if sorted_children[1] != 'girl':
continue
print(f"Simulation {i + 1}/{NUMBER_OF_SIMULATIONS}")
# If also the younger children is a girl, the outcome is favorable
if sorted_children[0] == 'girl':
number_of_favorable_outcomes += 1
i += 1
print(f"Estimated probability: {
number_of_favorable_outcomes/NUMBER_OF_SIMULATIONS}")