计算团队获胜的概率

时间:2013-12-02 01:40:50

标签: python probability

我正试图弄清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)部分的任何帮助,因为我不知道从哪里开始

2 个答案:

答案 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)