Created
September 16, 2014 16:00
-
-
Save sixmanguru/70b3e04ea4c8de6cae3e to your computer and use it in GitHub Desktop.
Generic Series Probabilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## calculate the probability of winning a series, | |
## given p(winning single game) and current score | |
## Granger Huntress | |
## based on Jeff Sackmann's gameProb code | |
## (https://gist.github.com/JeffSackmann/768862) | |
def fact(x): | |
if x in [0, 1]: return 1 | |
r = 1 | |
for a in range(1, (x+1)): r = r*a | |
return r | |
def ch(a, b): | |
return fact(a)/(fact(b)*fact(a-b)) | |
def seriesOutcome(s, a, b): | |
return ch((a+b), a)*(s**a)*((1-s)**b)*s | |
def seriesProb(s, v=0, w=0, g=1): | |
## function calculates the probability of team(s) winning | |
## a series, given p(winning a given game) [s], | |
## and the current series score. | |
## v, w = current series score | |
## [g] is the number of games to clinch the series | |
## check if series is clinched: | |
win = [] | |
if v == g: | |
return 1,0 | |
elif w == g: | |
return 0,1 | |
else: pass | |
## specific probabilities on favorite side: | |
for i in range(g): | |
vMax = g-1 | |
if ((g-1 != v or g-1 != w) and w<=i): | |
win.append(seriesOutcome(s, vMax-v, i-w)) | |
elif g-v != 1 or g-w != 1: | |
win.append(0) | |
lose = [] | |
## specific probabilities on the dog side: | |
for i in range(g): | |
wMax = g-1 | |
if ((g-1 != v or g-1 != w) and v<=i): | |
lose.append(seriesOutcome(1-s, wMax-w, i-v)) | |
elif g-v != 1 or g-w != 1: | |
lose.append(0) | |
## if we get to the final game | |
if g-v == 1 and g-w == 1: | |
a = 0 | |
b = 0 | |
d = ch((a+b), a)*(s**a)*((1-s)**b) | |
win.append(d*s) | |
lose.append(d*(1-s)) | |
## returns the individual series probabilities | |
## as well as the total for every possible outcome | |
## the sum part can be eliminated, as it is just a sanity | |
## check to make sure it all works | |
return win,sum(win),lose,sum(lose) | |
print seriesProb(.54,0,0,4) | |
## for the example seriesProb(.54,0,0,4), where team A has a 54% | |
## chance to win a particular game, the series is just beginning | |
## and it is a best of 7 series. | |
## this will return probability Team A exactly wins 4-0, 4-1, 4-2, 4-3 | |
## then the total probability Team A wins the series | |
## then the probability Team A loses the series exactly 0-4, 1-4, 2-4, 3-4 | |
## and the overall probability they lose the series |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment