def main(team1, team2, num_days, num_samples, week, half_life): league = get_league() decay_rate = np.log(2) / half_life print( tabulate([["Team", "Manager"]] + [[t.name, t.manager_name] for t in league.teams])) if team1 is None: team1 = league.current_team else: team1 = league.team_by_owner(team1) if team2 is None: team2 = league.get_matchup(team1, week=week) else: team2 = league.team_by_owner(team2) pyfiglet.print_figlet("%s vs. %s" % (team1.manager_name, team2.manager_name), font='banner', width=160) if week: pyfiglet.print_figlet("Week %u" % week, font='big') print("Roster:") print_roster(team1.roster(week=week)) print("Roster:") print_roster(team2.roster(week=week)) projections = visualize_matchup([team1], team2, num_days=num_days, num_samples=num_samples, week=week, decay_rate=decay_rate) #print(projections[0][0].round(2).to_csv()) print("Projections") print("=====================") with pd.option_context('display.max_rows', None, 'display.max_columns', None): print(projections[0][0].round(2))
import numpy as np import pandas as pd import pyfiglet import click from tabulate import tabulate import logging logging.basicConfig() logging.getLogger().setLevel(logging.INFO) from nba_matchup import get_league, visualize_matchup, CURRENT_WEEK, print_roster league = get_league() @click.command() @click.option('--team1', type=str, default=None) @click.option('--team2', type=str, default=None) @click.option('--num_days', type=int, default=30) @click.option('--num_samples', type=int, default=50000) @click.option('--week', type=int, default=CURRENT_WEEK) @click.option('--half_life', type=float, default=14) def main(team1, team2, num_days, num_samples, week, half_life): league = get_league() decay_rate = np.log(2) / half_life print( tabulate([["Team", "Manager"]] + [[t.name, t.manager_name] for t in league.teams])) if team1 is None: team1 = league.current_team else: team1 = league.team_by_owner(team1) if team2 is None: team2 = league.get_matchup(team1, week=week)
def main(team1, team2, num_days, num_samples, week, num_fa, num_minimax, num_iters, ignore_player, half_life, metric, ignore_injured): league = get_league() decay_rate = np.log(2) / half_life if team1 is None: team1 = league.current_team else: team1 = league.team_by_owner(team1) if team2 is None: team2 = league.get_matchup(team1, week=week) else: team2 = league.team_by_owner(team2) pyfiglet.print_figlet("%s vs. %s" % (team1.manager_name, team2.manager_name), font='banner', width=160) pyfiglet.print_figlet("Week %u" % week, font='big') if metric == 'ev': metric_fn = ev else: metric_fn = winning_prob old_team1_roster = team1.roster(week=week) old_team2_roster = team2.roster(week=week) def team1_score(roster): cats, points, scores, _ = simulate_h2h(roster, team2.roster(week=week), num_days=num_days, num_samples=num_samples, week=week, decay_rate=decay_rate) return metric_fn(cats, points, scores, num_samples) def team2_score(roster): cats, points, scores, _ = simulate_h2h(roster, team1.roster(week=week), num_days=num_days, num_samples=num_samples, week=week, decay_rate=decay_rate) return metric_fn(cats, points, scores, num_samples) print("%s's roster:" % team1.manager_name, team1_score(old_team1_roster)) print_roster(old_team1_roster) print("%s's roster:" % team2.manager_name, team2_score(old_team2_roster)) print_roster(old_team2_roster) print("Adding free agents:") for agent in get_free_agents(num_fa): print(agent.name) old_team1_roster = old_team1_roster.add(agent, "BN") # old_team2_roster = old_team2_roster.add(agent, "BN") team1.set_roster(old_team1_roster) team2.set_roster(old_team2_roster) print("Ignoring players:", ", ".join(ignore_player)) for i in range(num_minimax): for team, score_fn in zip([team2, team1], [team2_score, team1_score]): roster = team.roster(week=week) print("===========================================") print("Minimax[%u]: %s" % (i + 1, team.manager_name)) for roster, score in simulated_annealing( roster, score_fn, ignore_players={ roster.player_by_name(n) for n in ignore_player }, num_steps=num_iters, ignore_injured=ignore_injured): pass print("%s's optimized roster:" % team.manager_name, score) print( tabulate([[position, player.name] for player, position in roster.positions.items() if position not in {"BN", "IL"}])) team.set_roster(roster) projections = visualize_matchup([team1], team2, num_days=num_days, num_samples=100000, week=week, decay_rate=decay_rate, show_plots=False) with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.expand_frame_repr', False): for i, team in enumerate([team1, team2]): print("===========================================") print("%s's projections:" % team.manager_name) print(projections[0][i].round(2))
def main(team1, team2, num_days, num_samples, week, half_life, player1, player2, eval_team): player1_name, player2_name = team1, team2 decay_rate = np.log(2) / half_life league = get_league() print( tabulate([["Team", "Manager"]] + [[t.name, t.manager_name] for t in league.teams])) if team1 is None: team1 = league.current_team else: team1 = league.team_by_owner(team1) if team2 is None: team2 = league.get_matchup(team1, week=week) else: team2 = league.team_by_owner(team2) base = START_DATE + datetime.timedelta(days=7 * (week - 1) + 7) week_length = 7 team1_roster = team1.roster(week=week) team2_roster = team2.roster(week=week) old_team1_stats, _ = team1_roster.stats(num_days, base_date=base, week_length=week_length) old_team1_average = old_team1_stats.mean(axis=0) old_team1_average[ 'FG%'] = old_team1_average["FGM"] / old_team1_average["FGA"] old_team1_average[ 'FT%'] = old_team1_average["FTM"] / old_team1_average["FTA"] old_team2_stats, _ = team2_roster.stats(num_days, base_date=base, week_length=week_length) old_team2_average = old_team2_stats.mean(axis=0) old_team2_average[ 'FG%'] = old_team2_average["FGM"] / old_team2_average["FGA"] old_team2_average[ 'FT%'] = old_team2_average["FTM"] / old_team2_average["FTA"] print(f"{team1.manager_name}'s roster:") print_roster(team1_roster) print(f"{team2.manager_name}'s roster:") print_roster(team2_roster) new_team1_roster = team1_roster.copy() new_team2_roster = team2_roster.copy() player1 = set(player1) player2 = set(player2) for p in player1: player = new_team1_roster.player_by_name(p) new_team1_roster = new_team1_roster.remove(player) new_team2_roster = new_team2_roster.add(player, 'BN') for p in player2: player = new_team2_roster.player_by_name(p) new_team2_roster = new_team2_roster.remove(player) new_team1_roster = new_team1_roster.add(player, 'BN') team1.set_roster(new_team1_roster) print_roster(new_team1_roster, include_bench=True, include_injured=True) print_roster(new_team2_roster, include_bench=True, include_injured=True) other_players = [ team.roster(week=week) for team in league.teams if team.manager_name not in {player1_name, player2_name} ] teams_to_eval = [league.team_by_owner(name) for name in eval_team] print("Evaluating %s" % team1.manager_name) old_team1_scores = score_team(team1_roster, teams_to_eval, num_days, num_samples, week, decay_rate) team1_scores = score_team(new_team1_roster, teams_to_eval, num_days, num_samples, week, decay_rate) improvements = [ team1_scores[k] - old_team1_scores[k] for k in old_team1_scores ] print( tabulate([[ k.manager_name, old_team1_scores[k], team1_scores[k], team1_scores[k] - old_team1_scores[k] ] for k in old_team1_scores])) print("Average improvement:", np.mean(improvements)) print("Evaluating %s" % team2.manager_name) old_team2_scores = score_team(team2_roster, teams_to_eval, num_days, num_samples, week, decay_rate) team2_scores = score_team(new_team2_roster, teams_to_eval, num_days, num_samples, week, decay_rate) improvements = [ team2_scores[k] - old_team2_scores[k] for k in old_team2_scores ] print( tabulate([[ k.manager_name, old_team2_scores[k], team2_scores[k], team2_scores[k] - old_team2_scores[k] ] for k in old_team2_scores])) print("Average improvement:", np.mean(improvements))
def main(team1, team2, num_days, num_samples, week, num_fa, num_iters, ignore_player, half_life, metric, ignore_injured): league = get_league() decay_rate = np.log(2) / half_life # if week == 19: week = 18 # if week >= 19: # week -= 1 if team1 is None: team1 = league.current_team else: team1 = league.team_by_owner(team1) if team2 is None: team2 = league.get_matchup(team1, week=week) else: team2 = league.team_by_owner(team2) pyfiglet.print_figlet("%s vs. %s" % (team1.manager_name, team2.manager_name), font='banner', width=160) if week: pyfiglet.print_figlet("Week %u" % week, font='big') if metric == 'ev': metric_fn = ev else: metric_fn = winning_prob def roster_score(roster): cats, points, scores, _ = simulate_h2h(roster, team2.roster(week=week), num_days=num_days, num_samples=num_samples, week=week, decay_rate=decay_rate) return metric_fn(cats, points, scores, num_samples) def reverse_roster_score(roster): cats, points, scores, _ = simulate_h2h(roster, team1.roster(week=week), num_days=num_days, num_samples=num_samples, week=week, decay_rate=decay_rate) return metric_fn(cats, points, scores, num_samples) print("%s's roster:" % team1.manager_name, roster_score(team1.roster(week=week))) print( tabulate( [[position, player.name] for player, position in team1.roster(week=week).positions.items() if position not in {"BN", "IL"}])) print("%s's roster:" % team2.manager_name, reverse_roster_score(team2.roster(week=week))) print( tabulate( [[position, player.name] for player, position in team2.roster(week=week).positions.items() if position not in {"BN", "IL"}])) print("Optimizing %s's lineup" % team1.manager_name) print("===========================================") roster = team1.roster(week=week) old_roster = roster print("Adding free agents:") for agent in get_free_agents(num_fa): print(agent.name) roster = roster.add(agent, "BN") team1.set_roster(roster) print("Ignoring players:", ", ".join(ignore_player)) scores = [] for roster, score in simulated_annealing( roster, roster_score, ignore_players={ team1.roster(week=week).player_by_name(n) for n in ignore_player }, num_steps=num_iters, ignore_injured=ignore_injured): scores.append(score) # print(tabulate([ # [position, player.name] for player, position in # roster.positions.items() if position not in {"BN", "IL"} # ])) print("%s's optimized roster:" % team1.manager_name, score) print( tabulate([[position, player.name] for player, position in roster.positions.items() if position not in {"BN", "IL"}])) def team_generator(): for r in [old_roster, roster]: team1.set_roster(r) yield team1 projections = visualize_matchup(team_generator(), team2, num_days=num_days, num_samples=100000, week=week, decay_rate=decay_rate, show_plots=False) with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.expand_frame_repr', False): for i, team in enumerate([team1, team2]): print("===========================================") print("%s's projections:" % team.manager_name) print(projections[1][i].round(2)) plt.figure() plt.plot(scores) plt.show()