-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarloChildren.py
84 lines (59 loc) · 2.26 KB
/
MonteCarloChildren.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#Program runs a Monte Carlo simulation to determine the probability that at least one child will be a girl
import random
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
plt.style.use('ggplot')
def CoinFlip():
randNumber = random.randint(0,1)
if (randNumber == 0):
return 'boy'
#return 'heads'
elif (randNumber == 1):
return 'girl'
#return 'tails'
else:
return 'error'
#if I have 3 kids, what are the odds that at least one will be male
def MonteCarloKids(numTrials=100, numKids=3):
trialsDict = dict() #dictionary of all trials
numberWithGirl = 0 #initialize
probabilityList = []
powerTens = []
decrementer = numTrials
#iterate over number of trials in monte carlo simulation
for i in range(0, numTrials):
children = []
#generate set of kids for one generation
for j in range(0, numKids):
children.append(CoinFlip())
#print(children)
trialsDict[i] = children #append set of kids to the newest generation in the dictionary
#count the number of generations that have at least one girl
for key in trialsDict.keys():
if trialsDict[key].count('girl'):
numberWithGirl = numberWithGirl + 1
probability = (numberWithGirl / (key + 1))
probabilityList.append(probability)
#for key in trialsDict.keys():
#if (key + 1) % 10 == 0:
#print('Probability at trial ' + str(key + 1) + ' is ' + str(probabilityList[key]))
#generate log scale for trials
while decrementer >= 1:
powerTens.append(decrementer)
decrementer = decrementer / 10
powerTens.reverse()
for k in powerTens:
k = int(k)
print('Trial: ' + str(k) + ', estimated probability: ' + str(probabilityList[k - 1]))
print('------')
print('Odds of having at least one boy are: ' + str(numberWithGirl / numTrials))
plt.semilogx(list(range(numTrials)), probabilityList)
plt.title('Probility of having at least 1 girl')
plt.grid(True)
plt.ylabel('estimated probability')
plt.xlabel('simulation number')
plt.ylim(ymin=0, ymax=1.5)
plt.show()
#(number of trials, number of kids planned for one generation)
MonteCarloKids(1000000, 3)