{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../../data/'\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('fivethirtyeight')\n",
    "%matplotlib inline\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Finding Probabilities\n",
    "Over the centuries, there has been considerable philosophical debate about what probabilities are. Some people think that probabilities are relative frequencies; others think they are long run relative frequencies; still others think that probabilities are a subjective measure of their own personal degree of uncertainty.\n",
    "\n",
    "In this course, most probabilities will be relative frequencies, though many will have subjective interpretations. Regardless, the ways in which probabilities are calculated and combined are consistent across the different interpretations.\n",
    "\n",
    "By convention, probabilities are numbers between 0 and 1, or, equivalently, 0% and 100%. Impossible events have probability 0. Events that are certain have probability 1.\n",
    "\n",
    "Math is the main tool for finding probabilities exactly, though computers are useful for this purpose too. Simulation can provide excellent approximations, with high probability. In this section, we will informally develop a few simple rules that govern the calculation of probabilities. In subsequent sections we will return to simulations to approximate probabilities of complex events.\n",
    "\n",
    "We will use the standard notation $P(\\mbox{event})$ to denote the probability that \"event\" happens, and we will use the words \"chance\" and \"probability\" interchangeably."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## When an Event Doesn't Happen\n",
    "If the chance that event happens is 40%, then the chance that it doesn't happen is 60%. This natural calculation can be described in general as follows:\n",
    "\n",
    "$$\n",
    "P(\\mbox{an event doesn't happen}) ~=~ 1 - P(\\mbox{the event happens})\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## When All Outcomes are Equally Likely\n",
    "If you are rolling an ordinary die, a natural assumption is that all six faces are equally likely. Then probabilities of how one roll comes out can be easily calculated as a ratio. For example, the chance that the die shows an even number is\n",
    "$$\n",
    "\\frac{\\mbox{number of even faces}}{\\mbox{number of all faces}}\n",
    "~=~ \\frac{\\#\\{2, 4, 6\\}}{\\#\\{1, 2, 3, 4, 5, 6\\}}\n",
    "~=~ \\frac{3}{6}\n",
    "$$\n",
    "Similarly,\n",
    "$$\n",
    "P(\\mbox{die shows a multiple of 3}) ~=~\n",
    "\\frac{\\#\\{3, 6\\}}{\\#\\{1, 2, 3, 4, 5, 6\\}}\n",
    "~=~ \\frac{2}{6}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In general,\n",
    "$$\n",
    "P(\\mbox{an event happens}) ~=~\n",
    "\\frac{\\#\\{\\mbox{outcomes that make the event happen}\\}}\n",
    "{\\#\\{\\mbox{all outcomes}\\}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "provided all the outcomes are equally likely."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Not all random phenomena are as simple as one roll of a die. The two main rules of probability, developed below, allow mathematicians to find probabilities even in complex situations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## When Two Events Must Both Happen\n",
    "Suppose you have a box that contains three tickets: one red, one blue, and one green. Suppose you draw two tickets at random without replacement; that is, you shuffle the three tickets, draw one, shuffle the remaining two, and draw another from those two. What is the chance you get the green ticket first, followed by the red one?\n",
    "\n",
    "There are six possible pairs of colors: RB, BR, RG, GR, BG, GB (we've abbreviated the names of each color to just its first letter). All of these are equally likely by the sampling scheme, and only one of them (GR) makes the event happen. So\n",
    "$$\n",
    "P(\\mbox{green first, then red}) ~=~ \n",
    "\\frac{\\#\\{\\mbox{GR}\\}}{\\#\\{\\mbox{RB, BR, RG, GR, BG, GB}\\}} ~=~ \\frac{1}{6}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But there is another way of arriving at the answer, by thinking about the event in two stages. First, the green ticket has to be drawn. That has chance $1/3$, which means that the green ticket is drawn first in about $1/3$ of all repetitions of the experiment. But that doesn't complete the event. *Among the 1/3 of repetitions when green is drawn first*, the red ticket has to be drawn next. That happens in about $1/2$ of those repetitions, and so:\n",
    "$$\n",
    "P(\\mbox{green first, then red}) ~=~ \\frac{1}{2} ~\\mbox{of}~ \\frac{1}{3}\n",
    "~=~ \\frac{1}{6}\n",
    "$$\n",
    "This calculation is usually written \"in chronological order,\" as follows.\n",
    "$$\n",
    "P(\\mbox{green first, then red}) ~=~ \\frac{1}{3} ~\\times~ \\frac{1}{2}\n",
    "~=~ \\frac{1}{6}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The factor of $1/2$ is called \" the conditional chance that the red ticket appears second, given that the green ticket appeared first.\"\n",
    "\n",
    "In general, we have the **multiplication rule**:\n",
    "$$\n",
    "P(\\mbox{two events both happen})\n",
    "~=~ P(\\mbox{one event happens}) \\times \n",
    "P(\\mbox{the other event happens, given that the first one happened})\n",
    "$$\n",
    "\n",
    "Thus, when there are two conditions – one event must happen, as well as another – the chance is *a fraction of a fraction*, which is smaller than either of the two component fractions. The more conditions that have to be satisfied, the less likely they are to all be satisfied."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## When an Event Can Happen in Two Different Ways\n",
    "Suppose instead we want the chance that one of the two tickets is green and the other red. This event doesn't specify the order in which the colors must appear. So they can appear in either order. \n",
    "\n",
    "A good way to tackle problems like this is to *partition* the event so that it can happen in exactly one of several different ways. The natural partition of \"one green and one red\" is: GR, RG. \n",
    "\n",
    "Each of GR and RG has chance $1/6$ by the calculation above. So you can calculate the chance of \"one green and one red\" by adding them up.\n",
    "\n",
    "$$\n",
    "P(\\mbox{one green and one red}) ~=~ P(\\mbox{GR}) + P(\\mbox{RG}) ~=~ \n",
    "\\frac{1}{6} + \\frac{1}{6} ~=~ \\frac{2}{6}\n",
    "$$\n",
    "\n",
    "In general, we have the **addition rule**:\n",
    "\n",
    "$$\n",
    "P(\\mbox{an event happens}) ~=~\n",
    "P(\\mbox{first way it can happen}) + P(\\mbox{second way it can happen}) ~~~\n",
    "\\mbox{}\n",
    "$$\n",
    "\n",
    "provided the event happens in exactly one of the two ways.\n",
    "\n",
    "Thus, when an event can happen in one of two different ways, the chance that it happens is a sum of chances, and hence bigger than the chance of either of the individual ways."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The multiplication rule has a natural extension to more than two events, as we will see below. So also the addition rule has a natural extension to events that can happen in one of several different ways.\n",
    "\n",
    "We end the section with examples that use combinations of all these rules."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## At Least One Success\n",
    "Data scientists often work with random samples from populations. A question that sometimes arises is about the likelihood that a particular individual in the population is selected to be in the sample. To work out the chance, that individual is called a \"success,\" and the problem is to find the chance that the sample contains a success.\n",
    "\n",
    "To see how such chances might be calculated, we start with a simpler setting: tossing a coin two times.\n",
    "\n",
    "If you toss a coin twice, there are four equally likely outcomes: HH, HT, TH, and TT. We have abbreviated \"Heads\" to H and \"Tails\" to T. The chance of getting at least one head in two tosses is therefore 3/4.\n",
    "\n",
    "Another way of coming up with this answer is to work out what happens if you *don't* get at least one head: both the tosses have to land tails. So\n",
    "$$\n",
    "P(\\mbox{at least one head in two tosses}) ~=~ 1 - P(\\mbox{both tails}) ~=~ 1 - \\frac{1}{4}\n",
    "~=~ \\frac{3}{4}\n",
    "$$\n",
    "\n",
    "Notice also that \n",
    "$$\n",
    "P(\\mbox{both tails}) ~=~ \\frac{1}{4} ~=~ \\frac{1}{2} \\cdot \\frac{1}{2} ~=~ \\left(\\frac{1}{2}\\right)^2\n",
    "$$\n",
    "by the multiplication rule.\n",
    "\n",
    "These two observations allow us to find the chance of at least one head in any given number of tosses. For example,\n",
    "$$\n",
    "P(\\mbox{at least one head in 17 tosses}) ~=~ 1 - P(\\mbox{all 17 are tails})\n",
    "~=~ 1 - \\left(\\frac{1}{2}\\right)^{17}\n",
    "$$\n",
    "\n",
    "And now we are in a position to find the chance that the face with six spots comes up at least once in rolls of a die. \n",
    "\n",
    "For example,\n",
    "\n",
    "$$\n",
    "P(\\mbox{a single roll is not 6}) ~=~ P(1) + P(2) + P(3) + P(4) + P(5)\n",
    "~=~ \\frac{5}{6}\n",
    "$$\n",
    "\n",
    "Therefore,\n",
    "\n",
    "$$\n",
    "P(\\mbox{at least one 6 in two rolls}) ~=~ 1 - P(\\mbox{both rolls are not 6})\n",
    "~=~ 1 - \\left(\\frac{5}{6}\\right)^2\n",
    "$$\n",
    "and\n",
    "$$\n",
    "P(\\mbox{at least one 6 in 17 rolls})\n",
    "~=~ 1 - \\left(\\frac{5}{6}\\right)^{17}\n",
    "$$\n",
    "\n",
    "The table below shows these probabilities as the number of rolls increases from 1 to 50."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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>Rolls</th>\n",
       "      <th>Chance of at least one 6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.166667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.305556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.421296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.517747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.598122</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Rolls  Chance of at least one 6\n",
       "0      1                  0.166667\n",
       "1      2                  0.305556\n",
       "2      3                  0.421296\n",
       "3      4                  0.517747\n",
       "4      5                  0.598122"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rolls = np.arange(1, 51, 1)\n",
    "results = pd.DataFrame(\n",
    "    {'Rolls':rolls,\n",
    "    'Chance of at least one 6':(1 - (5/6)**rolls)}\n",
    ")\n",
    "results.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The chance that a 6 appears at least once rises rapidly as the number of rolls increases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 6764 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(results['Rolls'], results['Chance of at least one 6'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In 50 rolls, you are almost certain to get at least one 6."
   ]
  },
  {
   "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>Rolls</th>\n",
       "      <th>Chance of at least one 6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>50</td>\n",
       "      <td>0.99989</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Rolls  Chance of at least one 6\n",
       "49     50                   0.99989"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results[results['Rolls'] == 50]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculations like these can be used to find the chance that a particular individual is selected in a random sample. The exact calculation will depend on the sampling scheme. But what we have observed above can usually be generalized: increasing the size of the random sample increases the chance that an individual is selected."
   ]
  }
 ],
 "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
}
