Random Processes¶
Textbook link: https://inferentialthinking.com/chapters/09/Randomness.html¶
Coin Flipping¶
For one coin flip, P(Heads)=0.5, P(Tails)=0.5
Probability of five consecutive heads is
P(H)xP(H)xP(H)xP(H)xP(H)=1/32
This is same as probability of zero heads in five flips
P(T)xP(T)xP(T)xP(T)xP(T)=1/32
In [2]:
# Only Heads
(1/2)*(1/2)*(1/2)*(1/2)*(1/2)
Out[2]:
0.03125
Probability of exactly one head in 5 flips is
P(H)xP(T)xP(T)xP(T)xP(T)+
P(T)xP(H)xP(T)xP(T)xP(T)+
P(T)xP(T)xP(H)xP(T)xP(T)+
P(T)xP(T)xP(T)xP(H)xP(T)+
P(T)xP(T)xP(T)xP(T)xP(H)+
= 5/32
In [3]:
#Exactly One Heads
((1/2)*(1/2)*(1/2)*(1/2)*(1/2))*5
Out[3]:
0.15625
Probability of exactly two or more heads in 5 flips is
1 - P(zero heads) - P(one head) = 26/32
In [4]:
# Two or more Heads
1-(1/32)-(5/32)
Out[4]:
0.8125
Coin Flipping Simulator¶
In [1]:
import numpy as np
# Flip a coin once
print(np.random.choice(['heads','tails']))
# Flip a coin 5 times
print(np.random.choice(['heads','tails'], 5))
heads ['heads' 'tails' 'tails' 'tails' 'heads']
In [2]:
# Repeat 5 consecutive flips of a coin 10 times
for each_flip in np.arange(10):
outcomes = np.random.choice(['heads','tails'], 5)
print(outcomes)
['tails' 'heads' 'tails' 'tails' 'heads'] ['heads' 'tails' 'tails' 'tails' 'tails'] ['tails' 'heads' 'heads' 'heads' 'tails'] ['tails' 'tails' 'tails' 'tails' 'heads'] ['heads' 'heads' 'heads' 'tails' 'tails'] ['heads' 'heads' 'heads' 'tails' 'tails'] ['tails' 'heads' 'tails' 'tails' 'tails'] ['heads' 'tails' 'heads' 'tails' 'tails'] ['tails' 'heads' 'heads' 'heads' 'tails'] ['tails' 'heads' 'tails' 'tails' 'tails']
In [3]:
# Sum based on truth indices
sum(outcomes=='heads')
Out[3]:
1
In [4]:
# Create an array with number of heads from each run
headcount_list=[]
for each_flip in np.arange(1000):
outcomes = np.random.choice(['heads','tails'], 5)
num_heads = sum(outcomes=='heads')
headcount_list.append(num_heads)
print(headcount_list)
[2, 1, 0, 4, 4, 4, 4, 3, 4, 2, 3, 3, 2, 3, 4, 2, 4, 2, 2, 3, 3, 4, 2, 4, 4, 2, 2, 2, 2, 3, 3, 3, 2, 2, 3, 1, 4, 1, 2, 2, 2, 3, 3, 2, 3, 4, 5, 2, 4, 4, 2, 5, 2, 5, 5, 2, 3, 0, 1, 2, 2, 3, 3, 2, 3, 1, 3, 3, 1, 3, 2, 2, 3, 4, 3, 1, 2, 4, 1, 3, 0, 3, 3, 4, 1, 2, 3, 2, 2, 3, 5, 2, 3, 4, 3, 4, 4, 4, 2, 5, 2, 1, 2, 2, 4, 3, 3, 4, 3, 3, 2, 2, 4, 3, 3, 2, 1, 3, 4, 1, 0, 3, 4, 3, 2, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 1, 2, 0, 2, 2, 3, 2, 2, 4, 3, 3, 3, 1, 2, 2, 3, 1, 3, 4, 2, 1, 2, 1, 2, 3, 1, 2, 1, 3, 2, 3, 3, 3, 2, 2, 3, 3, 2, 4, 2, 0, 2, 3, 2, 2, 3, 1, 4, 2, 3, 1, 3, 2, 2, 3, 3, 3, 3, 3, 3, 2, 1, 1, 2, 3, 1, 2, 2, 2, 5, 3, 4, 3, 2, 3, 5, 4, 1, 3, 3, 2, 2, 4, 3, 1, 2, 4, 2, 4, 1, 0, 3, 0, 1, 3, 0, 4, 2, 3, 4, 3, 2, 2, 3, 5, 1, 3, 2, 4, 2, 3, 4, 2, 2, 4, 4, 3, 4, 3, 5, 4, 1, 1, 4, 1, 4, 2, 1, 3, 5, 3, 2, 2, 3, 4, 2, 3, 3, 1, 1, 3, 1, 3, 2, 3, 3, 3, 3, 2, 3, 2, 1, 3, 2, 3, 2, 1, 4, 2, 2, 2, 2, 3, 3, 1, 1, 2, 1, 2, 2, 3, 3, 2, 1, 3, 0,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, 4, 2, 4, 2, 3, 3]
In [5]:
# Plotting the head counts
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
ax.hist(headcount_list)
Out[5]:
(array([ 33., 0., 149., 0., 317., 0., 329., 0., 136., 36.]), array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ]), <BarContainer object of 10 artists>)
In [6]:
# Proportion of outcomes when all flips were heads
sum(np.array(headcount_list)==5)/1000
Out[6]:
0.036
In [7]:
# How many times did each outcome of total heads occur?
(values, counts) = np.unique(np.array(headcount_list), return_counts = True)
print(values)
print(counts)
[0 1 2 3 4 5] [ 33 149 317 329 136 36]
More than two outcomes¶
Suppose there are equal numbers of balls in a hat. What is an approximate value of getting exactly two blue and two purple balls if you take 7 balls out, replacing after each draw?
In [19]:
match_count = 0
num_simulations = 1000
# Simulate 1000 draws
for each_draw in range(num_simulations):
# Draw 7 times, replacing after each draw
results = np.random.choice(['red', 'blue', 'purple', 'cyan'], 7)
# Count number of blue balls
blue_count = sum(results=='blue')
# Count number of purple balls
purple_count = sum(results=='purple')
# Determine if two blue and two purple draw have been pulled
if (blue_count==2) & (purple_count==2):
# Add to match count if condition is met
match_count = match_count+1
# Calculate probability
prob_estimate = match_count/num_simulations
print(prob_estimate)
0.096