gpt4 book ai didi

python - 计算团队获胜的概率

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:00 25 4
gpt4 key购买 nike

我正在尝试计算 A 队赢得一场 Squash 比赛的概率。两队各有 3 名成员:

A 队:能力为 40、50 和 70 - B 队的能力为 75、25 和 30。

赢得至少两场比赛的球队赢得比赛。如果 A 队按照上面给出的顺序进行比赛,B 队选择随机顺序:

(a) 估计 A 队获胜的概率

(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) 部分的任何帮助,因为我不确定从哪里开始

最佳答案

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()

关于python - 计算团队获胜的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com