HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Soccer winning probabilies from ELO strength indicator

Submitted by: @import:stackexchange-codereview··
0
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:

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 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 do first += 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.0 vs 400) 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.