Commit f0b741c3 authored by Lukas Burgey's avatar Lukas Burgey

Undelete the elo_history

parent e8ca6ba1
......@@ -64,13 +64,69 @@ NON_TABLE_STATS = [
# the implementations need to be static!
ONCE_FOR_ALL_STATS = [
'elo_history',
'glicko2_history',
]
def elo_history():
return get_cached(modules[__name__], '_elo_history')
# hidden method with computating
# hidden method with computation
def _elo_history():
elo_ratings = {}
start = 800
k = 16
players = Player.objects.all()
for player in players:
elo_ratings[player] = [start]
for game in Game.objects.all().order_by('date'):
# the ratings from the other players stay the same
for player in (
players
.exclude(name=game.player0.name)
.exclude(name=game.player1.name)
.exclude(name=game.player2.name)
):
elo_ratings[player].append(elo_ratings[player][-1])
# ratings
r_allein = elo_ratings[game.player0][-1]
r_team = (elo_ratings[game.player1][-1] + elo_ratings[game.player2][-1]) / 2
# normalized ratings
R_allein = power(10, r_allein / 400)
R_team = power(10, r_team / 400)
# expected outcome
E_allein = R_allein / (R_allein + R_team)
E_team = R_team / (R_allein + R_team)
# calculate the resulting rating changes from the game
if game.won:
r_allein_new = r_allein + k * (1 - E_allein)
r_team_new = r_team + k * (0 - E_team)
else:
r_allein_new = r_allein + k * (0 - E_allein)
r_team_new = r_team + k * (1 - E_team)
# deltas to the old rating
delta_allein = r_allein_new - r_allein
delta_team = (r_team_new - r_team) / 2
elo_ratings[game.player0].append(
elo_ratings[game.player0][-1] + delta_allein
)
elo_ratings[game.player1].append(
elo_ratings[game.player1][-1] + delta_team
)
elo_ratings[game.player2].append(
elo_ratings[game.player2][-1] + delta_team
)
return elo_ratings
def glicko2():
glicko2 = Glicko2(800, 100, 0.1, 1.2)
players = Player.objects.all()
ratings = _glicko2(players=players, periods=_get_rating_periods(), glicko2=glicko2)
......@@ -257,6 +313,7 @@ class Player(dbmodels.Model):
return Game.objects.all().order_by('date')
STAT_DESCRIPTION['elo_history'] = "Elo history", "EloH"
STAT_DESCRIPTION['glicko2_history'] = "Glicko2 history", "Gl2H"
STAT_DESCRIPTION['elo'] = "Elo rating", "Elo"
def elo(self, it):
return elo_history()[self][-1]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment