run expectancy

class pybbda.analysis.run_expectancy.MarkovState(game_state: pybbda.analysis.simulations.components.state.GameState, probability: float)[source]

A MarkovState comprises a GameState and a probability for being in that state.

Parameters
  • game_state – The GameState

  • probability – Probability for the GameState

property lineup_slot
to_df()[source]

Converts the MarkovState to a Pandas DataFrame.

Returns

MarkovState as a DataFrame.

markov_state = MarkovState(GameState(), 1)
markov_state.to_df()
first_base  second_base  third_base  outs  score  pa_count  prob
         0            0           0     0      0         1     1
class pybbda.analysis.run_expectancy.MarkovEvent(game_event: pybbda.analysis.simulations.components.event.GameEvent, probability: float)[source]

A MarkovEvent comprises a GameEvent and a probability for that event to occur

Parameters
  • game_event – The GameEvent

  • probability – Probability for the GameEvent

class pybbda.analysis.run_expectancy.MarkovEvents(events: List[pybbda.analysis.run_expectancy.markov.markov.MarkovEvent])[source]

MarkovEvents comprise a list of MarkovEvent type

Parameters

events – List of MarkovEvent

static from_players(batter, first_base_runner=None, second_base_runner=None, third_base_runner=None)[source]
static from_probs(batting_event_probs, running_event_probs)[source]

Constructs a MarkovEvents from batting and running probabilities

Parameters
  • batting_event_probsBattingEventProbability

  • running_event_probsRunningEventProbability

Returns

MarkovEvents

markov_events = (
    MarkovEvents.from_probs(
     BattingEventProbability(0.08, 0.15, 0.05, 0.005, 0.03),
     RunningEventProbability(0.1, 0.1, 0.1, 0.1)
                           )
                 )
property total_probability

The total probability for the events to occur

class pybbda.analysis.run_expectancy.MarkovSimulation(state_vector: pybbda.analysis.run_expectancy.markov.markov.StateVector = StateVector(_states=[MarkovState(game_state=GameState(base_out_state=BaseOutState(base_state=BaseState(first_base=0, second_base=0, third_base=0), outs=0, max_outs=3), pa_count=1, lineup_slot=1, score=0), probability=1)]), 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), termination_threshold: float = 1e-06)[source]

A class for executing a Markov simulation by executing state transitions to an initial StateVector until a threshold of probability for being in the end state is crossed. The simulation is executed by calling the Markovsimulation object.

Parameters
  • state_vector – The intial StateVector

  • termination_threshold – Termination threshold. Simulation will stop when probability to be in the end state is larger than 1 - termination_threshold

static markov_step(state_vector, markov_events)[source]

A step in the Markov simulation. Applies the set of markov_events to the MarkovState in the state_vector, and then combines the results into a StateVector

Parameters
  • state_vector

  • markov_events

Returns

StateVector

static state_transition(markov_state, markov_event)[source]

Transition from markov_state based on markov_event

Parameters
  • markov_stateMarkovState

  • markov_eventMarkovEvent

Returns

MarkovState

static state_transition_tuple(markov_state_event)[source]

MarkovState expressed as a tuple. This is a helper to be able to apply map to a set of transitions.

Parameters

markov_state_event

Returns

static state_vectors_to_df(state_vectors)[source]

Converts list of StateVector to Pandas DataFrame

Parameters

state_vectors – List of StateVector

Returns

Pandas DataFrame


markov_simulation = MarkovSimulation() batting_event_probability = BattingEventProbability(0.08, 0.15, 0.05, 0.005, 0.03) running_event_probability = RunningEventProbability(0.1, 0.1, 0.1, 0.1) results = markov_simulation(batting_event_probability, running_event_probability) sim_df = MarkovSimulation.state_vectors_to_df(results) sim_df first_base second_base third_base outs score pa_count prob

0 0 0 0 0 1 1.000000e+00 0 0 0 1 0 2 6.850000e-01 1 0 0 0 0 2 2.300000e-01 0 1 0 0 0 2 5.000000e-02 0 0 1 0 0 2 5.000000e-03

… … … … … … …

1 0 0 0 17 19 8.443152e-11 0 1 1 0 16 19 9.725439e-11 0 1 0 0 17 19 5.074142e-11 0 0 1 0 17 19 1.479958e-11 0 0 0 0 18 19 8.879749e-11

class pybbda.analysis.run_expectancy.StateVector(states: List[pybbda.analysis.run_expectancy.markov.markov.MarkovState] = [MarkovState(game_state=GameState(base_out_state=BaseOutState(base_state=BaseState(first_base=0, second_base=0, third_base=0), outs=0, max_outs=3), pa_count=1, lineup_slot=1, score=0), probability=1)])[source]

A StateVector comprises a List of MarkovState objects

Parameters

_states – List of MarkovState

static combine_states(markov_states)[source]

Combines a list of MarkovState. It deduplicates states and sums the probabilities.

Parameters

markov_states – List of MarkovState

Returns

StateVector

m1 = MarkovState(GameState(), 0.2)
m2 = MarkovState(GameState(), 0.25)
StateVector.combine_states((m1, m2))
property end_probability

Proability for the state vector to be in an end state, example having 3 outs

Returns

Probability of being in an end state

property lineup_slot
property mean_score

Mean score of the state vector

Returns

The mean score of the state

property states
property std_score

Standard deviation of the score of the state vector

Returns

The std. dev score of the state

to_df()[source]

Converts StateVector object to Pandas DataFrame

Returns

state_vector = StateVector()
state_vector.to_df()
first_base  second_base  third_base  outs  score  pa_count  prob
         0            0           0     0      0         1     1