• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The model behind @NYT4thDownBot

Fourth Down Bot

This is the code that powers the New York Times 4th Down Bot. Using NFL play-by-play data from Armchair Analysis, this code will:

  • Munge the raw play-by-play data and transform it into a form suitable for modeling
  • Create a win probability model and serialize that model
  • Provide all of the functions to make optimal 4th down decisions for a given play

The Armchair Analysis data is not free. It costs $49 (one-time) to gain access to play-by-play data from the 2000-2014 NFL seasons. There is also a professional package that will provide weekly updates. Without the Armchair Analysis data, you will not be able to use much of this code.

This code currently requires Python 2.7 and is not Python 3 compliant to our knowledge. Questions about the Python code can be directed to Trey Causey.

Please note that none of the file operations are supported on Windows.

NOTE: If you are unable to purchase the Armchair Analysis data, Ben Dilday has created a fork of this code that uses freely available play-by-play data. There are no guarantees that this fork is current with the production version of the 4th Down Bot and this fork is not affiliated in any way with or supported by The Upshot or Trey Causey. You can find that fork here. Questions about that fork should be directed to Ben Dilday.

Python package requirements

  • click
  • matplotlib (if you want to visually diagnose your model's performance)
  • naked
  • numpy
  • pandas
  • scikit-learn

Usage

Unzip the play-by-play data into a directory. Run the following code from the directory where you want the Fourth Down Bot code to live. It will create the subdirectories models and data to store files.

python data_prep.py <pbp data dir>
python model_train.py

If you wish to view the calibration plots and ROC curves for the model, run model_train with the --plot flag, like so:

python model_train.py --plot

There is a rudimentary command line interface for interactively querying the bot's model, although the model was built to be queried programatically. Feel free to improve upon this. To query the model interactively, use the following syntax at the command line and follow the prompts:

python bot.py

Field goal model

The bot's field goal model is also accessible as a separate module, via either a node script (see model-fg/example.js for details) or the command line. A sample query:

node model-fg/model-fg.js --kicker_code=AH-2600 --temp=40 --wind=10 --yfog=67 --chanceOfRain=10 --is_dome=1 --is_turf=0

As an alternative to supplying the Armchair Analysis player code, you can instead specify the team on offense (team codes are fairly standard, but see model-fg.js for a lookup table). Similarly, you can supply the home team instead of is_dome and is_turf arguments:

node model-fg/model-fg.js --offense=PHI --home=NE --temp=40 --wind=10 --yfog=67 --chanceOfRain=10

More Repositories

1

Military-Surplus-Gear

Military equipment given to law enforcement agencies
299
star
2

leo-senate-model

Code and data for The Upshot's Senate model.
R
230
star
3

presidential-precinct-map-2020

The GeoJSON dataset behind our nationwide precinct map of the 2020 presidential general election
181
star
4

statement

A Ruby gem that extracts press releases and statements by members of Congress.
HTML
70
star
5

chipotle

The data behind the Upshot's recent article about what people actually order at Chipotle
56
star
6

Bedfellows

Command-line tool for exploring the PAC donor-recipient relationship
Python
54
star
7

2018-live-poll-results

Data from the 2018 NYT Upshot/Siena live polls
28
star
8

2004-2012-presidential-election-model

R
24
star
9

prison-admissions

U.S. state prison admissions by county
R
18
star
10

ifill

Ruby wrapper for the Compilation of Presidential Documents.
Ruby
11
star
11

nyt_weddings

List of New York Times wedding announcements used in an Upshot story on name-changing.
9
star
12

Sunday-Shows

Sunday talk show guests
7
star
13

Ferguson-Demographics

Census data of communities resembling Ferguson, Mo.
7
star
14

voting_mistakes

5
star
15

2016-upshot-siena-polls

Data from the 2016 NYT Upshot/Siena voter file polls http://www.nytimes.com/2016/09/19/upshot/introducing-the-new-york-times-upshot-siena-college-poll.html
R
5
star
16

world-cup-study

5
star
17

EshooPallone

4
star
18

upshot-siena-va-gov-poll

Data and methodology
3
star
19

senate_precinct_results

Precinct-level results from 2014 general election in selected states with Senate races
2
star
20

ericpac

Recipients of contributions by ERIC PAC in 2013-2014 cycle
2
star
21

rand_paul_online_donors

A partial listing of online donors from April 7-May 3 pulled from randpaul.com
2
star
22

senate-forecast-comparison

Daily Senate forecasts from seven different forecasters
2
star
23

state-department-press-briefings

The data behind our calendar of State department press briefings.
1
star