-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ranking.py
196 lines (139 loc) · 5.83 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
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
from trueskill import Rating, quality_1vs1, rate_1vs1
import trueskill
import itertools
import math
starting_rating = 25
alice, bob = Rating(25), Rating(30) # assign Alice and Bob's ratings
teams = {}
teams_home_prob = {}
teams_away_prob = {}
teamNames = []
def addSingleEntry(team1, team2):
if not teamNames.__contains__(team1[0]):
teamNames.append(team1[0])
teams[team1[0]] = [Rating()]
teams_home_prob[team1[0]] = [0, 0]
teams_away_prob[team1[0]] = [0, 0]
if not teamNames.__contains__(team2[0]):
teamNames.append(team2[0])
teams[team2[0]] = [Rating()]
teams_home_prob[team2[0]] = [0, 0]
teams_away_prob[team2[0]] = [0, 0]
if team1[1] > team2[1]:
(new_r1,), (new_r2,) = trueskill.rate([teams[team1[0]], teams[team2[0]]], ranks=[0, 1])
teams[team1[0]] = [new_r1]
teams[team2[0]] = [new_r2]
teams_home_prob[team2[0]] = [teams_home_prob[team2[0]][0], teams_home_prob[team2[0]][1] + 1]
teams_away_prob[team1[0]] = [teams_away_prob[team1[0]][0] + 1, teams_away_prob[team1[0]][1] + 1]
if team1[1] < team2[1]:
(new_r1,), (new_r2,) = trueskill.rate([teams[team1[0]], teams[team2[0]]], ranks=[1, 0])
teams[team1[0]] = [new_r1]
teams[team2[0]] = [new_r2]
teams_home_prob[team2[0]] = [teams_home_prob[team2[0]][0] + 1, teams_home_prob[team2[0]][1] + 1]
teams_away_prob[team1[0]] = [teams_away_prob[team1[0]][0], teams_away_prob[team1[0]][1] + 1]
if team1[1] == team2[1]:
(new_r1,), (new_r2,) = trueskill.rate([teams[team1[0]], teams[team2[0]]], ranks=[0, 0])
teams[team1[0]] = [new_r1]
teams[team2[0]] = [new_r2]
def addEntry(games):
for game in games:
team1 = game[0]
team2 = game[1]
addSingleEntry(team1, team2)
def predictOutcomesByProbability(games):
correct_guesses = 0
total_guesses = 0
for game in games:
team1 = game[0]
team2 = game[1]
total_guesses = total_guesses + 1
# team2 has a record of 4 wins out of 6 home games
# team2's home field advantage is (4/6) - 0.5
# = 0.66 - 0.5
# = 0.16
# home_advantage = getHomeWinRatio()-0.5
home_advantage = getHomeWinRatioTeam(team2)-0.5
# home_advantage = 0
# probability of team1 winning is their rating - team2's home field advantage
prob = win_probability(teams[team1[0]], teams[team2[0]]) - home_advantage
# print(team1[0], "has a", prob, "chance of beating", team2[0])
if team1[1] > team2[1]:
# print(team1[0], "beat", team2[0], team1[1], "to", team2[1])
if float(prob) > 0.5:
correct_guesses = correct_guesses + 1
elif team1[1] < team2[1]:
# print(team1[0], "lost to", team2[0], team1[1], "to", team2[1])
if float(prob) < 0.5:
correct_guesses = correct_guesses + 1
elif team1[1] == team2[1]:
# print(team1[0], "tied", team2[0], team1[1], "to", team2[1])
if float(prob) == 0.5:
correct_guesses = correct_guesses + 1
return correct_guesses, total_guesses
def predictOutcomesByRating(games):
for game in games:
team1 = game[0]
team2 = game[1]
r1 = getRating(team1)
r2 = getRating(team2)
print(team1[0], "[", r1, "]\t", team2[0], "[", r2, "]")
if team1[1] > team2[1]:
print(team1[0], "beat", team2[0], team1[1], "to", team2[1])
if team1[1] < team2[1]:
print(team1[0], "lost to", team2[0], team1[1], "to", team2[1])
if team1[1] == team2[1]:
print(team1[0], "tied", team2[0], team1[1], "to", team2[1])
print("")
def getRating(team):
return "{0:.2f}".format(teams[team[0]][0].mu)
def printTeamNames():
for name in teamNames:
print(name)
def printTeamRanks():
for name in teamNames:
print(name, "\t", teams[name])
def getHomeWinRatio():
total_wins = 0
total_games = 0
for name in teamNames:
total_wins = total_wins + teams_home_prob[name][0]
total_games = total_games + teams_home_prob[name][1]
return total_wins/total_games
def getAwayWinRatio():
total_wins = 0
total_games = 0
for name in teamNames:
total_wins = total_wins + teams_away_prob[name][0]
total_games = total_games + teams_away_prob[name][1]
return total_wins/total_games
def getAwayWinRatioTeam(team):
if teams_home_prob[team[0]][1] == 0:
return 0.5
return teams_away_prob[team[0]][0]/teams_away_prob[team[0]][1]
def getHomeWinRatioTeam(team):
if teams_home_prob[team[0]][1] == 0:
return 0.5
return teams_home_prob[team[0]][0] / teams_home_prob[team[0]][1]
def printTeamHomeProb():
total_wins = 0
total_games = 0
for name in teamNames:
total_wins = total_wins + teams_home_prob[name][0]
total_games = total_games + teams_home_prob[name][1]
print(name, "\t", teams_home_prob[name])
print("[Home wins, Home Games] =", "{0:.2f}".format(total_wins/total_games))
def printTeamAwayProb():
total_wins = 0
total_games = 0
for name in teamNames:
total_wins = total_wins + teams_away_prob[name][0]
total_games = total_games + teams_away_prob[name][1]
print(name, "\t", teams_away_prob[name])
print("[Away wins, Away Games] =", "{0:.2f}".format(total_wins / total_games))
def win_probability(team1, team2, home_advantage=0):
delta_mu = sum(r.mu for r in team1) - (sum(r.mu for r in team2)+home_advantage)
sum_sigma = sum(r.sigma ** 2 for r in itertools.chain(team1, team2))
size = len(team1) + len(team2)
denom = math.sqrt(size * (trueskill.BETA * trueskill.BETA) + sum_sigma)
ts = trueskill.global_env()
return ts.cdf(delta_mu / denom)