Programming with event and sample spaces (Python)
Let us go back to probability and do a few examples where we find exact values of some probabilities. Our approach will be finding exact solutions to enumeration problems by creating lists or vectors of events and sample spaces.
Probability of having at least one six when we roll two dice
sample = [(dice1,dice2) for dice1 in range(1,7) \
for dice2 in range(1,7)]
event = [roll for roll in sample if roll[0] == 6 or \
roll[1] == 6]
The result is found by
print(len(event), "/", len(sample))
Probability of having no sixes when we roll two dice
event = [roll for roll in sample \
if roll[1] != 6 and roll[0] != 6]
Probability of having sum 6 when we roll two dice
event = [roll for roll in sample if roll[1] + roll[0] == 6]
Probability of having sum 10 if we roll three dice
r = range(1,7)
sample = [(i,j,k) for i in r for j in r for k in r]
event = [roll for roll in sample if sum(roll)==10]
Probability of rolling sum 13 if we roll four dice
r = range(1,7)
sample = [(i,j,k,l) for i in r for j in r \
for k in r for l in r]
event = [roll for roll in sample if sum(roll)==13]
Probability of having one six when we roll four dice
def numsix(roll):
return len([dice for dice in roll if dice == 6])
event = [roll for roll in sample if numsix(roll)==1]
Probability of having four sixes when we roll eight dice
sample = [[i] for i in range(1,7)]
for i in range(7):
sample = [x + [i] for x in sample for i in range (1,7)]
event = [roll for roll in sample if numsix(roll)==4]
There are several algorithms to generate a sample space for an unordered selection without repetition. Fortunately, these are already implemented in the Python library. We need
from itertools import combinations, product, permutations
Lets do some card probabilities using itertools.combinations
Probability of drawing four aces
cards = list(it.product(range(1,5), range(1,14)))
sample = list(it.combinations(cards, 5))
def numval(hand, val):
return len([card for card in hand if card[1] == val])
def numace(hand):
return numval(hand, 1)
event = [hand for hand in sample if numace(hand)==4]
Probability of drawing 2 aces and two jacks
def numjack(hand):
return numval(hand,11)
event = [hand for hand in sample \
if numace(hand)==2 and numjack(hand)==3]
Probability of all having all of the same suit
def numsuit(hand, suit):
return len([card for card in hand if card[0]==suit])
event = [hand for hand in sample \
if numsuit(hand, hand[0][0]) == 5]
Probability of a pair
event = [hand for hand in sample \
if sum([numval(hand,hand[i][1]) \
for i in range(5)]) == 7]
Finally we consider queue-probabilities. We consider $6$ people labeled $0,\cdots, 5$ placed in a queue of length $6$.
We create the sample space by
sample = list(it.permutations(range(6)));
Note first that Python produces permutations for us with a function from the itertools library. Second, the sample space has two interpretations, sample[i] = j can mean that i has position j in the queue, or j has position i in the queue. These interpretations are equivalent, and we will use both in the solutions below. We compute the event and print out the solution.
Probability of 0 the front
event = [queue for queue in sample if queue[0] == 0]
Probability that 0 is not at the back
event = [queue for queue in sample if queue[-1] != 0]
Note that queue[-1] is the entry at the back of queue, and that queue[i]=j means that person j is at location i, in this solution.
Probability that 0 is ahead of 1
event = [queue for queue in sample if queue[0] < queue[1]]
Here we switch point of view on the sample space, so queue[0] now is the position of person 0.
Probability that a is in front of b
event = [queue for queue in sample if queue[0] < 2 and
queue[1] < 2]
What is the interpretation of queue[i] in this example?
Probability that a and b are both in front of c and d
event = [queue for queue in sample if queue[0] < queue[2]\
and queue[0] < queue[3]\
and queue[1] < queue[2]\
and queue[1] < queue[3]]