Reputation: 49
I'm trying to work out the probability of team A winning a game of squash. Both teams have 3 members:
Team A: Have abilities(r) 40, 50 and 70 - Team B have abilities(r) 75, 25 and 30.
The team winning at least two games wins the match. If team A play in the order given above and teamB pick a random order:
(a) Estimate the probability of TeamA winning
(b) If the match ends as soon as one team has won two games, what is the expected number of games played.
I have used the equation to work out probability of team A winning one round: (Probability of Team A winning) = rA / (rA + rB)
So far I have just tried to calculate the chance of Team A winning.
import random
def game(a,b,c,d,e,f):
overallprob = 0
for item in [a, b, c]:
probA = item / (item + d)
overallprob = overallprob + probA
for item in [a, b, c]:
probA = item / (item + e)
overallprob = overallprob + probA
for item in [a, b, c]:
probA = item / (item + f)
overallprob = overallprob + probA
print "Chances of team A winning =",round((overallprob / 9*100),2),"%"
game(40.0,50.0,60.0,75.0,25.0,30.0)
Which prints:
Chances of team A winning = 56.04 %
I am not sure if this is correct and I was wondering if I could get any help with part (b) as I'm not sure where to begin
Upvotes: 1
Views: 2733
Reputation: 42421
from itertools import permutations, product
def main():
teamA = [40, 50, 70]
teamB = [75, 25, 30]
# Compute two averages by processing every possible match:
# pa Probability that Team A wins a match.
# ng Expected N of games in a match.
tot_pa, tot_ng, n = (0, 0, 0)
for As, Bs in product(permutations(teamA), permutations(teamB)):
pa, ng = prob_a_wins(As, Bs)
tot_pa += pa
tot_ng += ng
n += 1
print tot_pa / n # 0.61233
print tot_ng / n # 2.50580
def prob_a_wins(As, Bs):
# Probabilities that Team A wins game 1, 2, 3, and the match.
g1, g2, g3 = [ a / float(a + b) for a, b in zip(As, Bs) ]
pa = (
g1 * g2 + # win g1 and g2
g1 * (1 - g2) * g3 + # win g1 and g3
(1 - g1) * g2 * g3 # win g2 and g3
)
# Probabability of a two-game match, and expected N of games.
two = (
g1 * g2 + # win g1 and g2
(1 - g1) * (1 - g2) # lose g1 and g2
)
ng = two * 2 + (1 - two) * 3
return (pa, ng)
main()
Upvotes: 1
Reputation: 56714
What you are calculating are the odds that a random player from team A will beat a random player from team B.
Once you take into account the possible pairings between teams, the odds that team A will beat a random ordering of team B becomes 61.23%
from itertools import permutations
def match_win_prob(ra, rb):
"""
Given the skills of players a and b,
return the probability of player a beating player b
"""
return ra / float(ra + rb)
def team_win_prob(x, y, z):
"""
Given the probability of a win in each match,
return the probability of being first to two games
(same as best-of-three)
"""
return x*y + x*(1.-y)*z + (1.-x)*y*z
def game_win_prob(As, Bs):
pairs = [[match_win_prob(a, b) for b in Bs] for a in As]
ways, prob = 0, 0.
for i,j,k in permutations(pairs):
ways += 1
prob += team_win_prob(i[0], j[1], k[2])
return prob / ways
def main():
p = game_win_prob([40, 50, 70], [75, 25, 30])
print('Chances of team A winning = {:.2f} %'.format(p * 100.))
if __name__=="__main__":
main()
Upvotes: 0