patternpythonMinor
Soccer winning probabilies from ELO strength indicator
Viewed 0 times
soccerelostrengthwinningindicatorfromprobabilies
Problem
What can I do to improve this function, which calculates the win expectancy of a soccer team, given their strength:
Especially the two boolean switches look clunky to me, but the explanation is simpler that way. What improvements do you see?
def winning_prob(first, second, first_at_home=False, second_at_home=False):
""" Return winning probability, given ELO ratings of two soccer teams
Args:
first: ELO rating of first team
second: ELO rating of second team
first_at_home: is first team playing at home?
second_at_home: is second team playing at home?
Returns:
Winning probability of first team against second team
"""
if first_at_home:
first = first + 100
if second_at_home:
second = second + 100
difference = (first - second) / 400.0
return 1.0 / (10**(-difference) + 1.0)Especially the two boolean switches look clunky to me, but the explanation is simpler that way. What improvements do you see?
Solution
With the possibility of
The only things I see are:
Here are my improvements:
first_at_home and second_at_home both being True, there really is not too much to improve on here.The only things I see are:
- Instead of doing
first = first + 100, you can dofirst += 100.
- Instead of doing
-((first - second)/ 400.0)you can do(second - first)/ 400.0.
- There is also no need to specify your operands as floats (
400.0vs400) because Python does float division with the basic/operator.
Here are my improvements:
def winning_prob(first, second, first_at_home=False, second_at_home=False):
""" Return winning probability, given ELO ratings of two soccer teams
Args:
first: ELO rating of first team
second: ELO rating of second team
first_at_home: is first team playing at home?
second_at_home: is second team playing at home?
Returns:
Winning probability of first team against second team
"""
if first_at_home:
first += 100
if second_at_home:
second += 100
return 1 / (10**((second - first)/400) + 1)Code Snippets
def winning_prob(first, second, first_at_home=False, second_at_home=False):
""" Return winning probability, given ELO ratings of two soccer teams
Args:
first: ELO rating of first team
second: ELO rating of second team
first_at_home: is first team playing at home?
second_at_home: is second team playing at home?
Returns:
Winning probability of first team against second team
"""
if first_at_home:
first += 100
if second_at_home:
second += 100
return 1 / (10**((second - first)/400) + 1)Context
StackExchange Code Review Q#53982, answer score: 4
Revisions (0)
No revisions yet.