Working with python 2.7.
The following code allows me to input the winning percentage of two teams (WP_1 and WP_2) a number of wins (k) and determine given the two team's winning percentages, the probability that team one will have more wins at the end of the season (Playoff_Probability):
def PlayoffProb(WP_1, k, WP_2):
TProb_2 = 0
p = float(WP_1)/1000
q = float(WP_2)/1000
n = 162.0
G = math.factorial(n)/(math.factorial(k)*math.factorial(n-k))
Prob = G*(p**k)*((1-p)**(n-k))
for c in range(0, k):
G_2 = math.factorial(n)/(math.factorial(c)*math.factorial(n-c))
Prob_2 = G_2*(q**c)*(1-q)**(n-c)
TProb_2 += Prob_2
Playoff_Probability = Prob*TProb_2
print Playoff_Probability
print TProb_2
But what would be a lot easier is if the function could be written recursively so that it would perform the same operation over every possible value of k and return the total probability of ending the season with more wins (which I believe should be given by the Playoff_Probability for each value run through the function of k, which I've tried to set equal to Total_Playoff_Probability).
I've tried the following code, but I get a TypeError telling me that 'float' object is not callable at the return Total_Playoff_Probability step. I'm also not at all sure that I've set up the recursion appropriately.
def PlayoffProb2(WP_1, k, WP_2):
TProb_2 = 0
Total_Playoff_Probability = 0
p = float(WP_1)/1000
q = float(WP_2)/1000
n = 162.0
G = math.factorial(n)/(math.factorial(k)*math.factorial(n-k))
Prob = G*(p**k)*(开发者_如何学编程(1-p)**(n-k))
for c in range(0, k):
G_2 = math.factorial(n)/(math.factorial(c)*math.factorial(n-c))
Prob_2 = G_2*(q**c)*(1-q)**(n-c)
TProb_2 += Prob_2
Playoff_Probability = Prob*TProb_2
Total_Playoff_Probability += Playoff_Probability
if k == 162:
return Total_Playoff_Probability
else:
return PlayoffProb2(WP_1, k+1, WP_2)
Any help would be greatly appreciated!
return Total_Playoff_Probability(WP_1, k+1, WP_2)
I think you meant
return PlayoffProb2(WP_1, k+1, WP_2)
You've got that error because you are trying to treat a floating point number as a function. Obviously, that doesn't compute.
EDIT
Actually, it should be:
return Total_Playoff_Probability + PlayoffProb2(WP_1, k+1, WP_2)
As it is, you aren't doing anything with Total_Playoff_Probability after you calculate it. If k != 167, you just return the value for k+1.
You've called your function PlayoffProb2
. You must use that name when you recurse.
精彩评论