Python:赢得决胜局比赛的条件概率

时间:2018-05-25 09:57:32

标签: recursion python-3.6 probability

这个问题再次被问到(Excel: Conditional probabilities of winning a tiebreaker game),但OP想要一个基于Excel的答案,而他/她得到的答案并不准确。

我正在尝试计算赢得决胜局比赛的条件概率,如{21}中http://strategicgames.com.au/PhD.pdf所述。

它需要编码两个递归公式: Formulas (由于低代表,我无法附上图片)

这是我迄今为止所做的:

def prob_tiebraker_game_A(Pa, Pb, a, b):
    if a == 7 and b >= 0 and b <=5:
        return 1
    elif b == 7 and a >= 0 and a <=5:
        return 0
    elif a == 6 and b == 6:
        return (Pa*(1-Pb))/(Pa*(1-Pb) + (1-Pa)*Pb)
    elif (a+b) % 2 ==0:
        return Pa*prob_tiebraker_game_B(Pa, Pb, a+1, b) + (1-Pa)*prob_tiebraker_game_B(Pa, Pb, a,b+1)
    elif (a+b) % 2 !=0:
        return Pa*prob_tiebraker_game_A(Pa, Pb, a+1, b) + (1-Pa)*prob_tiebraker_game_A(Pa, Pb, a,b+1)



 def prob_tiebraker_game_B(Pa, Pb, a, b):
    if b == 7 and a >= 0 and a <=5:
        return 1
    elif a == 7 and b >= 0 and b <=5:
        return 0
    elif a == 6 and b == 6:
        return (Pb*(1-Pa))/(Pb*(1-Pa) + (1-Pb)*Pa)
    elif (a+b) % 2 ==0:
        return Pb*prob_tiebraker_game_A(Pa, Pb, a+1, b) + (1-Pb) * prob_tiebraker_game_A(Pa, Pb, a,b+1)
    elif (a+b) % 2 !=0:
        return Pb*prob_tiebraker_game_B(Pa, Pb, a+1, b) + (1-Pb) *  prob_tiebraker_game_B(Pa, Pb, a,b+1)

对于Pa = 0.62,Pb = 0.6的值,我应该得到以下结果: Results

但是我得到(a,b)除(0,0)和(6,6),(7,。)和(。,7)之外的任何值的错误数字,因为这些分数取决于第一个这些函数中的3个条件很容易评估。

我尝试修改其他条件但没有成功。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您只需要一次递归。注意(a+b) mod 2标识当前正在播放的播放器。因此,应该相应地使用该玩家获胜/未获胜的概率。当玩家A正在服务时,这是解决方案。

def prob_tiebraker_game(Pa, Pb, a, b):
    if a == 7 and b >= 0 and b <=5:
        return 1
    elif b == 7 and a >= 0 and a <=5:
        return 0
    elif a == 6 and b == 6:
        return (Pa*(1-Pb))/(Pa*(1-Pb) + (1-Pa)*Pb)
    elif (a+b) % 2 != 0:
        # if player A wins (probability Pa) a increases by 1, b remains same
        # if player A doesn't win (probability 1-Pa) b increases by 1, a remains same
        return Pa*prob_tiebraker_game(Pa, Pb, a+1, b) + (1-Pa)*prob_tiebraker_game(Pa, Pb, a, b+1)
    elif (a+b) % 2 == 0:
        # if player B wins (probability Pb) b increases by 1, a remains same
        # if player B doesn't win (probability 1-Pb) a increases by 1, b remains same
        return Pb*prob_tiebraker_game(Pa, Pb, a, b+1) + (1-Pb)*prob_tiebraker_game(Pa, Pb, a+1, b)

当玩家B服务时,最后两个条件将被交换。您可以为此编写另一个函数,或者通过向函数添加一个额外的参数将两者合并为一个函数来指示玩家A是否首先服务。