def show_example(): batter = sing.get_players(name="Aaron Judge")[0] pitching_team = "Rays" candidate_pitchers = sing.get_players(team_name=pitching_team, position=["P"], active=True, on_40=True) atmosph = Atmosphere(sing.get_venues(stadium_name="Yankee Stadium")[0], temperature=70, home_team=sing.get_teams(name="Yankees")[0]) state = State(inning=9, top=False, bat_score=3, fld_score=3, on_1b=True, on_2b=True, on_3b=True, outs=2) matchups = [ Matchup(batter=batter, pitcher=p, atmosphere=atmosph, state=state) for p in candidate_pitchers ] results = pd.DataFrame(sing.get_pa_sim(matchups)) print(f'\nBest pitcher to face Aaron Judge with the game on the line') pprint(results.sort_values(by=["obp_exp"]))
def show_example(): batter_list = [ 'Mookie Betts', 'Cody Bellinger', 'Corey Seager', 'Manny Machado' ] pitcher_list = ['Chris Paddack', 'Clayton Kershaw'] candidate_batters = [ sing.get_players(name=player_name)[0] for player_name in batter_list ] candidate_pitchers = [ sing.get_players(name=player_name)[0] for player_name in pitcher_list ] venue = sing.get_venues(stadium_name="Dodger Stadium")[0] game_state = State(on_1b=False, on_2b=True, on_3b=False, inning=4, outs=0, top=True, bat_score=3, fld_score=5, pitch_number=50) atmosph = Atmosphere(venue, temperature=70, home_team=sing.get_teams(name="Dodgers")[0]) matchups = ([ Matchup(batter=m, pitcher=p, state=game_state, atmosphere=atmosph) for m in candidate_batters for p in candidate_pitchers ]) results = pd.DataFrame(sing.get_pa_sim(matchups)) print(f'\nResults') print(results.sort_values(by=["woba_exp"], ascending=False))
def get_predictions(args): batter_list = [ sing.get_players(name=b.strip())[0] for b in args.batters.split(',') ] pitcher_list = [ sing.get_players(name=p.strip())[0] for p in args.pitchers.split(',') ] state = State(inning=args.inning, on_1b=args.on1b, on_2b=args.on2b, on_3b=args.on3b, outs=args.outs, top=not (args.bottom), bat_score=args.batscore, fld_score=args.fieldscore, pitch_number=args.pitchnumber) venue = sing.get_venues(stadium_name=args.venue)[0] atmosphere = Atmosphere(venue=venue, home_team=sing.get_teams(name=args.hometeam)[0], temperature=args.temperature) matchups = [ Matchup(batter=m, pitcher=p, atmosphere=atmosphere, state=state, date=args.date) for m in batter_list for p in pitcher_list ] results = pd.DataFrame( sing.get_pa_sim(matchups, return_features=args.showinputs, model_name=args.predictiontype)) return results
def show_example(): batter_list = ['Mookie Betts'] pitcher_list = ['Chris Paddack'] candidate_batters = [ sing.get_players(name=player_name)[0] for player_name in batter_list ] candidate_pitchers = [ sing.get_players(name=player_name)[0] for player_name in pitcher_list ] venue = sing.get_venues(stadium_name="Dodger Stadium")[0] atmosph = Atmosphere(venue, temperature=70, home_team=sing.get_teams(name="Dodgers")[0]) state = State(on_1b=False, on_2b=True, on_3b=False, pitch_number=79, inning=4, top=False, bat_score=3, fld_score=5) matchups = ([ Matchup(batter=m, pitcher=p, atmosphere=atmosph, state=state) for m in candidate_batters for p in candidate_pitchers ]) results = pd.DataFrame(sing.get_pa_sim(matchups, return_features=True)) print(f'\nResults') print(results.sort_values(by=["woba_exp"], ascending=False))
def show_example(): candidate_batters = (sing.get_players(position=["1B", "DH"], bat_side=["R"], age_min=32, active=True)) candidate_pitchers = [ sing.get_players(name=p)[0] for p in ['J.A. Happ', 'James Paxton', 'Jordan Montgomery'] ] venue = sing.get_venues(stadium_name="Yankee Stadium")[0] atmosph = Atmosphere(venue, temperature=70, home_team=sing.get_teams(name="Yankee")[0]) matchups = ([ Matchup(batter=m, pitcher=p, atmosphere=atmosph, state=State()) for m in candidate_batters for p in candidate_pitchers ]) results = pd.DataFrame(sing.get_pa_sim(matchups)) print(results.sort_values(by=["woba_exp"], ascending=False))
def find_best_reliever(pitchers, sims): print(f'\nFinding the best closer by simulating {sims} innings') home_lineup_positions = [ LineupPos(player=sing.get_players(name='Austin Hays')[0], position='CF'), LineupPos(player=sing.get_players(name='Anthony Santander')[0], position='RF'), LineupPos(player=sing.get_players(name='Jose Iglesias')[0], position='SS'), LineupPos(player=sing.get_players(name='Rio Ruiz')[0], position='3B'), LineupPos(player=sing.get_players(name='Hanser Alberto')[0], position='2B'), LineupPos(player=sing.get_players(name='Renato Nunez')[0], position='1B'), LineupPos(player=sing.get_players(name='Dwight Smith')[0], position='DH'), LineupPos(player=sing.get_players(name='Pedro Severino')[0], position='C'), LineupPos(player=sing.get_players(name='DJ Stewart')[0], position='CF'), LineupPos(player=sing.get_players(name='Tommy Milone')[0], position='P'), ] visit_lineup_positions = [ LineupPos(player=sing.get_players(name='Yandy Diaz')[0], position='3B'), LineupPos(player=sing.get_players(name='Jose Martinez')[0], position='1B'), LineupPos(player=sing.get_players(name='Yoshi Tsutsugo')[0], position='DH'), LineupPos(player=sing.get_players(name='Hunter Renfroe')[0], position='RF'), LineupPos(player=sing.get_players(name='Manuel Margot')[0], position='LF'), LineupPos(player=sing.get_players(name='Mike Brosseau')[0], position='3B'), LineupPos(player=sing.get_players(name='Willy Adames')[0], position='SS'), LineupPos(player=sing.get_players(name='Kevin Kiermaier')[0], position='CF'), LineupPos(player=sing.get_players(name='Mike Zunino')[0], position='C'), LineupPos(player=sing.get_players(name='Tyler Glasnow')[0], position='P'), ] B = pd.Index([ f'{batter.player.full_name} ({batter.player.bat_side})' for batter in home_lineup_positions ], name="rows")[0:9] P = pd.Index([ f'{pitcher.full_name} ({pitcher.pitch_hand})' for pitcher in test_pitcher_list ], name="columns") home_lineup = Lineup(lineup=home_lineup_positions) df = pd.DataFrame('', index=B, columns=P) location = sing.get_venues(stadium_name='Oriole Park')[0] home_team = sing.get_teams(name="Orioles")[0] #assume, visit team has one run lead in the bottom of the 10th inning. Based on 2020 rules, home team starts with a runner on 2B and no one out bat_score_start = 2 fld_score_start = 3 for bat_lineup_start in range( 1, 10 ): #loop through batters. Skip last batter (pitcher) because of DH st = State(inning=10, top=False, on_1b=False, on_2b=True, on_3b=False, outs=0, bat_score=bat_score_start, fld_score=fld_score_start, bat_lineup_order=bat_lineup_start) pitcher_count = 0 for pitcher in pitchers: #loop through pitchers pitcher_count = pitcher_count + 1 #track which pitcher we are using visit_lineup_positions[9] = LineupPos( player=sing.get_players(id=pitcher.mlb_id)[0], position='P') visit_lineup = Lineup(lineup=visit_lineup_positions) game = Game(visit_lineup=visit_lineup, home_lineup=home_lineup, atmosphere=Atmosphere(venue=location, home_team=home_team)) #run the simulation. it will return an array of GameSimResults game_sim_results = sing.get_game_sim( { 'game': game, 'start_state': st }, num_sims=sims) saves = len( [1 for r in game_sim_results if (r.away_score > r.home_score)]) losses = len( [1 for r in game_sim_results if (r.away_score < r.home_score)]) ties = len([ 1 for r in game_sim_results if (r.away_score == r.home_score) ]) print( 'Pitcher: {:<20s} Lineup Start: {} Save Percentage: {:.1f}% Loss Percentage: {:.1f}% Tie Percentage: {:.1f}%' .format(pitcher.full_name, bat_lineup_start, 100 * saves / sims, 100 * losses / sims, 100 * ties / sims)) df.loc[B[bat_lineup_start - 1]][P[pitcher_count - 1]] = round( saves / sims, 3) for col in df.columns: df[col] = df[col].astype(float) return df
########################################## # Simple batter vs. pitcher plate appearance prediction using defaults ########################################## from common import sing from pprint import pprint from singlearity import State, Player, Team, Venue, Atmosphere, Matchup, ApiException from singlearity.rest import ApiException import pandas as pd batter_list = ['Mookie Betts', 'Cody Bellinger', 'Gavin Lux'] pitcher_list = ['Chris Paddack'] candidate_batters = [sing.get_players(name = player_name)[0] for player_name in batter_list] candidate_pitchers = [sing.get_players(name = player_name)[0] for player_name in pitcher_list] venue = sing.get_venues(stadium_name = "Dodger Stadium")[0] atmosph = Atmosphere(venue, temperature = 70, home_team = sing.get_teams(name = "Dodgers")[0]) matchups = ([Matchup(batter = m, pitcher = p, atmosphere = atmosph, state=State()) for m in candidate_batters for p in candidate_pitchers]) results = pd.DataFrame(sing.get_pa_sim(matchups)) #just get some interesting stats. comment below line out to see all predictions results = results[['batter_name', 'pitcher_name', 'hr_exp', 'so_exp', 'ba_exp', 'ops_exp', 'woba_exp']] print(f'Results:\n{results}')