In [1]:
import pandas as pd
from matplotlib.pyplot import subplots
import numpy as np
In [2]:
bta = pd.read_csv('bta.csv')
bta
Out[2]:
| Group | Result | |
|---|---|---|
| 0 | Control | 1.0 |
| 1 | Control | 1.0 |
| 2 | Control | 0.0 |
| 3 | Control | 0.0 |
| 4 | Control | 0.0 |
| 5 | Control | 0.0 |
| 6 | Control | 0.0 |
| 7 | Control | 0.0 |
| 8 | Control | 0.0 |
| 9 | Control | 0.0 |
| 10 | Control | 0.0 |
| 11 | Control | 0.0 |
| 12 | Control | 0.0 |
| 13 | Control | 0.0 |
| 14 | Control | 0.0 |
| 15 | Control | 0.0 |
| 16 | Treatment | 1.0 |
| 17 | Treatment | 1.0 |
| 18 | Treatment | 1.0 |
| 19 | Treatment | 1.0 |
| 20 | Treatment | 1.0 |
| 21 | Treatment | 1.0 |
| 22 | Treatment | 1.0 |
| 23 | Treatment | 1.0 |
| 24 | Treatment | 1.0 |
| 25 | Treatment | 0.0 |
| 26 | Treatment | 0.0 |
| 27 | Treatment | 0.0 |
| 28 | Treatment | 0.0 |
| 29 | Treatment | 0.0 |
| 30 | Treatment | 0.0 |
In [3]:
means = bta.groupby('Group')['Result'].mean()
means
Out[3]:
Group Control 0.125 Treatment 0.600 Name: Result, dtype: float64
In [4]:
obout = pd.read_csv('observed_outcomes.csv')
obout
Out[4]:
| Group | Outcome if assigned treatment | Outcome if assigned control | |
|---|---|---|---|
| 0 | Control | Unknown | 1 |
| 1 | Control | Unknown | 1 |
| 2 | Control | Unknown | 0 |
| 3 | Control | Unknown | 0 |
| 4 | Control | Unknown | 0 |
| 5 | Control | Unknown | 0 |
| 6 | Control | Unknown | 0 |
| 7 | Control | Unknown | 0 |
| 8 | Control | Unknown | 0 |
| 9 | Control | Unknown | 0 |
| 10 | Control | Unknown | 0 |
| 11 | Control | Unknown | 0 |
| 12 | Control | Unknown | 0 |
| 13 | Control | Unknown | 0 |
| 14 | Control | Unknown | 0 |
| 15 | Control | Unknown | 0 |
| 16 | Treatment | 1 | Unknown |
| 17 | Treatment | 1 | Unknown |
| 18 | Treatment | 1 | Unknown |
| 19 | Treatment | 1 | Unknown |
| 20 | Treatment | 1 | Unknown |
| 21 | Treatment | 1 | Unknown |
| 22 | Treatment | 1 | Unknown |
| 23 | Treatment | 1 | Unknown |
| 24 | Treatment | 1 | Unknown |
| 25 | Treatment | 0 | Unknown |
| 26 | Treatment | 0 | Unknown |
| 27 | Treatment | 0 | Unknown |
| 28 | Treatment | 0 | Unknown |
| 29 | Treatment | 0 | Unknown |
| 30 | Treatment | 0 | Unknown |
In [5]:
bta
Out[5]:
| Group | Result | |
|---|---|---|
| 0 | Control | 1.0 |
| 1 | Control | 1.0 |
| 2 | Control | 0.0 |
| 3 | Control | 0.0 |
| 4 | Control | 0.0 |
| 5 | Control | 0.0 |
| 6 | Control | 0.0 |
| 7 | Control | 0.0 |
| 8 | Control | 0.0 |
| 9 | Control | 0.0 |
| 10 | Control | 0.0 |
| 11 | Control | 0.0 |
| 12 | Control | 0.0 |
| 13 | Control | 0.0 |
| 14 | Control | 0.0 |
| 15 | Control | 0.0 |
| 16 | Treatment | 1.0 |
| 17 | Treatment | 1.0 |
| 18 | Treatment | 1.0 |
| 19 | Treatment | 1.0 |
| 20 | Treatment | 1.0 |
| 21 | Treatment | 1.0 |
| 22 | Treatment | 1.0 |
| 23 | Treatment | 1.0 |
| 24 | Treatment | 1.0 |
| 25 | Treatment | 0.0 |
| 26 | Treatment | 0.0 |
| 27 | Treatment | 0.0 |
| 28 | Treatment | 0.0 |
| 29 | Treatment | 0.0 |
| 30 | Treatment | 0.0 |
In [6]:
num_repetitions = 20000
L = []
for i in range(num_repetitions):
shuffle = bta.sample(frac=1.0, replace=False)['Group']
shuffled_bta = bta.assign(Shuffled_groups=shuffle.reset_index().iloc[:, 1])
means = shuffled_bta.groupby('Shuffled_groups')['Result'].mean().reset_index()
L.append(means.iloc[0, 1] - means.iloc[1, 1])
L = np.array(L)
L = np.abs(L)
In [7]:
fig, ax = subplots(figsize = (4, 4))
ax.hist(L, bins = np.arange(0, 0.7, 0.1));
ax.scatter(0.475, 0, color='red');
In [8]:
empirical_pvalue = np.sum(L >= 0.475)/num_repetitions
empirical_pvalue
Out[8]:
np.float64(0.0106)
Deflategate¶
In [9]:
balls = pd.read_csv('deflategate.csv')
balls
Out[9]:
| Team | Blakeman | Prioleau | |
|---|---|---|---|
| 0 | Patriots | 11.50 | 11.80 |
| 1 | Patriots | 10.85 | 11.20 |
| 2 | Patriots | 11.15 | 11.50 |
| 3 | Patriots | 10.70 | 11.00 |
| 4 | Patriots | 11.10 | 11.45 |
| 5 | Patriots | 11.60 | 11.95 |
| 6 | Patriots | 11.85 | 12.30 |
| 7 | Patriots | 11.10 | 11.55 |
| 8 | Patriots | 10.95 | 11.35 |
| 9 | Patriots | 10.50 | 10.90 |
| 10 | Patriots | 10.90 | 11.35 |
| 11 | Colts | 12.70 | 12.35 |
| 12 | Colts | 12.75 | 12.30 |
| 13 | Colts | 12.50 | 12.95 |
| 14 | Colts | 12.55 | 12.15 |
In [10]:
average_balls = balls.assign(Combined = (balls['Blakeman']+balls['Prioleau'])/2)
average_balls
Out[10]:
| Team | Blakeman | Prioleau | Combined | |
|---|---|---|---|---|
| 0 | Patriots | 11.50 | 11.80 | 11.650 |
| 1 | Patriots | 10.85 | 11.20 | 11.025 |
| 2 | Patriots | 11.15 | 11.50 | 11.325 |
| 3 | Patriots | 10.70 | 11.00 | 10.850 |
| 4 | Patriots | 11.10 | 11.45 | 11.275 |
| 5 | Patriots | 11.60 | 11.95 | 11.775 |
| 6 | Patriots | 11.85 | 12.30 | 12.075 |
| 7 | Patriots | 11.10 | 11.55 | 11.325 |
| 8 | Patriots | 10.95 | 11.35 | 11.150 |
| 9 | Patriots | 10.50 | 10.90 | 10.700 |
| 10 | Patriots | 10.90 | 11.35 | 11.125 |
| 11 | Colts | 12.70 | 12.35 | 12.525 |
| 12 | Colts | 12.75 | 12.30 | 12.525 |
| 13 | Colts | 12.50 | 12.95 | 12.725 |
| 14 | Colts | 12.55 | 12.15 | 12.350 |
In [11]:
L = np.ones(11)*12.5
L = np.append(L, np.ones(4)*13)
L
Out[11]:
array([12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5,
13. , 13. , 13. , 13. ])
In [12]:
balls2 = average_balls.assign(Drop = L - average_balls['Combined'])
balls2
Out[12]:
| Team | Blakeman | Prioleau | Combined | Drop | |
|---|---|---|---|---|---|
| 0 | Patriots | 11.50 | 11.80 | 11.650 | 0.850 |
| 1 | Patriots | 10.85 | 11.20 | 11.025 | 1.475 |
| 2 | Patriots | 11.15 | 11.50 | 11.325 | 1.175 |
| 3 | Patriots | 10.70 | 11.00 | 10.850 | 1.650 |
| 4 | Patriots | 11.10 | 11.45 | 11.275 | 1.225 |
| 5 | Patriots | 11.60 | 11.95 | 11.775 | 0.725 |
| 6 | Patriots | 11.85 | 12.30 | 12.075 | 0.425 |
| 7 | Patriots | 11.10 | 11.55 | 11.325 | 1.175 |
| 8 | Patriots | 10.95 | 11.35 | 11.150 | 1.350 |
| 9 | Patriots | 10.50 | 10.90 | 10.700 | 1.800 |
| 10 | Patriots | 10.90 | 11.35 | 11.125 | 1.375 |
| 11 | Colts | 12.70 | 12.35 | 12.525 | 0.475 |
| 12 | Colts | 12.75 | 12.30 | 12.525 | 0.475 |
| 13 | Colts | 12.50 | 12.95 | 12.725 | 0.275 |
| 14 | Colts | 12.55 | 12.15 | 12.350 | 0.650 |
In [13]:
means = balls2.groupby('Team')['Drop'].mean().reset_index()
means
Out[13]:
| Team | Drop | |
|---|---|---|
| 0 | Colts | 0.468750 |
| 1 | Patriots | 1.202273 |
In [14]:
num_repetitions = 30000
L = []
for i in range(num_repetitions):
shuffle = balls2.sample(frac=1.0, replace=False)['Team']
shuffled_balls = balls2.assign(Shuffled_teams=shuffle.reset_index().iloc[:, 1])
means = shuffled_balls.groupby('Shuffled_teams')['Drop'].mean().reset_index()
L.append(means.iloc[1, 1] - means.iloc[0, 1])
L = np.array(L)
In [15]:
fig, ax = subplots(figsize = (4, 4))
ax.hist(L);
ax.scatter(0.73352, 0, color='red');
In [16]:
empirical_pvalue = np.sum(L >= 0.73352)/num_repetitions
empirical_pvalue
Out[16]:
np.float64(0.0032)
In [ ]: