Laboratory 10: "What Happens in Lab 10, Stays in Lab 10"

In [3]:
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
DESKTOP-EH6HD63
desktop-eh6hd63\farha
C:\Users\Farha\Anaconda3\python.exe
3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)

Full name:

R#:

Title of the notebook:

Date:


Python for Probability

Important Terminology:

Experiment: An occurrence with an uncertain outcome that we can observe.
For example, rolling a die.
Outcome: The result of an experiment; one particular state of the world. What Laplace calls a "case."
For example: 4.
Sample Space: The set of all possible outcomes for the experiment.
For example, {1, 2, 3, 4, 5, 6}.
Event: A subset of possible outcomes that together have some property we are interested in.
For example, the event "even die roll" is the set of outcomes {2, 4, 6}.
Probability: As Laplace said, the probability of an event with respect to a sample space is the number of favorable cases (outcomes from the sample space that are in the event) divided by the total number of cases in the sample space. (This assumes that all outcomes in the sample space are equally likely.) Since it is a ratio, probability will always be a number between 0 (representing an impossible event) and 1 (representing a certain event).
For example, the probability of an even die roll is 3/6 = 1/2.

From https://people.math.ethz.ch/~jteichma/probability.html

In [29]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt  

Example: In a game of Russian Roulette, the chance of surviving each round is 5/6 which is almost 83%. Using a for loop, compute probability of surviving

  • For 2 rounds
  • For 5 rounds
  • For 10 rounds
In [30]:
nrounds =[]
probs =[]

for i in range(3):
    nrounds.append(i)
    probs.append((5/6)**i) #probability of surviving- not getting the bullet!

RRDF = pd.DataFrame({"# of Rounds": nrounds, "Probability of Surviving": probs})
RRDF
Out[30]:
# of Rounds Probability of Surviving
0 0 1.000000
1 1 0.833333
2 2 0.694444
In [31]:
nrounds =[]
probs =[]

for i in range(6):
    nrounds.append(i)
    probs.append((5/6)**i) #probability of surviving- not getting the bullet!

RRDF = pd.DataFrame({"# of Rounds": nrounds, "Probability of Surviving": probs})
RRDF
Out[31]:
# of Rounds Probability of Surviving
0 0 1.000000
1 1 0.833333
2 2 0.694444
3 3 0.578704
4 4 0.482253
5 5 0.401878
In [32]:
nrounds =[]
probs =[]

for i in range(11):
    nrounds.append(i)
    probs.append((5/6)**i) #probability of surviving- not getting the bullet!

RRDF = pd.DataFrame({"# of Rounds": nrounds, "Probability of Surviving": probs})
RRDF
Out[32]:
# of Rounds Probability of Surviving
0 0 1.000000
1 1 0.833333
2 2 0.694444
3 3 0.578704
4 4 0.482253
5 5 0.401878
6 6 0.334898
7 7 0.279082
8 8 0.232568
9 9 0.193807
10 10 0.161506
In [33]:
RRDF.plot.scatter(x="# of Rounds", y="Probability of Surviving",color="red")
Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0x2c7f96de308>

Example: What will be the probability of constantly throwing an even number with a D20 in

  • For 2 rolls
  • For 5 rolls
  • For 10 rolls
  • For 15 rolls
In [35]:
nrolls =[]
probs =[]

for i in range(1,3,1):
    nrolls.append(i)
    probs.append((1/2)**i) #probability of throwing an even number-10/20 or 1/2

DRDF = pd.DataFrame({"# of Rolls": nrolls, "Probability of constantly throwing an even number": probs})
DRDF
Out[35]:
# of Rolls Probability of constantly throwing an even number
0 1 0.50
1 2 0.25
In [6]:
DRDF.plot.scatter(x="# of Rolls", y="Probability of constantly throwing an even number",color="crimson")
Out[6]:
<AxesSubplot:xlabel='# of Rolls', ylabel='Probability of constantly throwing an even number'>
Out[6]:

Example: What will be the probability of throwing at least one 6 with a D6:

  • For 2 rolls
  • For 5 rolls
  • For 10 rolls
  • For 50 rolls - Make a scatter plot for this one!
In [36]:
nRolls =[]
probs =[]

for i in range(1,3,1):
    nRolls.append(i)
    probs.append(1-(5/6)**i) #probability of at least one 6: 1-(5/6)

rollsDF = pd.DataFrame({"# of Rolls": nRolls, "Probability of rolling at least one 6": probs})
rollsDF
Out[36]:
# of Rolls Probability of rolling at least one 6
0 1 0.166667
1 2 0.305556
In [37]:
nRolls =[]
probs =[]

for i in range(1,6,1):
    nRolls.append(i)
    probs.append(1-(5/6)**i) #probability of at least one 6: 1-(5/6)

rollsDF = pd.DataFrame({"# of Rolls": nRolls, "Probability of rolling at least one 6": probs})
rollsDF
Out[37]:
# of Rolls Probability of rolling at least one 6
0 1 0.166667
1 2 0.305556
2 3 0.421296
3 4 0.517747
4 5 0.598122
In [38]:
nRolls =[]
probs =[]

for i in range(1,11,1):
    nRolls.append(i)
    probs.append(1-(5/6)**i) #probability of at least one 6: 1-(5/6)

rollsDF = pd.DataFrame({"# of Rolls": nRolls, "Probability of rolling at least one 6": probs})
rollsDF
Out[38]:
# of Rolls Probability of rolling at least one 6
0 1 0.166667
1 2 0.305556
2 3 0.421296
3 4 0.517747
4 5 0.598122
5 6 0.665102
6 7 0.720918
7 8 0.767432
8 9 0.806193
9 10 0.838494
In [39]:
nRolls =[]
probs =[]

for i in range(1,51,1):
    nRolls.append(i)
    probs.append(1-(5/6)**i) #probability of at least one 6: 1-(5/6)

rollsDF = pd.DataFrame({"# of Rolls": nRolls, "Probability of rolling at least one 6": probs})
In [40]:
rollsDF.plot.scatter(x="# of Rolls", y="Probability of rolling at least one 6")
Out[40]:
<matplotlib.axes._subplots.AxesSubplot at 0x2c7f9797c88>

Example: What is the probability of drawing an ace at least once (with replacement):

  • in 2 tries
  • in 5 tries
  • in 10 tries
  • in 20 tries - make a scatter plot.
In [41]:
nDraws =[]
probs =[]

for i in range(1,3,1):
    nDraws.append(i)
    probs.append(1-(48/52)**i) #probability of drawing an ace least once : 1-(48/52)

DrawsDF = pd.DataFrame({"# of Draws": nDraws, "Probability of drawing an ace at least once": probs})
DrawsDF
Out[41]:
# of Draws Probability of drawing an ace at least once
0 1 0.076923
1 2 0.147929
In [42]:
nDraws =[]
probs =[]

for i in range(1,6,1):
    nDraws.append(i)
    probs.append(1-(48/52)**i) #probability of drawing an ace least once : 1-(48/52)

DrawsDF = pd.DataFrame({"# of Draws": nDraws, "Probability of drawing an ace at least once": probs})
DrawsDF
Out[42]:
# of Draws Probability of drawing an ace at least once
0 1 0.076923
1 2 0.147929
2 3 0.213473
3 4 0.273975
4 5 0.329823
In [43]:
nDraws =[]
probs =[]

for i in range(1,11,1):
    nDraws.append(i)
    probs.append(1-(48/52)**i) #probability of drawing an ace least once : 1-(48/52)

DrawsDF = pd.DataFrame({"# of Draws": nDraws, "Probability of drawing an ace at least once": probs})
DrawsDF
Out[43]:
# of Draws Probability of drawing an ace at least once
0 1 0.076923
1 2 0.147929
2 3 0.213473
3 4 0.273975
4 5 0.329823
5 6 0.381375
6 7 0.428962
7 8 0.472888
8 9 0.513435
9 10 0.550863
In [44]:
nDraws =[]
probs =[]

for i in range(1,21,1):
    nDraws.append(i)
    probs.append(1-(48/52)**i) #probability of drawing an ace at least once : 1-(48/52)

DrawsDF = pd.DataFrame({"# of Draws": nDraws, "Probability of drawing an ace at least once": probs})
DrawsDF
Out[44]:
# of Draws Probability of drawing an ace at least once
0 1 0.076923
1 2 0.147929
2 3 0.213473
3 4 0.273975
4 5 0.329823
5 6 0.381375
6 7 0.428962
7 8 0.472888
8 9 0.513435
9 10 0.550863
10 11 0.585412
11 12 0.617303
12 13 0.646742
13 14 0.673915
14 15 0.698999
15 16 0.722153
16 17 0.743525
17 18 0.763254
18 19 0.781466
19 20 0.798276
In [45]:
DrawsDF.plot.scatter(x="# of Draws", y="Probability of drawing an ace at least once")
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x2c7f95bb388>

Example:

  • A) Write a function to find the probability of an event in percentage form based on given outcomes and sample space
  • B) Use the function and compute the probability of rolling a 4 with a D6
  • C) Use the function and compute the probability of drawing a King from a standard deck of cards
  • D) Use the function and compute the probability of drawing the King of Hearts from a standard deck of cards
  • E) Use the function and compute the probability of drawing an ace after drawing a king
  • F) Use the function and compute the probability of drawing an ace after drawing an ace
  • G) Use the function and compute the probability of drawing a heart OR a club
  • F) Use the function and compute the probability of drawing a Royal Flush
    *hint: (in poker) a straight flush including ace, king, queen, jack, and ten all in the same suit, which is the hand of the highest possible value

This problem is designed based on an example by Daniel Poston from DataCamp, accessible @ https://www.datacamp.com/community/tutorials/statistics-python-tutorial-probability-1

In [46]:
# A
# Create function that returns probability percent rounded to one decimal place
def Prob(outcome, sampspace):
    probability = (outcome / sampspace) * 100
    return round(probability, 1)
In [47]:
# B
outcome = 1       #Rolling a 4 is only one of the possible outcomes
space = 6         #Rolling a D6 can have 6 different outcomes
Prob(outcome, space)
Out[47]:
16.7
In [48]:
# C
outcome = 4       #Drawing a king is four of the possible outcomes
space = 52        #Drawing from a standard deck of cards can have 52 different outcomes
Prob(outcome, space)
Out[48]:
7.7
In [49]:
# D
outcome = 1       #Drawing the king of hearts is only 1 of the possible outcomes
space = 52        #Drawing from a standard deck of cards can have 52 different outcomes
Prob(outcome, space)
Out[49]:
1.9
In [50]:
# E
outcome = 4       #Drawing an ace is 4 of the possible outcomes
space = 51        #One card has been drawn
Prob(outcome, space)
Out[50]:
7.8
In [51]:
# F
outcome = 3       #Once Ace is already drawn
space = 51        #One card has been drawn
Prob(outcome, space)
Out[51]:
5.9
In [52]:
# G
hearts = 13       #13 cards of hearts in a deck
space = 52        #total number of cards in a deck
clubs = 13        #13 cards of clubs in a deck
Prob_heartsORclubs= Prob(hearts, space) + Prob(clubs, space)
print("Probability of drawing a heart or a club is",Prob_heartsORclubs,"%")
Probability of drawing a heart or a club is 50.0 %
In [53]:
# F
draw1 = 5       #5 cards are needed
space1 = 52        #out of the possible 52 cards
draw2 = 4       #4 cards are needed
space2 = 51        #out of the possible 51 cards
draw3 = 3       #3 cards are needed
space3 = 50        #out of the possible 50 cards
draw4 = 2       #2 cards are needed
space4 = 49        #out of the possible 49 cards
draw5 = 1       #1 cards is needed
space5 = 48        #out of the possible 48 cards

#Probability of a getting a Royal Flush
Prob_RF= 4*(Prob(draw1, space1)/100) * (Prob(draw2, space2)/100) * (Prob(draw3, space3)/100) * (Prob(draw4, space4)/100) * (Prob(draw5, space5)/100)     
print("Probability of drawing a royal flush is",Prob_RF,"%")
Probability of drawing a royal flush is 1.5473203199999998e-06 %

Example: Two unbiased dice are thrown once and the total score is observed. Define an appropriate function and use a simulation to find the estimated probability that :

  • the total score is greater than 10?
  • the total score is even and greater than 7?

This problem is designed based on an example by Elliott Saslow from Medium.com, accessible @ https://medium.com/future-vision/simulating-probability-events-in-python-5dd29e34e381

In [54]:
import numpy as np
def DiceRoll1(nSimulation):
    count =0
    dice = np.array([1,2,3,4,5,6])         #create a numpy array with values of a D6
    for i in range(nSimulation):
        die1 = np.random.choice(dice,1)    #randomly selecting a value from dice - throw the D6 once
        die2 = np.random.choice(dice,1)    #randomly selecting a value from dice - throw the D6 once again!
        score = die1 + die2                #summing them up
        if score > 10:                     #if it meets our desired condition:
            count +=1                      #add one to the "count"
    return count/nSimulation               #compute the probability of the desired event by dividing count by the total number of trials

nSimulation = 10000
print("The probability of rolling a number greater than 10 after",nSimulation,"rolld is:",DiceRoll1(nSimulation)*100,"%")
The probability of rolling a number greater than 10 after 10000 rolld is: 8.25 %
In [55]:
import numpy as np
def DiceRoll2(nSimulation):
    count =0
    dice = np.array([1,2,3,4,5,6])         #create a numpy array with values of a D6
    for i in range(nSimulation):
        die1 = np.random.choice(dice,1)    #randomly selecting a value from dice - throw the D6 once
        die2 = np.random.choice(dice,1)    #randomly selecting a value from dice - throw the D6 once again!
        score = die1 + die2
        if score %2 ==0 or score > 7:      #the total score is even and greater than 7
            count +=1
    return count/nSimulation

nSimulation = 10000
print("The probability of rolling an even number or greater than 7 after",nSimulation," rolls is:",DiceRoll2(nSimulation)*100,"%")
The probability of rolling an even number or greater than 7 after 10000  rolls is: 66.43 %

Example: An urn contains 10 white balls, 20 reds and 30 greens. We want to draw 5 balls with replacement. Use a simulation (10000 trials) to find the estimated probability that:

  • we draw 3 white and 2 red balls
  • we draw 5 balls of the same color

This problem is designed based on an example by Elliott Saslow from Medium.com, accessible @ https://medium.com/future-vision/simulating-probability-events-in-python-5dd29e34e381

In [56]:
# A
import numpy as np
import random
d = {}                     #Create an empty dictionary to associate numbers and colors
for i in range(0,60,1):     #total of 60 balls
    if i <10:                 #10 white balls
        d[i]="White"
    elif i>9 and i<30:         #20 red balls
        d[i]="Red"
    else:                         #60-30=30 green balls
        d[i]="Green"
#
nSimulation= 10000         #How many trials?
outcome1= 0                #initial value on the desired outcome counter

for i in range(nSimulation):
    draw=[]                     #an empty list for the draws
    for i in range(5):                               #how many balls we want to draw?
        draw.append(d[random.randint(0,59)])         #randomly choose a number from 0 to 59- simulation of drawing balls
    drawarray = np.array(draw)                       #convert the list into a numpy array
    white = sum(drawarray== "White")                 #count the white balls
    red = sum(drawarray== "Red")                     #count the red balls
    green = sum(drawarray== "Green")                 #count the green balls
    if white ==3 and red==2:                         #If the desired condition is met, add one to the counter
        outcome1 +=1
print("The probability of drawing 3 white and 2 red balls is",(outcome1/nSimulation)*100,"%")
The probability of drawing 3 white and 2 red balls is 0.5499999999999999 %
In [57]:
# B
import numpy as np
import random
d = {}
for i in range(0,60,1):
    if i <10:
        d[i]="White"
    elif i>9 and i<30:
        d[i]="Red"
    else:
        d[i]="Green"
#
nSimulation= 10000
outcome1= 0
outcome2= 0         #we can consider multiple desired outcomes


for i in range(nSimulation):
    draw=[]
    for i in range(5):
        draw.append(d[random.randint(0,59)])
    drawarray = np.array(draw)
    white = sum(drawarray== "White")
    red = sum(drawarray== "Red")
    green = sum(drawarray== "Green")
    if white ==3 and red==2:
        outcome1 +=1
    if white ==5 or red==5 or green==5:
        outcome2 +=1

print("The probability of drawing 3 white and 2 red balls is",(outcome1/nSimulation)*100,"%")
print("The probability of drawing 5 balls of the same color is",(outcome2/nSimulation)*100,"%")
The probability of drawing 3 white and 2 red balls is 0.53 %
The probability of drawing 5 balls of the same color is 3.8 %


Here are some of the resources used for creating this notebook:

Here are some great reads on this topic:

Here are some great videos on these topics:



Exercise: Risk or Probability

Are they the same? Are they different? Discuss your opinion.

Make sure to cite any resources that you may use.

In [ ]:
 

In [ ]:
 
In [ ]: