-
Notifications
You must be signed in to change notification settings - Fork 6
/
draft_optimizer.py
315 lines (259 loc) · 11.8 KB
/
draft_optimizer.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
# get replacement level player by position
import pandas as pd
import numpy as np
from scipy.stats import norm
# league rules!
numberOfTeams = 10
isFlex = True
ppr = 1
rushYards = .1
receivingYards = .1
rushTD = 6
receivingTD = 6
passYards = 1/25
passTD = 4
fmb = -2
int = -2
# get data
individuals = pd.read_csv('data/individuals.csv')
preds_copy = pd.read_csv('data/preds_copy.csv')
ntrees = 500
# your team
draftedOverall = []
yourTeam = []
# roundup function
def roundUp(x, to=numberOfTeams):
return to * (x // to + (x % to > 0))
def draft_optimize(yourTeam, draftedOverall, ppr, num_teams):
# Assuming necessary data is already loaded: individuals, preds_copy, draftedOverall, yourTeam, etc.
# ... [Your data loading code here]
if ppr == '0':
individuals_path = "data/individuals_0_PPR.csv"
preds_copy_path = "data/preds_copy_0_PPR.csv"
elif ppr == '0.5':
individuals_path = "data/individuals_.5_PPR.csv"
preds_copy_path = "data/preds_copy_.5_PPR.csv"
else:
individuals_path = "data/individuals_1_PPR.csv"
preds_copy_path = "data/preds_copy_1_PPR.csv"
individuals = pd.read_csv(individuals_path)
preds_copy = pd.read_csv(preds_copy_path)
# Filtering data based on conditions
replacement_rb = individuals[(individuals['pos'] == 'RB') & (
individuals['posrank'] == (num_teams*2.5 + 1))].iloc[:, :ntrees]
replacement_wr = individuals[(individuals['pos'] == 'WR') & (
individuals['posrank'] == (num_teams*2.5 + 1))].iloc[:, :ntrees]
replacement_te = individuals[(individuals['pos'] == 'TE') & (
individuals['posrank'] == (num_teams + 1))].iloc[:, :ntrees]
replacement_qb = individuals[(individuals['pos'] == 'QB') & (
individuals['posrank'] == (num_teams + 1))].iloc[:, :ntrees]
# Initialize DataFrames and lists
createdDataframe = pd.DataFrame()
secondDataframe = pd.DataFrame()
created_rows = []
yourDraft = individuals[individuals['name'].isin(yourTeam)]
yourDraft['teamrank'] = np.int64(yourDraft.groupby(
'pos')['preds'].rank(ascending=False, method='min'))
first_rb = yourDraft[(yourDraft['teamrank'] == 1)
& (yourDraft['pos'] == 'RB')]
second_rb = yourDraft[(yourDraft['teamrank'] == 2)
& (yourDraft['pos'] == 'RB')]
third_rb = yourDraft[(yourDraft['teamrank'] == 3)
& (yourDraft['pos'] == 'RB')]
first_wr = yourDraft[(yourDraft['teamrank'] == 1)
& (yourDraft['pos'] == 'WR')]
second_wr = yourDraft[(yourDraft['teamrank'] == 2)
& (yourDraft['pos'] == 'WR')]
third_wr = yourDraft[(yourDraft['teamrank'] == 3)
& (yourDraft['pos'] == 'WR')]
first_te = yourDraft[(yourDraft['teamrank'] == 1)
& (yourDraft['pos'] == 'TE')]
first_qb = yourDraft[(yourDraft['teamrank'] == 1)
& (yourDraft['pos'] == 'QB')]
first_flex = third_rb if third_rb.preds.max() > third_wr.preds.max() else third_wr
# For RBs
t_rb = replacement_rb.transpose()
rbs = pd.concat([t_rb.sample(frac=1).transpose()
for _ in range(3)], axis=0)
if not first_rb.empty:
rbs.iloc[0, :] = first_rb.iloc[0, :ntrees]
if not second_rb.empty:
rbs.iloc[1, :] = second_rb.iloc[0, :ntrees]
if not first_flex.empty:
rbs.iloc[2, :] = first_flex.iloc[0, :ntrees]
# For WRs
t_wr = replacement_wr.transpose()
wrs = pd.concat([t_wr.sample(frac=1).transpose()
for _ in range(3)], axis=0)
if not first_wr.empty:
wrs.iloc[0, :] = first_wr.iloc[0, :ntrees]
if not second_wr.empty:
wrs.iloc[1, :] = second_wr.iloc[0, :ntrees]
if not first_flex.empty:
wrs.iloc[2, :] = first_flex.iloc[0, :ntrees]
# For TEs
# Create a copy to prevent modification of original dataframe
tes = replacement_te.copy()
if not first_te.empty:
tes.iloc[0, :] = first_te.iloc[0, :ntrees]
# For QBs
# Create a copy to prevent modification of original dataframe
qbs = replacement_qb.copy()
if not first_qb.empty:
qbs.iloc[0, :] = first_qb.iloc[0, :ntrees]
# Dictionary for positions
pos_dict = {
'RB': rbs,
'WR': wrs,
'TE': tes,
'QB': qbs
}
# Filter out players in draftedOverall from copy
available = preds_copy[~preds_copy['name'].isin(draftedOverall)]
available = available.sort_values(by=['pos', 'preds'], ascending=False).groupby(
'pos').head(25).reset_index(drop=True)
for j in range(len(available)):
player = available.iloc[j]
name = player['name']
position = player['pos']
# Use dictionary to get positional_df
positional_df = pos_dict[position]
indi_preds = individuals[(individuals['name'] == name) & (
individuals['pos'] == position)].iloc[:, :ntrees].iloc[0]
total_pt_gains = 0
pct_better = 0
for i in range(len(positional_df)):
row = positional_df.iloc[i]
better = indi_preds.values > row[::-1].values # Using values to get numpy arrays
elementwise_gains = np.sum(indi_preds[better].values - np.array(row[::-1])[better]) / ntrees
total_pt_gains = max(total_pt_gains, elementwise_gains)
# Finding pick number, your next pick, and the likelihood of a player staying on board
pickNumber = len(draftedOverall) + 1
ceiling = roundUp(pickNumber, num_teams)
leftTillEndOfRound = ceiling - pickNumber
nextPick = ceiling + leftTillEndOfRound + 1
# Calculating the chance of staying on board
chanceOfStayingOnBoard = round(
1 - norm.cdf(nextPick, player['adp'], player['adp_sd']), 2)
# For the round after the likelihood
ceiling = roundUp(nextPick, num_teams)
leftTillEndOfRound = ceiling - nextPick
pickAfter = ceiling + leftTillEndOfRound + 1
chanceOfStayingOnBoardTwoRounds = 1 - \
norm.cdf(pickAfter, player['adp'], player['adp_sd'])
# For the round after...
ceiling = roundUp(pickAfter, num_teams)
leftTillEndOfRound = ceiling - pickAfter
pickEvenAfter = ceiling + leftTillEndOfRound + 1
chanceOfStayingOnThreeRounds = 1 - \
norm.cdf(pickEvenAfter, player['adp'], player['adp_sd'])
# Creating a dictionary to represent the new row
created_row = {
'name': player['name'],
'pos': position,
'preds': player['preds'],
'pct_better': pct_better,
'ADP': player['adp'],
'total_pt_gains': total_pt_gains,
'chanceOfStayingOnBoard': chanceOfStayingOnBoard,
'chanceOfStayingOnBoardTwoRounds': chanceOfStayingOnBoardTwoRounds,
'chanceOfStayingOnThreeRounds': chanceOfStayingOnThreeRounds
}
created_rows.append(created_row)
# Convert the created rows to a DataFrame
createdDataframe = pd.DataFrame(created_rows)
# Process for the second dataframe
second_rows = []
for _, newRow in createdDataframe.iterrows():
playerPosition = newRow['pos']
# Filter and sort
positionallyFiltered = createdDataframe[createdDataframe['pos']
== playerPosition]
positionallyFiltered = positionallyFiltered.sort_values(
by='total_pt_gains', ascending=False).head(12).copy()
# Initialize new columns
positionallyFiltered['chance_of_best_option'] = 0
positionallyFiltered['chance_of_best_option_2'] = 0
positionallyFiltered['chance_of_best_option_3'] = 0
for j, row in positionallyFiltered.iterrows():
better_players = positionallyFiltered[positionallyFiltered['total_pt_gains']
> row['total_pt_gains']]
# Calculating probabilities for best option next round
p_noone_better = np.prod(
1 - better_players['chanceOfStayingOnBoard'])
p_avail = row['chanceOfStayingOnBoard']
p_best_option = p_noone_better * p_avail
positionallyFiltered.at[j, 'chance_of_best_option'] = p_best_option
# ... for two rounds later
p_noone_better = np.prod(
1 - better_players['chanceOfStayingOnBoardTwoRounds'])
p_avail = row['chanceOfStayingOnBoardTwoRounds']
p_best_option = p_noone_better * p_avail
positionallyFiltered.at[j,
'chance_of_best_option_2'] = p_best_option
# ... and for three rounds later
p_noone_better = np.prod(
1 - better_players['chanceOfStayingOnThreeRounds'])
p_avail = row['chanceOfStayingOnThreeRounds']
p_best_option = p_noone_better * p_avail
positionallyFiltered.at[j,
'chance_of_best_option_3'] = p_best_option
# Calculating average positional values
nextRoundValue = np.sum(
positionallyFiltered['chance_of_best_option'] * positionallyFiltered['total_pt_gains'])
nextRoundValue = 1000 if np.isinf(nextRoundValue) or np.isnan(
nextRoundValue) else nextRoundValue
newRow['valueOverNextRound'] = newRow['total_pt_gains'] - nextRoundValue
valueOverTwoRounds = np.sum(
positionallyFiltered['chance_of_best_option_2'] * positionallyFiltered['total_pt_gains'])
valueOverTwoRounds = 1000 if np.isinf(valueOverTwoRounds) or np.isnan(
valueOverTwoRounds) else valueOverTwoRounds
newRow['valueOverTwoRounds'] = newRow['total_pt_gains'] - \
valueOverTwoRounds
valueOverThreeRounds = np.sum(
positionallyFiltered['chance_of_best_option_3'] * positionallyFiltered['total_pt_gains'])
valueOverThreeRounds = 1000 if np.isinf(valueOverThreeRounds) or np.isnan(
valueOverThreeRounds) else valueOverThreeRounds
newRow['valueOverThreeRounds'] = newRow['total_pt_gains'] - \
valueOverThreeRounds
second_rows.append(newRow)
secondDataframe = pd.DataFrame(second_rows)
secondDataframe['total_pt_gains'] = round(secondDataframe['total_pt_gains'], 1)
secondDataframe['valueOverNextRound'] = round(secondDataframe['valueOverNextRound'], 1)
secondDataframe['ADP'] = round(secondDataframe['ADP'], 1)
# add kickers and defense
teams = {'NO', 'ARI', 'TEN', 'DET', 'WAS', 'BUF', 'ATL', 'NYJ', 'SF', 'TB', 'CLE', 'MIN', 'PHI', 'LV', 'FA', 'HOU', 'CIN', 'MIA', 'LAC', 'NE', 'PIT', 'IND', 'BAL', 'DAL', 'DEN', 'KC', 'NYG', 'SEA', 'GB', 'JAC', 'CHI', 'CAR', 'LAR'}
# Create a list to hold the new entries
new_entries = []
#default player
default_player = {
'name': 'Default Player',
'pos': 'Any',
'valueOverNextRound': -101,
'total_pt_gains': -101
}
# Loop over each unique team
for team in teams:
kicker_entry = {
'name': f"{team} Kicker",
'pos': 'K',
'valueOverNextRound': -100,
'total_pt_gains': -100,
# ... other columns with default or null values
}
defense_entry = {
'name': f"{team} Defense",
'pos': 'DEF',
'valueOverNextRound': -99,
'total_pt_gains': -99,
# ... other columns with default or null values
}
new_entries.append(kicker_entry)
new_entries.append(defense_entry)
new_entries.append(default_player)
# Create a new DataFrame with the same columns as `secondDataframe`
new_df = pd.DataFrame(new_entries, columns=secondDataframe.columns)
# Concatenate with the existing DataFrame
final_df = pd.concat([secondDataframe, new_df], ignore_index=True)
final_df.fillna(0, inplace = True)
return final_df