Spaces:
Runtime error
Runtime error
Create elo.py
Browse files
elo.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def calculate_elo(old_rating, opponent_rating, score, k_factor=32):
|
2 |
+
"""
|
3 |
+
Calculate the new ELO rating for a player.
|
4 |
+
|
5 |
+
:param old_rating: The current ELO rating of the player.
|
6 |
+
:param opponent_rating: The ELO rating of the opponent.
|
7 |
+
:param score: The score of the game (1 for win, 0.5 for draw, 0 for loss).
|
8 |
+
:param k_factor: The K-factor used in ELO rating (default is 32).
|
9 |
+
:return: The new ELO rating.
|
10 |
+
"""
|
11 |
+
expected_score = 1 / (1 + 10 ** ((opponent_rating - old_rating) / 400))
|
12 |
+
new_rating = old_rating + k_factor * (score - expected_score)
|
13 |
+
return new_rating
|
14 |
+
|
15 |
+
def update_elo_ratings(ratings, winner, loser, k_factor=32):
|
16 |
+
"""
|
17 |
+
Update ELO ratings for two players.
|
18 |
+
|
19 |
+
:param ratings: A dictionary of current ELO ratings.
|
20 |
+
:param winner: The model name of the winning player.
|
21 |
+
:param loser: The model name of the losing player.
|
22 |
+
:param k_factor: The K-factor used in ELO rating (default is 32).
|
23 |
+
:return: Updated ELO ratings.
|
24 |
+
"""
|
25 |
+
winner_old_rating = ratings[winner]
|
26 |
+
loser_old_rating = ratings[loser]
|
27 |
+
|
28 |
+
# Winner's new rating
|
29 |
+
winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, k_factor)
|
30 |
+
# Loser's new rating
|
31 |
+
loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, k_factor)
|
32 |
+
|
33 |
+
# Update the ratings
|
34 |
+
ratings[winner] = winner_new_rating
|
35 |
+
ratings[loser] = loser_new_rating
|
36 |
+
|
37 |
+
return ratings
|