-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ranking.py
49 lines (39 loc) · 1.45 KB
/
Ranking.py
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
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
from trueskill import Rating, rate_1vs1, setup, global_env, BETA
from math import sqrt
# Calculates the win probability of A vs B based on their
# respective ratings and our certainty of these.
def Pwin(rA=Rating(), rB=Rating()):
deltaMu = rA.mu - rB.mu
rsss = sqrt(2*(BETA**2) + rA.sigma**2 + rB.sigma**2)
return global_env().cdf(deltaMu/rsss)
class Ranking:
def __init__(self, name, games=[]):
self.name = name
self.gameCount = 0
self.db = {}
setup(draw_probability=0)
for g in reversed(games):
self.update(g)
def _getRating(self, team):
if team in self.db:
return self.db[team]
else:
return Rating()
# update ratings based on a game. 'game' is a tuple/list where
# game[0] won agains game[1]
def update(self, game):
ratings = [self._getRating(t) for t in game]
self.db[game[0]], self.db[game[1]] = rate_1vs1(ratings[0], ratings[1])
self.gameCount += 1
# Chance that t1 will win against t2 based on training data in db
def winChance(self, t1, t2):
return Pwin(self._getRating(t1), self._getRating(t2))
def confidence(self, team):
return self._getRating(team).sigma
def isKnown(self, team):
return (team in self.db)
# Ranking of the teams based on Rating and confidence
def ranking(self):
stats = [(t, self.db[t].mu, self.db[t].sigma, global_env().expose(self.db[t])) for t in self.db]
return sorted(stats, key = lambda x: x[3], reverse=True)