-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommon.py
336 lines (281 loc) · 12.8 KB
/
common.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import difflib
import numpy
import pandas as pd
import random
import re
import requests
FIELDS_TO_COMBINE = {
'home_assist_percentage': 'away_assist_percentage',
'home_assists': 'away_assists',
'home_block_percentage': 'away_block_percentage',
'home_blocks': 'away_blocks',
'home_defensive_rating': 'away_defensive_rating',
'home_defensive_rebound_percentage': 'away_defensive_rebound_percentage',
'home_defensive_rebounds': 'away_defensive_rebounds',
'home_effective_field_goal_percentage': 'away_effective_field_goal_percentage',
'home_field_goal_attempts': 'away_field_goal_attempts',
'home_field_goal_percentage': 'away_field_goal_percentage',
'home_field_goals': 'away_field_goals',
'home_free_throw_attempt_rate': 'away_free_throw_attempt_rate',
'home_free_throw_attempts': 'away_free_throw_attempts',
'home_free_throw_percentage': 'away_free_throw_percentage',
'home_free_throws': 'away_free_throws',
'home_losses': 'away_losses',
'home_minutes_played': 'away_minutes_played',
'home_offensive_rating': 'away_offensive_rating',
'home_offensive_rebound_percentage': 'away_offensive_rebound_percentage',
'home_offensive_rebounds': 'away_offensive_rebounds',
'home_personal_fouls': 'away_personal_fouls',
'home_simple_rating_system': 'away_simple_rating_system',
'home_steal_percentage': 'away_steal_percentage',
'home_steals': 'away_steals',
'home_strength_of_schedule': 'away_strength_of_schedule',
'home_three_point_attempt_rate': 'away_three_point_attempt_rate',
'home_three_point_field_goal_attempts': 'away_three_point_field_goal_attempts',
'home_three_point_field_goal_percentage': 'away_three_point_field_goal_percentage',
'home_three_point_field_goals': 'away_three_point_field_goals',
'home_total_rebound_percentage': 'away_total_rebound_percentage',
'home_total_rebounds': 'away_total_rebounds',
'home_true_shooting_percentage': 'away_true_shooting_percentage',
'home_turnover_percentage': 'away_turnover_percentage',
'home_turnovers': 'away_turnovers',
'home_two_point_field_goal_attempts': 'away_two_point_field_goal_attempts',
'home_two_point_field_goal_percentage': 'away_two_point_field_goal_percentage',
'home_two_point_field_goals': 'away_two_point_field_goals',
'home_win_percentage': 'away_win_percentage',
'home_wins': 'away_wins',
'pace': 'pace',
}
FIELDS_TO_DROP = ['abbreviation', 'conference', 'name']
class MatchInfo:
def __init__(self, away, home, away_name, home_name, away_abbreviation,
home_abbreviation, top_25, game_time):
self.away = away
self.away_abbreviation = away_abbreviation
self.away_name = away_name
self.game_time = game_time
self.home = home
self.home_abbreviation = home_abbreviation
self.home_name = home_name
self.top_25 = top_25
def read_team_stats_file(team_filename):
team_filename = re.sub('\(\d+\) +', '', team_filename)
return pd.read_pickle('%s.plk' % team_filename)
def filter_stats(match_stats):
fields_to_drop = ['abbreviation', 'date', 'location', 'name', 'pace',
'winning_abbr', 'winning_name', 'losing_abbr',
'losing_name', 'winner']
for field in fields_to_drop:
try:
match_stats.drop(field, 1, inplace=True)
except KeyError:
continue
match_stats['away_ranking'] = match_stats['away_ranking'] \
.notnull().astype('int')
match_stats['home_ranking'] = match_stats['home_ranking'] \
.notnull().astype('int')
return match_stats
def differential_vector(stats):
for home_feature, away_feature in FIELDS_TO_COMBINE.items():
try:
feature = home_feature.replace('home_', '')
stats[feature] = stats[home_feature] - stats[away_feature]
stats.drop(away_feature, 1, inplace=True)
stats.drop(home_feature, 1, inplace=True)
except KeyError:
continue
return stats
def convert_team_totals_to_averages(stats):
fields_to_average = ['assists', 'blocks', 'defensive_rebounds',
'field_goal_attempts', 'field_goals',
'free_throw_attempts', 'free_throws',
'minutes_played', 'offensive_rebounds',
'personal_fouls', 'points', 'steals',
'three_point_field_goal_attempts',
'three_point_field_goals', 'total_rebounds',
'turnovers', 'two_point_field_goal_attempts',
'two_point_field_goals']
num_games = stats['games_played']
new_stats = stats.copy()
for field in fields_to_average:
new_value = float(stats[field]) / num_games
new_stats.loc[:,field] = new_value
return new_stats
def extract_stats_components(stats, away=False):
# Get all of the stats that don't start with 'opp', AKA all of the
# stats that are directly related to the indicated team.
filtered_columns = [col for col in stats if not str(col).startswith('opp')]
stats = stats[filtered_columns]
stats = convert_team_totals_to_averages(stats)
if away:
# Prepend all stats with 'away_' to signify the away team as such.
away_columns = ['away_%s' % col for col in stats]
stats.columns = away_columns
else:
# Prepend all stats with 'home_' to signify the home team as such.
home_columns = ['home_%s' % col for col in stats]
stats.columns = home_columns
return stats
def find_stdev_for_every_stat(teams, rankings):
stats_dict = {}
stdev_dict = {}
combined_stats = pd.DataFrame()
for team in teams:
abbr = team.abbreviation.lower()
stats = update_stats(team.dataframe, abbr, rankings)
home_stats = extract_stats_components(stats)
away_stats = extract_stats_components(stats, away=True)
home_stats, away_stats = drop_stats(home_stats, away_stats)
stats_dict[abbr] = home_stats
stats_dict['%s_away' % abbr] = away_stats
combined_stats = combined_stats.append(home_stats)
combined_stats = combined_stats.append(away_stats)
for col in combined_stats.columns.values:
stdev_dict[col] = combined_stats[col].std()
return stats_dict, stdev_dict
def drop_stats(home_stats, away_stats):
for field in FIELDS_TO_DROP:
home_stats.drop('home_%s' % field, 1, inplace=True)
away_stats.drop('away_%s' % field, 1, inplace=True)
return home_stats, away_stats
def update_stats(stats, abbreviation, rankings):
defensive_rebound_percentage = 100.0 * stats['defensive_rebounds'] /\
(stats['defensive_rebounds'] + stats['opp_offensive_rebounds'])
stats['defensive_rebound_percentage'] = defensive_rebound_percentage
if 'defensive_rating' not in stats and \
'offensive_rating' in stats and \
'net_rating' in stats:
stats['defensive_rating'] = stats['offensive_rating'] - \
stats['net_rating']
if abbreviation.lower() in rankings.keys():
stats['ranking'] = rankings[abbreviation.lower()]
else:
stats['ranking'] = 0
return stats
def create_variance(stats, stdev_dict, away, location_specified=False):
local_stats = {}
for stat in stats:
if stat.startswith('opp_'):
continue
if location_specified:
min_val = -1 * float(stdev_dict[stat])
elif away:
min_val = -1 * float(stdev_dict['away_%s' % stat])
else:
min_val = -1 * float(stdev_dict['home_%s' % stat])
max_val = abs(min_val)
variance = random.uniform(min_val, max_val)
new_value = float(stats[stat]) + variance
local_stats[stat] = new_value
return pd.DataFrame([local_stats])
def create_predictions(prediction_stats, predictor):
prediction_data = pd.concat(prediction_stats)
matches_vector = differential_vector(prediction_data)
matches_vector['points_difference'] = matches_vector['home_points'] - \
matches_vector['away_points']
matches_vector = predictor.simplify(matches_vector)
return predictor.predict(matches_vector, int)
def add_winner(num_wins, home, away, spread):
if spread < 0:
num_wins[away] = num_wins.get(away, 0) + 1
# In the case of a tie, give precedence to the home team.
else:
num_wins[home] = num_wins.get(home, 0) + 1
return num_wins
def add_points(total_points, home, away, spread):
total_points[home] = total_points.get(home, 0) + spread
total_points[away] = total_points.get(away, 0) - spread
return total_points
def accumulate_points_and_wins(total_points, num_wins, spread, game):
home, away = game
total_points = add_points(total_points, home, away, spread)
num_wins = add_winner(num_wins, home, away, spread)
return total_points, num_wins
def update_total_wins(standings, total_wins):
for team, points in standings.items():
total_wins[team] = total_wins.get(team, 0) + points
return total_wins
def update_standings(standings, standings_dict, num_sims=1):
sorted_standings = [(v,k) for k,v in standings.items()]
sorted_standings.sort(reverse=True)
for position, standings in enumerate(sorted_standings):
_, team = standings
standings_dict[team]['points'][position] += num_sims
return standings_dict
def determine_conference_standings(num_wins, conference_wins):
standings_dict = {}
for team in conference_wins:
standings_dict[team] = num_wins.get(team, 0) + \
conference_wins.get(team, 0)
return standings_dict
def print_probabilities_ordered(probabilities):
sorted_ranks = [(v,k) for k,v in probabilities.items()]
sorted_ranks.sort(reverse=True)
for probability, team in sorted_ranks:
print('%s: %s%%' % (team, probability * 100.0))
def print_simulation_results(standings_dict, num_sims):
for i in range(len(standings_dict)):
print('=' * 80)
print(' Place: %s' % (i+1))
print('=' * 80)
probabilities = {}
for team, standings in standings_dict.items():
probability = float(standings['points'][i]) / float(num_sims)
probabilities[team] = probability
print_probabilities_ordered(probabilities)
def determine_outcomes(predictions, games_list, standings_dict=None,
conference_wins=None, num_sims=None):
total_points = {}
total_wins = {}
num_wins = {}
# Occurs when the season is already complete and no future games will
# be played in the conference.
if len(games_list) == 0 and conference_wins and standings_dict:
standings = update_standings(conference_wins, standings_dict, num_sims)
return standings, conference_wins
for i in range(len(games_list)):
total_points, num_wins = accumulate_points_and_wins(total_points,
num_wins,
predictions[i],
games_list[i])
if not standings_dict:
continue
if (i + 1) % (len(games_list) / num_sims) == 0:
standings = determine_conference_standings(num_wins,
conference_wins)
standings_dict = update_standings(standings, standings_dict)
total_wins = update_total_wins(standings, total_wins)
num_wins = {}
if (i + 1) == len(games_list):
print_simulation_results(standings_dict, num_sims)
return standings_dict, total_wins
return total_points, num_wins
def aggregate_match_stats(stats_dict, stdev_dict, games, num_sims):
match_stats = []
new_games = []
for iteration in range(num_sims):
for game in games:
away = game.away_abbreviation
home = game.home_abbreviation
home_stats = create_variance(stats_dict[home],
stdev_dict, False, True)
away_stats = create_variance(stats_dict['%s_away' % away],
stdev_dict, True, True)
match_stats.append(pd.concat([away_stats, home_stats], axis=1))
new_games.append([home, away])
return match_stats, new_games
def create_team_name(game):
home = game['home_name']
away = game['away_name']
if game['home_rank']:
home = '(%s) ' % game['home_rank'] + home
if game['away_rank']:
away = '(%s) ' % game['away_rank'] + away
return home, away
def populate_game_info(teams, game):
home_name, away_name = create_team_name(game)
away = teams(game['away_abbr'])
home = teams(game['home_abbr'])
match = MatchInfo(away, home, away_name, home_name, game['away_abbr'],
game['home_abbr'], game['top_25'], None)
return match