1 Assignment 4 1.1 Description
assignment4
September 25, 2020
In this assignment you must read in a file of metropolitan regions and associated sports teams from assets/wikipedia_data.html and answer some questions about each metropolitan region. Each of these regions may have one or more teams from the “Big 4”: NFL (football, in assets/ nfl.csv), MLB (baseball, in assets/mlb.csv), NBA (basketball, in assets/nba.csv or NHL (hockey, in assets/nhl.csv). Please keep in mind that all questions are from the perspective of the metropolitan region, and that this file is the “source of authority” for the location of a given sports team. Thus teams which are commonly known by a different area (e.g. “Oakland Raiders”) need to be mapped into the metropolitan region given (e.g. San Francisco Bay Area). This will require some human data understanding outside of the data you’ve been given (e.g. you will have to hand-code some names, and might need to google to find out where teams are)!
For each sport I would like you to answer the question: what is the win/loss ratio’s corre- lation with the population of the city it is in? Remember that to calculate the correlation with pearsonr, so you are going to send in two ordered lists of values, the populations from the wikipedia_data.html file and the win/loss ratio for a given sport in the same order. Average the win/loss ratios for those cities which have multiple teams of a single sport. Each sport is worth an equal amount in this assignment (20%*4=80%) of the grade for this assignment. You should only use sports data from year 2018 for your analysis but use populations data from year 2016 – this is important!
1.2 Notes
1. Do not including data about the MLS or CFL in any of the work you are doing, we’re only interested in the Big 4 in this assignment.
2. I highly suggest that you first tackle the four correlation questions in order, as they are all similar and worth the majority of grades for this assignment. This is by design!
3. It’s fair game to talk with peers about high level strategy as well as the relationship between metropolitan areas and sports teams. However, do not post code solving aspects of the assignment (including such as dictionaries mapping areas to teams, or regexes which will clean up names).
4. There may be more teams than the assert statements test, remember to collapse multiple teams in one city into a single value!
1
1.3 Question 1
For this question, calculate the win/loss ratio’s correlation with the population of the city it is in for the NHL using 2018 data.
[ ]:
import pandas as pd
import numpy as np
import scipy.stats as stats
import re
nhl_df=pd.read_csv(“assets/nhl.csv”)
cities=pd.read_html(“assets/wikipedia_data.html”)[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]
def nhl_correlation():
# YOUR CODE HERE
raise NotImplementedError()
population_by_region = [] # pass in metropolitan area population from cities
win_loss_by_region = [] # pass in win/loss ratio from nhl_df in the same␣ →order as cities[“Metropolitan area”]
assert len(population_by_region) == len(win_loss_by_region), “Q1: Your␣ →lists must be the same length”
assert len(population_by_region) == 28, “Q1: There should be 28 teams being␣ →analysed for NHL”
return stats.pearsonr(population_by_region, win_loss_by_region)
[ ]:
[ ]:
1.4 Question 2
For this question, calculate the win/loss ratio’s correlation with the population of the city it is in for the NBA using 2018 data.
import pandas as pd
import numpy as np
import scipy.stats as stats
import re
nba_df=pd.read_csv(“assets/nba.csv”)
cities=pd.read_html(“assets/wikipedia_data.html”)[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]
def nba_correlation():
2
# YOUR CODE HERE
raise NotImplementedError()
population_by_region = [] # pass in metropolitan area population from cities
win_loss_by_region = [] # pass in win/loss ratio from nba_df in the same␣ →order as cities[“Metropolitan area”]
assert len(population_by_region) == len(win_loss_by_region), “Q2: Your␣ →lists must be the same length”
assert len(population_by_region) == 28, “Q2: There should be 28 teams being␣ →analysed for NBA”
return stats.pearsonr(population_by_region, win_loss_by_region)
[ ]:
[ ]:
1.5 Question 3
For this question, calculate the win/loss ratio’s correlation with the population of the city it is in for the MLB using 2018 data.
import pandas as pd
import numpy as np
import scipy.stats as stats
import re
mlb_df=pd.read_csv(“assets/mlb.csv”)
cities=pd.read_html(“assets/wikipedia_data.html”)[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]
def mlb_correlation():
# YOUR CODE HERE
raise NotImplementedError()
population_by_region = [] # pass in metropolitan area population from cities
win_loss_by_region = [] # pass in win/loss ratio from mlb_df in the same␣ →order as cities[“Metropolitan area”]
assert len(population_by_region) == len(win_loss_by_region), “Q3: Your␣ →lists must be the same length”
assert len(population_by_region) == 26, “Q3: There should be 26 teams being␣ →analysed for MLB”
return stats.pearsonr(population_by_region, win_loss_by_region)
[ ]:
3
1.6 Question 4
For this question, calculate the win/loss ratio’s correlation with the population of the city it is in for the NFL using 2018 data.
[ ]:
import pandas as pd
import numpy as np
import scipy.stats as stats
import re
nfl_df=pd.read_csv(“assets/nfl.csv”)
cities=pd.read_html(“assets/wikipedia_data.html”)[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]
def nfl_correlation():
# YOUR CODE HERE
raise NotImplementedError()
population_by_region = [] # pass in metropolitan area population from cities
win_loss_by_region = [] # pass in win/loss ratio from nfl_df in the same␣ →order as cities[“Metropolitan area”]
assert len(population_by_region) == len(win_loss_by_region), “Q4: Your␣ →lists must be the same length”
assert len(population_by_region) == 29, “Q4: There should be 29 teams being␣ →analysed for NFL”
return stats.pearsonr(population_by_region, win_loss_by_region)
[ ]:
1.7 Question 5
In this question I would like you to explore the hypothesis that given that an area has two sports teams in different sports, those teams will perform the same within their respective sports. How I would like to see this explored is with a series of paired t-tests (so use ttest_rel) between all pairs of sports. Are there any sports where we can reject the null hypothesis? Again, average values where a sport has multiple teams in one region. Remember, you will only be including, for each sport, cities which have teams engaged in that sport, drop others as appropriate. This question is worth 20% of the grade for this assignment.
import pandas as pd
import numpy as np
import scipy.stats as stats
import re
[ ]:
4
mlb_df=pd.read_csv(“assets/mlb.csv”)
nhl_df=pd.read_csv(“assets/nhl.csv”)
nba_df=pd.read_csv(“assets/nba.csv”)
nfl_df=pd.read_csv(“assets/nfl.csv”)
cities=pd.read_html(“assets/wikipedia_data.html”)[1]
cities=cities.iloc[:-1,[0,3,5,6,7,8]]
def sports_team_performance(): # YOUR CODE HERE
raise NotImplementedError()
# Note: p_values is a full dataframe, so df.loc[“NFL”,”NBA”] should be the␣ →same as df.loc[“NBA”,”NFL”] and
# df.loc[“NFL”,”NFL”] should return np.nan
sports = [‘NFL’, ‘NBA’, ‘NHL’, ‘MLB’]
p_values = pd.DataFrame({k:np.nan for k in sports}, index=sports)
assert abs(p_values.loc[“NBA”, “NHL”] – 0.02) <= 1e-2, "The NBA-NHL p-value␣ →should be around 0.02"
assert abs(p_values.loc["MLB", "NFL"] - 0.80) <= 1e-2, "The MLB-NFL p-value␣ →should be around 0.80"
return p_values
[ ]:
5