{
 "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",
    "%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": [
    "# Simulation\n",
    "Simulation is the process of using a computer to mimic a physical experiment. In this class, those experiments will almost invariably involve chance. \n",
    "\n",
    "We have seen how to simulate the results of tosses of a coin. The steps in that simulation were examples of the steps that will constitute every simulation we do in this course. In this section we will set out those steps and follow them in examples.\n",
    "\n",
    "## Step 1: What to Simulate\n",
    "Specify the quantity you want to simulate. For example, you might decide that you want to simulate the outcomes of tosses of a coin. \n",
    "\n",
    "## Step 2: Simulating One Value\n",
    "Figure out how to simulate *one* value of the quantity you specified in Step 1. In our example, you have to figure out how to simulate the outcome of *one* toss of a coin. If your quantity is more complicated, you might need several lines of code to come up with one simulated value.\n",
    "\n",
    "## Step 3: Number of Repetitions\n",
    "Decide how many times you want to simulate the quantity. You will have to repeat Step 2 that many times. In one of our earlier examples we had decided to simulate the outcomes of 1000 tosses of a coin, and so we needed 1000 repetitions of generating the outcome of a single toss.\n",
    "\n",
    "## Step 4: Coding the Simulation\n",
    "Put it all together in code.\n",
    "- Create an empty array in which to collect all the simulated values. We will call this the collection array.\n",
    "- Create a \"repetitions sequence,\" that is, a sequence whose length is the number of repetitions you specified in Step 3. For `n` repetitions we will almost always use the sequence `np.arange(n)`.\n",
    "- Create a `for` loop. For each element of the repetitions sequence:\n",
    "    - Simulate *one* value based on the code you developed in Step 2.\n",
    "    - Augment the collection array with this simulated value.\n",
    "    \n",
    "That's it! Once you have carried out the steps above, your simulation is done. The collection array contains all the simulated values. \n",
    "\n",
    "At this point you can use the collection array as you would any other array. You can visualize the distribution of the simulated values, count how many simulated values fall into a particular category, and so on."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of Heads in 100 Tosses\n",
    "It is natural to expect that in 100 tosses of a coin, there will be 50 heads, give or take a few. \n",
    "\n",
    "But how many is \"a few\"? What's the chance of getting exactly 50 heads? Questions like these matter in data science not only because they are about interesting aspects of randomness, but also because they can be used in analyzing experiments where assignments to treatment and control groups are decided by the toss of a coin.\n",
    "\n",
    "In this example we will simulate the number of heads in 100 tosses of a coin. The histogram of our results will give us some insight into how many heads are likely.\n",
    "\n",
    "Let's get started on the simulation, following the steps above.\n",
    "\n",
    "### Step 1: What to Simulate\n",
    "The quantity we are going to simulate is the number of heads in 100 tosses. \n",
    "\n",
    "### Step 2: Simulating One Value\n",
    "We have to figure out how to make one set of 100 tosses and count the number of heads. Let's start by creating a coin."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "coin = np.array(['Heads', 'Tails'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In our earlier example we used `np.random.choice` and a `for` loop to generate multiple tosses. But sets of coin tosses are needed so often in data science that `np.random.choice` simulates them for us if we include a second argument that is the number of times to toss.\n",
    "\n",
    "Here are the results of 10 tosses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Heads', 'Tails', 'Tails', 'Heads', 'Heads', 'Tails', 'Heads',\n",
       "       'Heads', 'Heads', 'Tails'], dtype='<U5')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ten_tosses = np.random.choice(coin, 10)\n",
    "ten_tosses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can count the number of heads by using `np.count_nonzero` as before:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.count_nonzero(ten_tosses == 'Heads')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our goal is to simulate the number of heads in 100 tosses, not 10. To do that we can just repeat the same code, replacing 10 by 100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "54"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outcomes = np.random.choice(coin, 100)\n",
    "num_heads = np.count_nonzero(outcomes == 'Heads')\n",
    "num_heads"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Number of Repetitions\n",
    "How many repetitions we want is up to us. The more we use, the more reliable our simulations will be, but the longer it will take to run the code. Python is pretty fast at tossing coins. Let's go for 10,000 repetitions. That means we are going to do the following 10,000 times:\n",
    "- Toss a coin 100 times and count the number of heads.\n",
    "\n",
    "That's a lot of tossing! It's good that we have Python to do it for us.\n",
    "\n",
    "### Step 4: Coding the Simulation\n",
    "We are ready to write the code to execute the entire simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# An empty array to collect the simulated values\n",
    "heads = np.array([])\n",
    "\n",
    "# Repetitions sequence\n",
    "num_repetitions = 10000\n",
    "repetitions_sequence = np.arange(num_repetitions)\n",
    "\n",
    "# for loop\n",
    "for i in repetitions_sequence:\n",
    "    \n",
    "    # simulate one value\n",
    "    outcomes = np.random.choice(coin, 100)\n",
    "    num_heads = np.count_nonzero(outcomes == 'Heads')\n",
    "    \n",
    "    # augment the collection array with the simulated value\n",
    "    heads = np.append(heads, num_heads)  \n",
    "\n",
    "# That's it! The simulation is done."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that the array `heads` contains 10,000 entries, one for each repetition of the experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(heads)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a sense of the variability in the number of heads in 100 tosses, we can collect the results in a table and draw a histogram."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation_results = pd.DataFrame(\n",
    "    {'Repetition':np.arange(1, num_repetitions + 1),\n",
    "    'Number of Heads':heads}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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>Repetition</th>\n",
       "      <th>Number of Heads</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>46.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>51.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>51.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>54.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9995</th>\n",
       "      <td>9996</td>\n",
       "      <td>45.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9996</th>\n",
       "      <td>9997</td>\n",
       "      <td>43.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9997</th>\n",
       "      <td>9998</td>\n",
       "      <td>48.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9998</th>\n",
       "      <td>9999</td>\n",
       "      <td>51.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999</th>\n",
       "      <td>10000</td>\n",
       "      <td>53.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Repetition  Number of Heads\n",
       "0              1             50.0\n",
       "1              2             46.0\n",
       "2              3             51.0\n",
       "3              4             51.0\n",
       "4              5             54.0\n",
       "...          ...              ...\n",
       "9995        9996             45.0\n",
       "9996        9997             43.0\n",
       "9997        9998             48.0\n",
       "9998        9999             51.0\n",
       "9999       10000             53.0\n",
       "\n",
       "[10000 rows x 2 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simulation_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13676 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax1 = plt.subplots()\n",
    "\n",
    "unit = 'unit'\n",
    "\n",
    "ax1.hist(simulation_results['Number of Heads'], bins = np.arange(30.5, 69.6, 1), density = True, ec='white')\n",
    "\n",
    "y_vals = ax1.get_yticks()\n",
    "\n",
    "y_label = 'Percent per ' + (unit if unit else 'unit')\n",
    "\n",
    "x_label = 'Number of Heads'\n",
    "\n",
    "ax1.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.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each bins has width 1 and is centered at each value of the number of heads.\n",
    "\n",
    "Not surprisingly, the histogram looks roughly symmetric around 50 heads. The height of the bar at 50 is about 8% per unit. Since each bin is 1 unit wide, this is the same as saying that about 8% of the repetitions produced exactly 50 heads. That's not a huge percent, but it's the largest compared to the percent at every other number of heads.\n",
    "\n",
    "The histogram also shows that in almost all of the repetitions, the number of heads in 100 tosses was somewhere between 35 and 65. Indeed, the bulk of the repetitions produced numbers of heads in the range 45 to 55. \n",
    "\n",
    "While in theory it is *possible* that the number of heads can be anywhere between 0 and 100, the simulation shows that the range of *probable* values is much smaller.\n",
    "\n",
    "This is an instance of a more general phenomenon about the variability in coin tossing, as we will see later in the course."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### A More Compact Version of the Code ###\n",
    "We wrote the code for the simulation to show each of the steps in detail. Here are the same steps written in a more compact form. You can see that the code starts out the same way as before, but then some steps are combined."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "heads = np.array([])\n",
    "\n",
    "num_repetitions = 10000\n",
    "\n",
    "for i in np.arange(num_repetitions):\n",
    "    outcomes = np.random.choice(coin, 100)\n",
    "    heads = np.append(heads, np.count_nonzero(outcomes == 'Heads'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([43., 50., 59., ..., 51., 52., 50.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heads"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Moves in Monopoly\n",
    "Each move in the game Monopoly is determined by the total number of spots of two rolls of a die. If you play Monopoly, what should you expect to get when you roll the die two times?\n",
    "\n",
    "We can explore this by simulating the sum of two rolls of a die. We will run the simulation 10,000 times as we did in the previous example. Notice that is this paragraph we have completed Steps 1 and 3 of our simulation process.\n",
    "\n",
    "Step 2 is the one in which we simulate one pair of rolls and add up the number of spots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "die = np.arange(1, 7)\n",
    "sum(np.random.choice(die, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That simulates one value of the sum of two rolls. We are now all set to run the simulation according to the steps that are now familiar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "moves = np.array([])\n",
    "\n",
    "num_repetitions = 10000\n",
    "\n",
    "for i in np.arange(num_repetitions):\n",
    "    one_move = sum(np.random.choice(die, 2))\n",
    "    moves = np.append(moves, one_move)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is a histogram of the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15544 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "results = pd.DataFrame({'Repetition':np.arange(1, num_repetitions + 1), 'Sum of Two Rolls':moves})\n",
    "\n",
    "fig, ax1 = plt.subplots()\n",
    "\n",
    "unit = 'unit'\n",
    "\n",
    "ax1.hist(results['Sum of Two Rolls'], bins = np.arange(1.5, 12.6, 1), density = True, color='darkblue', ec='white')\n",
    "\n",
    "y_vals = ax1.get_yticks()\n",
    "\n",
    "y_label = 'Percent per ' + (unit if unit else 'unit')\n",
    "\n",
    "x_label = 'Sum of Two Rolls'\n",
    "\n",
    "ax1.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.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Seven is the most common value, with the frequencies falling off symmetrically on either side."
   ]
  }
 ],
 "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
}
