Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NBA betting analysis
# Function that calculates win probability from moneyline
def ml_to_win_prob(ml):
if ml < 0:
prob = -ml / (-ml + 100)
else:
prob = 100 / (ml + 100)
return prob
# Data wrangling: add new columns for W/L outcomes and win probabilities
df_generator = df_nba_lines.iterrows()
for (i, row1), (j, row2) in zip(df_generator, df_generator):
# Determine winner of each game and insert values in new column
if row1['score'] < row2['score']:
df_nba_lines.at[i, 'outcome'] = 'L'
df_nba_lines.at[j, 'outcome'] = 'W'
elif row1['score'] > row2['score']:
df_nba_lines.at[i, 'outcome'] = 'W'
df_nba_lines.at[j, 'outcome'] = 'L'
# Calculate implied win probabilities for each team
row1_win_prob = ml_to_win_prob(row1['ml_PIN'])
row2_win_prob = ml_to_win_prob(row2['ml_PIN'])
df_nba_lines.at[i, 'win_prob_PIN'] = row1_win_prob
df_nba_lines.at[j, 'win_prob_PIN'] = row2_win_prob
# Calculate NORMALIZED win probabilities for each team
prob_sum = row1_win_prob + row2_win_prob
df_nba_lines.at[i, 'win_prob_norm_PIN'] = row1_win_prob / prob_sum
df_nba_lines.at[j, 'win_prob_norm_PIN'] = row2_win_prob / prob_sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment