Repository navigation
Expand file tree
/
Copy pathmonte_carlo.py
More file actions
156 lines (113 loc) · 5.19 KB
/
Copy pathmonte_carlo.py
File metadata and controls
156 lines (113 loc) · 5.19 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# monte carlo estimate
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
start_time = time.time()
########## configuration ######################################################
val1 = "heads"
prob1 = 0.9
val2 = "tails"
prob2 = 0.1
n_runs = 5
n_events = 250
# monte carlo says that as number of events increases the probability tends to the expected probability
# check values equal 1
if prob1 + prob2 != 1:
raise ValueError("Probability values do not equal 1 - please adjust")
######## define functions #####################################################
def grapher(lister,prob,xxx,xxx1,word,n):
plt.plot(lister)
plt.axhline(y=prob, color='r', linestyle='dashed')
[plt.axvline(_x, color='#111212',linewidth=1,alpha=0.1) for _x in xxx]
[plt.axvline(_x, color='#42f5f2',linewidth=1,alpha=0.1) for _x in xxx1]
plt.gca().spines[['right','top']].set_visible(False)
plt.xlabel("Iterations")
plt.ylabel("Probability")
plt.title(f"Total Probability of {word} outcome from coin flip; n={n}\n{val1}={len(xxx1)}; {val2}={len(xxx)}")
plt.show()
def coin_flip():
val = random.choices([0,1],weights=(prob1,prob2))[0]#random.randint(0,10)/10 # shows last entry random.randint(0,10)/10
if val > 0.5:
typer = val2
else:
typer = val1
return val, typer
dff = pd.DataFrame()
def monte_carlo_coin_xtra(runs, n, dff):
for i in range(runs):
results = 0
for ii in range(n):
flip_result, typer = coin_flip()
results = results + flip_result
prob_value = results/(ii+1)
df1 = pd.DataFrame({'run':i,'event':ii,'result':[prob_value], 'type':typer})
dff = pd.concat([dff,df1])
dff = dff.reset_index()
#heads = [i for i, x in enumerate(list2) if x == "heads"]
#tails = [i for i, x in enumerate(list2) if x == "tails"]
#fig,axs = plt.subplots(2)
for runr in dff['run']:
#plt.plot(dff['event'][dff['run']==runr], dff['result'][dff['run']==runr], c=np.random.rand(3,))
x_valer = dff['event'][dff['run']==runr]
y_valer = dff['result'][dff['run']==runr]
y_valer1 = [1-x for x in y_valer]
y_valer2 = [x for x in y_valer]
for ix in range(2):
if ix == 0:
plt.figure(1)
plt.plot(x_valer, y_valer1, c=np.random.rand(3,))
plt.axhline(y=prob1, color='r', linestyle='dashed')
plt.gca().spines[['right','top']].set_visible(False)
plt.xlabel("Iterations")
plt.ylabel("Probability")
plt.title(f"Total Probability of {val1} outcome from coin flip; n={n}, runs={runs}", size=10)
else:
plt.figure(2)
plt.plot(x_valer, y_valer2, c=np.random.rand(3,))
plt.axhline(y=prob2, color='r', linestyle='--')
plt.gca().spines[['right','top']].set_visible(False)
plt.xlabel("Iterations")
plt.ylabel("Probability")
plt.title(f"Total Probability of {val2} outcome from coin flip; n={n}, runs={runs}", size=10)
plt.show()
# plot last event run
if i == runs-1:
list1 = dff['result'][dff['run']==i]
heads = dff['event'][(dff['run']==i) & (dff['type']==val1)]
tails = dff['event'][(dff['run']==i) & (dff['type']==val2)]
#print("length of heads is:",len(heads))
#print("length of tails is:",len(tails))
heads_chkr = [1-x for x in list1]
tails_chkr = [x for x in list1]
grapher(heads_chkr,prob1,tails,heads,val1,n)
grapher(tails_chkr,prob2,tails,heads,val2,n)
return sum(dff['result']/(runs*n))
def monte_carlo_coin(n):
results = 0
for i in range(n):
flip_result, typer = coin_flip()
results = results + flip_result
prob_value = results/(i+1)
list1.append(prob_value)
list2.append(typer)
heads = [i for i, x in enumerate(list2) if x == val1]
tails = [i for i, x in enumerate(list2) if x == val2]
heads_chkr = [1-x for x in list1]
tails_chkr = [x for x in list1]
grapher(heads_chkr,prob1,tails,heads,val1,n)
grapher(tails_chkr,prob2,tails,heads,val2,n)
return results/n
################## monte carlo coin flip probability multiple runs ##########################
tst = monte_carlo_coin_xtra(n_runs,n_events,dff)
################## monte carlo coin flip probability ##########################
list1 = []
list2 = []
tst1 = monte_carlo_coin(n_events)
heads_chkr = [list1[i] for i, x in enumerate(list2) if x == "heads"]
coin_abs = [1 if x == "heads" else 0 for x in list2]
coin_abs_clr = ["black" if x == "heads" else "green" for x in list2]
tails_chkr = [list1[i] for i, x in enumerate(list2) if x == "tails"]
###############################################################################
print("runtime:",round(time.time()-start_time,3),"seconds")