-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratingModels.py
231 lines (187 loc) · 8.8 KB
/
ratingModels.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import json
import math
import numpy as np
import trueskill as ts
import lib.glicko2 as glicko
from data import getLeagues, getGames
with open('matches/names.json') as data_file:
namesTranslate = json.load(data_file)
def getTeams(games):
teams = {}
for team in np.unique(np.append(games['t1'], games['t2'])):
teams[team] = {
'name': team,
'shortName': namesTranslate[team],
'wins': 0,
'losses': 0,
'games': 0
}
for (team, nGames) in games.groupby('t1').size().iteritems():
teams[team]['games'] += nGames
for (team, nGames) in games.groupby('t2').size().iteritems():
teams[team]['games'] += nGames
for ((team, result), nGames) in games.groupby(['t1', 'result']).size().iteritems():
teams[team]['wins'] += nGames if result == 1 else 0
teams[team]['losses'] += nGames if result == 0 else 0
for ((team, result), nGames) in games.groupby(['t2', 'result']).size().iteritems():
teams[team]['wins'] += nGames if result == 0 else 0
teams[team]['losses'] += nGames if result == 1 else 0
return teams
ts.setup(mu=1500, sigma=300, beta=200, draw_probability=0)
def trueskillExpectedWinRate(r1, r2, blueSide=0):
deltaMu = r1.mu - r2.mu + blueSide
sumSigma = r1.sigma ** 2 + r2.sigma ** 2
denominator = math.sqrt(4 * (200 * 200) + sumSigma)
return ts.global_env().cdf(deltaMu / denominator)
def g(variance):
return 1 / math.sqrt(1 + 3 * math.pow(math.log(10) / 400 / math.pi, 2) * variance)
def glickoExpectedWinRate(r1, r2, blueSide=0):
return 1 / (1 + math.pow(10, g(r1.getRd() * r1.getRd() + r2.getRd() * r2.getRd()) * (r2.getRating() - r1.getRating() - blueSide) / 400))
class TrueskillModel():
@staticmethod
def applyModel(allGames, games, teams, blueSideAdvantage={}):
for team in teams:
teams[team]['rating'] = ts.Rating()
if 'expected' not in allGames:
allGames['expected'] = np.nan
if 'snapshot' not in allGames:
allGames['snapshot'] = 0
allGames['snapshot'] = allGames['snapshot'].astype(dict)
league = games['league'].iloc[0]
blueSide = 0 if league not in blueSideAdvantage else blueSideAdvantage[league]
for i in games.index:
allGames.set_value(i, 'expected', trueskillExpectedWinRate(teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], blueSide))
(teams[games.ix[i, 't1']]['rating'],), (teams[games.ix[i, 't2']]['rating'],) = ts.rate(
[(teams[games.ix[i, 't1']]['rating'],), (teams[games.ix[i, 't2']]['rating'],)],
[1 - games.ix[i, 'result'], games.ix[i, 'result']])
allGames.set_value(i, 'snapshot', {k: v['rating'] for k, v in teams.items()})
@staticmethod
def expectedWinRate(r1, r2):
return trueskillExpectedWinRate(r1, r2)
@staticmethod
def getRatingMu(r):
return r.mu
@staticmethod
def getRatingSigma(r):
return r.sigma
class TrueskillModelPeriod():
@staticmethod
def applyModel(allGames, games, teams, blueSideAdvantage={}):
for team in teams:
teams[team]['rating'] = ts.Rating()
if 'expected' not in allGames:
allGames['expected'] = np.nan
if 'snapshot' not in allGames:
allGames['snapshot'] = 0
allGames['snapshot'] = allGames['snapshot'].astype(dict)
league = games['league'].iloc[0]
blueSide = 0 if league not in blueSideAdvantage else blueSideAdvantage[league]
for period, periodGames in games.groupby("period"):
for i in periodGames.index:
allGames.set_value(i, 'expected', trueskillExpectedWinRate(teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], blueSide))
for i in periodGames.index:
(teams[games.ix[i, 't1']]['rating'],), (teams[games.ix[i, 't2']]['rating'],) = ts.rate(
[(teams[games.ix[i, 't1']]['rating'],), (teams[games.ix[i, 't2']]['rating'],)],
[1 - games.ix[i, 'result'], games.ix[i, 'result']])
allGames.set_value(i, 'snapshot', {k: v['rating'] for k, v in teams.items()})
@staticmethod
def expectedWinRate(r1, r2):
return trueskillExpectedWinRate(r1, r2)
@staticmethod
def getRatingMu(r):
return r.mu
@staticmethod
def getRatingSigma(r):
return r.sigma
class GlickoModel():
@staticmethod
def applyModel(allGames, games, teams, blueSideAdvantage={}):
ratings = []
for team in teams:
teams[team]['rating'] = glicko.Player()
ratings.append(teams[team]['rating'])
if 'expected' not in allGames:
allGames['expected'] = np.nan
if 'snapshot' not in allGames:
allGames['snapshot'] = 0
allGames['snapshot'] = allGames['snapshot'].astype(dict)
league = games['league'].iloc[0]
blueSide = 0 if league not in blueSideAdvantage else blueSideAdvantage[league]
for period, periodGames in games.groupby("period"):
snapshot = {k: v['rating'].clone() for k, v in teams.items()}
for i in periodGames.index:
allGames.set_value(i, 'expected', glickoExpectedWinRate(snapshot[games.ix[i, 't1']], snapshot[games.ix[i, 't2']], blueSide))
glicko.updateResults([(snapshot[games.ix[i, 't1']], snapshot[games.ix[i, 't2']], games.ix[i, 'result'])])
allGames.set_value(i, 'snapshot', {k: v.clone() for k, v in snapshot.items()})
g = []
for i in periodGames.index:
g.append((teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], games.ix[i, 'result']))
glicko.updatePeriod(ratings, g)
allGames.set_value(periodGames.index[-1], 'snapshot', {k: v['rating'].clone() for k, v in teams.items()})
@staticmethod
def expectedWinRate(r1, r2):
return glickoExpectedWinRate(r1, r2)
@staticmethod
def getRatingMu(r):
return r.getRating()
@staticmethod
def getRatingSigma(r):
return r.getRd()
class GlickoModelPerGame():
@staticmethod
def applyModel(allGames, games, teams, blueSideAdvantage={}):
for team in teams:
teams[team]['rating'] = glicko.Player()
if 'expected' not in allGames:
allGames['expected'] = np.nan
if 'snapshot' not in allGames:
allGames['snapshot'] = 0
allGames['snapshot'] = allGames['snapshot'].astype(dict)
league = games['league'].iloc[0]
blueSide = 0 if league not in blueSideAdvantage else blueSideAdvantage[league]
for i in games.index:
allGames.set_value(i, 'expected', glickoExpectedWinRate(teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], blueSide))
glicko.updateResults([(teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], games.ix[i, 'result'])])
allGames.set_value(i, 'snapshot', {k: v['rating'].clone() for k, v in teams.items()})
@staticmethod
def expectedWinRate(r1, r2):
return glickoExpectedWinRate(r1, r2)
@staticmethod
def getRatingMu(r):
return r.getRating()
@staticmethod
def getRatingSigma(r):
return r.getRd()
class GlickoModelPeriod():
@staticmethod
def applyModel(allGames, games, teams, blueSideAdvantage={}):
ratings = []
for team in teams:
teams[team]['rating'] = glicko.Player()
ratings.append(teams[team]['rating'])
if 'expected' not in allGames:
allGames['expected'] = np.nan
if 'snapshot' not in allGames:
allGames['snapshot'] = 0
league = games['league'].iloc[0]
blueSide = 0 if league not in blueSideAdvantage else blueSideAdvantage[league]
allGames['snapshot'] = allGames['snapshot'].astype(dict)
for period, periodGames in games.groupby("period"):
for i in periodGames.index:
allGames.set_value(i, 'expected', glickoExpectedWinRate(teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], blueSide))
for i in periodGames.index:
allGames.set_value(i, 'snapshot', {k: v['rating'].clone() for k, v in teams.items()})
g = []
for i in periodGames.index:
g.append((teams[games.ix[i, 't1']]['rating'], teams[games.ix[i, 't2']]['rating'], games.ix[i, 'result']))
glicko.updatePeriod(ratings, g)
allGames.set_value(periodGames.index[-1], 'snapshot', {k: v['rating'].clone() for k, v in teams.items()})
@staticmethod
def expectedWinRate(r1, r2):
return glickoExpectedWinRate(r1, r2)
@staticmethod
def getRatingMu(r):
return r.getRating()
@staticmethod
def getRatingSigma(r):
return r.getRd()