{ "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": [ "### Iteration ###\n", "It is often the case in programming – especially when dealing with randomness – that we want to repeat a process multiple times. For example, recall the game of betting on one roll of a die with the following rules:\n", "\n", "- If the die shows 1 or 2 spots, my net gain is -1 dollar.\n", "- If the die shows 3 or 4 spots, my net gain is 0 dollars.\n", "- If the die shows 5 or 6 spots, my net gain is 1 dollar.\n", "\n", "The function `bet_on_one_roll` takes no argument. Each time it is called, it simulates one roll of a fair die and returns the net gain in dollars." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def bet_on_one_roll():\n", " \"\"\"Returns my net gain on one bet\"\"\"\n", " x = np.random.choice(np.arange(1, 7)) # roll a die once and record the number of spots\n", " if x <= 2:\n", " return -1\n", " elif x <= 4:\n", " return 0\n", " elif x <= 6:\n", " return 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Playing this game once is easy:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bet_on_one_roll()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a sense of how variable the results are, we have to play the game over and over again. We could run the cell repeatedly, but that's tedious, and if we wanted to do it a thousand times or a million times, forget it. \n", "\n", "A more automated solution is to use a `for` statement to loop over the contents of a sequence. This is called *iteration*. A `for` statement begins with the word `for`, followed by a name we want to give each item in the sequence, followed by the word `in`, and ending with an expression that evaluates to a sequence. The indented body of the `for` statement is executed once *for each item in that sequence*." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat\n", "dog\n", "rabbit\n" ] } ], "source": [ "for animal in np.array(['cat', 'dog', 'rabbit']):\n", " print(animal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is helpful to write code that exactly replicates a `for` statement, without using the `for` statement. This is called ***unrolling* the loop**. \n", "\n", "A `for` statement simple replicates the code inside it, but before each iteration, it assigns a new value from the given sequence to the name we chose. For example, here is an unrolled version of the loop above." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat\n", "dog\n", "rabbit\n" ] } ], "source": [ "animal = np.array(['cat', 'dog', 'rabbit']).item(0)\n", "print(animal)\n", "animal = np.array(['cat', 'dog', 'rabbit']).item(1)\n", "print(animal)\n", "animal = np.array(['cat', 'dog', 'rabbit']).item(2)\n", "print(animal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the name `animal` is arbitrary, just like any name we assign with `=`.\n", "\n", "Here we use a `for` statement in a more realistic way: we print the results of betting five times on the die as described earlier. This is called *simulating* the results of five bets. We use the word *simulating* to remind ourselves that we are not physically rolling dice and exchanging money but using Python to mimic the process.\n", "\n", "To repeat a process `n` times, it is common to use the sequence `np.arange(n)` in the `for` statement. It is also common to use a very short name for each item. In our code we will use the name `i` to remind ourselves that it refers to an item." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "-1\n", "0\n", "1\n", "0\n" ] } ], "source": [ "for i in np.arange(5):\n", " print(bet_on_one_roll())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we simply perform exactly the same (random) action several times, so the code in the body of our `for` statement does not actually refer to `i`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Augmenting Arrays ###\n", "\n", "While the `for` statement above does simulate the results of five bets, the results are simply printed and are not in a form that we can use for computation. Any array of results would be more useful. Thus a typical use of a `for` statement is to create an array of results, by augmenting the array each time.\n", "\n", "The `append` method in `NumPy` helps us do this. The call `np.append(array_name, value)` evaluates to a new array that is `array_name` augmented by `value`. When you use `append`, keep in mind that all the entries of an array must have the same type." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['Cat', 'Dog', 'Another Pet'], dtype='" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "outcome_table = pd.DataFrame({'Outcome':outcomes})\n", "\n", "outcome_table = outcome_table.reset_index()\n", "\n", "outcome_table = outcome_table.groupby(['Outcome']).count().reset_index()\n", "\n", "outcome_table.plot.barh('Outcome')\n", "\n", "plt.legend(bbox_to_anchor=(1.04,1), loc=\"upper left\")\n", "\n", "plt.xlabel('count')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not surprisingly, each of the three outcomes -1, 0, and 1 appeared about about 100 of the 300 times, give or take. We will examine the \"give or take\" amounts more closely in later chapters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.12" } }, "nbformat": 4, "nbformat_minor": 1 }