Python:赢得决胜局的条件概率
Python: Conditional probabilities of winning a tiebreaker game
这个问题又被问到了 (Excel: Conditional probabilities of winning a tiebreaker game),但 OP 想要一个基于 Excel 的答案,而 s/he 得到的答案并不准确。
我正在尝试计算赢得决胜局比赛的条件概率,如 http://strategicgames.com.au/PhD.pdf,第 21-22 页所述。
需要编写两个递归公式:
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
但是除了 (0,0) 和 (6,6)、(7,.) 和 (.,7) 之外,我得到的 (a,b) 的任何值都是错误的,因为这些分数取决于函数中的前 3 个条件很容易评估。
我尝试修改其他条件但没有成功。任何帮助将不胜感激
你只需要一个递归。注意 (a+b) mod 2
确定哪个玩家正在发球。所以玩家 winning/not 获胜的概率应该使用 accordingly.Here 玩家 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 是否先发球,将两者合并为一个。
这个问题又被问到了 (Excel: Conditional probabilities of winning a tiebreaker game),但 OP 想要一个基于 Excel 的答案,而 s/he 得到的答案并不准确。
我正在尝试计算赢得决胜局比赛的条件概率,如 http://strategicgames.com.au/PhD.pdf,第 21-22 页所述。
需要编写两个递归公式: 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
但是除了 (0,0) 和 (6,6)、(7,.) 和 (.,7) 之外,我得到的 (a,b) 的任何值都是错误的,因为这些分数取决于函数中的前 3 个条件很容易评估。
我尝试修改其他条件但没有成功。任何帮助将不胜感激
你只需要一个递归。注意 (a+b) mod 2
确定哪个玩家正在发球。所以玩家 winning/not 获胜的概率应该使用 accordingly.Here 玩家 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 是否先发球,将两者合并为一个。