{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../../data/'\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('fivethirtyeight')\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deflategate\n",
    "On January 18, 2015, the Indianapolis Colts and the New England Patriots played the American Football Conference (AFC) championship game to determine which of those teams would play in the Super Bowl. After the game, there were allegations that the Patriots' footballs had not been inflated as much as the regulations required; they were softer. This could be an advantage, as softer balls might be easier to catch.\n",
    "\n",
    "For several weeks, the world of American football was consumed by accusations, denials, theories, and suspicions: the press labeled the topic Deflategate, after the Watergate political scandal of the 1970's. The National Football League (NFL) commissioned an independent analysis. In this example, we will perform our own analysis of the data.\n",
    "\n",
    "Pressure is often measured in pounds per square inch (psi). NFL rules stipulate that game balls must be inflated to have pressures in the range 12.5 psi and 13.5 psi. Each team plays with 12 balls. Teams have the responsibility of maintaining the pressure in their own footballs, but game officials inspect the balls. Before the start of the AFC game, all the Patriots' balls were at about 12.5 psi. Most of the Colts' balls were at about 13.0 psi. However, these pre-game data were not recorded.\n",
    "\n",
    "During the second quarter, the Colts intercepted a Patriots ball. On the sidelines, they measured the pressure of the ball and determined that it was below the 12.5 psi threshold. Promptly, they informed officials. \n",
    "\n",
    "At half-time, all the game balls were collected for inspection. Two officials, Clete Blakeman and Dyrol Prioleau, measured the pressure in each of the balls. \n",
    "\n",
    "Here are the data. Each row corresponds to one football. Pressure is measured in psi. The Patriots ball that had been intercepted by the Colts was not inspected at half-time. Nor were most of the Colts' balls – the officials simply ran out of time and had to relinquish the balls for the start of second half play."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>Blakeman</th>\n",
       "      <th>Prioleau</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.50</td>\n",
       "      <td>11.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.85</td>\n",
       "      <td>11.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.15</td>\n",
       "      <td>11.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.70</td>\n",
       "      <td>11.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.10</td>\n",
       "      <td>11.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.60</td>\n",
       "      <td>11.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.85</td>\n",
       "      <td>12.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.10</td>\n",
       "      <td>11.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.95</td>\n",
       "      <td>11.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.50</td>\n",
       "      <td>10.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.90</td>\n",
       "      <td>11.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.70</td>\n",
       "      <td>12.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.75</td>\n",
       "      <td>12.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.50</td>\n",
       "      <td>12.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.55</td>\n",
       "      <td>12.15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Team  Blakeman  Prioleau\n",
       "0   Patriots     11.50     11.80\n",
       "1   Patriots     10.85     11.20\n",
       "2   Patriots     11.15     11.50\n",
       "3   Patriots     10.70     11.00\n",
       "4   Patriots     11.10     11.45\n",
       "5   Patriots     11.60     11.95\n",
       "6   Patriots     11.85     12.30\n",
       "7   Patriots     11.10     11.55\n",
       "8   Patriots     10.95     11.35\n",
       "9   Patriots     10.50     10.90\n",
       "10  Patriots     10.90     11.35\n",
       "11     Colts     12.70     12.35\n",
       "12     Colts     12.75     12.30\n",
       "13     Colts     12.50     12.95\n",
       "14     Colts     12.55     12.15"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "football = pd.read_csv(path_data + 'deflategate.csv')\n",
    "football"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For each of the 15 balls that were inspected, the two officials got different results. It is not uncommon that repeated measurements on the same object yield different results, especially when the measurements are performed by different people. So we will assign to each the ball the average of the two measurements made on that ball."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>Combined</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.650</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>12.075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.350</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Team  Combined\n",
       "0   Patriots    11.650\n",
       "1   Patriots    11.025\n",
       "2   Patriots    11.325\n",
       "3   Patriots    10.850\n",
       "4   Patriots    11.275\n",
       "5   Patriots    11.775\n",
       "6   Patriots    12.075\n",
       "7   Patriots    11.325\n",
       "8   Patriots    11.150\n",
       "9   Patriots    10.700\n",
       "10  Patriots    11.125\n",
       "11     Colts    12.525\n",
       "12     Colts    12.525\n",
       "13     Colts    12.725\n",
       "14     Colts    12.350"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "football_1 = football.copy()\n",
    "\n",
    "football_1['Combined'] = (football_1['Blakeman']+football_1['Prioleau'])/2\n",
    "\n",
    "football_combined = football_1.drop(columns=['Blakeman','Prioleau'])\n",
    "\n",
    "football_combined"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At a glance, it seems apparent that the Patriots' footballs were at a lower pressure than the Colts' balls. Because some deflation is normal during the course of a game, the independent analysts decided to calculate the drop in pressure from the start of the game. Recall that the Patriots' balls had all started out at about 12.5 psi, and the Colts' balls at about 13.0 psi. Therefore the drop in pressure for the Patriots' balls was computed as 12.5 minus the pressure at half-time, and the drop in pressure for the Colts' balls was 13.0 minus the pressure at half-time.\n",
    "\n",
    "We can calculate the drop in pressure for each football, by first setting up an array of the starting values. For this we will need an array consisting of 11 values each of which is 12.5, and another consisting of four values each of which is all 13. We will use the NumPy function `np.ones`, which takes a count as its argument and returns an array of that many elements, each of which is 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5,\n",
       "       13. , 13. , 13. , 13. ])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patriots_start = 12.5 * np.ones(11)\n",
    "colts_start = 13 * np.ones(4)\n",
    "start = np.append(patriots_start, colts_start)\n",
    "start"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The drop in pressure for each football is the difference between the starting pressure and the combined pressure measurement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>Combined</th>\n",
       "      <th>Pressure Drop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.650</td>\n",
       "      <td>0.850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.025</td>\n",
       "      <td>1.475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.325</td>\n",
       "      <td>1.175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.850</td>\n",
       "      <td>1.650</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.275</td>\n",
       "      <td>1.225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.775</td>\n",
       "      <td>0.725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>12.075</td>\n",
       "      <td>0.425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.325</td>\n",
       "      <td>1.175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.150</td>\n",
       "      <td>1.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>10.700</td>\n",
       "      <td>1.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>11.125</td>\n",
       "      <td>1.375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.525</td>\n",
       "      <td>0.475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.525</td>\n",
       "      <td>0.475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.725</td>\n",
       "      <td>0.275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Colts</td>\n",
       "      <td>12.350</td>\n",
       "      <td>0.650</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Team  Combined  Pressure Drop\n",
       "0   Patriots    11.650          0.850\n",
       "1   Patriots    11.025          1.475\n",
       "2   Patriots    11.325          1.175\n",
       "3   Patriots    10.850          1.650\n",
       "4   Patriots    11.275          1.225\n",
       "5   Patriots    11.775          0.725\n",
       "6   Patriots    12.075          0.425\n",
       "7   Patriots    11.325          1.175\n",
       "8   Patriots    11.150          1.350\n",
       "9   Patriots    10.700          1.800\n",
       "10  Patriots    11.125          1.375\n",
       "11     Colts    12.525          0.475\n",
       "12     Colts    12.525          0.475\n",
       "13     Colts    12.725          0.275\n",
       "14     Colts    12.350          0.650"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drop = start - football_combined['Combined']\n",
    "\n",
    "football_combined['Pressure Drop'] = drop\n",
    "\n",
    "football_combined"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks as though the Patriots' drops were larger than the Colts'. Let's look at the average drop in each of the two groups. We no longer need the combined scores."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>Pressure Drop average</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Colts</td>\n",
       "      <td>0.468750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.202273</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Team  Pressure Drop average\n",
       "0     Colts               0.468750\n",
       "1  Patriots               1.202273"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "football_combined1 = football_combined.copy()\n",
    "\n",
    "football_combined_average = football_combined1.drop(columns=['Combined'])\n",
    "\n",
    "football_combined_average = football_combined_average.groupby(by=['Team']).mean()\n",
    "\n",
    "football_combined_average = football_combined_average.reset_index()\n",
    "\n",
    "football_combined_average = football_combined_average.rename(columns={'Pressure Drop':'Pressure Drop average'})\n",
    "\n",
    "football_combined_average"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The average drop for the Patriots was about 1.2 psi compared to about 0.47 psi for the Colts. \n",
    "\n",
    "The question now is why the Patriots' footballs had a larger drop in pressure, on average, than the Colts footballs. Could it be due to chance?\n",
    "\n",
    "## The Hypotheses\n",
    "How does chance come in here? Nothing was being selected at random. But we can make a chance model by hypothesizing that the 11 Patriots' drops look like a random sample of 11 out of all the 15 drops, with the Colts' drops being the remaining four. That's a completely specified chance model under which we can simulate data. So it's the **null hypothesis**.\n",
    "\n",
    "For the alternative, we can take the position that the Patriots' drops are too large, on average, to resemble a random sample drawn from all the drops. \n",
    "\n",
    "## Test Statistic\n",
    "A natural statistic is the difference between the two average drops, which we will compute as \"average drop for Patriots - average drop for Colts\". Large values of this statistic will favor the alternative hypothesis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.733522727272728"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "observed_means = football_combined_average['Pressure Drop average']\n",
    "\n",
    "observed_difference = observed_means.iloc[1] - observed_means.iloc[0]\n",
    "\n",
    "observed_difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This positive difference reflects the fact that the average drop in pressure of the Patriots' footballs was greater than that of the Colts."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function `difference_of_means` takes three arguments:\n",
    "\n",
    "- the name of the table of data\n",
    "- the label of the column containing the numerical variable whose average is of interest\n",
    "- the label of the column containing the two group labels\n",
    "\n",
    "It returns the difference between the means of the two groups. \n",
    "\n",
    "We have defined this function in an earlier section. The definition is repeated here for ease of reference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def difference_of_means(table, label, group_label):\n",
    "    reduced = table[[label, group_label]]\n",
    "    means_table = reduced.groupby(group_label).mean()\n",
    "    means = means_table[label]\n",
    "    return means[1] - means[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.733522727272728"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "difference_of_means(football_combined, 'Pressure Drop', 'Team')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the difference has been calculated as Patriots' drops minus Colts' drops as before."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predicting the Statistic Under the Null Hypothesis\n",
    "If the null hypothesis were true, then it shouldn't matter which footballs are labeled Patriots and which are labeled Colts. The distributions of the two sets of drops would be the same. We can simulate this by randomly shuffling the team labels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Team</th>\n",
       "      <th>Pressure Drop</th>\n",
       "      <th>Shuffled Label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>0.850</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.475</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.175</td>\n",
       "      <td>Colts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.650</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.225</td>\n",
       "      <td>Colts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>0.725</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>0.425</td>\n",
       "      <td>Colts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.175</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.350</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.800</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Patriots</td>\n",
       "      <td>1.375</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Colts</td>\n",
       "      <td>0.475</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Colts</td>\n",
       "      <td>0.475</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Colts</td>\n",
       "      <td>0.275</td>\n",
       "      <td>Colts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Colts</td>\n",
       "      <td>0.650</td>\n",
       "      <td>Patriots</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Team  Pressure Drop Shuffled Label\n",
       "0   Patriots          0.850       Patriots\n",
       "1   Patriots          1.475       Patriots\n",
       "2   Patriots          1.175          Colts\n",
       "3   Patriots          1.650       Patriots\n",
       "4   Patriots          1.225          Colts\n",
       "5   Patriots          0.725       Patriots\n",
       "6   Patriots          0.425          Colts\n",
       "7   Patriots          1.175       Patriots\n",
       "8   Patriots          1.350       Patriots\n",
       "9   Patriots          1.800       Patriots\n",
       "10  Patriots          1.375       Patriots\n",
       "11     Colts          0.475       Patriots\n",
       "12     Colts          0.475       Patriots\n",
       "13     Colts          0.275          Colts\n",
       "14     Colts          0.650       Patriots"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_labels3 = football_combined.copy()\n",
    "\n",
    "shuffled_labels4 = shuffled_labels3\n",
    "\n",
    "shuffled_labels5 = shuffled_labels4.sample(len(shuffled_labels3), replace = False)\n",
    "\n",
    "shuffled_labels6 = np.array(shuffled_labels5['Team'])\n",
    "\n",
    "shuffled_labels3['Shuffled Label'] = shuffled_labels6\n",
    "\n",
    "original_and_shuffled = shuffled_labels3.drop(columns=['Combined'])\n",
    "\n",
    "original_and_shuffled"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How do all the group averages compare?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3159090909090907"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "difference_of_means(original_and_shuffled, 'Pressure Drop', 'Shuffled Label')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.733522727272728"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "difference_of_means(original_and_shuffled, 'Pressure Drop', 'Team')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The two teams' average drop values are closer when the team labels are randomly assigned to the footballs than they were for the two groups actually used in the game.\n",
    "\n",
    "### Permutation Test\n",
    "It's time for a step that is now familiar. We will do repeated simulations of the test statistic under the null hypothesis, by repeatedly permuting the footballs and assigning random sets to the two teams.\n",
    "\n",
    "Once again, we will use the function `one_simulated_difference` defined in an earlier section as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def one_simulated_difference(table, label, group_label):\n",
    "    \n",
    "    table_copy = table.copy()\n",
    "\n",
    "    shuffled_labels = table_copy.sample(len(table_copy), replace = False)\n",
    "\n",
    "    shuffled_labels = np.array(shuffled_labels[group_label])\n",
    "\n",
    "    table_copy['Shuffled Label'] = shuffled_labels\n",
    "\n",
    "    original_and_shuffled = table_copy.drop(columns=['Combined'])\n",
    "\n",
    "    shuffled_group_means = original_and_shuffled.groupby('Shuffled Label').mean()\n",
    "\n",
    "    table1 = shuffled_group_means.reset_index()\n",
    "    \n",
    "    return difference_of_means(table1, label, 'Shuffled Label')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now use this function to create an array `differences` that contains 10,000 values of the test statistic simulated under the null hypothesis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "differences = np.array([])\n",
    "\n",
    "repetitions = 10000\n",
    "for i in np.arange(repetitions):\n",
    "    new_difference = one_simulated_difference(football_combined, 'Pressure Drop', 'Team')\n",
    "    differences = np.append(differences, new_difference)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conclusion of the Test\n",
    "To calculate the empirical P-value, it's important to recall the alternative hypothesis, which is that the Patriots' drops are too large to be the result of chance variation alone.\n",
    "\n",
    "Larger drops for the Patriots favor the alternative hypothesis. So the P-value is the chance (computed under the null hypothesis) of getting a test statistic equal to our observed value of 0.733522727272728 or larger."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0031"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empirical_P = np.count_nonzero(differences >= observed_difference) / 10000\n",
    "empirical_P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's a pretty small P-value. To visualize this, here is the empirical distribution of the test statistic under the null hypothesis, with the observed statistic marked on the horizontal axis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empirical P-value: 0.0031\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 31288 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "test_conclusion = pd.DataFrame({'Difference Between Group Averages':differences})\n",
    "\n",
    "print('Empirical P-value:', empirical_P)\n",
    "\n",
    "unit = ''\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,5))\n",
    "\n",
    "ax.hist(test_conclusion, density=True, color='blue', alpha=0.8, ec='white')\n",
    "\n",
    "y_vals = ax.get_yticks()\n",
    "\n",
    "y_label = 'Percent per ' + (unit if unit else 'unit')\n",
    "\n",
    "x_label = 'Diference Between Group Averages'\n",
    "\n",
    "ax.set_yticklabels(['{:g}'.format(x * 100) for x in y_vals])\n",
    "\n",
    "plt.ylabel(y_label)\n",
    "\n",
    "plt.xlabel(x_label)\n",
    "\n",
    "plt.title('Prediction Under the Null Hypothesis');\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As in previous examples of this test, the bulk of the distribution is centered around 0. Under the null hypothesis, the Patriots' drops are a random sample of all 15 drops, and therefore so are the Colts'. Therefore the two sets of drops should be about equal on average, and therefore their difference should be around 0.\n",
    "\n",
    "But the observed value of the test statistic is quite far away from the heart of the distribution. By any reasonable cutoff for what is \"small\", the empirical P-value is small. So we end up rejecting the null hypothesis of randomness, and conclude that the Patriots drops were too large to reflect chance variation alone.\n",
    "\n",
    "The independent investigative team analyzed the data in several different ways, taking into account the laws of physics. The final report said, \n",
    "\n",
    "> \"[T]he average pressure drop of the Patriots game balls exceeded the average pressure drop of the Colts balls by 0.45 to 1.02 psi, depending on various possible assumptions regarding the gauges used, and assuming an initial pressure of 12.5 psi for the Patriots balls and 13.0 for the Colts balls.\"\n",
    "> \n",
    "> -- *Investigative report commissioned by the NFL regarding the AFC Championship game on January 18, 2015*\n",
    "\n",
    "Our analysis shows an average pressure drop of about 0.73 psi, which is close to the center of the interval \"0.45 to 1.02 psi\" and therefore consistent with the official analysis."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember that our test of hypotheses does not establish the reason *why* the difference is not due to chance. Establishing causality is usually more complex than running a test of hypotheses.\n",
    "\n",
    "But the all-important question in the football world was about causation: the question was whether the excess drop of pressure in the Patriots' footballs was deliberate. If you are curious about the answer given by the investigators, here is the [full report](https://nfllabor.files.wordpress.com/2015/05/investigative-and-expert-reports-re-footballs-used-during-afc-championsh.pdf)."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
