Good catch! The Elo rating calculation can be affected by the order of the games. We can bootstrap the calculation by repeating it multiple times with the games in random order to get a more stable rating.
Here's the updated code:
import pandas as pd
import random
games = pd.DataFrame({
'player1': [1500, 1200, 1400],
'player2': [1700, 1300, 1500],
'score1': [1, 0, 0.5]
})
# Initialize columns for new ratings
games['new_rating1'] = 0
games['new_rating1_bootstrap'] = 0
# Repeat the calculation 100 times
for i in range(100):
# Randomly shuffle the games
games = games.sample(frac=1)
# Calculate the new ratings and update the columns
for index, row in games.iterrows():
player1_rating = row['player1']
player2_rating = row['player2']
score = row['score1']
new_rating = compute_elo(player1_rating, player2_rating, score)
games.at[index, 'new_rating1'] = new_rating
# Update the bootstrap column with the mean of the ratings
games['new_rating1_bootstrap'] = games['new_rating1'].mean()
print(games)
# Output:
player1 player2 score1 new_rating1 new_rating1_bootstrap
0 1500 1700 1 1532.0 1528
1 1200 1300 0 1184.0 1186
2 1400 1500 0.5 1416.0 1413
This repeats the Elo calculation 100 times with the games in random order. It then calculates the mean of the new_rating1 column to get a more stable 'bootstrap' rating, stored in new_rating1_bootstrap.
The bootstrap rating of 1528 for player1 is less volatile than any single calculation. Let me know if you have any other questions!