-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelo.py
206 lines (178 loc) · 6.37 KB
/
elo.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
csvFile = open('data.csv', 'r')
results = open('results.txt', 'w')
teamRatingsFile = open('ratings.txt', 'w')
ratings = open('ratingsGraph.csv', 'w')
ratingsGraph = {}
games = []
#bounds
earliestYear = 2012
latestYear = 2013
postSeasonGames = True
preSeasonGames = False
regularSeasonGames = True
useTeamFilter = False
teamFilter = ['Jets', 'Giants', 'Patriots', 'Bills']
numberOfWeeksInSeason = 0
if(regularSeasonGames):
numberOfWeeksInSeason += 17
if(postSeasonGames):
numberOfWeeksInSeason += 4
if(preSeasonGames):
numberOfWeeksInSeason += 4
def computeEstimatedResult(team1, team2):
exponent = (team2 - team1)/400
value = 1/(1 + (10 ** exponent))
return value
def computeNewRatings(team1, predictedScore, actualScore, isPreseason):
#teams tend to use new players as a test during the preseason so we have a larger skill margin
skillFactor = 0
if(isPreseason):
skillFactor = 32
else:
if(postseason):
skillFactor = 8
else:
skillFactor = 16
value = team1 + (skillFactor * (actualScore - predictedScore))
return value
def loadCSV():
header = csvFile.readline()
for line in csvFile:
(homeTeam,homeFirstQuater,homeSecondQuater,homeThirdQuater,
homeFourthQuater,homeOTQuater,homeTotalScore,awayTeam,
awayFirstQuater,awaySecondQuater,awayThirdQuater,
awayFourthQuater,awayOTQuater,awayTotalScore,date, preseason, postseason) = line.split(',')
awayBoxScore = []
homeBoxScore = []
awayBoxScore.append(int(awayFirstQuater))
awayBoxScore.append(int(awaySecondQuater))
awayBoxScore.append(int(awayThirdQuater))
awayBoxScore.append(int(awayFourthQuater))
awayBoxScore.append(int(awayOTQuater))
homeBoxScore.append(int(homeFirstQuater))
homeBoxScore.append(int(homeSecondQuater))
homeBoxScore.append(int(homeThirdQuater))
homeBoxScore.append(int(homeFourthQuater))
homeBoxScore.append(int(homeOTQuater))
games.append(Game(awayTeam, homeTeam, awayBoxScore, homeBoxScore, int(awayTotalScore), int(homeTotalScore), preseason, postseason, date))
csvFile.close()
def inDataRange(game):
if(game.year < earliestYear):
return False
if(game.year > latestYear):
return False
if(game.preseason == 'True'):
if(not preSeasonGames):
return False
else:
return True
if(game.postseason == 'True'):
if(not postSeasonGames):
return False
else:
return True
if(game.preseason == 'False'):
if(game.postseason == 'False'):
if(not regularSeasonGames):
return False
return True
def initData():
loadCSV()
#compute ratings
for game in games:
#add a check that a game is in the key set of the ratings maps
skip = False
if(game.homeTeam not in teamRating):
teamRating[game.homeTeam] = 1500
if(game.awayTeam not in teamRating):
teamRating[game.awayTeam] = 1500
if(inDataRange(game)):
for i in range(0, 5):
skip = False
if(i == 4):
if(game.homeScore == 0):
if(game.awayScore == 0):
skip = True
if(not skip):
eSHome = computeEstimatedResult(teamRating[game.homeTeam], teamRating[game.awayTeam])
eSAway = computeEstimatedResult(teamRating[game.awayTeam], teamRating[game.homeTeam])
scoreHome = 0
scoreAway = 0
if((game.awayBoxScore[i] + game.homeBoxScore[i]) != 0):
scoreHome = (game.homeBoxScore[i]/(game.awayBoxScore[i] + game.homeBoxScore[i]))
scoreAway = (game.awayBoxScore[i]/(game.awayBoxScore[i] + game.homeBoxScore[i]))
else:
scoreHome = 0.5
scoreAway = 0.5
teamRating[game.homeTeam] = computeNewRatings(teamRating[game.homeTeam], eSHome, scoreHome, game.isPreseason)
teamRating[game.awayTeam] = computeNewRatings(teamRating[game.awayTeam], eSAway, scoreAway, game.isPreseason)
results.write('\n')
results.write('Home team: ' + game.homeTeam + '\n')
results.write('Away team: ' + game.awayTeam + '\n')
results.write('eSHome: ' + str(eSHome) + '\n')
results.write('eSAway: ' + str(eSAway) + '\n')
results.write('scoreHome: ' + str(scoreHome) + '\n')
results.write('scoreAway: ' + str(scoreAway) + '\n')
results.write('Home Rating: ' + str(teamRating[game.homeTeam]) + '\n')
results.write('Away Rating: ' + str(teamRating[game.awayTeam]) + '\n')
overallWeek = int(game.week) + ((int(game.year) - earliestYear) * numberOfWeeksInSeason)
if(overallWeek not in ratingsGraph):
ratingsGraph[overallWeek] = {}
ratingsGraph[overallWeek][game.homeTeam] = teamRating[game.homeTeam]
ratingsGraph[overallWeek][game.awayTeam] = teamRating[game.awayTeam]
else:
ratingsGraph[overallWeek][game.homeTeam] = teamRating[game.homeTeam]
ratingsGraph[overallWeek][game.awayTeam] = teamRating[game.awayTeam]
#ratingsGraph.write(game.homeTeam + ',' + str(overallWeek) + ',' + str(teamRating[game.homeTeam]) + '\n')
#ratingsGraph.write(game.awayTeam + ',' + str(overallWeek) + ',' + str(teamRating[game.awayTeam]) + '\n')
for team in teamRating:
teamRatingsFile.write('\nTeam: ' + team + '\n')
teamRatingsFile.write('\tRating: ' + str(teamRating[team]) + '\n')
headerLabel = 'Week'
for team in teamRating:
if(useTeamFilter):
if(team in teamFilter):
headerLabel += ',' + team
else:
headerLabel += ',' + team
for week in ratingsGraph:
headerLabel += '\n'
headerLabel += str(week)
for team in teamRating:
if(useTeamFilter):
if(team in teamFilter):
if(team in ratingsGraph[week]):
headerLabel += ',' + str(ratingsGraph[week][team])
else:
ratingsGraph[week][team] = ratingsGraph[week - 1][team]
headerLabel += ',' + str(ratingsGraph[week][team])
else:
if(team in ratingsGraph[week]):
headerLabel += ',' + str(ratingsGraph[week][team])
else:
ratingsGraph[week][team] = ratingsGraph[week - 1][team]
headerLabel += ',' + str(ratingsGraph[week][team])
print(headerLabel)
ratings.write(headerLabel)
class Game:
def __init__(self, awayTeam, homeTeam, awayBoxScore, homeBoxScore, awayScore, homeScore, preseason, postseason, date):
self.awayTeam = awayTeam
self.homeTeam = homeTeam
self.awayScore = awayScore
self.homeScore = homeScore
self.preseason = preseason
self.postseason = postseason
self.homeBoxScore = homeBoxScore
self.awayBoxScore = awayBoxScore
self.date = date
(self.week, self.year,x,y) = date.split(':')
self.week = int(self.week)
self.year = int(self.year)
def numQuaters(self):
return len(self.awayScore)
def isPreseason(self):
return self.preseason
def isPostseason(self):
return self.postseason
teamRating = {}
initData()