Beispiel #1
0
def test_calculate_team_ratings_increases_home_rating_and_decreases_away_rating_for_a_draw_where_away_is_rated_higher(
):
    [home, away] = elo.calculate_team_ratings(k_factor=25,
                                              home_team=1667.03,
                                              away_team=1723.12,
                                              home_goals=1,
                                              away_goals=1)

    assert home == 1669.03
    assert away == 1721.12
Beispiel #2
0
def test_calculate_team_ratings_returns_new_higher_home_rating_and_lower_away_ratings_for_home_win(
):
    [home, away] = elo.calculate_team_ratings(k_factor=25,
                                              home_team=1567.03,
                                              away_team=1463.12,
                                              home_goals=2,
                                              away_goals=1)

    assert home == 1575.9
    assert away == 1454.25
Beispiel #3
0
def __apply_historic_elo_ratings(df, historic_elos, soft_reset_factor,
                                 goal_points: int) -> pd.DataFrame:
    """
    Calculate rolling ELO ratings for teams based on results provided in data frame
    """
    # Initialise a dictionary with default elos for each team
    for team in df['homeTeam'].unique():
        if team not in historic_elos.keys():
            historic_elos[team] = 1500

    current_elos = historic_elos.copy()
    current_home_attack_elos = historic_elos.copy()
    current_home_defence_elos = historic_elos.copy()
    current_away_attack_elos = historic_elos.copy()
    current_away_defence_elos = historic_elos.copy()
    match_elos = {}
    match_home_strength = {}
    match_away_strength = {}

    last_season = 0

    # Loop over the rows in the DataFrame
    for index, row in df.iterrows():
        # Get the current year
        current_season = row['season']

        # If it is a new season, soft-reset elos
        if current_season != last_season:
            current_elos = __revert_elos_to_mean(current_elos,
                                                 soft_reset_factor)
            current_home_attack_elos = __revert_elos_to_mean(
                current_home_attack_elos, soft_reset_factor)
            current_home_defence_elos = __revert_elos_to_mean(
                current_home_defence_elos, soft_reset_factor)
            current_away_attack_elos = __revert_elos_to_mean(
                current_away_attack_elos, soft_reset_factor)
            current_away_defence_elos = __revert_elos_to_mean(
                current_away_defence_elos, soft_reset_factor)

        # Get the Game ID
        match_id = row['fixtureID']

        # Get the team and opposition
        home_team = row['homeTeam']
        away_team = row['awayTeam']

        # Get the team and opposition elo score
        home_team_elo = current_elos[home_team]
        away_team_elo = current_elos[away_team]
        home_attack_elo = current_home_attack_elos[home_team]
        home_defence_elo = current_home_defence_elos[home_team]
        away_attack_elo = current_away_attack_elos[away_team]
        away_defence_elo = current_away_defence_elos[away_team]

        # Add the elos and probabilities our elos dictionary and elo_probs dictionary based on the Match ID
        match_elos[match_id] = [home_team_elo, away_team_elo]
        match_home_strength[match_id] = [home_attack_elo, home_defence_elo]
        match_away_strength[match_id] = [away_attack_elo, away_defence_elo]

        # Calculate the new elos of each team
        new_home_team_elo, new_away_team_elo = elo.calculate_team_ratings(
            25, home_team_elo, away_team_elo, row['homeGoals'],
            row['awayGoals'])

        new_home_attack, new_away_defence = elo.calculate_attack_and_defence_ratings(
            goal_points, home_attack_elo, away_defence_elo, row['homeGoals'])

        new_away_attack, new_home_defence = elo.calculate_attack_and_defence_ratings(
            goal_points, away_attack_elo, home_defence_elo, row['awayGoals'])

        # Update elos in elo dictionary
        current_elos[home_team] = new_home_team_elo
        current_elos[away_team] = new_away_team_elo
        current_home_attack_elos[home_team] = new_home_attack
        current_away_attack_elos[away_team] = new_away_attack
        current_home_defence_elos[home_team] = new_home_defence
        current_away_defence_elos[away_team] = new_away_defence

        last_season = current_season

    updated = (df.assign(
        homeElo=lambda frame: frame.fixtureID.map(match_elos).str[0],
        awayElo=lambda frame: frame.fixtureID.map(match_elos).str[1],
        homeAttackStrength=lambda frame: frame.fixtureID.map(
            match_home_strength).str[0],
        homeDefenceStrength=lambda frame: frame.fixtureID.map(
            match_home_strength).str[1],
        awayAttackStrength=lambda frame: frame.fixtureID.map(
            match_away_strength).str[0],
        awayDefenceStrength=lambda frame: frame.fixtureID.map(
            match_away_strength).str[1],
    ))

    return updated