# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
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.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
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
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
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
RRDF.plot.scatter(x="# of Rounds", y="Probability of Surviving",color="red")
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
DRDF.plot.scatter(x="# of Rolls", y="Probability of constantly throwing an even number",color="crimson")
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
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
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
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})
rollsDF.plot.scatter(x="# of Rolls", y="Probability of rolling at least one 6")
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
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
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
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
DrawsDF.plot.scatter(x="# of Draws", y="Probability of drawing an ace at least once")
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
# A
# Create function that returns probability percent rounded to one decimal place
def Prob(outcome, sampspace):
probability = (outcome / sampspace) * 100
return round(probability, 1)
# 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)
# 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)
# 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)
# E
outcome = 4 #Drawing an ace is 4 of the possible outcomes
space = 51 #One card has been drawn
Prob(outcome, space)
# F
outcome = 3 #Once Ace is already drawn
space = 51 #One card has been drawn
Prob(outcome, space)
# 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,"%")
# 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,"%")
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
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,"%")
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,"%")
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
# 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,"%")
# 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,"%")
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: