simulations

class pybbda.analysis.simulations.BaseState(first_base: int, second_base: int, third_base: int)[source]

A class to represent a base state. An occupied base is represented by a 1 and an empty base by a 0.

evolve(batting_event, first_base_running_event=<FirstBaseRunningEvent.DEFAULT: 0>, second_base_running_event=<SecondBaseRunningEvent.DEFAULT: 0>, third_base_running_event=<ThirdBaseRunningEvent.DEFAULT: 0>)[source]
class pybbda.analysis.simulations.BaseOutState(base_state: pybbda.analysis.simulations.components.state.BaseState, outs: int, max_outs: int = 3)[source]
evolve(batting_event, first_base_running_event=<FirstBaseRunningEvent.DEFAULT: 0>, second_base_running_event=<SecondBaseRunningEvent.DEFAULT: 0>, third_base_running_event=<ThirdBaseRunningEvent.DEFAULT: 0>)[source]
static get_running_events(batting_event, first_base_running_event, second_base_running_event, third_base_running_event)[source]
static runs_scored(initial_state, end_state)[source]

Computes the runs scored from state initial_state to state end_state. The computation is based on the assumption that the number of base runners plus the number of outs in the end state is equal to the number of base runners plus the number of outs in the start state plus one (the batter) minus the runs.

Parameters
  • initial_stateBaseOutState

  • end_stateBaseOutState

Returns

int

Raises

ValueError if runs scored is less than zero

class pybbda.analysis.simulations.GameState(base_out_state: pybbda.analysis.simulations.components.state.BaseOutState = BaseOutState(base_state=BaseState(first_base=0, second_base=0, third_base=0), outs=0, max_outs=3), pa_count: int = 1, lineup_slot: int = 1, score: int = 0)[source]
evolve(batting_event, first_base_running_event=<FirstBaseRunningEvent.DEFAULT: 0>, second_base_running_event=<SecondBaseRunningEvent.DEFAULT: 0>, third_base_running_event=<ThirdBaseRunningEvent.DEFAULT: 0>)[source]
static pa_count_to_lineup_slot(pa_count)[source]
class pybbda.analysis.simulations.BattingEvent[source]

An enumeration.

BASE_ON_BALLS = 0
DOUBLE = 2
HOME_RUN = 4
OUT = 5
SINGLE = 1
TRIPLE = 3
class pybbda.analysis.simulations.RunningEvent(first_base_running_event, second_base_running_event, third_base_running_event)
count()

Return number of occurrences of value.

property first_base_running_event

Alias for field number 0

index()

Return first index of value.

Raises ValueError if the value is not present.

property second_base_running_event

Alias for field number 1

property third_base_running_event

Alias for field number 2

class pybbda.analysis.simulations.FirstBaseRunningEvent[source]

An enumeration.

DEFAULT = 0
FIRST_TO_HOME = 3
FIRST_TO_SECOND = 1
FIRST_TO_THIRD = 2
class pybbda.analysis.simulations.SecondBaseRunningEvent[source]

An enumeration.

DEFAULT = 0
SECOND_TO_HOME = 2
SECOND_TO_THIRD = 1
class pybbda.analysis.simulations.ThirdBaseRunningEvent[source]

An enumeration.

DEFAULT = 0
THIRD_TO_HOME = 1
class pybbda.analysis.simulations.GameEvent(batting_event: pybbda.analysis.simulations.components.event.BattingEvent, first_base_running_event: pybbda.analysis.simulations.components.event.FirstBaseRunningEvent = <FirstBaseRunningEvent.DEFAULT: 0>, second_base_running_event: pybbda.analysis.simulations.components.event.SecondBaseRunningEvent = <SecondBaseRunningEvent.DEFAULT: 0>, third_base_running_event: pybbda.analysis.simulations.components.event.ThirdBaseRunningEvent = <ThirdBaseRunningEvent.DEFAULT: 0>)[source]

Class for a GameEvent. A GameEvent is a BattingEvent a FirstBaseRunningEvent, a SecondBaseRunningEvent, and a ThirdBaseRunningEvent

class pybbda.analysis.simulations.Batter(*, player_id: str, batting_event_probabilities: pybbda.analysis.simulations.components.event.BattingEventProbability = BattingEventProbability(base_on_balls=0, single=0, double=0, triple=0, home_run=0), running_event_probabilities: pybbda.analysis.simulations.components.event.RunningEventProbability = RunningEventProbability(first_to_third_on_single=0.26, first_to_home_on_single=0.01, first_to_home_on_double=0.41, second_to_home_on_single=0.6))[source]
set_batting_event_probs(**batting_event_probs)[source]
set_running_event_probs(**running_event_probs)[source]
class pybbda.analysis.simulations.BattingEventProbability(base_on_balls: float, single: float, double: float, triple: float, home_run: float)[source]
property probs
class pybbda.analysis.simulations.Runner(*, player_id: str, running_event_probabilities: pybbda.analysis.simulations.components.event.RunningEventProbability = RunningEventProbability(first_to_third_on_single=0.26, first_to_home_on_single=0.01, first_to_home_on_double=0.41, second_to_home_on_single=0.6))[source]
set_event_probs(**running_event_probs)[source]
class pybbda.analysis.simulations.RunningEventProbability(first_to_third_on_single: float = 0.26, first_to_home_on_single: float = 0.01, first_to_home_on_double: float = 0.41, second_to_home_on_single: float = 0.6)[source]
property probs
class pybbda.analysis.simulations.Lineup(lineup: List[pybbda.analysis.simulations.components.player.Batter])[source]

Class for a lineup, which comprises 9 Batters

get_batting_probs(lineup_slot)[source]

Gets the batting event probabilities for the batter in the lineup_slot

Parameters

lineup_slot – int

Returns

BattingEventProbability

set_lineup_slot(lineup_slot, batter)[source]

Sets lineup_slot to batter. lineup_slot can be from 1 to 9, i.e. it is not 0-indexed

Parameters
  • lineup_slot – int

  • batterBatter

Returns

None

class pybbda.analysis.simulations.PlayerRegistry[source]

Class for a registry of players. This class is a Singleton.

add(batters)[source]

Adds batters to the registry

Parameters

batters – Iterable of Batter

Returns

dict

init(*args, **kwargs)
property len
load_from_lahman(pa_limit=180)[source]

Loads a player registry from the Lahman batting table. The set of players are filtered to have at least pa_limit PAs

Parameters

pa_limit – int

Returns

None