from pylab import * # library for making plots
# the probability that Alice wins a playoff of n matches, n is odd
def playoffs_prob(n, p):
term = p ** n
prob = term
for k in range(1, (n + 1)/2):
term = term * (n - k + 1) * (1 - p) / (k * p)
prob = prob + term
return prob
# plot the probability of Alice winning a playoff for 1, 3, ..., up to n matches
def plot_playoffs_prob(n, p):
matches = range(1, n, 2)
plot(matches, map(lambda n: playoffs_prob(n, p), matches), 'x')
ylim([p, 1.0]) # sets the boundary values of the y axis
show() # displays the plot