Sorry if this is a rather complicated question.
I have a pandas dataframe that records the result of races between different players:
Race_ID
records different races
Racer_ID
records different players
N
denotes the number of players in that game
Place
denotes the outcome of that game, with 1
being the winner, 2
being the first runner up, etc.
I want to add a new column called Elo_rating
to represent the current elo rating of that player using the following algorithm for n-player elo:
- If this is the first game for that player, then give them an elo rating of 400:
- Compute the expected rating of player x by the following formula:
And if this is the first game for that player, then set E_i = 1/N
- After the race result is out, the Score function for player i is given by
S_i = (N – place) / (N(N-1)/2)
So for example, the score for player 1, 2, 3, 4 after the first race (Race_ID = 10055116) are respectively
S_1 = (4-3) / (4(3)/2) = 1/6,
S_2 = (4-2) / (4(3)/2) = 2/6,
S_3 = (4-1) / (4(3)/2) = 3/6,
S_4 = (4-4) / (4(3)/2) = 0/6.
- Finally the elo rating update for the next race is given by
E_i <- R_i + 30(S_i – E_i)
So for example the elo rating for the second race (Race_ID = 10055117) is given by
And the desired output is given by
I have no idea how I can approach this problem, but I guess we should start by groupby
the Racer_ID and then applying .map
but I have no idea how to implement it. Thank you so much.