data-science-ipython-notebooks/scipy/hypothesis.ipynb

653 lines
30 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hypothesis Testing\n",
"=============================\n",
"\n",
"Credits: Forked from [CompStats](https://github.com/AllenDowney/CompStats) by Allen Downey. License: [Creative Commons Attribution 4.0 International](http://creativecommons.org/licenses/by/4.0/)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import print_function, division\n",
"\n",
"import numpy\n",
"import scipy.stats\n",
"\n",
"import matplotlib.pyplot as pyplot\n",
"\n",
"from IPython.html.widgets import interact, fixed\n",
"from IPython.html import widgets\n",
"\n",
"import first\n",
"\n",
"# seed the random number generator so we all get the same results\n",
"numpy.random.seed(19)\n",
"\n",
"# some nicer colors from http://colorbrewer2.org/\n",
"COLOR1 = '#7fc97f'\n",
"COLOR2 = '#beaed4'\n",
"COLOR3 = '#fdc086'\n",
"COLOR4 = '#ffff99'\n",
"COLOR5 = '#386cb0'\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Part One\n",
"========\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an example, let's look at differences between groups. The example I use in _Think Stats_ is first babies compared with others. The `first` module provides code to read the data into three pandas Dataframes."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"live, firsts, others = first.MakeFrames()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The apparent effect we're interested in is the difference in the means. Other examples might include a correlation between variables or a coefficient in a linear regression. The number that quantifies the size of the effect, whatever it is, is the \"test statistic\"."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def TestStatistic(data):\n",
" group1, group2 = data\n",
" test_stat = abs(group1.mean() - group2.mean())\n",
" return test_stat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the first example, I extract the pregnancy length for first babies and others. The results are pandas Series objects."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"group1 = firsts.prglngth\n",
"group2 = others.prglngth"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The actual difference in the means is 0.078 weeks, which is only 13 hours."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.078037266777549519"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"actual = TestStatistic((group1, group2))\n",
"actual"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The null hypothesis is that there is no difference between the groups. We can model that by forming a pooled sample that includes first babies and others."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"n, m = len(group1), len(group2)\n",
"pool = numpy.hstack((group1, group2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we can simulate the null hypothesis by shuffling the pool and dividing it into two groups, using the same sizes as the actual sample."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def RunModel():\n",
" numpy.random.shuffle(pool)\n",
" data = pool[:n], pool[n:]\n",
" return data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result of running the model is two NumPy arrays with the shuffled pregnancy lengths:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(array([36, 40, 39, ..., 43, 42, 40]), array([43, 39, 32, ..., 37, 35, 41]))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RunModel()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we compute the same test statistic using the simulated data:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.081758440969863955"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TestStatistic(RunModel())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we run the model 1000 times and compute the test statistic, we can see how much the test statistic varies under the null hypothesis."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1000,)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_stats = numpy.array([TestStatistic(RunModel()) for i in range(1000)])\n",
"test_stats.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's the sampling distribution of the test statistic under the null hypothesis, with the actual difference in means indicated by a gray line."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEPCAYAAABRHfM8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAE0ZJREFUeJzt3X2QnVVhx/HvJQEhxmxIwbzsRoO8TEWCQCyk4svVpim2\nClgqkFaNhaozFHGsM5XoTMnaim/FKqXasQgGlNSMlDSMtSRQbwsdkkAgBFioJCVjNkKCvIaxYrK5\n/eOczT7Zvbs5u3ef+zy7+/3MPHPPPc/b2cPl/nKetwuSJEmSJEmSJEmSJEmSJEmScnQksAHYDHQB\nX4z1M4B1wE+BtcD0zDrLgCeAx4HFLWupJKlwU+LrZGA98DbgK8BfxvrPAF+K5ZMJ4XI4MA/YChzW\nqoZKksphCnAf8CbC6GFmrJ8V30MYXXwms86/Awtb1UBJ0tDy/hf8YYRRwy7gJ8CjhLDYFefvoi88\n5gDdmXW7gfac2ydJSjQ55+3vB04D2oA7gHf1m1+P02CGmidJaqG8A6PXi8CPgAWEUcUs4GlgNrA7\nLrMTmJtZpyPWHeT444+vb9u2LdfGStI4tA04oZkN5HlI6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gFPA7OB3bF+JzA3s1xHrBtg7llL8myfJI151WqVarV64H1n\nZ2fT2yzikNQaYGksLwVWZ+ovBo4AjgNOBDa2vHWSpIbyHmGsBN4JHAPsAP4K+BKwCriUcHL7wrhs\nV6zvAvYBlzH04SpJUgvlHRiDHTtaNEj91XGSJJWM9zlIkpIYGJKkJAaGJCmJgSFJSmJgSJKSGBiS\npCQGhiQpiYEhSUpiYEiSkhgYkqQkBoYkKYmBIUlKYmBIkpIYGJKkJAaGJCmJgSFJSmJgSJKSGBiS\npCQGhiQpiYGhcqpMolK
"text/plain": [
"<matplotlib.figure.Figure at 0x7f99fc3fb410>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def VertLine(x):\n",
" \"\"\"Draws a vertical line at x.\"\"\"\n",
" pyplot.plot([x, x], [0, 300], linewidth=3, color='0.8')\n",
"\n",
"VertLine(actual)\n",
"pyplot.hist(test_stats, color=COLOR5)\n",
"pyplot.xlabel('difference in means')\n",
"pyplot.ylabel('count')\n",
"None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The p-value is the probability that the test statistic under the null hypothesis exceeds the actual value."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.14999999999999999"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pvalue = sum(test_stats >= actual) / len(test_stats)\n",
"pvalue"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this case the result is about 15%, which means that even if there is no difference between the groups, it is plausible that we could see a sample difference as big as 0.078 weeks.\n",
"\n",
"We conclude that the apparent effect might be due to chance, so we are not confident that it would appear in the general population, or in another sample from the same population."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Part Two\n",
"========\n",
"\n",
"We can take the pieces from the previous section and organize them in a class that represents the structure of a hypothesis test."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class HypothesisTest(object):\n",
" \"\"\"Represents a hypothesis test.\"\"\"\n",
"\n",
" def __init__(self, data):\n",
" \"\"\"Initializes.\n",
"\n",
" data: data in whatever form is relevant\n",
" \"\"\"\n",
" self.data = data\n",
" self.MakeModel()\n",
" self.actual = self.TestStatistic(data)\n",
" self.test_stats = None\n",
"\n",
" def PValue(self, iters=1000):\n",
" \"\"\"Computes the distribution of the test statistic and p-value.\n",
"\n",
" iters: number of iterations\n",
"\n",
" returns: float p-value\n",
" \"\"\"\n",
" self.test_stats = numpy.array([self.TestStatistic(self.RunModel()) \n",
" for _ in range(iters)])\n",
"\n",
" count = sum(self.test_stats >= self.actual)\n",
" return count / iters\n",
"\n",
" def MaxTestStat(self):\n",
" \"\"\"Returns the largest test statistic seen during simulations.\n",
" \"\"\"\n",
" return max(self.test_stats)\n",
"\n",
" def PlotHist(self, label=None):\n",
" \"\"\"Draws a Cdf with vertical lines at the observed test stat.\n",
" \"\"\"\n",
" def VertLine(x):\n",
" \"\"\"Draws a vertical line at x.\"\"\"\n",
" pyplot.plot([x, x], [0, max(ys)], linewidth=3, color='0.8')\n",
"\n",
" ys, xs, patches = pyplot.hist(ht.test_stats, color=COLOR4)\n",
" VertLine(self.actual)\n",
" pyplot.xlabel('test statistic')\n",
" pyplot.ylabel('count')\n",
"\n",
" def TestStatistic(self, data):\n",
" \"\"\"Computes the test statistic.\n",
"\n",
" data: data in whatever form is relevant \n",
" \"\"\"\n",
" raise UnimplementedMethodException()\n",
"\n",
" def MakeModel(self):\n",
" \"\"\"Build a model of the null hypothesis.\n",
" \"\"\"\n",
" pass\n",
"\n",
" def RunModel(self):\n",
" \"\"\"Run the model of the null hypothesis.\n",
"\n",
" returns: simulated data\n",
" \"\"\"\n",
" raise UnimplementedMethodException()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`HypothesisTest` is an abstract parent class that encodes the template. Child classes fill in the missing methods. For example, here's the test from the previous section."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class DiffMeansPermute(HypothesisTest):\n",
" \"\"\"Tests a difference in means by permutation.\"\"\"\n",
"\n",
" def TestStatistic(self, data):\n",
" \"\"\"Computes the test statistic.\n",
"\n",
" data: data in whatever form is relevant \n",
" \"\"\"\n",
" group1, group2 = data\n",
" test_stat = abs(group1.mean() - group2.mean())\n",
" return test_stat\n",
"\n",
" def MakeModel(self):\n",
" \"\"\"Build a model of the null hypothesis.\n",
" \"\"\"\n",
" group1, group2 = self.data\n",
" self.n, self.m = len(group1), len(group2)\n",
" self.pool = numpy.hstack((group1, group2))\n",
"\n",
" def RunModel(self):\n",
" \"\"\"Run the model of the null hypothesis.\n",
"\n",
" returns: simulated data\n",
" \"\"\"\n",
" numpy.random.shuffle(self.pool)\n",
" data = self.pool[:self.n], self.pool[self.n:]\n",
" return data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can run the test by instantiating a DiffMeansPermute object:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"means permute pregnancy length\n",
"p-value = 0.16\n",
"actual = 0.0780372667775\n",
"ts max = 0.173695697482\n"
]
}
],
"source": [
"data = (firsts.prglngth, others.prglngth)\n",
"ht = DiffMeansPermute(data)\n",
"p_value = ht.PValue(iters=1000)\n",
"print('\\nmeans permute pregnancy length')\n",
"print('p-value =', p_value)\n",
"print('actual =', ht.actual)\n",
"print('ts max =', ht.MaxTestStat())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And we can plot the sampling distribution of the test statistic under the null hypothesis."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEPCAYAAABRHfM8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEn5JREFUeJzt3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+7+npGfY2iw6MZcB5\nwDHAa8BfAzcSLjNyJWFw+5K47IbYvgHoBa7CLilJKo2iA+OyJu3nN2m/IU6SpJLJPegtSRohDAxJ\nUhIDQ5KUxMCQJCUxMDTijB07hkqlMqSp0VC30dU1KdO/XMorx3kY0rD09n5AX9/tQ1p39er++aFu\no1JZPKT1pJHOIwxJUhIDQ5KUxMCQJCUxMCRJSQwMSVISA0OSlMTAkCQlMTAkSUkMDElSEgNDkpTE\nwJAkJTEwJElJDAxJUhIDQ5KUxMCQJCUxMCRJSQwMSVISA0OSlMTAkCQlMTAkSUkMDElSEgNDkpTE\nwJAkJTEwJElJDAxJUhIDQ5KUxMCQJCUxMCRJSQwMSVISA0OSlGRsxvfeCvwU2APsBs4CuoDvAh+J\nr18CvJ2nPElSo5xHGH1AFZhDCAuA64DHgJnA4/G5JKkEcndJVfZ7fiGwNM4vBS7qbDmSpGZyH2F8\nH3gO+HxsOx7YHue3x+dSqYwdO4ZKpZJ16uqalPvHoFEo5xjGucAbwLGEbqiN+73eF6cDdHev3Dtf\nrc6kWp1VUInSgXp7P6Cv7/asNVQqi7O+v8qvVqtRq9Xaus2cgfFGfHwL+B5hHGM7MBl4E5gC7Bho\nxe7uBZ2oT5JGrGq1SrVa3fu8p6dn2NvM1SV1BHBUnD8SmAesB1YAi2L7ImB550uTJA0k1xHG8YSj\ninoN9wOPEsYzHgSupP9
"text/plain": [
"<matplotlib.figure.Figure at 0x7f99fd66c990>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ht.PlotHist()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an exercise, write a class named `DiffStdPermute` that extends `DiffMeansPermute` and overrides `TestStatistic` to compute the difference in standard deviations. Is the difference in standard deviations statistically significant?"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"std permute pregnancy length\n",
"p-value = 0.155\n",
"actual = 0.176049064229\n",
"ts max = 0.44299505029\n"
]
}
],
"source": [
"class DiffStdPermute(DiffMeansPermute):\n",
" \"\"\"Tests a difference in means by permutation.\"\"\"\n",
"\n",
" def TestStatistic(self, data):\n",
" \"\"\"Computes the test statistic.\n",
"\n",
" data: data in whatever form is relevant \n",
" \"\"\"\n",
" group1, group2 = data\n",
" test_stat = abs(group1.std() - group2.std())\n",
" return test_stat\n",
"\n",
"data = (firsts.prglngth, others.prglngth)\n",
"ht = DiffStdPermute(data)\n",
"p_value = ht.PValue(iters=1000)\n",
"print('\\nstd permute pregnancy length')\n",
"print('p-value =', p_value)\n",
"print('actual =', ht.actual)\n",
"print('ts max =', ht.MaxTestStat())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's run DiffMeansPermute again to see if there is a difference in birth weight between first babies and others."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"means permute birthweight\n",
"p-value = 0.0\n",
"actual = 0.124761184535\n",
"ts max = 0.0917504268392\n"
]
}
],
"source": [
"data = (firsts.totalwgt_lb.dropna(), others.totalwgt_lb.dropna())\n",
"ht = DiffMeansPermute(data)\n",
"p_value = ht.PValue(iters=1000)\n",
"print('\\nmeans permute birthweight')\n",
"print('p-value =', p_value)\n",
"print('actual =', ht.actual)\n",
"print('ts max =', ht.MaxTestStat())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this case, after 1000 attempts, we never see a sample difference as big as the observed difference, so we conclude that the apparent effect is unlikely under the null hypothesis. Under normal circumstances, we can also make the inference that the apparent effect is unlikely to be caused by random sampling.\n",
"\n",
"One final note: in this case I would report that the p-value is less than 1/1000 or 0.001. I would not report that p=0, because the apparent effect is not impossible under the null hypothesis; just unlikely."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}