def get_wins(team_id, season_year, start_datetime, end_datetime): """ Return wins from a given season for a given team. Parameters ---------- team_id : team id (1=ATL,30=WAS) season_year : season ending in year X start_datetime : UNIX timestamp of start of query end_datetime : UNIX timestamp of end of query Returns ------- dict: away_wins of team, home_wins of team, and overall record. """ from nba_database.nba_data_models import BballrefScores as BS from nba_database.queries import team_abbreviation, epochtime away_query = BS.select().where( BS.season_year == season_year, BS.away_team_id == team_id, BS.away_pts > 0, BS.datetime >= epochtime(start_datetime), BS.datetime <= epochtime(end_datetime), ) away_results = [[i.away_pts, i.home_pts] for i in away_query] away_wins_total = sum([1 if x[0] > x[1] else 0 for x in away_results]) away_games_total = len(away_results) # home_query = BS.select().where(BS.season_year == season_year, BS.home_team_id == team_id, BS.home_pts > 0) home_query = BS.select().where( BS.season_year == season_year, BS.home_team_id == team_id, BS.away_pts > 0, BS.datetime >= epochtime(start_datetime), BS.datetime <= epochtime(end_datetime), ) home_results = [[i.home_pts, i.away_pts] for i in home_query] home_wins_total = sum([1 if x[0] > x[1] else 0 for x in home_results]) home_games_total = len(home_results) away_record = str(away_wins_total) + "-" + str(away_games_total - away_wins_total) home_record = str(home_wins_total) + "-" + str(home_games_total - home_wins_total) record = (str(away_wins_total + home_wins_total) + "-" + str(away_games_total + home_games_total - away_wins_total - home_wins_total)) return { "away_record": away_record, "home_record": home_record, "record": record }
# Custom SRS calculation options max_MOV = 100 # no real max MOV home_team_adv = 0 win_floor = 0 wins_dict_list = [ get_wins(i, season_year, start_datetime, end_datetime) for i in range(1, 31) ] wins_list = [[x["away_record"], x["home_record"], x["record"]] for x in wins_dict_list] # Pythagorean Wins lpw_results = league_pythagorean_wins( Game, mincalcdatetime=epochtime(start_datetime), maxcalcdatetime=epochtime(end_datetime), ) srs_list = SRS(games_list, max_MOV=max_MOV, home_team_adv=home_team_adv, win_floor=win_floor) elo_list = elo_ratings_list(epochtime(end_datetime)) form_list = [form_query(i) for i in range(1, 31)] lpw_results.sort(key=lambda x: x[0]) results = list(zip(lpw_results, srs_list, wins_list, elo_list, form_list))
def playoff_odds_calc(start_datetime, end_datetime, season_year, ratings_mode="Elo"): """ Given a start, end, season_year, and a ratings calculation method with some other factors, determine the odds of every team of making the playoffs at any given time. Parameters ---------- start_datetime : start of period to be used for analysis (Unix timestamp) end_datetime : end of period to be used for analysis (Unix timestamp) season_year : year that season nominally ends in (e.g. if season ends in 2021, use 2021) ratings_mode : Elo or SRS. Default mode is Elo. Returns ------- a list of 2-item lists for each team (first item ATL, last item WAS) each list consists of [playoff odds,average wins] """ from predict.cython_mcss.mcss_ext2 import simulations_result_vectorized from analytics.SRS import SRS from analytics.morey import SRS_regress, Elo_regress from nba_database.queries import games_query, games_won_query, future_games_query # Test results/inputs if end_datetime < start_datetime: print("Start date is after end date, please check inputs") return 1 predict_date = end_datetime predict_season_year = season_year # Get List Of Known Wins games_list = games_query(start_datetime, end_datetime) games_won_list_cpp = games_won_query(games_list, return_format="matrix").tolist() # Get team data. teams_list = Team.select().order_by(Team.bball_ref) teams_list = [[ x.bball_ref, x.team_name, x.abbreviation, x.division, x.conf_or_league ] for x in teams_list] #pprint(teams_list) # Division changes go here for z in teams_list: if season_year <= 2003 and z[0] == 19: #Charlotte Hornets to NOH change z[4] = "E" z[3] = "Central" #pprint("Fixed") #pprint(teams_list) # Get future games (away_team, home_team, home_team_win_probability) future_games_list = future_games_query(predict_date, predict_season_year) if ratings_mode == "SRS": # Get Team Ratings (and create Team object list) ratings_list = SRS( games_query(start_datetime, end_datetime)).tolist() # get ratings for that time. for i, x in enumerate(teams_list): x.append(ratings_list[i]) for j in range(1, 5): # "all strings" x[j] = x[j].encode("utf-8") for x in future_games_list: away_team_rating = teams_list[x[0] - 1][5] home_team_rating = teams_list[x[1] - 1][5] SRS_diff = home_team_rating - away_team_rating x.append(SRS_regress(SRS_diff)) if ratings_mode == "Elo": ratings_list = elo_ratings_list(epochtime(end_datetime)) for i, x in enumerate(teams_list): x.append(ratings_list[i]) for j in range(1, 5): # "all strings" x[j] = x[j].encode("utf-8") for x in future_games_list: away_team_rating = teams_list[x[0] - 1][5] home_team_rating = teams_list[x[1] - 1][5] Elo_diff = home_team_rating - away_team_rating x.append(Elo_regress(Elo_diff)) team_results = simulations_result_vectorized(games_won_list_cpp, future_games_list, teams_list, season_year) # Return (top 8 odds, average wins, top 6 odds, and play in tournament odds). team_results = [[x[0] * 100.0, x[1], x[2] * 100.0, x[3] * 100.0] for x in team_results] return team_results
# pprint(d) d["home_team"] = d["Home/Neutral"] d["away_team"] = d["Visitor/Neutral"] d["away_pts"] = d["Visitor_PTS"] d["home_pts"] = d["Home_PTS"] d["home_team_id"] = full_name_to_id(d["Home/Neutral"]) d["away_team_id"] = full_name_to_id(d["Visitor/Neutral"]) d["date"] = d["Date"] d["season_year"] = season_year_start + 1 # standardized date conversion datestr = d["Date"] datefmt = "%a %b %d %Y" date_datetime = datetime.strptime(datestr, datefmt) d["datetime"] = epochtime(date_datetime) d["date"] = date_datetime.strftime("%Y-%m-%d") # remove unused keys d.pop("Visitor/Neutral", None) d.pop("Home/Neutral", None) d.pop("Attend.", None) d.pop("x1", None) d.pop("x2", None) d.pop("Home_PTS", None) d.pop("Visitor_PTS", None) d.pop("Notes", None) d.pop("Start (ET)", None) d.pop("Date", None) d.pop("Unnamed: 5", None) d.pop("Unnamed: 6", None)