-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepl.py
297 lines (204 loc) · 9.04 KB
/
epl.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
import pandas as pd
import random
import pagerank
import sys
def getData(csvFileName, flip_result=False):
data = pd.read_csv(csvFileName)
indexToTeam = []
teamToIndex = {}
indexToGamesPlayed = []
for i in range(len(data["Home Team"])):
team = data["Home Team"][i]
index = len(indexToTeam)
if not(team in teamToIndex):
indexToTeam.append(team)
teamToIndex[team] = index
indexToGamesPlayed.append(0.0)
if (type(data["Result"][i]) is str):
if len(data["Result"][i].split("-")) == 2:
index = teamToIndex[team]
indexToGamesPlayed[index] += 1
for i in range(len(data["Home Team"])):
if not(type(data["Result"][i]) is str):
continue
if flip_result:
data["Result"][i] = data["Result"][i][::-1]
return data, indexToTeam, teamToIndex, indexToGamesPlayed
def createWeekMultiplier(reg=8):
def f(week, round_number):
return 1.0 / (week - round_number + reg) ** 2
return f
def getMatrixForSeason(week, model, week_multiplier=None):
data, indexToTeam, teamToIndex, indexToGamesPlayed = model
# Calculate Games Played
indexToGamesPlayed = [0 for i in range(len(indexToTeam))]
for i in range(len(data["Home Team"])):
if data["Round Number"][i] > week:
break
if not(type(data["Result"][i]) is str):
continue
result = data["Result"][i].split("-")
if len(result) != 2:
continue
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
indexToGamesPlayed[homeIndex] += 1
indexToGamesPlayed[awayIndex] += 1
A = [[0.0 for i in range(len(indexToTeam))] for j in range(len(indexToTeam))]
for i in range(len(data["Home Team"])):
if data["Round Number"][i] > week:
break
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
if not(type(data["Result"][i]) is str):
continue
result = data["Result"][i].split("-")
if len(result) != 2:
continue
homeScore = int(result[0].strip())
awayScore = int(result[1].strip())
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
homeTeamPlayed = indexToGamesPlayed[homeIndex]
awayTeamPlayed = indexToGamesPlayed[awayIndex]
# Adds in a weight to latest games
#weight = 1.0 #/ (week - data["Round Number"][i] + 8) ** 2
weight = 1.0
if week_multiplier != None:
weight = week_multiplier(week, data["Round Number"][i])
if homeScore > awayScore:
A[awayIndex][homeIndex] += 3.0 * weight # * (1/homeTeamPlayed)
elif homeScore < awayScore:
A[homeIndex][awayIndex] += 3.0 * weight # * (1/awayTeamPlayed)
else:
A[awayIndex][homeIndex] += 1.0 * weight # * (1/homeTeamPlayed)
A[homeIndex][awayIndex] += 1.0 * weight # * (1/awayTeamPlayed)
A[awayIndex][homeIndex] += (0.5) * homeScore # * (1/homeTeamPlayed)
A[homeIndex][awayIndex] += (0.5) * awayScore # * (1/awayTeamPlayed)
return A + np.identity(len(A)) + 0.1
# Get the rankings
def getValue(item):
return item[1]
def printRankings(rankings, model):
data, indexToTeam, teamToIndex, indexToGamesPlayed = model
rankings = sorted(rankings, key=getValue, reverse=True)
for i in range(len(rankings)):
print((i+1), "%.3f " % rankings[i][1], indexToTeam[rankings[i][0]])
# Project on to the future games
def getSeasonStats(R, week, data, teamToIndex):
indexToExpectedPoints = [ 0 for i in range(len(teamToIndex)) ]
indexToRandomPoints = [ 0 for i in range(len(teamToIndex)) ]
indexToMorePoints = [ 0 for i in range(len(teamToIndex)) ]
for i in range(len(data["Home Team"])):
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
# Not exact
if data["Round Number"][i] > week or not(type(data["Result"][i]) is str) or len(data["Result"][i].split("-")) != 2:
homeTeamValue = R[homeIndex][0]
awayTeamValue = R[awayIndex][0]
# Calulate More Points
if homeTeamValue > awayTeamValue:
indexToMorePoints[homeIndex] += 3
elif homeTeamValue < awayTeamValue:
indexToMorePoints[awayIndex] += 3
else:
indexToMorePoints[homeIndex] += 1
indexToMorePoints[awayIndex] += 1
# Calulate Random Points
total = homeTeamValue + awayTeamValue
if total == 0:
homeTeamValue += 0.01
awayTeamValue += 0.01
total = homeTeamValue + awayTeamValue
#if ((homeTeamValue / total) * (1-(homeTeamValue / total)))**2 > random.random():
if random.random() > 0.72:
indexToRandomPoints[homeIndex] += 1
indexToRandomPoints[awayIndex] += 1
elif homeTeamValue / total > random.random():
indexToRandomPoints[homeIndex] += 3
else:
indexToRandomPoints[awayIndex] += 3
# Calulate Expected Points
# Maybe change 3 to like 2.2 or expected points per game
hS = (homeTeamValue / total) ** 2
aS = (awayTeamValue / total) ** 2
hChance = hS / (hS + aS)
aChance = aS / (hS + aS)
indexToExpectedPoints[homeIndex] += 3 * hChance
indexToExpectedPoints[awayIndex] += 3 * aChance
continue
result = data["Result"][i].split("-")
# Calculate the known score
homeScore = int(result[0].strip())
awayScore = int(result[1].strip())
if homeScore > awayScore:
indexToExpectedPoints[homeIndex] += 3
indexToRandomPoints[homeIndex] += 3
indexToMorePoints[homeIndex] += 3
elif awayScore > homeScore:
indexToExpectedPoints[awayIndex] += 3
indexToRandomPoints[awayIndex] += 3
indexToMorePoints[awayIndex] += 3
else:
indexToExpectedPoints[homeIndex] += 1
indexToRandomPoints[homeIndex] += 1
indexToMorePoints[homeIndex] += 1
indexToExpectedPoints[awayIndex] += 1
indexToRandomPoints[awayIndex] += 1
indexToMorePoints[awayIndex] += 1
return indexToExpectedPoints, indexToRandomPoints, indexToMorePoints
# Graph Stats
def getRankings(week, model, teamToIndex, week_multiplier=None):
A = np.array(getMatrixForSeason(week, model, week_multiplier))
R = pagerank.rank(A)
rankings = [(i, R[i]) for i in range(len(indexToTeam))]
indexToExpectedPoints, indexToRandomPoints, indexToMorePoints = getSeasonStats(R, week, model[0], teamToIndex)
return indexToExpectedPoints
def plotSeason(maxWeek, model, week_multiplier=None):
data, indexToTeam, teamToIndex, indexToGamesPlayed = model
indexTeamToWeekToPoints = [[] for i in range(len(indexToTeam))]
for i in range(maxWeek+1):
indexToPoints = getRankings(i, model, teamToIndex, week_multiplier)
for j in range(len(indexToTeam)):
indexTeamToWeekToPoints[j].append(indexToPoints[j])
import matplotlib.pyplot as plt
rankings = [(i, indexTeamToWeekToPoints[i][-1]) for i in range(len(indexTeamToWeekToPoints))]
rankings = sorted(rankings, key=getValue, reverse=True)
for i in range(len(rankings)):
j = rankings[i][0]
plt.plot(indexTeamToWeekToPoints[j], label=indexToTeam[j])
plt.ylabel('Expected Points')
plt.xlabel('Week')
plt.legend(bbox_to_anchor=(1.00, 1), loc="upper left")
#plt.legend(bbox_to_anchor=(0, 0), loc="upper left")
plt.show()
if __name__ == "__main__":
csvFileName = "epl.csv"
week = 40
if len(sys.argv) > 1:
csvFileName = sys.argv[1]
if len(sys.argv) > 2:
week = int(sys.argv[2])
model = getData(csvFileName)
data, indexToTeam, teamToIndex, indexToGamesPlayed = model
# Get matrix A and Ranking
A = getMatrixForSeason(week, model, None) #createWeekMultiplier(8))
R = pagerank.rank(A)
rankings = [(i, R[i]) for i in range(len(indexToTeam))]
printRankings(rankings, model)
indexToExpectedPoints, indexToRandomPoints, indexToMorePoints = getSeasonStats(R, week, data, teamToIndex)
expectedPointsRanking = [(i, indexToExpectedPoints[i]) for i in range(len(indexToExpectedPoints))]
randomPointsRanking = [(i, indexToRandomPoints[i]) for i in range(len(indexToRandomPoints))]
morePointsRanking = [(i, indexToMorePoints[i]) for i in range(len(indexToMorePoints))]
print("\nExpected Points")
printRankings(expectedPointsRanking, model)
print("\nWeighted Random Points")
printRankings(randomPointsRanking, model)
print("\nExact Points")
printRankings(morePointsRanking, model)
plotSeason(week, model)