-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_descriptives_elo_odds.py
210 lines (169 loc) · 9.82 KB
/
plot_descriptives_elo_odds.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
import csv
import pandas as pd
import numpy as np
import warnings
import argparse
from datetime import datetime
import ast
import os
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rcParams
# Set global font to Times New Roman and increase font sizes
rcParams['font.family'] = 'serif'
rcParams['font.serif'] = ['Times New Roman']
rcParams['font.size'] = 20 # Default font size for all text
rcParams['axes.titlesize'] = 20 # Title font size
rcParams['axes.labelsize'] = 20 # Axes labels font size
rcParams['xtick.labelsize'] = 20 # X-axis tick labels font size
rcParams['ytick.labelsize'] = 20 # Y-axis tick labels font size
rcParams['legend.fontsize'] = 16 # Legend font size
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('-p1', '--player_1', type=str, required=False, default='all', help='player name in format Last Name Initial., enclosed in double quotes e.g., "Djokovic N." (default = all players)')
parser.add_argument('-p2', '--player_2', type=str, required=False, default='all', help='player name in format Last Name Initial., enclosed in double quotes e.g., "Djokovic N." (default = all players)')
parser.add_argument('-d', '--dataset', type=str, required=True, help='atp or wta')
parser.add_argument('-g', '--grand_slam', type=int, required=False, default=2, help='0 = non grand slams, 1 = grand slams only, 2 = grand slams and non grand slams (default)')
parser.add_argument('-t', '--tournament', type=str, required=False, default='all', help='tournament name, e.g., Australian Open')
parser.add_argument('-s', '--s_date', type=str, required=False, default='min', help='start date in YYYY-MM-DD format (default = min date in dataset)')
parser.add_argument('-e', '--e_date', type=str, required=False, default='max', help='end date in YYYY-MM-DD format (default = max date in dataset)')
parser.add_argument('-p', '--p_adj_method', type=str, required=False, default='BH', help='p-value adjustment for multiple comparisons method, e.g., bonferroni, hochberg, BH, holm, hommel, BY')
parser.add_argument('-u', '--upset', type=str, required=False, choices=['odds','elo'], default='odds', help='whether an unexpected result is based on the betting odds or elo rating (default=odds)')
args, _ = parser.parse_known_args()
def get_grand_slam_description(grand_slam_value):
if grand_slam_value == 0:
return "Non Grand Slams"
elif grand_slam_value == 1:
return "Grand Slams Only"
elif grand_slam_value == 2:
return "All Tournaments" # This could also be "Grand Slams and Non Grand Slams"
def main():
if args.dataset == 'test':
df = pd.read_csv('Data_Clean_Test.csv', low_memory=False)
elif args.dataset == 'wta':
df = pd.read_csv('Data_Clean_WTA.csv', low_memory=False)
elif args.dataset == 'atp':
df = pd.read_csv('Data_Clean_ATP_Elo_WElo.csv', low_memory=False)
if args.tournament != 'all':
df = df[(df["Tournament"] == args.tournament)]
if args.grand_slam == 0:
df = df[(df["Series"] != "Grand Slam")]
elif args.grand_slam == 1:
df = df[(df["Series"] == "Grand Slam")]
if args.s_date == 'min':
start_date = min(df["Date"])
elif args.s_date != 'min':
start_date = args.s_date
if args.e_date == 'max':
end_date = max(df["Date"])
elif args.e_date != 'max':
end_date = args.e_date
# Restrict the dataframe to be between start date and end date
df = df[((df["Date"] >= start_date) & (df["Date"] <= end_date))]
# if AvgW or AvgL is null, replace with B365 odds
df['AvgW'] = df['AvgW'].fillna(df['B365W'])
df['AvgL'] = df['AvgL'].fillna(df['B365L'])
if args.dataset == 'wta':
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y')
elif args.dataset == 'atp':
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.to_period('Y')
# Combine Elo ratings into a single column
df_elo_melted = df.melt(id_vars=['Year'], value_vars=['Elo_i_after_match', 'Elo_j_after_match'], var_name='Elo_Type', value_name='Elo_rating')
# Combine betting odds into a single column
df_odds_melted = df.melt(id_vars=['Year'], value_vars=['AvgW', 'AvgL'], var_name='Odds_Type', value_name='Betting_odds')
# Group the data by year
grouped = df.groupby(['Year'])
grouped_elo = df_elo_melted.groupby(['Year'])
grouped_odds = df_odds_melted.groupby(['Year'])
# Calculate the variance and mean for Elo ratings and betting odds within each group
agg_winner_df = grouped.agg({
'AvgW': ['mean', 'var'],
'AvgL': ['mean', 'var']}).reset_index()
agg_elo_df = grouped_elo.agg({
'Elo_rating': ['mean', 'var']}).reset_index()
agg_odds_df = grouped_odds.agg({
'Betting_odds': ['mean', 'var']}).reset_index()
# Rename columns for clarity
agg_winner_df.columns = ['Year', 'Betting_Win_Odds_mean', 'Betting_Win_Odds_variance', 'Betting_Lose_Odds_mean', 'Betting_Lose_Odds_variance']
agg_elo_df.columns = ['Year', 'Elo_mean', 'Elo_variance']
agg_odds_df.columns = ['Year', 'Betting_Odds_mean', 'Betting_Odds_variance']
# Calculate the combined mean and variance for betting odds
combined_mean = agg_odds_df['Betting_Odds_mean']
combined_variance = agg_odds_df['Betting_Odds_variance']
# Calculate the coefficient of variation for Elo and Betting odds
agg_elo_df['Elo_CV'] = np.sqrt(agg_elo_df['Elo_variance']) / agg_elo_df['Elo_mean']
agg_winner_df['Betting_Win_Odds_CV'] = np.sqrt(agg_winner_df['Betting_Win_Odds_variance']) / agg_winner_df['Betting_Win_Odds_mean']
agg_winner_df['Betting_Lose_Odds_CV'] = np.sqrt(agg_winner_df['Betting_Lose_Odds_variance']) / agg_winner_df['Betting_Lose_Odds_mean']
agg_winner_df['Combined_Odds_CV'] = np.sqrt(combined_variance) / combined_mean
# Filter out NaN values
agg_elo_df_filtered = agg_elo_df.dropna()
agg_winner_df_filtered = agg_winner_df.dropna()
agg_odds_df_filtered = agg_odds_df.dropna()
# Grand Slam description
grand_slam_desc = get_grand_slam_description(args.grand_slam)
# Plotting the CVs
plt.figure(figsize=(12, 6))
plt.plot(agg_elo_df_filtered['Year'].astype(str), agg_elo_df_filtered['Elo_CV'], marker='o', label='Elo CV')
plt.plot(agg_winner_df_filtered['Year'].astype(str), agg_winner_df_filtered['Betting_Win_Odds_CV'], marker='o', label='Betting Win Odds CV')
plt.plot(agg_winner_df_filtered['Year'].astype(str), agg_winner_df_filtered['Betting_Lose_Odds_CV'], marker='o', label='Betting Lose Odds CV')
plt.plot(agg_winner_df_filtered['Year'].astype(str), agg_winner_df_filtered['Combined_Odds_CV'], marker='o', label='Combined Odds CV')
plt.xlabel('Year')
plt.ylabel('Coefficient of Variation (CV)')
plt.title(f'Yearly Coefficient of Variation (CV) for Betting Odds and Elo Ratings - {args.dataset}, {grand_slam_desc}')
plt.legend(loc='upper left', bbox_to_anchor=(1, 1), framealpha=0.8) # Place legend outside the plot
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
# Create figure and axes objects for Elo Mean and CV
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
# Plot Elo mean
ax1.plot(agg_elo_df_filtered['Year'].astype(str), agg_elo_df_filtered['Elo_mean'], marker='o', color='b', label='Elo Mean')
ax1.set_ylabel('Elo Mean')
ax1.set_title(f'Elo Mean and CV Over Time - {args.dataset}, {grand_slam_desc}')
# Plot Elo variance
ax2.plot(agg_elo_df_filtered['Year'].astype(str), agg_elo_df_filtered['Elo_CV'], marker='o', color='r', label='Elo CV')
ax2.set_ylabel('Elo CV')
ax2.set_xlabel('Year')
# Show legend and grid
ax1.legend(loc='upper left', bbox_to_anchor=(1, 1), framealpha=0.8)
ax2.legend(loc='upper left', bbox_to_anchor=(1, 1), framealpha=0.8)
ax1.grid(True)
ax2.grid(True)
# Rotate x-axis labels for better readability
plt.xticks(rotation=45)
# Adjust layout
plt.tight_layout()
plt.show()
# Create figure and axes objects for Odds Mean and CV
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
# Plot Winner Odds mean
ax1.plot(agg_winner_df_filtered['Year'].astype(str), agg_winner_df_filtered['Betting_Win_Odds_mean'], marker='o', color='b', label='Winner Odds Mean')
ax1.set_ylabel('Odds Mean')
ax1.set_title(f'Odds Mean and CV Over Time - {args.dataset}, {grand_slam_desc}')
# Plot Loser Odds mean
ax1.plot(agg_winner_df_filtered['Year'].astype(str), agg_winner_df_filtered['Betting_Lose_Odds_mean'], marker='o', color='r', label='Loser Odds Mean')
# Plot Combined Odds mean
ax1.plot(agg_winner_df_filtered['Year'].astype(str), combined_mean, marker='o', color='g', label='Combined Odds Mean')
# Plot Winner Odds CV (square root of variance)
ax2.plot(agg_winner_df_filtered['Year'].astype(str), np.sqrt(agg_winner_df_filtered['Betting_Win_Odds_variance']), marker='o', color='b', label='Winner Odds CV')
ax2.set_ylabel('Odds CV')
ax2.set_xlabel('Year')
# Plot Loser Odds CV (square root of variance)
ax2.plot(agg_winner_df_filtered['Year'].astype(str), np.sqrt(agg_winner_df_filtered['Betting_Lose_Odds_variance']), marker='o', color='r', label='Loser Odds CV')
# Plot Combined Odds CV (square root of variance)
ax2.plot(agg_winner_df_filtered['Year'].astype(str), np.sqrt(combined_variance), marker='o', color='g', label='Combined Odds CV')
# Show legend and grid
ax1.legend(loc='upper left', bbox_to_anchor=(1, 1), framealpha=0.8)
ax2.legend(loc='upper left', bbox_to_anchor=(1, 1), framealpha=0.8)
ax1.grid(True)
ax2.grid(True)
# Rotate x-axis labels for better readability
plt.xticks(rotation=45)
# Adjust layout
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()