-
Notifications
You must be signed in to change notification settings - Fork 0
/
Probability.py
148 lines (104 loc) · 3.58 KB
/
Probability.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
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
'''
################################################################################
# Practical examples of probability calculation with python
# Authod : Wajdi Ben Saad | December 2020. | V.01
# for citation, please link to my website :
# www.WajdiBenSaad.com
# Not to be used for commercial purposes.
# Thanks to Google & Stackoverflow for helping to provide the matrials
################################################################################
This code covers some practical aspects of calculating probability using python
It is needless to say that the theoratical understanding of these concepts is crucial.
The code is extremely simplified and split to answer each question individually.
This should not be use as it is in a production context.
More work should be done into making more flexible functions .
Please use this for educational purposes only!!!!
'''
'''
Poker.. probability and chance
'''
# the probability of drawing an Ace from a poker deck:
# Sample Space
cards = 52
# Outcomes
aces = 4
# Divide possible outcomes by the sample set
ace_probability = aces / cards
# Print probability rounded to two decimal places
print(round(ace_probability, 2))
# Ace Probability Percent Code
ace_probability_percent = ace_probability * 100
# Print probability percent rounded to one decimal place
print(str(round(ace_probability_percent, 0)) + '%')
# Create function that returns probability percent rounded to one decimal place
def event_probability(event_outcomes, sample_space):
probability = round((event_outcomes / sample_space) * 100,2)
return(probability)
# Sample Space
cards = 52
# Determine the probability of drawing a heart
hearts = 13
heart_probability = event_probability(hearts, cards)
# Determine the probability of drawing a face card
face_cards = 12
face_card_probability = event_probability(face_cards, cards)
# Determine the probability of drawing the queen of hearts
queen_of_hearts = 1
queen_of_hearts_probability = event_probability(queen_of_hearts, cards)
# Print each probability
print(str(heart_probability) )
print(str(face_card_probability) )
print(str(queen_of_hearts_probability) )
#Probability with Combinations and Permutations
# =============================================
# Permutations Code
import math
n = 4
k = 2
# Determine permutations and print result
Permutations = math.factorial(n) / math.factorial(k)
print(Permutations)
'''
finding the number of starting hand combinations
that can be dealt in Texas Hold’em.
'''
# Combinations Code
n = 52
k = 2
# Determine Permutations
Permutations = math.factorial(n) / math.factorial(n - k)
# Determine Combinations and print result
Combinations = Permutations / math.factorial(k)
print(Combinations)
'''
the probability of drawing an Ace on the second draw,
if the first card drawn was either a King or an Ace:
'''
# Sample Space
cards = 52
cards_drawn = 1
cards = cards - cards_drawn
# Determine the probability of drawing an Ace after drawing a King on the first draw
aces = 4
ace_probability1 = event_probability(aces, cards)
# Determine the probability of drawing an Ace after drawing an Ace on the first draw
aces_drawn = 1
aces = aces - aces_drawn
ace_probability2 = event_probability(aces, cards)
# Print each probability
print(ace_probability1)
print(ace_probability2)
#### simulating a coin toss
import random
def coin_trial():
heads = 0
for i in range(100):
if random.random() <= 0.5:
heads +=1
return heads
def simulate(n):
trials = []
for i in range(n):
trials.append(coin_trial())
return(sum(trials)/n)
simulate(10)