{
 "metadata": {
  "name": "talk"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Monte Carlo Business Case Analysis using pandas\n",
      "## Demonstration\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from pandas import Series, DataFrame\n",
      "import pandas as pd"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The inputs are:\n",
      "\n",
      "- revenues\n",
      " - volume = number of trees &times; fruit per tree\n",
      " - price/volume\n",
      "- costs\n",
      " - fixed costs\n",
      " - variable costs\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We will:\n",
      "\n",
      "- Read & combine inputs  \n",
      "- Simulate (draw random values)\n",
      "- Calculate revenue and costs\n",
      "- Report results"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Read inputs"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "num_trees = pd.read_csv('data/numTrees.csv', skipinitialspace=True)\n",
      "num_trees"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td>  50</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 200</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 400</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 4 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "     farm  fruit       variety  num trees\n",
        "0  Farm A  apple  Granny Smith        100\n",
        "1  Farm A  apple      Jonathon        200\n",
        "2  Farm A   pear         Nashi         50\n",
        "3  Farm B  apple  Granny Smith        200\n",
        "4  Farm B  apple      Jonathon        400\n",
        "\n",
        "[5 rows x 4 columns]"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fruit_per_tree = pd.read_csv('data/fruitPerTree2.csv', skipinitialspace=True)\n",
      "fruit_per_tree"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> apple</td>\n",
        "      <td>  Granny Smith</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> apple</td>\n",
        "      <td>      Jonathon</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> apple</td>\n",
        "      <td> Red Delicious</td>\n",
        "      <td> 150</td>\n",
        "      <td> 20</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td>  pear</td>\n",
        "      <td>         Nashi</td>\n",
        "      <td>  50</td>\n",
        "      <td>  8</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>4 rows \u00d7 4 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "   fruit        variety  mean  sd\n",
        "0  apple   Granny Smith   200  30\n",
        "1  apple       Jonathon   150  22\n",
        "2  apple  Red Delicious   150  20\n",
        "3   pear          Nashi    50   8\n",
        "\n",
        "[4 rows x 4 columns]"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Combine inputs"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fruit = pd.merge(num_trees, fruit_per_tree) \n",
      "fruit\n",
      "# note broadcasting just happened"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 200</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 400</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td>  50</td>\n",
        "      <td>  50</td>\n",
        "      <td>  8</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "     farm  fruit       variety  num trees  mean  sd\n",
        "0  Farm A  apple  Granny Smith        100   200  30\n",
        "1  Farm B  apple  Granny Smith        200   200  30\n",
        "2  Farm A  apple      Jonathon        200   150  22\n",
        "3  Farm B  apple      Jonathon        400   150  22\n",
        "4  Farm A   pear         Nashi         50    50   8\n",
        "\n",
        "[5 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# also note the Red Delicious line is missing; include it explicitly below\n",
      "pd.merge(num_trees, fruit_per_tree, how='outer')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>  Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>  Granny Smith</td>\n",
        "      <td> 200</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>      Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>      Jonathon</td>\n",
        "      <td> 400</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>         Nashi</td>\n",
        "      <td>  50</td>\n",
        "      <td>  50</td>\n",
        "      <td>  8</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5</th>\n",
        "      <td>    NaN</td>\n",
        "      <td> apple</td>\n",
        "      <td> Red Delicious</td>\n",
        "      <td> NaN</td>\n",
        "      <td> 150</td>\n",
        "      <td> 20</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "     farm  fruit        variety  num trees  mean  sd\n",
        "0  Farm A  apple   Granny Smith        100   200  30\n",
        "1  Farm B  apple   Granny Smith        200   200  30\n",
        "2  Farm A  apple       Jonathon        200   150  22\n",
        "3  Farm B  apple       Jonathon        400   150  22\n",
        "4  Farm A   pear          Nashi         50    50   8\n",
        "5     NaN  apple  Red Delicious        NaN   150  20\n",
        "\n",
        "[6 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Simulate (draw random samples)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We want to expand out every line in this table into another dimension.  \n",
      "In the `dataframe` paradigm, we add another column, and repeat the indices.  \n",
      "We can achieve this by using `groupby` and `apply`"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fruit"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 200</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 400</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td>  50</td>\n",
        "      <td>  50</td>\n",
        "      <td>  8</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "     farm  fruit       variety  num trees  mean  sd\n",
        "0  Farm A  apple  Granny Smith        100   200  30\n",
        "1  Farm B  apple  Granny Smith        200   200  30\n",
        "2  Farm A  apple      Jonathon        200   150  22\n",
        "3  Farm B  apple      Jonathon        400   150  22\n",
        "4  Farm A   pear         Nashi         50    50   8\n",
        "\n",
        "[5 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fruit.groupby(['farm']).count()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>Farm A</th>\n",
        "      <td> 3</td>\n",
        "      <td> 3</td>\n",
        "      <td> 3</td>\n",
        "      <td> 3</td>\n",
        "      <td> 3</td>\n",
        "      <td> 3</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Farm B</th>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>2 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "        farm  fruit  variety  num trees  mean  sd\n",
        "farm                                             \n",
        "Farm A     3      3        3          3     3   3\n",
        "Farm B     2      2        2          2     2   2\n",
        "\n",
        "[2 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fruit.groupby(['farm','fruit']).count()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"2\" valign=\"top\">Farm A</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>pear</th>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Farm B</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>3 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "              farm  fruit  variety  num trees  mean  sd\n",
        "farm   fruit                                           \n",
        "Farm A apple     2      2        2          2     2   2\n",
        "       pear      1      1        1          1     1   1\n",
        "Farm B apple     2      2        2          2     2   2\n",
        "\n",
        "[3 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def test_func(grp):\n",
      "    return grp.count()\n",
      "fruit.groupby(['farm','fruit']).apply(test_func)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"2\" valign=\"top\">Farm A</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>pear</th>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "      <td> 1</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Farm B</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "      <td> 2</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>3 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 18,
       "text": [
        "              farm  fruit  variety  num trees  mean  sd\n",
        "farm   fruit                                           \n",
        "Farm A apple     2      2        2          2     2   2\n",
        "       pear      1      1        1          1     1   1\n",
        "Farm B apple     2      2        2          2     2   2\n",
        "\n",
        "[3 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def sim(grp):\n",
      "    return Series({'random': np.random.normal(), 'count': grp.count().mean()})\n",
      "fruit.groupby(['farm','fruit']).apply(sim)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>count</th>\n",
        "      <th>random</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"2\" valign=\"top\">Farm A</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td>-0.712683</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>pear</th>\n",
        "      <td> 1</td>\n",
        "      <td>-0.463357</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Farm B</th>\n",
        "      <th>apple</th>\n",
        "      <td> 2</td>\n",
        "      <td> 0.909406</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>3 rows \u00d7 2 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "              count    random\n",
        "farm   fruit                 \n",
        "Farm A apple      2 -0.712683\n",
        "       pear       1 -0.463357\n",
        "Farm B apple      2  0.909406\n",
        "\n",
        "[3 rows x 2 columns]"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def sim(grp):\n",
      "    return DataFrame(np.random.normal(size=3), columns=['fruit per tree'])\n",
      "fruit.groupby(['farm','fruit']).apply(sim)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>fruit per tree</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"6\" valign=\"top\">Farm A</th>\n",
        "      <th rowspan=\"3\" valign=\"top\">apple</th>\n",
        "      <th>0</th>\n",
        "      <td>-1.118708</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 0.166747</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td>-0.531929</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">pear</th>\n",
        "      <th>0</th>\n",
        "      <td>-0.743631</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td>-0.655569</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td>-0.553312</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">Farm B</th>\n",
        "      <th rowspan=\"3\" valign=\"top\">apple</th>\n",
        "      <th>0</th>\n",
        "      <td>-0.875545</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 0.883906</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 0.282449</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>9 rows \u00d7 1 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "                fruit per tree\n",
        "farm   fruit                  \n",
        "Farm A apple 0       -1.118708\n",
        "             1        0.166747\n",
        "             2       -0.531929\n",
        "       pear  0       -0.743631\n",
        "             1       -0.655569\n",
        "             2       -0.553312\n",
        "Farm B apple 0       -0.875545\n",
        "             1        0.883906\n",
        "             2        0.282449\n",
        "\n",
        "[9 rows x 1 columns]"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "keys = ['farm','fruit','variety']\n",
      "def sim(grp, size):\n",
      "    return DataFrame(np.random.normal(grp['mean'],grp['sd'],size=size), columns=['fruit per tree'])\n",
      "\n",
      "sim_fruit_per_tree = fruit.groupby(keys).apply(sim, 3)\n",
      "sim_fruit_per_tree"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>fruit per tree</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"9\" valign=\"top\">Farm A</th>\n",
        "      <th rowspan=\"6\" valign=\"top\">apple</th>\n",
        "      <th rowspan=\"3\" valign=\"top\">Granny Smith</th>\n",
        "      <th>0</th>\n",
        "      <td> 230.130175</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 177.329619</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 206.511362</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">Jonathon</th>\n",
        "      <th>0</th>\n",
        "      <td> 158.947631</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 122.824318</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 139.762772</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">pear</th>\n",
        "      <th rowspan=\"3\" valign=\"top\">Nashi</th>\n",
        "      <th>0</th>\n",
        "      <td>  51.233758</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td>  46.097449</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td>  53.058483</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"6\" valign=\"top\">Farm B</th>\n",
        "      <th rowspan=\"6\" valign=\"top\">apple</th>\n",
        "      <th rowspan=\"3\" valign=\"top\">Granny Smith</th>\n",
        "      <th>0</th>\n",
        "      <td> 206.760033</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 154.380444</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 161.996342</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">Jonathon</th>\n",
        "      <th>0</th>\n",
        "      <td> 154.227499</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 108.107354</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 162.213587</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>15 rows \u00d7 1 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "                             fruit per tree\n",
        "farm   fruit variety                       \n",
        "Farm A apple Granny Smith 0      230.130175\n",
        "                          1      177.329619\n",
        "                          2      206.511362\n",
        "             Jonathon     0      158.947631\n",
        "                          1      122.824318\n",
        "                          2      139.762772\n",
        "       pear  Nashi        0       51.233758\n",
        "                          1       46.097449\n",
        "                          2       53.058483\n",
        "Farm B apple Granny Smith 0      206.760033\n",
        "                          1      154.380444\n",
        "                          2      161.996342\n",
        "             Jonathon     0      154.227499\n",
        "                          1      108.107354\n",
        "                          2      162.213587\n",
        "\n",
        "[15 rows x 1 columns]"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_fruit_per_tree = sim_fruit_per_tree.reset_index().rename(columns={'level_3':'iteration'})\n",
      "sim_fruit_per_tree"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2</td>\n",
        "      <td> 139.762772</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>6 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 0</td>\n",
        "      <td>  51.233758</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>7 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 1</td>\n",
        "      <td>  46.097449</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>8 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 2</td>\n",
        "      <td>  53.058483</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>9 </th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0</td>\n",
        "      <td> 206.760033</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>10</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 1</td>\n",
        "      <td> 154.380444</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>11</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2</td>\n",
        "      <td> 161.996342</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>12</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 0</td>\n",
        "      <td> 154.227499</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>13</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 1</td>\n",
        "      <td> 108.107354</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>14</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2</td>\n",
        "      <td> 162.213587</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>15 rows \u00d7 5 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 31,
       "text": [
        "      farm  fruit       variety  iteration  fruit per tree\n",
        "0   Farm A  apple  Granny Smith          0      230.130175\n",
        "1   Farm A  apple  Granny Smith          1      177.329619\n",
        "2   Farm A  apple  Granny Smith          2      206.511362\n",
        "3   Farm A  apple      Jonathon          0      158.947631\n",
        "4   Farm A  apple      Jonathon          1      122.824318\n",
        "5   Farm A  apple      Jonathon          2      139.762772\n",
        "6   Farm A   pear         Nashi          0       51.233758\n",
        "7   Farm A   pear         Nashi          1       46.097449\n",
        "8   Farm A   pear         Nashi          2       53.058483\n",
        "9   Farm B  apple  Granny Smith          0      206.760033\n",
        "10  Farm B  apple  Granny Smith          1      154.380444\n",
        "11  Farm B  apple  Granny Smith          2      161.996342\n",
        "12  Farm B  apple      Jonathon          0      154.227499\n",
        "13  Farm B  apple      Jonathon          1      108.107354\n",
        "14  Farm B  apple      Jonathon          2      162.213587\n",
        "\n",
        "[15 rows x 5 columns]"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = 1000\n",
      "sim2_fruit_per_tree = fruit.groupby(keys).apply(sim, N) \n",
      "sim2_fruit_per_tree = sim2_fruit_per_tree.reset_index().rename(columns={'level_3':'iteration'})\n",
      "\n",
      "sim2_fruit_per_tree.drop('iteration', axis=1).groupby(keys).describe().unstack()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th colspan=\"8\" halign=\"left\">fruit per tree</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>count</th>\n",
        "      <th>mean</th>\n",
        "      <th>std</th>\n",
        "      <th>min</th>\n",
        "      <th>25%</th>\n",
        "      <th>50%</th>\n",
        "      <th>75%</th>\n",
        "      <th>max</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"3\" valign=\"top\">Farm A</th>\n",
        "      <th rowspan=\"2\" valign=\"top\">apple</th>\n",
        "      <th>Granny Smith</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 198.976349</td>\n",
        "      <td> 29.317363</td>\n",
        "      <td> 111.245709</td>\n",
        "      <td> 178.636120</td>\n",
        "      <td> 198.595669</td>\n",
        "      <td> 218.364087</td>\n",
        "      <td> 291.925764</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Jonathon</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 149.770180</td>\n",
        "      <td> 22.068844</td>\n",
        "      <td>  93.908548</td>\n",
        "      <td> 134.214130</td>\n",
        "      <td> 150.331061</td>\n",
        "      <td> 164.927689</td>\n",
        "      <td> 219.883789</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>pear</th>\n",
        "      <th>Nashi</th>\n",
        "      <td> 1000</td>\n",
        "      <td>  50.024324</td>\n",
        "      <td>  8.163186</td>\n",
        "      <td>  26.928023</td>\n",
        "      <td>  44.303949</td>\n",
        "      <td>  49.907168</td>\n",
        "      <td>  55.795602</td>\n",
        "      <td>  74.941092</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"2\" valign=\"top\">Farm B</th>\n",
        "      <th rowspan=\"2\" valign=\"top\">apple</th>\n",
        "      <th>Granny Smith</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 199.842617</td>\n",
        "      <td> 29.973449</td>\n",
        "      <td>  82.211135</td>\n",
        "      <td> 179.849653</td>\n",
        "      <td> 200.065176</td>\n",
        "      <td> 219.643034</td>\n",
        "      <td> 293.538759</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>Jonathon</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 149.589624</td>\n",
        "      <td> 21.641614</td>\n",
        "      <td>  78.585807</td>\n",
        "      <td> 135.736765</td>\n",
        "      <td> 150.431874</td>\n",
        "      <td> 164.945984</td>\n",
        "      <td> 213.109087</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 8 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "                           fruit per tree                                     \\\n",
        "                                    count        mean        std         min   \n",
        "farm   fruit variety                                                           \n",
        "Farm A apple Granny Smith            1000  198.976349  29.317363  111.245709   \n",
        "             Jonathon                1000  149.770180  22.068844   93.908548   \n",
        "       pear  Nashi                   1000   50.024324   8.163186   26.928023   \n",
        "Farm B apple Granny Smith            1000  199.842617  29.973449   82.211135   \n",
        "             Jonathon                1000  149.589624  21.641614   78.585807   \n",
        "\n",
        "                                                                           \n",
        "                                  25%         50%         75%         max  \n",
        "farm   fruit variety                                                       \n",
        "Farm A apple Granny Smith  178.636120  198.595669  218.364087  291.925764  \n",
        "             Jonathon      134.214130  150.331061  164.927689  219.883789  \n",
        "       pear  Nashi          44.303949   49.907168   55.795602   74.941092  \n",
        "Farm B apple Granny Smith  179.849653  200.065176  219.643034  293.538759  \n",
        "             Jonathon      135.736765  150.431874  164.945984  213.109087  \n",
        "\n",
        "[5 rows x 8 columns]"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Calculations"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Merge the simulation with the number of trees to calculate the total fruit produced in each iteration"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_fruit_per_tree.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 5 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "     farm  fruit       variety  iteration  fruit per tree\n",
        "0  Farm A  apple  Granny Smith          0      230.130175\n",
        "1  Farm A  apple  Granny Smith          1      177.329619\n",
        "2  Farm A  apple  Granny Smith          2      206.511362\n",
        "3  Farm A  apple      Jonathon          0      158.947631\n",
        "4  Farm A  apple      Jonathon          1      122.824318\n",
        "\n",
        "[5 rows x 5 columns]"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_fruit = pd.merge(num_trees, sim_fruit_per_tree, how='left')\n",
      "sim_fruit.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 33,
       "text": [
        "     farm  fruit       variety  num trees  iteration  fruit per tree\n",
        "0  Farm A  apple  Granny Smith        100          0      230.130175\n",
        "1  Farm A  apple  Granny Smith        100          1      177.329619\n",
        "2  Farm A  apple  Granny Smith        100          2      206.511362\n",
        "3  Farm A  apple      Jonathon        200          0      158.947631\n",
        "4  Farm A  apple      Jonathon        200          1      122.824318\n",
        "\n",
        "[5 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Add new columns with our calculations"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_fruit[\"volume\"] = sim_fruit[\"num trees\"] * sim_fruit[\"fruit per tree\"]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 34
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_fruit.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "      <td> 23013.017525</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "      <td> 17732.961946</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "      <td> 20651.136223</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "      <td> 31789.526143</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "      <td> 24564.863628</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 7 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 35,
       "text": [
        "     farm  fruit       variety  num trees  iteration  fruit per tree  \\\n",
        "0  Farm A  apple  Granny Smith        100          0      230.130175   \n",
        "1  Farm A  apple  Granny Smith        100          1      177.329619   \n",
        "2  Farm A  apple  Granny Smith        100          2      206.511362   \n",
        "3  Farm A  apple      Jonathon        200          0      158.947631   \n",
        "4  Farm A  apple      Jonathon        200          1      122.824318   \n",
        "\n",
        "         volume  \n",
        "0  23013.017525  \n",
        "1  17732.961946  \n",
        "2  20651.136223  \n",
        "3  31789.526143  \n",
        "4  24564.863628  \n",
        "\n",
        "[5 rows x 7 columns]"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Did you notice that `df[x]` refers to the _column_ x, not the row?  \n",
      "You can use `df.ix[r,c]` to index using (row, column) order."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "And so on to calculate profit..."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# read prices, var and fixed costs, calculate profit\n",
      "revenue_per_fruit = pd.read_csv('data/revenuePerFruit.csv', skipinitialspace=True)\n",
      "fixed_cost = pd.read_csv('data/fixedCosts.csv', skipinitialspace=True)\n",
      "cost_per_fruit = pd.read_csv('data/varCostsPerFruit.csv', skipinitialspace=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 36
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_var_cost = pd.merge(sim_fruit, cost_per_fruit)\n",
      "sim_var_cost['var cost'] = sim_var_cost['volume'] * sim_var_cost['cost per fruit']\n",
      "sim_var_cost.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "      <td> 23013.017525</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3451.952629</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "      <td> 17732.961946</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 2659.944292</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "      <td> 20651.136223</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3097.670433</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "      <td> 31789.526143</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 4768.428921</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "      <td> 24564.863628</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3684.729544</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 9 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 37,
       "text": [
        "     farm  fruit       variety  num trees  iteration  fruit per tree  \\\n",
        "0  Farm A  apple  Granny Smith        100          0      230.130175   \n",
        "1  Farm A  apple  Granny Smith        100          1      177.329619   \n",
        "2  Farm A  apple  Granny Smith        100          2      206.511362   \n",
        "3  Farm A  apple      Jonathon        200          0      158.947631   \n",
        "4  Farm A  apple      Jonathon        200          1      122.824318   \n",
        "\n",
        "         volume  cost per fruit     var cost  \n",
        "0  23013.017525            0.15  3451.952629  \n",
        "1  17732.961946            0.15  2659.944292  \n",
        "2  20651.136223            0.15  3097.670433  \n",
        "3  31789.526143            0.15  4768.428921  \n",
        "4  24564.863628            0.15  3684.729544  \n",
        "\n",
        "[5 rows x 9 columns]"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sim_revenue = pd.merge(revenue_per_fruit, sim_fruit)\n",
      "sim_revenue['revenue'] = sim_revenue['volume'] * sim_revenue['revenue per fruit']\n",
      "sim_revenue.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>farm</th>\n",
        "      <th>num trees</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>revenue</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0.9</td>\n",
        "      <td> Farm A</td>\n",
        "      <td> 100</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "      <td> 23013.017525</td>\n",
        "      <td> 20711.715772</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0.9</td>\n",
        "      <td> Farm A</td>\n",
        "      <td> 100</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "      <td> 17732.961946</td>\n",
        "      <td> 15959.665751</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0.9</td>\n",
        "      <td> Farm A</td>\n",
        "      <td> 100</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "      <td> 20651.136223</td>\n",
        "      <td> 18586.022600</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0.9</td>\n",
        "      <td> Farm B</td>\n",
        "      <td> 200</td>\n",
        "      <td> 0</td>\n",
        "      <td> 206.760033</td>\n",
        "      <td> 41352.006590</td>\n",
        "      <td> 37216.805931</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 0.9</td>\n",
        "      <td> Farm B</td>\n",
        "      <td> 200</td>\n",
        "      <td> 1</td>\n",
        "      <td> 154.380444</td>\n",
        "      <td> 30876.088826</td>\n",
        "      <td> 27788.479944</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 9 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 38,
       "text": [
        "   fruit       variety  revenue per fruit    farm  num trees  iteration  \\\n",
        "0  apple  Granny Smith                0.9  Farm A        100          0   \n",
        "1  apple  Granny Smith                0.9  Farm A        100          1   \n",
        "2  apple  Granny Smith                0.9  Farm A        100          2   \n",
        "3  apple  Granny Smith                0.9  Farm B        200          0   \n",
        "4  apple  Granny Smith                0.9  Farm B        200          1   \n",
        "\n",
        "   fruit per tree        volume       revenue  \n",
        "0      230.130175  23013.017525  20711.715772  \n",
        "1      177.329619  17732.961946  15959.665751  \n",
        "2      206.511362  20651.136223  18586.022600  \n",
        "3      206.760033  41352.006590  37216.805931  \n",
        "4      154.380444  30876.088826  27788.479944  \n",
        "\n",
        "[5 rows x 9 columns]"
       ]
      }
     ],
     "prompt_number": 38
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cashflows = pd.merge(sim_var_cost, sim_revenue, how='outer')\n",
      "cashflows.head()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>num trees</th>\n",
        "      <th>iteration</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>revenue</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 0</td>\n",
        "      <td> 230.130175</td>\n",
        "      <td> 23013.017525</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3451.952629</td>\n",
        "      <td> 0.90</td>\n",
        "      <td> 20711.715772</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 1</td>\n",
        "      <td> 177.329619</td>\n",
        "      <td> 17732.961946</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 2659.944292</td>\n",
        "      <td> 0.90</td>\n",
        "      <td> 15959.665751</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 100</td>\n",
        "      <td> 2</td>\n",
        "      <td> 206.511362</td>\n",
        "      <td> 20651.136223</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3097.670433</td>\n",
        "      <td> 0.90</td>\n",
        "      <td> 18586.022600</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 0</td>\n",
        "      <td> 158.947631</td>\n",
        "      <td> 31789.526143</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 4768.428921</td>\n",
        "      <td> 0.95</td>\n",
        "      <td> 30200.049836</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 200</td>\n",
        "      <td> 1</td>\n",
        "      <td> 122.824318</td>\n",
        "      <td> 24564.863628</td>\n",
        "      <td> 0.15</td>\n",
        "      <td> 3684.729544</td>\n",
        "      <td> 0.95</td>\n",
        "      <td> 23336.620447</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>5 rows \u00d7 11 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 39,
       "text": [
        "     farm  fruit       variety  num trees  iteration  fruit per tree  \\\n",
        "0  Farm A  apple  Granny Smith        100          0      230.130175   \n",
        "1  Farm A  apple  Granny Smith        100          1      177.329619   \n",
        "2  Farm A  apple  Granny Smith        100          2      206.511362   \n",
        "3  Farm A  apple      Jonathon        200          0      158.947631   \n",
        "4  Farm A  apple      Jonathon        200          1      122.824318   \n",
        "\n",
        "         volume  cost per fruit     var cost  revenue per fruit       revenue  \n",
        "0  23013.017525            0.15  3451.952629               0.90  20711.715772  \n",
        "1  17732.961946            0.15  2659.944292               0.90  15959.665751  \n",
        "2  20651.136223            0.15  3097.670433               0.90  18586.022600  \n",
        "3  31789.526143            0.15  4768.428921               0.95  30200.049836  \n",
        "4  24564.863628            0.15  3684.729544               0.95  23336.620447  \n",
        "\n",
        "[5 rows x 11 columns]"
       ]
      }
     ],
     "prompt_number": 39
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Aggregate\n",
      "\n",
      "At this point we have all the cashflows at the `farm` &times; `fruit` &times; `variety` level.  \n",
      "But fixed costs are only defined at the `farm` level, so to go further, we need to aggregate."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fixed_cost"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fixed cost</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>1 rows \u00d7 2 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 40,
       "text": [
        "     farm  fixed cost\n",
        "0  Farm A       30000\n",
        "\n",
        "[1 rows x 2 columns]"
       ]
      }
     ],
     "prompt_number": 40
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "agg_cashflows = cashflows.groupby(['farm','iteration']).sum().reset_index()\n",
      "agg_cashflows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>iteration</th>\n",
        "      <th>num trees</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>revenue</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 0</td>\n",
        "      <td> 350</td>\n",
        "      <td> 440.311564</td>\n",
        "      <td>  57364.231568</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  8732.719130</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 53550.304145</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 1</td>\n",
        "      <td> 350</td>\n",
        "      <td> 346.251386</td>\n",
        "      <td>  44602.698000</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  6805.648321</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 41670.304797</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 2</td>\n",
        "      <td> 350</td>\n",
        "      <td> 399.332617</td>\n",
        "      <td>  51256.614764</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  7821.138422</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 47873.461146</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 0</td>\n",
        "      <td> 600</td>\n",
        "      <td> 360.987532</td>\n",
        "      <td> 103043.006100</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 18547.741098</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 95823.255465</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 1</td>\n",
        "      <td> 600</td>\n",
        "      <td> 262.487798</td>\n",
        "      <td>  74119.030415</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 13341.425475</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 68869.274453</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 2</td>\n",
        "      <td> 600</td>\n",
        "      <td> 324.209929</td>\n",
        "      <td>  97284.703335</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 17511.246600</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 90800.504746</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 9 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 41,
       "text": [
        "     farm  iteration  num trees  fruit per tree         volume  \\\n",
        "0  Farm A          0        350      440.311564   57364.231568   \n",
        "1  Farm A          1        350      346.251386   44602.698000   \n",
        "2  Farm A          2        350      399.332617   51256.614764   \n",
        "3  Farm B          0        600      360.987532  103043.006100   \n",
        "4  Farm B          1        600      262.487798   74119.030415   \n",
        "5  Farm B          2        600      324.209929   97284.703335   \n",
        "\n",
        "   cost per fruit      var cost  revenue per fruit       revenue  \n",
        "0            0.50   8732.719130               2.88  53550.304145  \n",
        "1            0.50   6805.648321               2.88  41670.304797  \n",
        "2            0.50   7821.138422               2.88  47873.461146  \n",
        "3            0.36  18547.741098               1.85  95823.255465  \n",
        "4            0.36  13341.425475               1.85  68869.274453  \n",
        "5            0.36  17511.246600               1.85  90800.504746  \n",
        "\n",
        "[6 rows x 9 columns]"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "agg_cashflows = agg_cashflows.merge(fixed_cost, how='outer')\n",
      "agg_cashflows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>iteration</th>\n",
        "      <th>num trees</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>revenue</th>\n",
        "      <th>fixed cost</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 0</td>\n",
        "      <td> 350</td>\n",
        "      <td> 440.311564</td>\n",
        "      <td>  57364.231568</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  8732.719130</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 53550.304145</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 1</td>\n",
        "      <td> 350</td>\n",
        "      <td> 346.251386</td>\n",
        "      <td>  44602.698000</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  6805.648321</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 41670.304797</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 2</td>\n",
        "      <td> 350</td>\n",
        "      <td> 399.332617</td>\n",
        "      <td>  51256.614764</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  7821.138422</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 47873.461146</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 0</td>\n",
        "      <td> 600</td>\n",
        "      <td> 360.987532</td>\n",
        "      <td> 103043.006100</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 18547.741098</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 95823.255465</td>\n",
        "      <td>   NaN</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 1</td>\n",
        "      <td> 600</td>\n",
        "      <td> 262.487798</td>\n",
        "      <td>  74119.030415</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 13341.425475</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 68869.274453</td>\n",
        "      <td>   NaN</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 2</td>\n",
        "      <td> 600</td>\n",
        "      <td> 324.209929</td>\n",
        "      <td>  97284.703335</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 17511.246600</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 90800.504746</td>\n",
        "      <td>   NaN</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 10 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 42,
       "text": [
        "     farm  iteration  num trees  fruit per tree         volume  \\\n",
        "0  Farm A          0        350      440.311564   57364.231568   \n",
        "1  Farm A          1        350      346.251386   44602.698000   \n",
        "2  Farm A          2        350      399.332617   51256.614764   \n",
        "3  Farm B          0        600      360.987532  103043.006100   \n",
        "4  Farm B          1        600      262.487798   74119.030415   \n",
        "5  Farm B          2        600      324.209929   97284.703335   \n",
        "\n",
        "   cost per fruit      var cost  revenue per fruit       revenue  fixed cost  \n",
        "0            0.50   8732.719130               2.88  53550.304145       30000  \n",
        "1            0.50   6805.648321               2.88  41670.304797       30000  \n",
        "2            0.50   7821.138422               2.88  47873.461146       30000  \n",
        "3            0.36  18547.741098               1.85  95823.255465         NaN  \n",
        "4            0.36  13341.425475               1.85  68869.274453         NaN  \n",
        "5            0.36  17511.246600               1.85  90800.504746         NaN  \n",
        "\n",
        "[6 rows x 10 columns]"
       ]
      }
     ],
     "prompt_number": 42
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Easy to fix up those `NaN`s"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "agg_cashflows = agg_cashflows.fillna(0)\n",
      "agg_cashflows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>iteration</th>\n",
        "      <th>num trees</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>revenue</th>\n",
        "      <th>fixed cost</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 0</td>\n",
        "      <td> 350</td>\n",
        "      <td> 440.311564</td>\n",
        "      <td>  57364.231568</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  8732.719130</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 53550.304145</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 1</td>\n",
        "      <td> 350</td>\n",
        "      <td> 346.251386</td>\n",
        "      <td>  44602.698000</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  6805.648321</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 41670.304797</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 2</td>\n",
        "      <td> 350</td>\n",
        "      <td> 399.332617</td>\n",
        "      <td>  51256.614764</td>\n",
        "      <td> 0.50</td>\n",
        "      <td>  7821.138422</td>\n",
        "      <td> 2.88</td>\n",
        "      <td> 47873.461146</td>\n",
        "      <td> 30000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 0</td>\n",
        "      <td> 600</td>\n",
        "      <td> 360.987532</td>\n",
        "      <td> 103043.006100</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 18547.741098</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 95823.255465</td>\n",
        "      <td>     0</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 1</td>\n",
        "      <td> 600</td>\n",
        "      <td> 262.487798</td>\n",
        "      <td>  74119.030415</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 13341.425475</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 68869.274453</td>\n",
        "      <td>     0</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 2</td>\n",
        "      <td> 600</td>\n",
        "      <td> 324.209929</td>\n",
        "      <td>  97284.703335</td>\n",
        "      <td> 0.36</td>\n",
        "      <td> 17511.246600</td>\n",
        "      <td> 1.85</td>\n",
        "      <td> 90800.504746</td>\n",
        "      <td>     0</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 10 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 43,
       "text": [
        "     farm  iteration  num trees  fruit per tree         volume  \\\n",
        "0  Farm A          0        350      440.311564   57364.231568   \n",
        "1  Farm A          1        350      346.251386   44602.698000   \n",
        "2  Farm A          2        350      399.332617   51256.614764   \n",
        "3  Farm B          0        600      360.987532  103043.006100   \n",
        "4  Farm B          1        600      262.487798   74119.030415   \n",
        "5  Farm B          2        600      324.209929   97284.703335   \n",
        "\n",
        "   cost per fruit      var cost  revenue per fruit       revenue  fixed cost  \n",
        "0            0.50   8732.719130               2.88  53550.304145       30000  \n",
        "1            0.50   6805.648321               2.88  41670.304797       30000  \n",
        "2            0.50   7821.138422               2.88  47873.461146       30000  \n",
        "3            0.36  18547.741098               1.85  95823.255465           0  \n",
        "4            0.36  13341.425475               1.85  68869.274453           0  \n",
        "5            0.36  17511.246600               1.85  90800.504746           0  \n",
        "\n",
        "[6 rows x 10 columns]"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "agg_cashflows['profit'] = agg_cashflows['revenue'] - agg_cashflows['fixed cost'] - agg_cashflows['var cost']\n",
      "del agg_cashflows['fruit per tree'], agg_cashflows['cost per fruit'], agg_cashflows['revenue per fruit']\n",
      "agg_cashflows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>iteration</th>\n",
        "      <th>num trees</th>\n",
        "      <th>volume</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue</th>\n",
        "      <th>fixed cost</th>\n",
        "      <th>profit</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 0</td>\n",
        "      <td> 350</td>\n",
        "      <td>  57364.231568</td>\n",
        "      <td>  8732.719130</td>\n",
        "      <td> 53550.304145</td>\n",
        "      <td> 30000</td>\n",
        "      <td> 14817.585015</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 1</td>\n",
        "      <td> 350</td>\n",
        "      <td>  44602.698000</td>\n",
        "      <td>  6805.648321</td>\n",
        "      <td> 41670.304797</td>\n",
        "      <td> 30000</td>\n",
        "      <td>  4864.656475</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> 2</td>\n",
        "      <td> 350</td>\n",
        "      <td>  51256.614764</td>\n",
        "      <td>  7821.138422</td>\n",
        "      <td> 47873.461146</td>\n",
        "      <td> 30000</td>\n",
        "      <td> 10052.322724</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 0</td>\n",
        "      <td> 600</td>\n",
        "      <td> 103043.006100</td>\n",
        "      <td> 18547.741098</td>\n",
        "      <td> 95823.255465</td>\n",
        "      <td>     0</td>\n",
        "      <td> 77275.514367</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 1</td>\n",
        "      <td> 600</td>\n",
        "      <td>  74119.030415</td>\n",
        "      <td> 13341.425475</td>\n",
        "      <td> 68869.274453</td>\n",
        "      <td>     0</td>\n",
        "      <td> 55527.848979</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> 2</td>\n",
        "      <td> 600</td>\n",
        "      <td>  97284.703335</td>\n",
        "      <td> 17511.246600</td>\n",
        "      <td> 90800.504746</td>\n",
        "      <td>     0</td>\n",
        "      <td> 73289.258146</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 8 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 44,
       "text": [
        "     farm  iteration  num trees         volume      var cost       revenue  \\\n",
        "0  Farm A          0        350   57364.231568   8732.719130  53550.304145   \n",
        "1  Farm A          1        350   44602.698000   6805.648321  41670.304797   \n",
        "2  Farm A          2        350   51256.614764   7821.138422  47873.461146   \n",
        "3  Farm B          0        600  103043.006100  18547.741098  95823.255465   \n",
        "4  Farm B          1        600   74119.030415  13341.425475  68869.274453   \n",
        "5  Farm B          2        600   97284.703335  17511.246600  90800.504746   \n",
        "\n",
        "   fixed cost        profit  \n",
        "0       30000  14817.585015  \n",
        "1       30000   4864.656475  \n",
        "2       30000  10052.322724  \n",
        "3           0  77275.514367  \n",
        "4           0  55527.848979  \n",
        "5           0  73289.258146  \n",
        "\n",
        "[6 rows x 8 columns]"
       ]
      }
     ],
     "prompt_number": 44
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "iter_cashflows = agg_cashflows.groupby('iteration').sum()  # sum over farms\n",
      "iter_cashflows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>num trees</th>\n",
        "      <th>volume</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue</th>\n",
        "      <th>fixed cost</th>\n",
        "      <th>profit</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>iteration</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td> 950</td>\n",
        "      <td> 160407.237668</td>\n",
        "      <td> 27280.460228</td>\n",
        "      <td> 149373.559611</td>\n",
        "      <td> 30000</td>\n",
        "      <td> 92093.099382</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td> 950</td>\n",
        "      <td> 118721.728415</td>\n",
        "      <td> 20147.073796</td>\n",
        "      <td> 110539.579250</td>\n",
        "      <td> 30000</td>\n",
        "      <td> 60392.505454</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td> 950</td>\n",
        "      <td> 148541.318098</td>\n",
        "      <td> 25332.385022</td>\n",
        "      <td> 138673.965893</td>\n",
        "      <td> 30000</td>\n",
        "      <td> 83341.580870</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>3 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 45,
       "text": [
        "           num trees         volume      var cost        revenue  fixed cost  \\\n",
        "iteration                                                                      \n",
        "0                950  160407.237668  27280.460228  149373.559611       30000   \n",
        "1                950  118721.728415  20147.073796  110539.579250       30000   \n",
        "2                950  148541.318098  25332.385022  138673.965893       30000   \n",
        "\n",
        "                 profit  \n",
        "iteration                \n",
        "0          92093.099382  \n",
        "1          60392.505454  \n",
        "2          83341.580870  \n",
        "\n",
        "[3 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 45
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## All together"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "All the code in one place (except for a few small functions we defined)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = 10000\n",
      "keys = ['farm','fruit','variety']\n",
      "\n",
      "# Read data\n",
      "num_trees = pd.read_csv('data/numTrees.csv', skipinitialspace=True)\n",
      "fruit_per_tree = pd.read_csv('data/fruitPerTree.csv', skipinitialspace=True)\n",
      "revenue_per_fruit = pd.read_csv('data/revenuePerFruit.csv', skipinitialspace=True)\n",
      "fixed_cost = pd.read_csv('data/fixedCosts.csv', skipinitialspace=True)\n",
      "cost_per_fruit = pd.read_csv('data/varCostsPerFruit.csv', skipinitialspace=True)\n",
      "\n",
      "# Simulate\n",
      "def sim(grp, size):\n",
      "    return DataFrame(np.random.normal(grp['mean'],grp['sd'],size=size), columns=['fruit per tree'])\n",
      "\n",
      "sim_fruit_per_tree = fruit_per_tree.groupby(keys).apply(sim, N).reset_index().rename(columns={'level_3':'iteration'})\n",
      "\n",
      "# Calculate var costs and revenues\n",
      "sim_fruit = pd.merge(num_trees, sim_fruit_per_tree, how='left')\n",
      "sim_fruit['volume'] = sim_fruit['fruit per tree'] * sim_fruit['num trees']\n",
      "sim_var_cost = pd.merge(sim_fruit, cost_per_fruit)\n",
      "sim_var_cost['var cost'] = sim_var_cost['volume'] * sim_var_cost['cost per fruit']\n",
      "sim_revenue = pd.merge(revenue_per_fruit, sim_fruit)\n",
      "sim_revenue['revenue'] = sim_revenue['volume'] * sim_revenue['revenue per fruit']\n",
      "cashflows = pd.merge(sim_var_cost, sim_revenue, how='outer')\n",
      "cashflows = cashflows.merge(fixed_cost, how='outer').fillna({'fixed cost':0})\n",
      "\n",
      "# Aggregate and include fixed costs\n",
      "agg_cashflows = cashflows.groupby(['farm','iteration']).sum().reset_index()\n",
      "agg_cashflows = agg_cashflows.merge(fixed_cost, how='outer')\n",
      "agg_cashflows['profit'] = agg_cashflows['revenue'] - agg_cashflows['fixed cost'] - agg_cashflows['var cost']\n",
      "del agg_cashflows['fruit per tree'], agg_cashflows['cost per fruit'], agg_cashflows['revenue per fruit']\n",
      "\n",
      "# Calculate profit\n",
      "agg_cashflows['profit'] = agg_cashflows['revenue'] - agg_cashflows['fixed cost'] - agg_cashflows['var cost']\n",
      "iter_cashflows = agg_cashflows.groupby('iteration').sum()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 46
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "iter_cashflows[['profit','volume']].hist(xrot=90);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEmCAYAAABlB/tmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X90VPWZP/D3QLBQIRKKBsgAUye/CAlJBMKu52sNhSRQ\nTyOtCAQFAvEsi6cKu5SNUhFswSSuHgvdza67iy1yBLRrIbRLYqwk4nZLWBDUA275YSKQDFFIIgGR\nAfJ8/8CMxITJ/PjMnc+d+36dk0Pmztzn89xPnvlk8sydi01EBEREZBl9wp0AEREZiws/EZHFcOEn\nIrIYLvxERBbDhZ+IyGK48BMRWQwXfpP7l3/5F8TGxiI6OhotLS0YNGgQGhoawp0Wkc/WrFmDefPm\nhTsNS4kKdwIUuCtXrmD58uXYt28fUlNTAQDt7e2e+wsLCzFy5Ej84he/CFeKRL2y2WzhTsFy+Ipf\nY1evXvV6/5kzZ/Dll19izJgxBmVEpB4/Q2o8Lvxh4HA4UFpairFjx2LIkCFYtGgRLl++jNraWtjt\ndjz33HMYPnw4ioqK4Ha7sWzZMsTFxSEuLg5/93d/B7fbjaNHj3oW/MGDB2Pq1KkAgD59+uDEiRP4\nt3/7N2zZsgXPPfccBg0ahPvvvz+ch0wWUFZWhgcffLDLtqVLl2Lp0qVwuVzIz8/Hd77zHSQkJOA/\n/uM/eoxRW1uLkSNHdtnmcDiwe/duANfbQg8++CDmzZuH6OhojBs3DseOHUNJSQliY2MxevRovPXW\nW559P//8cxQVFWHEiBGw2+1YtWoVOjo6FB+5+XDhD5MtW7aguroaJ06cwNGjR7F27VrYbDY0Nzej\ntbUVJ0+exEsvvYS1a9di3759eP/99/H+++9j3759WLt2LRITE3H48GEA14v7j3/8oye2zWbD3/zN\n3+Chhx5CcXEx2tvbUVFREa5DJYsoKCjArl27cOHCBQDAtWvX8Nvf/hYPPfQQZs+ejVGjRsHlcuE/\n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OTuywEIiK//vWvJSUlRUaNGhV0/JSUFPnLX/7S4326LgxGO3nypLhcrm7bOzo65N13\n31UyxqVLl3rc/tlnn8kHH3ygZIwb/f73v5cnn3xSeVxVPvzwQ/ntb38rH330UVBxpk6dKmVlZV1a\nDS6XS0pLS2XKlCk+x1H1PFH1fL5xIV25cmWX+/z9f4lPnjwpM2fOlGXLlsnnn38e8MIfFxcnL7zw\ngjz//PMyevToLgu/P+3KDRs2yNSpU+Xtt9+W1atXy+OPPy61tbXy9NNPy8MPP+xznJ5+kV65ckUq\nKyulsLDQ5zhhWfh/+tOfSnV1dbftlZWVSv4z9Ndff/2mT67f/e53QccnCqdz587JihUrPD3+wYMH\nS1JSkqxYscKvHra358n27dt9jqPq+fzUU0/J+fPnu20/evSoPPDAAz7HudGOHTskKytL7rjjjoD2\nX716taxZs8bz1fleQ1NTk8ybN8+vWLt375YHH3xQMjIyJDU1VaZNmyb/+q//2uPlum9m9uzZfo15\nM2FZ+L9p586dpo5PpIuXX35ZSZzOVlQkxLl48aLnL0xV8yOi7thU5eRPHC1O50xLS8OHH35o2vhE\nulBxqi/jGBsrHHHC9n/uElFg0tLSbnrfjZeEYJzuPv30U5/j6JiTqjhc+IlMRtWpvoxjvpxUxeHC\nT2Qy9913Hy5cuIDMzMxu9/nzGQbGMV9OquKwx09EZDFh+x+4bhTq/xKR/+UiEdHXtHjFT0RExtHi\nFT8RERmHCz8RkcVw4Scishgu/EREFvP/ASnlyAnG2GYSAAAAAElFTkSuQmCC\n"
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Based on these assumptions:\n",
      "\n",
      " - the expected profit is 11,100\n",
      " - the 80% confidence band is 1,100 to 21,000\n",
      " - there is about an 8% chance of loss"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "iter_cashflows.describe(percentile_width=80).T"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>count</th>\n",
        "      <th>mean</th>\n",
        "      <th>std</th>\n",
        "      <th>min</th>\n",
        "      <th>10.0%</th>\n",
        "      <th>50%</th>\n",
        "      <th>90%</th>\n",
        "      <th>max</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>num trees</th>\n",
        "      <td> 10000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td>   950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>volume</th>\n",
        "      <td> 10000</td>\n",
        "      <td> 132531.002583</td>\n",
        "      <td> 10077.223030</td>\n",
        "      <td> 95614.986851</td>\n",
        "      <td> 119407.202274</td>\n",
        "      <td> 132508.424102</td>\n",
        "      <td> 145229.107164</td>\n",
        "      <td> 169735.090971</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>var cost</th>\n",
        "      <td> 10000</td>\n",
        "      <td>  22405.051342</td>\n",
        "      <td>  1738.099191</td>\n",
        "      <td> 16008.139927</td>\n",
        "      <td>  20142.094211</td>\n",
        "      <td>  22404.731080</td>\n",
        "      <td>  24580.565159</td>\n",
        "      <td>  28820.012684</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>revenue</th>\n",
        "      <td> 10000</td>\n",
        "      <td> 123503.212976</td>\n",
        "      <td>  9425.981656</td>\n",
        "      <td> 88576.492461</td>\n",
        "      <td> 111277.416442</td>\n",
        "      <td> 123449.475693</td>\n",
        "      <td> 135410.669389</td>\n",
        "      <td> 158317.758554</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>fixed cost</th>\n",
        "      <td> 10000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td> 90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>profit</th>\n",
        "      <td> 10000</td>\n",
        "      <td>  11098.161634</td>\n",
        "      <td>  7694.926234</td>\n",
        "      <td>-17431.647466</td>\n",
        "      <td>   1127.497613</td>\n",
        "      <td>  11052.297244</td>\n",
        "      <td>  20830.179123</td>\n",
        "      <td>  39497.745870</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>6 rows \u00d7 8 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 48,
       "text": [
        "            count           mean           std           min          10.0%  \\\n",
        "num trees   10000     950.000000      0.000000    950.000000     950.000000   \n",
        "volume      10000  132531.002583  10077.223030  95614.986851  119407.202274   \n",
        "var cost    10000   22405.051342   1738.099191  16008.139927   20142.094211   \n",
        "revenue     10000  123503.212976   9425.981656  88576.492461  111277.416442   \n",
        "fixed cost  10000   90000.000000      0.000000  90000.000000   90000.000000   \n",
        "profit      10000   11098.161634   7694.926234 -17431.647466    1127.497613   \n",
        "\n",
        "                      50%            90%            max  \n",
        "num trees      950.000000     950.000000     950.000000  \n",
        "volume      132508.424102  145229.107164  169735.090971  \n",
        "var cost     22404.731080   24580.565159   28820.012684  \n",
        "revenue     123449.475693  135410.669389  158317.758554  \n",
        "fixed cost   90000.000000   90000.000000   90000.000000  \n",
        "profit       11052.297244   20830.179123   39497.745870  \n",
        "\n",
        "[6 rows x 8 columns]"
       ]
      }
     ],
     "prompt_number": 48
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"%3.1f%% chance of loss\" % (100*(iter_cashflows['profit']<=0).sum()/(N+.0))  # uses Boolean indexing"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7.8% chance of loss\n"
       ]
      }
     ],
     "prompt_number": 56
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<div class='summary'>\n",
      "We have used these key pandas features:\n",
      "\n",
      "- `read_csv`\n",
      "- `merge`\n",
      "- `groupby` & `apply`\n",
      "- `fillna`\n",
      "- `hist`\n",
      "- Broadcasting\n",
      "- Boolean indexing\n",
      "\n",
      "And:\n",
      "\n",
      "- `drop`\n",
      "- `reset_index`\n",
      "- `rename`\n",
      "- `describe`\n",
      "- `unstack`"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Montepylib"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import os\n",
      "os.sys.path.append(os.path.dirname(os.path.abspath('..')))\n",
      "from examples import example1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 57
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = example1.read_data(\"../../examples/data/\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 58
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data['fruit_per_tree']  # similar to before, but by year"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>farm</th>\n",
        "      <th>fruit</th>\n",
        "      <th>variety</th>\n",
        "      <th>year</th>\n",
        "      <th>mean</th>\n",
        "      <th>sd</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2014</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2014</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22.50</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 2014</td>\n",
        "      <td>  50</td>\n",
        "      <td>  7.50</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3 </th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2014</td>\n",
        "      <td> 160</td>\n",
        "      <td> 24.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4 </th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2014</td>\n",
        "      <td> 120</td>\n",
        "      <td> 18.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>5 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2015</td>\n",
        "      <td> 220</td>\n",
        "      <td> 33.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>6 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2015</td>\n",
        "      <td> 175</td>\n",
        "      <td> 26.25</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>7 </th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 2015</td>\n",
        "      <td>  55</td>\n",
        "      <td>  8.25</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>8 </th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2015</td>\n",
        "      <td> 180</td>\n",
        "      <td> 27.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>9 </th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2015</td>\n",
        "      <td> 150</td>\n",
        "      <td> 22.50</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>10</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2016</td>\n",
        "      <td> 240</td>\n",
        "      <td> 36.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>11</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2016</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>12</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 2016</td>\n",
        "      <td>  60</td>\n",
        "      <td>  9.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>13</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2016</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>14</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2016</td>\n",
        "      <td> 180</td>\n",
        "      <td> 27.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>15</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2017</td>\n",
        "      <td> 250</td>\n",
        "      <td> 37.50</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>16</th>\n",
        "      <td> Farm A</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2017</td>\n",
        "      <td> 225</td>\n",
        "      <td> 33.75</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>17</th>\n",
        "      <td> Farm A</td>\n",
        "      <td>  pear</td>\n",
        "      <td>        Nashi</td>\n",
        "      <td> 2017</td>\n",
        "      <td>  60</td>\n",
        "      <td>  9.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>18</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td> Granny Smith</td>\n",
        "      <td> 2017</td>\n",
        "      <td> 220</td>\n",
        "      <td> 33.00</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>19</th>\n",
        "      <td> Farm B</td>\n",
        "      <td> apple</td>\n",
        "      <td>     Jonathon</td>\n",
        "      <td> 2017</td>\n",
        "      <td> 200</td>\n",
        "      <td> 30.00</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>20 rows \u00d7 6 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 59,
       "text": [
        "      farm  fruit       variety  year  mean     sd\n",
        "0   Farm A  apple  Granny Smith  2014   200  30.00\n",
        "1   Farm A  apple      Jonathon  2014   150  22.50\n",
        "2   Farm A   pear         Nashi  2014    50   7.50\n",
        "3   Farm B  apple  Granny Smith  2014   160  24.00\n",
        "4   Farm B  apple      Jonathon  2014   120  18.00\n",
        "5   Farm A  apple  Granny Smith  2015   220  33.00\n",
        "6   Farm A  apple      Jonathon  2015   175  26.25\n",
        "7   Farm A   pear         Nashi  2015    55   8.25\n",
        "8   Farm B  apple  Granny Smith  2015   180  27.00\n",
        "9   Farm B  apple      Jonathon  2015   150  22.50\n",
        "10  Farm A  apple  Granny Smith  2016   240  36.00\n",
        "11  Farm A  apple      Jonathon  2016   200  30.00\n",
        "12  Farm A   pear         Nashi  2016    60   9.00\n",
        "13  Farm B  apple  Granny Smith  2016   200  30.00\n",
        "14  Farm B  apple      Jonathon  2016   180  27.00\n",
        "15  Farm A  apple  Granny Smith  2017   250  37.50\n",
        "16  Farm A  apple      Jonathon  2017   225  33.75\n",
        "17  Farm A   pear         Nashi  2017    60   9.00\n",
        "18  Farm B  apple  Granny Smith  2017   220  33.00\n",
        "19  Farm B  apple      Jonathon  2017   200  30.00\n",
        "\n",
        "[20 rows x 6 columns]"
       ]
      }
     ],
     "prompt_number": 59
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# the base case (no simulation)\n",
      "example1.simulate(data, N=0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>num trees</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>volume</th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>revenue</th>\n",
        "      <th>fixed cost</th>\n",
        "      <th>profit</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>iteration</th>\n",
        "      <th>year</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">base case</th>\n",
        "      <th>2014</th>\n",
        "      <td> 950</td>\n",
        "      <td> 680</td>\n",
        "      <td> 132500</td>\n",
        "      <td> 0.86</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 4.70</td>\n",
        "      <td> 123400</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 11000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 950</td>\n",
        "      <td> 780</td>\n",
        "      <td> 155750</td>\n",
        "      <td> 0.86</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 4.90</td>\n",
        "      <td> 151430</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 35050</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 950</td>\n",
        "      <td> 880</td>\n",
        "      <td> 179000</td>\n",
        "      <td> 0.86</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 5.08</td>\n",
        "      <td> 180680</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 60320</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 950</td>\n",
        "      <td> 955</td>\n",
        "      <td> 197000</td>\n",
        "      <td> 0.86</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 5.28</td>\n",
        "      <td> 206800</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 83380</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>4 rows \u00d7 9 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 60,
       "text": [
        "                num trees  fruit per tree  volume  cost per fruit  var cost  \\\n",
        "iteration year                                                                \n",
        "base case 2014        950             680  132500            0.86     22400   \n",
        "          2015        950             780  155750            0.86     26380   \n",
        "          2016        950             880  179000            0.86     30360   \n",
        "          2017        950             955  197000            0.86     33420   \n",
        "\n",
        "                revenue per fruit  revenue  fixed cost  profit  \n",
        "iteration year                                                  \n",
        "base case 2014               4.70   123400       90000   11000  \n",
        "          2015               4.90   151430       90000   35050  \n",
        "          2016               5.08   180680       90000   60320  \n",
        "          2017               5.28   206800       90000   83380  \n",
        "\n",
        "[4 rows x 9 columns]"
       ]
      }
     ],
     "prompt_number": 60
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# one-factor sensitivities\n",
      "sensitivities = example1.simulate(data, N=-1)\n",
      "sensitivities"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th>cost per fruit</th>\n",
        "      <th>fixed cost</th>\n",
        "      <th>fruit per tree</th>\n",
        "      <th>num trees</th>\n",
        "      <th>revenue</th>\n",
        "      <th>revenue per fruit</th>\n",
        "      <th>var cost</th>\n",
        "      <th>volume</th>\n",
        "      <th>profit</th>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>iteration</th>\n",
        "      <th>year</th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "      <th></th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">base case</th>\n",
        "      <th>2014</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 680</td>\n",
        "      <td> 950</td>\n",
        "      <td> 123400</td>\n",
        "      <td> 4.700</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 132500</td>\n",
        "      <td>  11000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 780</td>\n",
        "      <td> 950</td>\n",
        "      <td> 151430</td>\n",
        "      <td> 4.900</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 155750</td>\n",
        "      <td>  35050</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 880</td>\n",
        "      <td> 950</td>\n",
        "      <td> 180680</td>\n",
        "      <td> 5.080</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 179000</td>\n",
        "      <td>  60320</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 955</td>\n",
        "      <td> 950</td>\n",
        "      <td> 206800</td>\n",
        "      <td> 5.280</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 197000</td>\n",
        "      <td>  83380</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">high fixed cost</th>\n",
        "      <th>2014</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 680</td>\n",
        "      <td> 950</td>\n",
        "      <td> 123400</td>\n",
        "      <td> 4.700</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 132500</td>\n",
        "      <td>  11000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 780</td>\n",
        "      <td> 950</td>\n",
        "      <td> 151430</td>\n",
        "      <td> 4.900</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 155750</td>\n",
        "      <td>  35050</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 880</td>\n",
        "      <td> 950</td>\n",
        "      <td> 180680</td>\n",
        "      <td> 5.080</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 179000</td>\n",
        "      <td>  60320</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 955</td>\n",
        "      <td> 950</td>\n",
        "      <td> 206800</td>\n",
        "      <td> 5.280</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 197000</td>\n",
        "      <td>  83380</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">high revenue per fruit</th>\n",
        "      <th>2014</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 680</td>\n",
        "      <td> 950</td>\n",
        "      <td> 148080</td>\n",
        "      <td> 5.640</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 132500</td>\n",
        "      <td>  35680</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 780</td>\n",
        "      <td> 950</td>\n",
        "      <td> 181716</td>\n",
        "      <td> 5.880</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 155750</td>\n",
        "      <td>  65336</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 880</td>\n",
        "      <td> 950</td>\n",
        "      <td> 216816</td>\n",
        "      <td> 6.096</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 179000</td>\n",
        "      <td>  96456</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 955</td>\n",
        "      <td> 950</td>\n",
        "      <td> 248160</td>\n",
        "      <td> 6.336</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 197000</td>\n",
        "      <td> 124740</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">low fixed cost</th>\n",
        "      <th>2014</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 680</td>\n",
        "      <td> 950</td>\n",
        "      <td> 123400</td>\n",
        "      <td> 4.700</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 132500</td>\n",
        "      <td>  11000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 780</td>\n",
        "      <td> 950</td>\n",
        "      <td> 151430</td>\n",
        "      <td> 4.900</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 155750</td>\n",
        "      <td>  35050</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 880</td>\n",
        "      <td> 950</td>\n",
        "      <td> 180680</td>\n",
        "      <td> 5.080</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 179000</td>\n",
        "      <td>  60320</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 955</td>\n",
        "      <td> 950</td>\n",
        "      <td> 206800</td>\n",
        "      <td> 5.280</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 197000</td>\n",
        "      <td>  83380</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th rowspan=\"4\" valign=\"top\">low revenue per fruit</th>\n",
        "      <th>2014</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 680</td>\n",
        "      <td> 950</td>\n",
        "      <td>  98720</td>\n",
        "      <td> 3.760</td>\n",
        "      <td> 22400</td>\n",
        "      <td> 132500</td>\n",
        "      <td> -13680</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2015</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 780</td>\n",
        "      <td> 950</td>\n",
        "      <td> 121144</td>\n",
        "      <td> 3.920</td>\n",
        "      <td> 26380</td>\n",
        "      <td> 155750</td>\n",
        "      <td>   4764</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2016</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 880</td>\n",
        "      <td> 950</td>\n",
        "      <td> 144544</td>\n",
        "      <td> 4.064</td>\n",
        "      <td> 30360</td>\n",
        "      <td> 179000</td>\n",
        "      <td>  24184</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2017</th>\n",
        "      <td> 0.86</td>\n",
        "      <td> 90000</td>\n",
        "      <td> 955</td>\n",
        "      <td> 950</td>\n",
        "      <td> 165440</td>\n",
        "      <td> 4.224</td>\n",
        "      <td> 33420</td>\n",
        "      <td> 197000</td>\n",
        "      <td>  42020</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>20 rows \u00d7 9 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 61,
       "text": [
        "                             cost per fruit  fixed cost  fruit per tree  \\\n",
        "iteration              year                                               \n",
        "base case              2014            0.86       90000             680   \n",
        "                       2015            0.86       90000             780   \n",
        "                       2016            0.86       90000             880   \n",
        "                       2017            0.86       90000             955   \n",
        "high fixed cost        2014            0.86       90000             680   \n",
        "                       2015            0.86       90000             780   \n",
        "                       2016            0.86       90000             880   \n",
        "                       2017            0.86       90000             955   \n",
        "high revenue per fruit 2014            0.86       90000             680   \n",
        "                       2015            0.86       90000             780   \n",
        "                       2016            0.86       90000             880   \n",
        "                       2017            0.86       90000             955   \n",
        "low fixed cost         2014            0.86       90000             680   \n",
        "                       2015            0.86       90000             780   \n",
        "                       2016            0.86       90000             880   \n",
        "                       2017            0.86       90000             955   \n",
        "low revenue per fruit  2014            0.86       90000             680   \n",
        "                       2015            0.86       90000             780   \n",
        "                       2016            0.86       90000             880   \n",
        "                       2017            0.86       90000             955   \n",
        "\n",
        "                             num trees  revenue  revenue per fruit  var cost  \\\n",
        "iteration              year                                                    \n",
        "base case              2014        950   123400              4.700     22400   \n",
        "                       2015        950   151430              4.900     26380   \n",
        "                       2016        950   180680              5.080     30360   \n",
        "                       2017        950   206800              5.280     33420   \n",
        "high fixed cost        2014        950   123400              4.700     22400   \n",
        "                       2015        950   151430              4.900     26380   \n",
        "                       2016        950   180680              5.080     30360   \n",
        "                       2017        950   206800              5.280     33420   \n",
        "high revenue per fruit 2014        950   148080              5.640     22400   \n",
        "                       2015        950   181716              5.880     26380   \n",
        "                       2016        950   216816              6.096     30360   \n",
        "                       2017        950   248160              6.336     33420   \n",
        "low fixed cost         2014        950   123400              4.700     22400   \n",
        "                       2015        950   151430              4.900     26380   \n",
        "                       2016        950   180680              5.080     30360   \n",
        "                       2017        950   206800              5.280     33420   \n",
        "low revenue per fruit  2014        950    98720              3.760     22400   \n",
        "                       2015        950   121144              3.920     26380   \n",
        "                       2016        950   144544              4.064     30360   \n",
        "                       2017        950   165440              4.224     33420   \n",
        "\n",
        "                             volume  profit  \n",
        "iteration              year                  \n",
        "base case              2014  132500   11000  \n",
        "                       2015  155750   35050  \n",
        "                       2016  179000   60320  \n",
        "                       2017  197000   83380  \n",
        "high fixed cost        2014  132500   11000  \n",
        "                       2015  155750   35050  \n",
        "                       2016  179000   60320  \n",
        "                       2017  197000   83380  \n",
        "high revenue per fruit 2014  132500   35680  \n",
        "                       2015  155750   65336  \n",
        "                       2016  179000   96456  \n",
        "                       2017  197000  124740  \n",
        "low fixed cost         2014  132500   11000  \n",
        "                       2015  155750   35050  \n",
        "                       2016  179000   60320  \n",
        "                       2017  197000   83380  \n",
        "low revenue per fruit  2014  132500  -13680  \n",
        "                       2015  155750    4764  \n",
        "                       2016  179000   24184  \n",
        "                       2017  197000   42020  \n",
        "\n",
        "[20 rows x 9 columns]"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# full simulation with NPV calculation\n",
      "N = 1000\n",
      "total_per_iteration = example1.simulate(data, N, discount_rate=1.09, base_year=2014)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 62
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# example1.sim_output(total_per_iteration, N=1000)\n",
      "# the example output is:\n",
      "total_per_iteration.xs('2017', level='year').describe(percentile_width=80).T"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>count</th>\n",
        "      <th>mean</th>\n",
        "      <th>std</th>\n",
        "      <th>min</th>\n",
        "      <th>10.0%</th>\n",
        "      <th>50%</th>\n",
        "      <th>90%</th>\n",
        "      <th>max</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>num trees</th>\n",
        "      <td> 1000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "      <td>    950.000000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>fruit per tree</th>\n",
        "      <td> 1000</td>\n",
        "      <td>    956.971171</td>\n",
        "      <td>    70.561931</td>\n",
        "      <td>    762.397112</td>\n",
        "      <td>    862.889296</td>\n",
        "      <td>    958.357739</td>\n",
        "      <td>   1043.667005</td>\n",
        "      <td>   1253.695033</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>volume</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 197156.863761</td>\n",
        "      <td> 16665.839963</td>\n",
        "      <td> 146779.042820</td>\n",
        "      <td> 175219.295045</td>\n",
        "      <td> 197257.724424</td>\n",
        "      <td> 218751.500684</td>\n",
        "      <td> 268160.548834</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>cost per fruit</th>\n",
        "      <td> 1000</td>\n",
        "      <td>      0.860000</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td>      0.860000</td>\n",
        "      <td>      0.860000</td>\n",
        "      <td>      0.860000</td>\n",
        "      <td>      0.860000</td>\n",
        "      <td>      0.860000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>var cost</th>\n",
        "      <td> 1000</td>\n",
        "      <td>  33445.297851</td>\n",
        "      <td>  2889.865534</td>\n",
        "      <td>  24669.381359</td>\n",
        "      <td>  29644.206583</td>\n",
        "      <td>  33483.367807</td>\n",
        "      <td>  37248.647198</td>\n",
        "      <td>  45775.678659</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>revenue per fruit</th>\n",
        "      <td> 1000</td>\n",
        "      <td>      5.270118</td>\n",
        "      <td>     0.598442</td>\n",
        "      <td>      3.334018</td>\n",
        "      <td>      4.476001</td>\n",
        "      <td>      5.279304</td>\n",
        "      <td>      6.010670</td>\n",
        "      <td>      7.336881</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>revenue</th>\n",
        "      <td> 1000</td>\n",
        "      <td> 205977.316765</td>\n",
        "      <td> 33825.977248</td>\n",
        "      <td>  96935.914581</td>\n",
        "      <td> 163613.290877</td>\n",
        "      <td> 203543.695460</td>\n",
        "      <td> 248732.687212</td>\n",
        "      <td> 326867.809221</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>fixed cost</th>\n",
        "      <td> 1000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "      <td>  90000.000000</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>profit</th>\n",
        "      <td> 1000</td>\n",
        "      <td>  82532.018913</td>\n",
        "      <td> 32461.113966</td>\n",
        "      <td> -25193.699049</td>\n",
        "      <td>  42029.555969</td>\n",
        "      <td>  80669.712054</td>\n",
        "      <td> 124297.498582</td>\n",
        "      <td> 198214.032854</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>discount factor</th>\n",
        "      <td> 1000</td>\n",
        "      <td>      0.772183</td>\n",
        "      <td>     0.000000</td>\n",
        "      <td>      0.772183</td>\n",
        "      <td>      0.772183</td>\n",
        "      <td>      0.772183</td>\n",
        "      <td>      0.772183</td>\n",
        "      <td>      0.772183</td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>discounted profit</th>\n",
        "      <td> 1000</td>\n",
        "      <td>  63729.861581</td>\n",
        "      <td> 25065.935949</td>\n",
        "      <td> -19454.158207</td>\n",
        "      <td>  32454.528793</td>\n",
        "      <td>  62291.818989</td>\n",
        "      <td>  95980.475018</td>\n",
        "      <td> 153057.601686</td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>\n",
        "<p>11 rows \u00d7 8 columns</p>\n",
        "</div>"
       ],
       "output_type": "pyout",
       "prompt_number": 63,
       "text": [
        "                   count           mean           std            min  \\\n",
        "num trees           1000     950.000000      0.000000     950.000000   \n",
        "fruit per tree      1000     956.971171     70.561931     762.397112   \n",
        "volume              1000  197156.863761  16665.839963  146779.042820   \n",
        "cost per fruit      1000       0.860000      0.000000       0.860000   \n",
        "var cost            1000   33445.297851   2889.865534   24669.381359   \n",
        "revenue per fruit   1000       5.270118      0.598442       3.334018   \n",
        "revenue             1000  205977.316765  33825.977248   96935.914581   \n",
        "fixed cost          1000   90000.000000      0.000000   90000.000000   \n",
        "profit              1000   82532.018913  32461.113966  -25193.699049   \n",
        "discount factor     1000       0.772183      0.000000       0.772183   \n",
        "discounted profit   1000   63729.861581  25065.935949  -19454.158207   \n",
        "\n",
        "                           10.0%            50%            90%            max  \n",
        "num trees             950.000000     950.000000     950.000000     950.000000  \n",
        "fruit per tree        862.889296     958.357739    1043.667005    1253.695033  \n",
        "volume             175219.295045  197257.724424  218751.500684  268160.548834  \n",
        "cost per fruit          0.860000       0.860000       0.860000       0.860000  \n",
        "var cost            29644.206583   33483.367807   37248.647198   45775.678659  \n",
        "revenue per fruit       4.476001       5.279304       6.010670       7.336881  \n",
        "revenue            163613.290877  203543.695460  248732.687212  326867.809221  \n",
        "fixed cost          90000.000000   90000.000000   90000.000000   90000.000000  \n",
        "profit              42029.555969   80669.712054  124297.498582  198214.032854  \n",
        "discount factor         0.772183       0.772183       0.772183       0.772183  \n",
        "discounted profit   32454.528793   62291.818989   95980.475018  153057.601686  \n",
        "\n",
        "[11 rows x 8 columns]"
       ]
      }
     ],
     "prompt_number": 63
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"%3.1f%% chance of loss in 2014\" % (100*(total_per_iteration.xs('2014', level='year')['profit']<=0).sum()/(N+.0))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "28.6% chance of loss in 2014\n"
       ]
      }
     ],
     "prompt_number": 67
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# histogram of profit in 2014\n",
      "total_per_iteration.xs('2014', level='year')[['profit']].hist(xrot=90);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEmCAYAAACZEtCsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X9UVGX+B/D3KHy3bUXBrLEYa0hQHNBACdo924opoGcT\nrVwSW8OVtnP0dHat9qRtpem6gp3d02qbu22rSbWK/lFiu0lkcjvVKXELqhOWWCAGyEaIaZkoPN8/\nyFEChmfg3nnmufN+nTPncO8M83nPj/th5jN3Lg4hhAAREdnKENUBiIjIfGzuREQ2xOZORGRDbO5E\nRDbE5k5EZENs7kRENsTmTuTD3/72NzidTgwfPhytra2IiIhAXV2d6lhE/XJwP3ei3p09exYjRoxA\nRUUFEhMTe5y/aNEijBkzBn/4wx8UpCPyja/cKWSdO3fO5/nHjh3Dt99+iwkTJgQoEZF52NzJdtxu\nNwoLC5GQkICRI0di8eLFOHPmDAzDgMvlwmOPPYYrr7wS+fn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V02233SY2b94shBBi0aJF3bb1lJQUldG68Xg84ttvvxWtra1i2LBh\noqWlRQghxDfffNPjOdEXS5v7xRvzmDFjup0XTE/M+Ph470F6vv9/PmX/SgbCn/70J5GVlSXef/99\n77pg24C+/+pn7dq14ic/+Yn44osv2NwHID09XVRWVnZb197eLhYuXCgcDoeiVH07evSomDdvnli6\ndKlwuVyq4/Rw/Phxceedd4qYmBiRmprqfXF04403iqqqKtXxvNatWydiYmLEuHHjxFNPPSUmTJgg\n8vPzRUJCgli/fr3UdVja3K+//npRWloqduzYIVwul3jhhReEEEIYhiFSU1OtLO2XjRs3ihkzZojX\nXntNrFq1SvzmN78RhmGIlStXil/+8peq43VTX18v5s2bJ5YtWyZOnDgRdM09Pj5edHR0dFv3zDPP\nCI/HI66++mpFqfRVX18vmpqaeqzv7OwUb7zxhoJEcl566SXx4IMPqo7Rp7a2NlFZWSkOHDjQ6/0b\nDGpra73j1sOHD4vi4mK//gBZ2tz3798vpk6dKubPny/q6urE9OnTRUREhEhOThYHDhywsrTf9u3b\nJ37xi1+IpKQkkZiYKGbOnCn+/ve/93rYzWCwa9cukZaWJq644grVUbr53e9+J8rKynqs37NnT5+H\nAyYi8wV0b5ndu3cjOzs7UOUGTKec1157LRITE1VH6dNLL72E2bNnq45BFHIC2twnTpyIDz/8MFDl\nBow5zaNDRiI7svyokEREFHhs7kRENsTmTkRkQ2zuREQ2FNDmrsu/WmNO8+iQkciOeOAwIiIb4liG\niMiG2NyJiGyIzZ2IyIbY3ImIbOj/AUmOylNG6v5bAAAAAElFTkSuQmCC\n"
      }
     ],
     "prompt_number": 68
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#histogram of NPV\n",
      "example1.calc_NPVs(total_per_iteration).hist(xrot=90);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAElCAYAAAAfhqICAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz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Dgxx9/xEsvvQQhBE6dOjXoMaHAx3Q8/gDbMi40\nNTWhsbERjY2NOHr0KOLi4gAAw4cPx44dOySnc21gcl/awmhvb8fzzz+v1OT+y1/+omRx2bZtG06c\nOIHbb78dMTExiImJQVZWlhJ/IhAA7r//fvT09ODMmTNYsmQJfvzxRwDOa52kpaVJTudeqB7/9vZ2\nj48/V8uEKJWXcQLA6tWrB11mYNmyZYiNjUVbWxtKS0uxefNmiek889VXX6GlpQWZmZmIiorS99fU\n1CjxB0cG8t9yyy0YOXKkvv/dd9/FvHnzJCbzzEcffYSYmBhMnjwZtbW1OHDgANLT0zF79mzZ0Txy\naX5N0/DZZ58Zy++7LhGpoqKiQnYEUzZt2iQ7glvr168XN910k5g/f764/vrrxY4dO/THfv75TTBS\nPX9ZWZnIzMwUM2bMEI8//rjIzMwUa9euFbNmzRIvvPCC7Hhu+SI/i3sYstlssiOYokL+yZMni56e\nHiGEEI2NjWLatGli3bp1Qgg1iuPP80+fPl2p/JMmTRLnz58XP/30kxg5cqTo7u4WQghx9uxZkZqa\nKjmde77Izw9UQ1RqauoVH1NhKaHq+YUQeivDbrdj7969uOeee3Ds2DElPjP4eX5N05TK/4tf/AIR\nERGIiIhAQkICRo8eDQC45pprMGxY8H/U6Iv8LO4h6ocffkBNTQ1iYmKGPHbrrbdKSGSM6vljY2NR\nX1+vf/g1cuRI/Pvf/0ZJSQm++OILyencUz3/VVddhbNnz2LEiBH4/PPP9f3d3d1KFHdf5GdxD1F3\n3nknzpw5g/T09CGP3X777RISGaN6/s2bNyMyMnLQvsjISFRVVeHBBx+UlMpzquffu3cvrr76agAY\nVAwvXLiAqqoqWbE85ov8XC1DRBSCgv/nEyIiMozFnYgoBLG4ExGFIBZ3IqIQxOJORBSC/h+hwBl8\nhpeQsQAAAABJRU5ErkJggg==\n"
      }
     ],
     "prompt_number": 69
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# And back to some final slides..."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  \n",
      ".  "
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Styling"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from IPython.core.display import HTML\n",
      "def css_styling():\n",
      "    styles = open(\"./css/notebook.css\", \"r\").read()\n",
      "    return HTML(styles)\n",
      "css_styling()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<style>\n",
        "div.summary {    \n",
        "    background-color: #F0FCF0;\n",
        "    border-left: 15px solid #c4ddc4;\n",
        "    padding: .5em;\n",
        "}\n",
        "div.cell {\n",
        "    font-size: 105%;\n",
        "}\n",
        "</style>\n",
        "\n"
       ],
       "output_type": "pyout",
       "prompt_number": 37,
       "text": [
        "<IPython.core.display.HTML at 0x10409a1d0>"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 37
    }
   ],
   "metadata": {}
  }
 ]
}
