Skip to content

Instantly share code, notes, and snippets.

@lambertchu
Last active December 29, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambertchu/fa3ca617c05a731da1488368bc3196a4 to your computer and use it in GitHub Desktop.
Save lambertchu/fa3ca617c05a731da1488368bc3196a4 to your computer and use it in GitHub Desktop.
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