Skip to content

Instantly share code, notes, and snippets.

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)
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']:[i, 'outcome'] = 'L'[j, 'outcome'] = 'W'
elif row1['score'] > row2['score']:[i, 'outcome'] = 'W'[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'])[i, 'win_prob_PIN'] = row1_win_prob[j, 'win_prob_PIN'] = row2_win_prob
# Calculate NORMALIZED win probabilities for each team
prob_sum = row1_win_prob + row2_win_prob[i, 'win_prob_norm_PIN'] = row1_win_prob / prob_sum[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