I am trying to work out the probability of 'Susie' winning a match.
Probability of 'Susie' winning a game = 0.837
Probability of 'Bob' winning a game = 0.163
If the first person to win n games wins a match, what is the smallest value of n such that Susie has a better than 0.9 chance of winning the match?
So far I have this code:
import itertools
W = 0.837
L = 0.163
for product in itertools.product(['W','L'], repeat=3): #3=number of games
print product
Which prints:
('W', 'W', 'W')
('W', 'W', 'L')
('W', 'L', 'W')
('W', 'L', 'L')
('L', 'W', 'W')
('L', 'W', 'L')
('L', 'L', 'W')
('L', 'L', 'L')
I then want to use these results to work out the probability of 'Susie' winning the match overall.
I have worked out this problem on paper, the more games played, the more chance there is of 'Susie' winning the match.
You can use a dictionary for probabilities:
import itertools
import operator
probabilities = {'W':0.837, 'L':0.163}
for product in itertools.product(['W','L'], repeat=3): #3=number of games
p = reduce(operator.mul,
[probabilities[p] for p in product])
print product, ":", p
The reduce
function accumulates all elements of a list using function given in the first argument - here we accumulate them by multiplying.
This gives you probabilities of each event sequence. From this you can easily choose which one is "Susie winning a match", and sum the probabilities. One possibility to do this is:
import itertools
import operator
probabilities = {'W':0.837, 'L':0.163}
winProbability = 0
for product in itertools.product(['W','L'], repeat=3): #3=number of games
p = reduce(operator.mul,
[probabilities[p] for p in product])
if product.count('W') > 1: #works only for 3 games
winProbability += p
print "Susie wins:", product, "with probability:", p
else:
print "Susie looses:", product, "with probability:", p
print "Total probability of Susie winning:", winProbability
The condition works only for 3
games, but I'm really leaving this one to you - it's easy to generalize this for n
games :)