我正试图弄清A队赢得壁球比赛的可能性。两个团队都有3名成员:
A队:有能力(r)40,50和70 - B队有能力(r)75,25和30。
赢得至少两场比赛的球队赢得比赛。如果A队按照上面给出的顺序进行比赛,那么teamB会选择一个随机顺序:
(a)估计TeamA获胜的可能性
(b)如果比赛在一支球队赢得两场比赛后结束,那么预期的比赛次数是多少。
我用这个等式来计算A队获胜一轮的概率:( A队获胜的可能性)= rA /(rA + rB)
到目前为止,我刚刚尝试计算A队获胜的机会。
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)
打印哪些:
Chances of team A winning = 56.04 %
我不确定这是否正确,我想知道我是否可以获得(b)部分的任何帮助,因为我不知道从哪里开始
答案 0 :(得分:1)
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()
答案 1 :(得分:0)
你在计算的是来自A队的随机玩家将击败来自B队的随机玩家的几率。
一旦你考虑到球队之间可能的配对,球队A击败B队随机排序的几率就变为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()