{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "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": [
    "# Error Probabilities\n",
    "\n",
    "In the process by which we decide which of two hypotheses is better supported by our data, the final step involves a judgment about the consistency of the data and the null hypothesis. While this step results in a good decision a vast majority of the time, it can sometimes lead us astray. The reason is chance variation. For example, even when the null hypothesis is true, chance variation might cause the sample to look quite different from what the null hypothesis predicts. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wrong Conclusions\n",
    "\n",
    "If you are testing a null hypothesis against the alternative that the null hypothesis isn't true, then there are four ways of classifying reality and the result of the test.\n",
    "\n",
    "|     | Null is True | Alternative is True |\n",
    "|----:|:-----------------------:|:------------------------:|\n",
    "|**Test Favors the Null**| Correct result | Error |\n",
    "|**Test Favors the Alternative**| Error | Correct result |\n",
    "\n",
    "In two out of the four cells of this table, the test result is wrong. One type of error occurs if the test favors the alternative hypothesis when in fact the null hypothesis is true. The other type of error occurs if the test favors the null hypothesis when in fact the alternative hypothesis is true.\n",
    "\n",
    "Since the null hypothesis is a completely specified chance model, we can estimate the chance of the first type of error. The answer turns out to be essentially the cutoff that we use for the P-value. Let's see how."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Chance of an Error\n",
    "Suppose you want to test whether a coin is fair or not. Then the hypotheses are:\n",
    "\n",
    "**Null:** The coin is fair. That is, the results are like draws made at random with replacement from *Heads, Tails*.\n",
    "\n",
    "**Alternative:** The coin is not fair.\n",
    "\n",
    "Suppose you are going to test this hypothesis based on 2000 tosses of the coin. You would expect a fair coin to land heads 1000 times out of 2000, so a reasonable test statistic to use is\n",
    "\n",
    "$$\n",
    "\\text{test statistic} ~ = ~ \\big{\\vert} ~ \\text{number of heads} - 1000 ~ \\big{\\vert}\n",
    "$$\n",
    "\n",
    "Small values of this statistic favor the null hypothesis, and large values favor the alternative.\n",
    "\n",
    "We have simulated this statistic under the null hypothesis many times, and drawn its empirical distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true,
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "fair_coin = [1, 0]\n",
    "\n",
    "def one_simulated_statistic():\n",
    "    number_of_heads = sum(np.random.choice(fair_coin, 2000))\n",
    "    return abs(number_of_heads - 1000)\n",
    "\n",
    "statistics = np.array([])\n",
    "\n",
    "for i in np.arange(50000):\n",
    "    statistics = np.append(statistics, one_simulated_statistic())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17572 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "results = pd.DataFrame({'|Number of Heads - 1000|':statistics})\n",
    "\n",
    "unit = ''\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "ax.hist(results, bins=np.arange(0, 101, 5),density=True, alpha=0.8, ec='white', zorder=5)\n",
    "\n",
    "ax.plot([45, 45], [0, 0.035], color='gold', lw=2, zorder=10)\n",
    "\n",
    "y_vals = ax.get_yticks()\n",
    "\n",
    "y_label = 'Percent per ' + (unit if unit else 'unit')\n",
    "\n",
    "x_label = '|Number of Heads - 1000|'\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('');\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The area to the right of 45 (where the gold line is) is about 5%. Large values of the test statistic favor the alternative. So if the test statistic comes out to be 45 or more, the test will conclude that the coin is unfair.\n",
    "\n",
    "However, as the figure shows, a fair coin can produce test statistics with values 45 or more. In fact it does so with chance about 5%.\n",
    "\n",
    "So *if the coin is fair* and our test uses a 5% cutoff for deciding whether it is fair or not, then there is about a 5% chance that the test will wrongly conclude that the coin is unfair."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Cutoff for the P-value is an Error Probability\n",
    "The example above is a special case of a general fact:\n",
    "\n",
    "**If you use a $p$% cutoff for the P-value, and the null hypothesis happens to be true, then there is about a $p$% chance that your test will conclude that the alternative is true.**\n",
    "\n",
    "The 1% cutoff is therefore more conservative than 5%. There is less chance of concluding \"alternative\" if the null happens to be true. For this reason, randomized controlled trials of medical treatments usually use 1% as the cutoff for deciding between the following two hypotheses:\n",
    "\n",
    "**Null Hypothesis.** The treatment has no effect; observed differences between the outcomes of the treatment and control groups of patients are due to randomization.\n",
    "\n",
    "**Alternative Hypothesis.** The treatment has an effect.\n",
    "\n",
    "The idea is to control the chance of concluding the treatment does something if in fact it does nothing. This reduces the risk of giving patients a useless treatment.\n",
    "\n",
    "Still, even if you set the cutoff to be as low as 1%, and the treatment does nothing, there is about a 1% chance of concluding that the treatment does something. This is due to chance variation. There is a small chance that data from random samples end up leading you astray."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data Snooping and P-Hacking\n",
    "The discussion above implies that if each of 100 different research groups runs a separate randomized controlled experiment about the effect of a treatment that in fact has no effect, and each experiment uses a 1% cutoff for the P-value, then by chance variation, one of the experiments is expected to wrongly conclude that the treatment does have an effect.\n",
    "\n",
    "Unfortunately, that could be the one that gets published. This is why it is important that experiments be *replicated*. That is, other researchers ought to be able to carry out the experiment and see if they get similar results.\n",
    "\n",
    "It is not uncommon for researchers to test multiple hypotheses using the same data. For example, in a randomized controlled trial about the effect of a drug, researchers might test whether the drug has an effect on various different diseases. \n",
    "\n",
    "Now suppose the drug has no effect on anything. Just by chance variation, a small percent of the tests might conclude that it does have an effect. So, when you read a study that uses tests of hypotheses and concludes that a treatment has an effect, always ask how many different effects were tested before the researchers found the one that was reported.\n",
    "\n",
    "If the researchers ran multiple different tests before finding one that gave a \"highly statistically significant\" result, use the result with caution. The study could be marred by *data snooping*, which essentially means torturing the data into making a false confession. This is sometimes also called *p-hacking*.\n",
    "\n",
    "In such a situation, one way to validate the reported result is by replicating the experiment and testing for that particular effect alone. If it comes out significant again, that will validate the original conclusion."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Technical Note: The Other Kind of Error\n",
    "There is, of course, another kind of error: concluding that the treatment does nothing when in fact it does something. Discussions of that error are outside the scope of this course. Just be aware that life isn't easy: if you set up your test to reduce one of the two errors, you almost always increase the other one. "
   ]
  }
 ],
 "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": 1
}
