STATISTICAL THINKING IN PYTHON II
Formulating and simulating hypotheses
Statistical Thinking in Python II
2008 US swing state election results
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
Statistical Thinking in Python II
Hypothesis testing ●
Assessment of how reasonable the observed data are assuming a hypothesis is true
Statistical Thinking in Python II
Null hypothesis
●
Another name for the hypothesis you are testing
Statistical Thinking in Python II
ECDFs of swing state election results
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
Percent vote for Obama PA
OH
PA — OH difference
mean
45.5%
44.3%
1.2%
median
44.0%
43.7%
0.4%
standard deviation
9.8%
9.9%
—0.1%
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
Simulating the hypothesis 60.08, 44.71, 37.77, 43.82, 50.96, 57.88, 54.58, 44.03, 40.09, 45.26, 48.36, 40.18, 39.26, 50.10, 31.56, 41.32, 38.25, 40.47, 58.36, 49.58,
40.64, 46.15, 40.11, 55.97, 47.43, 36.05, 32.88, 33.56, 60.81, 57.01, 45.89, 43.05, 47.47, 39.84, 39.86, 48.46, 6.80, 49.44, 68.02, 41.97,
36.07, 63.10, 49.85, 58.23, 56.24, 37.72, 54.37, 37.26, 49.81, 53.61, 48.62, 41.56, 27.92, 36.43, 45.31, 53.14, 31.75, 37.62, 38.53, 38.11
41.21, 52.20, 48.61, 42.97, 45.60, 50.36, 40.45, 54.64, 56.94, 59.10, 43.92, 42.49, 38.01, 35.71, 35.47, 34.01, 46.33, 36.71, 34.58,
31.04, 43.18, 38.62, 42.38, 46.39, 32.12, 47.61, 43.12, 50.46, 61.48, 38.23, 36.06, 45.45, 31.47, 51.38, 54.74, 44.90, 46.73, 69.64,
Data retrieved from Data.gov (h!ps://www.data.gov/)
43.78, 40.24, 54.25, 36.11, 35.22, 41.55, 60.49, 25.34, 65.99, 43.43, 28.79, 52.76, 29.07, 47.01, 46.33, 40.67, 33.57, 42.20, 60.50,
44.08, 39.92, 34.84, 37.53, 48.56, 54.66, 43.11, 49.79, 45.88, 44.69, 63.57, 46.07, 28.94, 40.10, 48.73, 38.96, 38.10, 53.16, 53.53,
46.85, 47.87, 47.75, 42.65, 32.97, 57.81, 27.32, 83.56, 42.23, 54.59, 38.07, 39.43, 51.28, 48.76, 41.77, 46.29, 39.67, 52.40, 36.54,
Pennsylvania
Ohio
Statistical Thinking in Python II
Simulating the hypothesis 60.08, 44.71, 37.77, 43.82, 50.96, 57.88, 54.58, 44.03, 40.09, 45.26, 48.36, 40.18, 39.26, 50.10, 31.56, 41.32, 38.25, 40.47, 58.36, 49.58,
40.64, 46.15, 40.11, 55.97, 47.43, 36.05, 32.88, 33.56, 60.81, 57.01, 45.89, 43.05, 47.47, 39.84, 39.86, 48.46, 6.80, 49.44, 68.02, 41.97,
36.07, 63.10, 49.85, 58.23, 56.24, 37.72, 54.37, 37.26, 49.81, 53.61, 48.62, 41.56, 27.92, 36.43, 45.31, 53.14, 31.75, 37.62, 38.53, 38.11
41.21, 52.20, 48.61, 42.97, 45.60, 50.36, 40.45, 54.64, 56.94, 59.10, 43.92, 42.49, 38.01, 35.71, 35.47, 34.01, 46.33, 36.71, 34.58,
31.04, 43.18, 38.62, 42.38, 46.39, 32.12, 47.61, 43.12, 50.46, 61.48, 38.23, 36.06, 45.45, 31.47, 51.38, 54.74, 44.90, 46.73, 69.64,
Data retrieved from Data.gov (h!ps://www.data.gov/)
43.78, 40.24, 54.25, 36.11, 35.22, 41.55, 60.49, 25.34, 65.99, 43.43, 28.79, 52.76, 29.07, 47.01, 46.33, 40.67, 33.57, 42.20, 60.50,
44.08, 39.92, 34.84, 37.53, 48.56, 54.66, 43.11, 49.79, 45.88, 44.69, 63.57, 46.07, 28.94, 40.10, 48.73, 38.96, 38.10, 53.16, 53.53,
46.85, 47.87, 47.75, 42.65, 32.97, 57.81, 27.32, 83.56, 42.23, 54.59, 38.07, 39.43, 51.28, 48.76, 41.77, 46.29, 39.67, 52.40, 36.54,
Statistical Thinking in Python II
Simulating the hypothesis 59.10, 48.36, 28.79, 48.62, 48.76, 44.08, 35.71, 31.56, 45.88, 25.34, 38.25, 36.06, 47.47, 38.07, 46.33, 40.09, 42.20, 40.10, 42.38, 61.48,
38.62, 54.59, 55.97, 54.66, 41.77, 46.29, 39.86, 38.23, 40.45, 46.73, 40.47, 47.61, 34.84, 31.47, 46.39, 36.43, 47.43, 46.33, 38.10, 50.10,
51.38, 40.11, 33.57, 54.74, 36.54, 49.81, 40.67, 37.26, 32.12, 44.90, 31.04, 51.28, 49.58, 36.11, 43.11, 36.71, 44.71, 53.16, 43.82, 31.75
60.49, 57.81, 42.23, 48.46, 47.01, 69.64, 65.99, 41.21, 35.22, 56.94, 54.25, 43.43, 48.73, 39.26, 38.53, 60.08, 43.78, 32.88, 45.31,
6.80, 45.89, 48.61, 36.07, 52.76, 60.50, 50.46, 37.53, 49.79, 58.23, 46.15, 42.97, 29.07, 41.56, 33.56, 50.36, 39.92, 38.96, 60.81,
Data retrieved from Data.gov (h!ps://www.data.gov/)
41.97, 83.56, 44.69, 43.92, 49.44, 27.32, 37.72, 46.85, 43.12, 39.84, 57.01, 38.01, 54.58, 52.40, 42.65, 39.43, 37.62, 41.55, 54.37,
48.56, 40.64, 39.67, 49.85, 34.58, 45.60, 50.96, 44.03, 43.18, 36.05, 52.20, 54.64, 27.92, 40.18, 68.02, 28.94, 63.57, 56.24, 53.14,
37.77, 46.07, 57.88, 53.53, 40.24, 63.10, 42.49, 41.32, 45.45, 43.05, 47.75, 45.26, 34.01, 47.87, 35.47, 58.36, 53.61, 38.11, 32.97,
Statistical Thinking in Python II
Simulating the hypothesis 59.10, 48.36, 28.79, 48.62, 48.76, 44.08, 35.71, 31.56, 45.88, 25.34, 38.25, 36.06, 47.47, 38.07, 46.33, 40.09, 42.20, 40.10, 42.38, 61.48,
38.62, 54.59, 55.97, 54.66, 41.77, 46.29, 39.86, 38.23, 40.45, 46.73, 40.47, 47.61, 34.84, 31.47, 46.39, 36.43, 47.43, 46.33, 38.10, 50.10,
51.38, 40.11, 33.57, 54.74, 36.54, 49.81, 40.67, 37.26, 32.12, 44.90, 31.04, 51.28, 49.58, 36.11, 43.11, 36.71, 44.71, 53.16, 43.82, 31.75
60.49, 57.81, 42.23, 48.46, 47.01, 69.64, 65.99, 41.21, 35.22, 56.94, 54.25, 43.43, 48.73, 39.26, 38.53, 60.08, 43.78, 32.88, 45.31,
6.80, 45.89, 48.61, 36.07, 52.76, 60.50, 50.46, 37.53, 49.79, 58.23, 46.15, 42.97, 29.07, 41.56, 33.56, 50.36, 39.92, 38.96, 60.81,
Data retrieved from Data.gov (h!ps://www.data.gov/)
41.97, 83.56, 44.69, 43.92, 49.44, 27.32, 37.72, 46.85, 43.12, 39.84, 57.01, 38.01, 54.58, 52.40, 42.65, 39.43, 37.62, 41.55, 54.37,
48.56, 40.64, 39.67, 49.85, 34.58, 45.60, 50.96, 44.03, 43.18, 36.05, 52.20, 54.64, 27.92, 40.18, 68.02, 28.94, 63.57, 56.24, 53.14,
37.77, 46.07, 57.88, 53.53, 40.24, 63.10, 42.49, 41.32, 45.45, 43.05, 47.75, 45.26, 34.01, 47.87, 35.47, 58.36, 53.61, 38.11, 32.97,
"Pennsylvania"
"Ohio"
Statistical Thinking in Python II
Permutation
●
Random reordering of entries in an array
Statistical Thinking in Python II
Generating a permutation sample In [1]: import numpy as np In [2]: dem_share_both = np.concatenate( ...: (dem_share_PA, dem_share_OH)) In [3]: dem_share_perm = np.random.permutation(dem_share_both) In [4]: perm_sample_PA = dem_share_perm[:len(dem_share_PA)] In [5]: perm_sample_OH = dem_share_perm[len(dem_share_PA):]
STATISTICAL THINKING IN PYTHON II
Let’s practice!
STATISTICAL THINKING IN PYTHON II
Test statistics and p-values
Statistical Thinking in Python II
Are OH and PA different?
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
Hypothesis testing ●
Assessment of how reasonable the observed data are assuming a hypothesis is true
Statistical Thinking in Python II
Test statistic ●
A single number that can be computed from observed data and from data you simulate under the null hypothesis
●
It serves as a basis of comparison between the two
Statistical Thinking in Python II
Permutation replicate In [1]: np.mean(perm_sample_PA) - np.mean(perm_sample_OH) Out[1]: 1.122220149253728 In [2]: np.mean(dem_share_PA) - np.mean(dem_share_OH) # orig. data Out[2]: 1.1582360922659518
Statistical Thinking in Python II
Mean vote difference under null hypothesis
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
Mean vote difference under null hypothesis
p-value
Data retrieved from Data.gov (h!ps://www.data.gov/)
Statistical Thinking in Python II
p-value ●
The probability of obtaining a value of your test statistic that is at least as extreme as what was observed, under the assumption the null hypothesis is true
●
NOT the probability that the null hypothesis is true
Statistical Thinking in Python II
Statistical significance
●
Determined by the smallness of a p-value
Statistical Thinking in Python II
Null hypothesis significance testing (NHST)
●
Another name for what we are doing in this chapter
Statistical Thinking in Python II
statistical significance ≠ practical significance
STATISTICAL THINKING IN PYTHON II
Let’s practice!
STATISTICAL THINKING IN PYTHON II
Bootstrap hypothesis tests
Statistical Thinking in Python II
Pipeline for hypothesis testing ●
Clearly state the null hypothesis
●
Define your test statistic
●
Generate many sets of simulated data assuming the null hypothesis is true
●
Compute the test statistic for each simulated data set
●
The p-value is the fraction of your simulated data sets for which the test statistic is at least as extreme as for the real data
Statistical Thinking in Python II
Michelson and Newcomb: speed of light pioneers
Albert Michelson
299,852 km/s Michelson image: public domain, Smithsonian Newcomb image: US Library of Congress
Simon Newcomb
299,860 km/s
Statistical Thinking in Python II
The data we have Michelson:
Newcomb:
mean = 299,860 km/s
Data: Michelson, 1880
Statistical Thinking in Python II
Null hypothesis ●
The true mean speed of light in Michelson’s experiments was actually Newcomb's reported value
Statistical Thinking in Python II
Shi"ing the Michelson data In [1]: newcomb_value = 299860
# km/s
In [2]: michelson_shifted = michelson_speed_of_light \ ...: - np.mean(michelson_speed_of_light) + newcomb_value
Statistical Thinking in Python II
Shi"ing the Michelson data In [1]: newcomb_value = 299860
# km/s
In [2]: michelson_shifted = michelson_speed_of_light \ ...: - np.mean(michelson_speed_of_light) + newcomb_value
Michelson's data with same mean as Newcomb
Michelson's data
Statistical Thinking in Python II
Calculating the test statistic In [1]: def diff_from_newcomb(data, newcomb_value=299860): ...: return np.mean(data) - newcomb_value ...: In [2]: diff_obs = diff_from_newcomb(michelson_speed_of_light) In [3]: diff_obs Out[3]: -7.5999999999767169
Statistical Thinking in Python II
Computing the p-value In [1]: bs_replicates = draw_bs_reps(michelson_shifted, ...: diff_from_newcomb, 10000) In [2]: p_value = np.sum(bs_replicates