-
-
Save lambertchu/fa3ca617c05a731da1488368bc3196a4 to your computer and use it in GitHub Desktop.
NBA betting analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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