File size: 1,424 Bytes
79132a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def calculate_elo(old_rating, opponent_rating, score, k_factor=32):
    """
    Calculate the new ELO rating for a player.
    
    :param old_rating: The current ELO rating of the player.
    :param opponent_rating: The ELO rating of the opponent.
    :param score: The score of the game (1 for win, 0.5 for draw, 0 for loss).
    :param k_factor: The K-factor used in ELO rating (default is 32).
    :return: The new ELO rating.
    """
    expected_score = 1 / (1 + 10 ** ((opponent_rating - old_rating) / 400))
    new_rating = old_rating + k_factor * (score - expected_score)
    return new_rating

def update_elo_ratings(ratings, winner, loser, k_factor=32):
    """
    Update ELO ratings for two players.

    :param ratings: A dictionary of current ELO ratings.
    :param winner: The model name of the winning player.
    :param loser: The model name of the losing player.
    :param k_factor: The K-factor used in ELO rating (default is 32).
    :return: Updated ELO ratings.
    """
    winner_old_rating = ratings[winner]
    loser_old_rating = ratings[loser]

    # Winner's new rating
    winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, k_factor)
    # Loser's new rating
    loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, k_factor)

    # Update the ratings
    ratings[winner] = winner_new_rating
    ratings[loser] = loser_new_rating

    return ratings