How Long Will The Warriors' Streak Last?
December 9, 2015 ยท View on GitHub
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How Long Will The Warriors' Streak Last?\n", "\n", "The code below estimates the Golden State Warriors' odds of matching or beating the NBA's win-streak record of 33 games. To do so, it uses per-game odds produced by FiveThirtyEight's "CARM-Elo" projections.\n", "\n", "The longest regular-season win streak belongs to the '71-'72 Los Angeles Lakers, who won 33 games straight in a single season. The Warriors would tie the streak if they win their first 29 games this season, adding to a four-game streak carried over from the end of last regular season." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests\n", "import random\n", "import re\n", "import yaml\n", "import pandas as pd\n", "import matplotlib as mpl\n", "import seaborn as sb\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fetch the projections from FiveThirtyEight" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "URL = "http://projects.fivethirtyeight.com/2016-nba-picks/js/bundle.js"\n", "js = requests.get(URL, params={"r": random.random()}).content.decode("utf-8")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def fix_json(json_str):\n", " json_str = re.sub(r"([,:])", r"\1 ", json_str)\n", " json_str = re.sub(r"!1", "false", json_str)\n", " json_str = re.sub(r"!0", "true", json_str)\n", " return json_str" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "game_strings = re.findall(r"\{[^}[]+team1[^}[]+\}", fix_json(js))[:82 * 15]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "games = pd.DataFrame([ yaml.load(game) for game in game_strings ])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Make sure every team has 41 home games.\n", "assert((games["team1"].value_counts() == 41).sum() == 30)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
game_no refers to the sequence of Warriors games this regular season, not the game number in the streak."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"