-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3.py
More file actions
50 lines (40 loc) · 1.04 KB
/
p3.py
File metadata and controls
50 lines (40 loc) · 1.04 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
import numpy as np
import matplotlib.pyplot as plt
N = 100000
n = 1000
success = 0
def flip_coin(n):
heads = sum(np.round(np.random.rand(n, 1)))
tails = N - heads
p_heads = heads / N
#print(p_heads)
p_tails = tails / N
yes = 0
if p_heads == 0.500000:
yes = 1
return yes
print('start')
for i in range(N):
yes = flip_coin(n)
success = success + yes
fails = N - success
print(success)
S = [success/N, fails/N]
print(S[0])
# Create a list of the possible outcomes
outcomes = ["success", "fail"]
# Calculate the probability of each outcome
probabilities = [S[0], S[1]]
# Plot the probabilities of each outcome as a bar graph
plt.figure(1)
plt.bar(outcomes, probabilities, width=1.0)
plt.title('PMF of the outcome of rolling two fair dice')
plt.xlabel('Outcome')
plt.ylabel('Probability')
# Plot the probabilities of each outcome as a stem plot
plt.figure(2)
plt.stem(outcomes, probabilities)
plt.title('PMF of the outcome of rolling two fair dice')
plt.xlabel('Outcome')
plt.ylabel('Probability')
plt.show()