02 Probability
02 Probability
02 Probability
15/05/2021
1. Define function 𝑃
The probability is simply a fraction in which the numerator is the number of
events occurring and the denominator is the total number of cases in the sample
space.
Calculate the probability of an event even when roll a dice.
1 from fractions import Fraction
2
7 D = {1, 2, 3, 4, 5, 6}
8 even = {2, 4, 6}
9
2. Urn Problems
A closed urn has 23 balls: 8 white, 6 blue and 9 red. Randomly select 6 balls
(know that each ball has the same probability of selection). What is the probability
of getting:
1
• Problem 1: all 6 balls are red
Firstly, we set the symbol for each white ball to be ‘W1’ to ‘W8’ with the first
character ‘W’ is the color and the second number character is the order of the ball.
Similarly 6 blue balls are ‘B1’ to ‘B6’ and 9 red balls are ‘R1’ to ‘R9’.
1 urn = cross('W', '12345678 ') | cross ('B', '123456 ') | cross (
'R', '123456789 ')
2 print (urn)
Each event is a set of 6 balls, so the sample space will be the set of all possible
cases of 6 balls.
6 23.22.21.20.19.18
𝑈 6 = 𝐶23 = = 100947
6!
1 import itertools
2
7 U6 = combos (urn , 6)
8
9 print (len(U6))
Take 10 random subsets from set U6.
1 import random
2
[‘R7 W3 B1 R4 B3 W2’,
‘R7 R3 B1 W4 R5 W6’,
‘B5 B4 B6 W1 R3 R2’,
‘W3 B1 R4 W8 R9 W5’,
‘W7 B6 W1 R3 R9 B3’,
‘W3 W1 R8 R2 R1 W5’,
‘B5 R7 R4 W8 R2 W6’,
‘W7 W1 R3 W4 R5 B3’,
‘B4 R7 B2 B1 W6 W2’,
‘W7 B1 W4 W8 W6 W2’]
2
1 red6 = {s for s in U6 if s. count ('R') == 6}
2
3 P(red6 , U6)
Problem 2: 3 blue balls, 2 white balls and 1 red ball
1 b3w2r1 = {s for s in U6 if s. count ('B') == 3 and s. count ('W'
) == 2 and s. count ('R') == 1}
2
3 P(b3w2r1 , U6)
Problem 3: exactly 4 white balls
1 w4 = {s for s in U6 if s. count ('W') == 4}
2
3 P(w4 , U6)
3. Coin experiment
Write a program to calculate the probability of getting the tail when toss a
coin in 𝑛 times. The larger number of tests, the closer to 0.5 we get.
The function 𝑟𝑎𝑛𝑑𝑜𝑚.𝑟𝑎𝑛𝑑𝑖𝑛𝑡(0, 1) returns a random number of 0 or 1. Con-
ventionally, the tail is 1 and the head is 0.
1 import random
2 n = 10
3 count = 0;
4 for simulations in range (n):
5 tosses = random . randint (0, 1)
6 if tosses == 1:
7 count += 1
8 print (count/n)
Running the above program with n is 10, 100, 1000, etc., and observing the
results.
4. Dices experiment
Roll two dices. Write a program to calculate the probability of the even that
the first dice gives 1 and the other gives 6.
The function 𝑟𝑎𝑛𝑑𝑜𝑚.𝑟𝑎𝑛𝑑𝑖𝑛𝑡(1, 6) returns a random integer number from 1
to 6.
1 import random
2 count = 0;
3 n = 1000000;
4 for i in range (n):
5 die1 = random . randint (1, 6)
6 die2 = random . randint (1, 6)
7 if die1 ==1 and die2 ==6:
3
8 count += 1
9 print (count/n)
Running the above program with n is 10, 100, 1000, etc., and observing the
results.
5. Cards experiment
All cards are divided into 4 suits including hearts (♥), diamonds (♦), clubs
(♣) and spades (♠). In each suit there are 13 cards including a 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, J (jack), Q (queen), K (king).
Define cards as:
1 # Import the required function libraries
2 from itertools import product
3
6. Exercises
1. Write a simulator function of rolling two dices n times. Calculate the experi-
mental probability of the event that both dices are even.
1 def simualtor_dice1(n):
2 # your code
2. Write a simulator function of rolling two dices n times. Calculate the experi-
mental probability of the event that one dice is even and the other is odd.
4
1 def simualtor_dice2(n):
2 # your code
3. Write a simulator function of rolling two dices n times. Calculate the experi-
mental probability of the event that two dices are the same.
1 def simualtor_dice3(n):
2 # your code
4. Write a simulator function of rolling two dices n times. Calculate the ex-
perimental probability of the event that one dice gives 1 and the other gives
6.
1 def simualtor_dice4(n):
2 # your code
5. Write a simulator function of rolling two dices n times. Calculate the experi-
mental probability of the event that sum of two dices larger than 6.
1 def simualtor_dice5(n):
2 # your code
7. Write a simulator function of taking 4 cards from 52-cards deck. Calculate the
experimental probability of the event that 4 cards are all in different types (1
heart (♥), 1 diamond (♦), 1 club (♣), and 1 spade (♠)). Parameter n is the
number of trials.
1 def simualtor_poker2(n):
2 # your code
8. A closed urn has 23 balls: 8 white, 6 blue and 9 red. Randomly select 6
balls (know that each ball has the same probability of selection). What is the
probability of getting two white, two blue and two red balls.