Skip to main content

Predicting when AL managers will remove their starting pitchers.

Project description

Pull the pitcher

Documentation

Predicting when MLB managers in the AL will pull their starting pitchers. This Deep, Recurrent Survival Analysis model is trained to predict the at-bat at which a pitcher is removed from the game, earning an f1-score of 0.97.

performance

Installation

$ pip install pull-the-pitcher

How to get the data

The ptp library comes with two main command-line utilites. After you install ptp, these should be directly available to you at the command-line, assuming you're in the environment that ptp was installed in.

Storing data in a sqlite3 db

The first command-line utility is query_statcast, which invokes pybaseball's statcast() function to retrieve pitch-level data from statcast. This data will then be stored in a sqlite3 db file. Here's an example of how you could use it.

$ query_statcast --start_dt 2019-05-07 --end_dt 2019-06-09 --output_type db --output_path /tmp
This is a large query, it may take a moment to complete
Completed sub-query from 2019-05-07 to 2019-05-12
Completed sub-query from 2019-05-13 to 2019-05-18
Completed sub-query from 2019-05-19 to 2019-05-24
Completed sub-query from 2019-05-25 to 2019-05-30
Completed sub-query from 2019-05-31 to 2019-06-05
Completed sub-query from 2019-06-06 to 2019-06-09

Preparing data for modeling

The next command-line utility is prep_data_for_modeling, which pulls data from the database created in the previous command, then performs feature engineering and various aggregations to yield clean, at-bat level data amenable to a machine learning model. Here's an example of how you might use it.

$ prep_data_for_modeling --db_path /tmp/statcast_pitches.db --train_test_split_by start --output_path /tmp/
querying db at /tmp/statcast_pitches.db now.
In this dataset, there are 457 total games.
There are 63 'openers' in the dataset.
There are 851 total eligible game-pitcher combinations in this dataset.
Just processed 0th start.
Just processed 100th start.
Just processed 200th start.
Just processed 300th start.
Just processed 400th start.
Just processed 500th start.
Just processed 600th start.
Just processed 700th start.
Just processed 800th start.
There are 91 unique pitcher's in this dataset
['2019'] data ready for modeling and saved at /tmp/.

FAQ

  • Should any of the data be considered "uncensored"?

    • The great thing about baseball data is that it is comprehensive, clean, and public! So, no, none of the data is "censored" in the survival analysis sense. We know the exact at bat at which every pitcher was removed from the game.
  • If none of the data is uncensored, why are you using survival analysis techniques?

    • Well, the short answer is that they perform the best. Much of survival analysis is dedicated to modeling with both censored and uncensored data. Since none of our data is cenored, we have free reign to leverage any predictive modeling technique under the sun. Here, however, the process of predicting when a pitcher will be removed from a game fits very nicely in a time-to-event modeling framework, which survival analysis techniques are designed to handle.
  • How does this approach compare to traditional survival analysis?

    • Traditional survival analysis is typically framed as a regression problem, which involves regressing the estimated units of time until the event of interest occurs. Alternatively, the approach we employ is framed as a classification problem, and involves predicting the probability that the event of interest (pitcher removed from the game) occurs at every unit of time (at bat).
      • While this neural network, classification-esque approach is non-traditional, it is not unheard of, as seen here and here.
  • How is this Deep, Recurrent Survival Analysis model different from a traditional LSTM?

    1. Like other recurrent neural networks, our model predicts the conditional probability that a pitcher was pulled after each at-bat (conditioned on the game that has occurred up to that point). The novelty here occurs in the way that the model "estimates the survival rate through the probability chain rule, which captures the sequential dependency patterns between neighboring at-bats and back-propagates the gradient more efficiently." (quote from page 2 of the original paper).

    2. This notion of estimation of the survival rate through the probability chain rule is further enforced by the use of the event time and the event rate loss functions. Notice that while our targets are binary, we are not using traditional log loss to train this model.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pull_the_pitcher-0.1.8.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

pull_the_pitcher-0.1.8-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file pull_the_pitcher-0.1.8.tar.gz.

File metadata

  • Download URL: pull_the_pitcher-0.1.8.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.7

File hashes

Hashes for pull_the_pitcher-0.1.8.tar.gz
Algorithm Hash digest
SHA256 8931b853597bfaf5b994c41766e989645571e79e47ea4ada9b0e55a2a9a4279f
MD5 95e9a7a75fa585e04786bb5a3266b2e2
BLAKE2b-256 195fbb02951e78a192ccfe49d8a61beeabe4be1a29f993e99079d8a5d027a01d

See more details on using hashes here.

File details

Details for the file pull_the_pitcher-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: pull_the_pitcher-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.7

File hashes

Hashes for pull_the_pitcher-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3c32b6ae3e74dad5b68c3b0ee5285aa1381cabbcbd992c0aba2799fc319ba900
MD5 eabc8396bb37478268a3a67f83e06c07
BLAKE2b-256 20ccc4ffbfc3b4109348995817e95fb3dbfbcab736ec0a322a89e19608769d7c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page