{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction: Looking at Data to Correct Cognitive Biases\n",
    "\n",
    "[Medium article: \"How to Overcome Biases with Data\"](https://medium.com/p/692c8c35f4a5)\n",
    "\n",
    "In this notebook, we will investigate a simple problem: is the smoking rate higher in the metro area of my hometown (Peoria, Il) or in Boston. The purpose is to see if I can combat my cognitive biases with data and explore the data to determine why I made a incorrect conclusion.\n",
    "\n",
    "# City Level Data\n",
    "\n",
    "https://nccd.cdc.gov/500_Cities/rdPage.aspx?rdReport=DPH_500_Cities.ComparisonReport&Locations=1759000,2507000&rdRequestForwarding=Form\n",
    "\n",
    "![](images/rates_smoking.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:25.874443Z",
     "start_time": "2018-11-02T02:22:25.695890Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:25.878214Z",
     "start_time": "2018-11-02T02:22:25.875892Z"
    }
   },
   "outputs": [],
   "source": [
    "# Boston\n",
    "b_p = 0.164\n",
    "b_ci = [0.161, 0.166]\n",
    "b_adults = 472582\n",
    "b_smokers = b_p * b_adults\n",
    "\n",
    "# Peoria\n",
    "p_p = 0.199\n",
    "p_ci = [0.194, 0.203]\n",
    "p_adults = 83925\n",
    "p_smokers = p_p * p_adults"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Difference Between Two Proportions\n",
    "\n",
    "This is assuming 10% of adults in each city surveyed. \n",
    "\n",
    "Relevant Data:\n",
    "\n",
    "* Boston number of adults = 472582: https://www.bumc.bu.edu/inspir/files/HTML/Boston%20Population%20and%20Demographics.htm\n",
    "* Peoria number of adults = 83925: https://www.infoplease.com/us/illinois/demographic-statistics-61\n",
    "* Boston smoking rate = 0.164 (from first source in image)\n",
    "* Peoria smoking rate = 0.199(from first source in image)\n",
    "\n",
    "z-score for 95% CI: http://www.stat.yale.edu/Courses/1997-98/101/confint.htm\n",
    "\n",
    "Difference between two proportions formula: https://onlinecourses.science.psu.edu/stat100/node/57/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:25.884108Z",
     "start_time": "2018-11-02T02:22:25.879415Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The 95% confidence interval for difference between smoking rates is from 2.5829% to 4.4171%\n"
     ]
    }
   ],
   "source": [
    "# Standard errors individual\n",
    "b_se = np.sqrt((b_p * (1 - b_p)) / (0.1 * b_adults))\n",
    "p_se = np.sqrt((p_p * (1 - p_p)) / (0.1 * p_adults))\n",
    "\n",
    "# Standard error combined\n",
    "se_diff = np.sqrt(np.square(b_se) + np.square(p_se))\n",
    "\n",
    "z_95 = 1.96\n",
    "\n",
    "# Upper confidence bound for difference (two-tailed)\n",
    "uci = (p_p - b_p) + z_95 * se_diff\n",
    "# Lower confidence bound\n",
    "lci = (p_p - b_p) - z_95 * se_diff\n",
    "\n",
    "print(f'The 95% confidence interval for difference between smoking rates is from {round(100 * lci, 4)}% to {round(100 * uci, 4)}%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This indicates the difference is significant. We can also tell this from the non-overlapping individual confidence intervals."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Distributions\n",
    "\n",
    "If we assume the smoking rate proportion is normally distributed then we can construct a normal distribution and sample from it. Then we can visually show the data in boxplots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:25.902707Z",
     "start_time": "2018-11-02T02:22:25.885493Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Standard deviation for Boston: 0.001020408163265307\n",
      "Standard deviation for Peoria: 0.002040816326530614\n"
     ]
    }
   ],
   "source": [
    "z_95 = 1.96\n",
    "\n",
    "b_sigma = ((b_ci[1] - b_p) / z_95) * 1\n",
    "p_sigma = ((p_ci[1] - p_p) / z_95) * 1\n",
    "\n",
    "print(f'Standard deviation for Boston: {b_sigma}')\n",
    "print(f'Standard deviation for Peoria: {p_sigma}')\n",
    "\n",
    "# Sample distributions \n",
    "p_dist = np.random.normal(loc = p_p, scale = p_sigma, size = 100000)\n",
    "b_dist = np.random.normal(loc = b_p, scale = b_sigma, size = 100000)\n",
    "# Names\n",
    "b = np.repeat('boston', repeats = len(b_dist))\n",
    "p = np.repeat('peoria', repeats = len(p_dist))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Confidence interval for Boston."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:26.108357Z",
     "start_time": "2018-11-02T02:22:25.904047Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.16200003675046934, 0.16599996324953067)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import stats\n",
    "b_ci = stats.norm.interval(0.95, loc = b_p, scale = b_sigma)\n",
    "b_ci"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Confidence interval for Peoria"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:26.113098Z",
     "start_time": "2018-11-02T02:22:26.109723Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.19500007350093868, 0.20299992649906135)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_ci = stats.norm.interval(0.95, loc = p_p, scale = p_sigma)\n",
    "p_ci"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The confidence intervals from our simulated data are basically the same as those from the survey data. The survey data does have uneven confidence intervals though. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:26.377082Z",
     "start_time": "2018-11-02T02:22:26.114286Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"8\" halign=\"left\">proportion</th>\n",
       "    </tr>\n",
       "    <tr>\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>city</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>boston</th>\n",
       "      <td>100000.0</td>\n",
       "      <td>0.164003</td>\n",
       "      <td>0.001018</td>\n",
       "      <td>0.159767</td>\n",
       "      <td>0.163316</td>\n",
       "      <td>0.164006</td>\n",
       "      <td>0.164690</td>\n",
       "      <td>0.168284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peoria</th>\n",
       "      <td>100000.0</td>\n",
       "      <td>0.199006</td>\n",
       "      <td>0.002042</td>\n",
       "      <td>0.190117</td>\n",
       "      <td>0.197627</td>\n",
       "      <td>0.199007</td>\n",
       "      <td>0.200389</td>\n",
       "      <td>0.209150</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       proportion                                                              \\\n",
       "            count      mean       std       min       25%       50%       75%   \n",
       "city                                                                            \n",
       "boston   100000.0  0.164003  0.001018  0.159767  0.163316  0.164006  0.164690   \n",
       "peoria   100000.0  0.199006  0.002042  0.190117  0.197627  0.199007  0.200389   \n",
       "\n",
       "                  \n",
       "             max  \n",
       "city              \n",
       "boston  0.168284  \n",
       "peoria  0.209150  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "dist_df = pd.DataFrame({'proportion': list(b_dist) + list(p_dist)})\n",
    "dist_df['city'] = list(b) + list(p)\n",
    "dist_df.groupby('city').describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:26.381295Z",
     "start_time": "2018-11-02T02:22:26.378461Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "default_c = plt.rcParams['axes.prop_cycle'].by_key()['color']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:26.838869Z",
     "start_time": "2018-11-02T02:22:26.382721Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 43124 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "plt.figure(figsize = (10, 8))\n",
    "p1 = sns.kdeplot(p_dist, label = 'Peoria')\n",
    "p2 = sns.kdeplot(b_dist, label = 'Boston')\n",
    "plt.legend(); plt.title('Distribution of Percentages');\n",
    "\n",
    "# Means and confidence intervals\n",
    "plt.vlines([b_p, p_p], ymin = 0, ymax = 400, colors = ['#D4CA3A', '#00BEFF']);\n",
    "plt.hlines([25], xmin = b_ci[0], xmax = b_ci[1], colors = ['#D4CA3A'], linestyles = '-')\n",
    "plt.hlines([25], xmin = p_ci[0], xmax = p_ci[1], colors = ['#00BEFF'], linestyles = '-');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The confidence interval for Boston is slightly narrower. Boxplots are a good way to show a distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:27.152755Z",
     "start_time": "2018-11-02T02:22:26.840449Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30716 characters]\n",
      "text/plain": [
       "<Figure size 1440x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.rcParams['font.size'] = 32\n",
    "plt.figure(figsize = (20, 8))\n",
    "sns.boxplot(x = 'proportion', y = 'city', data = dist_df);\n",
    "plt.xlabel('Proportion of Smokers');\n",
    "plt.title('Boxplot of Smoking Rates');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:28.108076Z",
     "start_time": "2018-11-02T02:22:27.154401Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34244 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize = (10, 8))\n",
    "sns.violinplot(y = 'proportion', x = 'city', data = dist_df);\n",
    "plt.ylabel('Percentage of Smokers');\n",
    "plt.title('Violinplot of Smoking Rates');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How Many Smokers Do I See Per Day?\n",
    "\n",
    "To show why I would see many more smokers in Boston, we can simulate 10,000 days with the known rates and the number of people I meet in my hometown and Boston. Putting it in these terms shows why I would be susceptible to the availability heuristic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:28.120926Z",
     "start_time": "2018-11-02T02:22:28.109502Z"
    }
   },
   "outputs": [],
   "source": [
    "n_samples = 10000\n",
    "people_meet_peoria = 50\n",
    "peoria_see = np.random.binomial(1, p_p, size = (n_samples, people_meet_peoria))\n",
    "peoria_smokers = np.sum(peoria_see, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.078006Z",
     "start_time": "2018-11-02T02:22:28.122301Z"
    }
   },
   "outputs": [],
   "source": [
    "people_meet_boston = 5000\n",
    "boston_see = np.random.binomial(1, p_p, size = (n_samples, people_meet_boston))\n",
    "boston_smokers = np.sum(boston_see, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.520301Z",
     "start_time": "2018-11-02T02:22:29.079488Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47764 characters]\n",
      "text/plain": [
       "<Figure size 1440x576 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize = (20, 8))\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.xlabel('Number of Smokers Seen'); plt.title('Hometown'); plt.ylabel('Days');\n",
    "plt.hist(peoria_smokers, bins = 20, edgecolor = 'k');\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.hist(boston_smokers, bins = 20, edgecolor = 'k');\n",
    "plt.xlabel('Number of Smokers Seen'); plt.title('Boston'); plt.ylabel('Days');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Costs\n",
    "\n",
    "Out of curiousity, I wanted to calculate the economic costs of smoking and compare these to the revenues from taxes. The following data was used.\n",
    "\n",
    "\n",
    "* Economic Costs: https://www.cdc.gov/tobacco/data_statistics/fact_sheets/economics/econ_facts/index.htm\n",
    "     * \\$326 billion = 170 billion for direct medical care and 156 billion in lost productivity\n",
    "     * Number of smokers = 37.8 million: https://www.cdc.gov/tobacco/data_statistics/fact_sheets/adult_data/cig_smoking/index.htm\n",
    "     * Cost per smoker = 326 billion / 37.8 million = \\$8624\n",
    "* Taxes: \n",
    "    * \\$1.98 / pack in Illinois: https://www.salestaxhandbook.com/illinois/tobacco\n",
    "    * \\$3.51 / pack in Massachusetts: https://www.mass.gov/info-details/dor-cigarette-and-tobacco-excise-tax\n",
    "    * Assume 1 pack / smoker / day\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.524862Z",
     "start_time": "2018-11-02T02:22:29.521785Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8624.338624338625"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_cost = 326_000_000_000\n",
    "n_smokers = 37_800_000\n",
    "cost_per_smoker = total_cost / n_smokers\n",
    "cost_per_smoker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.528746Z",
     "start_time": "2018-11-02T02:22:29.526236Z"
    }
   },
   "outputs": [],
   "source": [
    "n_packs = 1\n",
    "days = 365 \n",
    "\n",
    "p_packtax = 1.98\n",
    "b_packtax = 3.51"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.534377Z",
     "start_time": "2018-11-02T02:22:29.530098Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tax revenue in Peoria from smoking: $12,069,866.90.\n",
      "Tax revenue in Boston from smoking: $99,293,542.41.\n"
     ]
    }
   ],
   "source": [
    "p_revenue = p_smokers * p_packtax * n_packs * days\n",
    "b_revenue = b_smokers * b_packtax * n_packs * days\n",
    "\n",
    "print(f'Tax revenue in Peoria from smoking: ${p_revenue:,.2f}.')\n",
    "print(f'Tax revenue in Boston from smoking: ${b_revenue:,.2f}.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.538267Z",
     "start_time": "2018-11-02T02:22:29.535708Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cost to Peoria from smoking: $144,035,726.19.\n",
      "Cost to Boston from smoking: $668,415,980.11.\n"
     ]
    }
   ],
   "source": [
    "p_cost = cost_per_smoker * p_smokers\n",
    "b_cost = cost_per_smoker * b_smokers\n",
    "\n",
    "print(f'Cost to Peoria from smoking: ${p_cost:,.2f}.')\n",
    "print(f'Cost to Boston from smoking: ${b_cost:,.2f}.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.769525Z",
     "start_time": "2018-11-02T02:22:29.539555Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32024 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = pd.DataFrame({'value': [b_cost, p_cost, b_revenue, p_revenue],\n",
    "                     'metric': ['cost', 'cost', 'revenue', 'revenue'],\n",
    "                     'city': ['Boston', 'Peoria', 'Boston', 'Peoria']})\n",
    "\n",
    "plt.figure(figsize = (10, 8))\n",
    "\n",
    "ax = sns.barplot(x = 'city', y = 'value', hue = 'metric', palette= {'cost': 'red', 'revenue': 'black'}, \n",
    "                 data = data)\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.yaxis.get_ticklocs()]\n",
    "plt.legend(prop = {'size': 20})\n",
    "plt.title('Smoking Cost vs Revenue')\n",
    "plt.ylabel(None)\n",
    "ax.yaxis.set_ticklabels(tick_labels);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Break Even Point for Tax Per Pack\n",
    "\n",
    "To figure out how high taxes should be to make revenue equal to cost, we can find the point where $\\text{cost} == \\text{revenue}$\n",
    "\n",
    "$$\\text{tax_required} = \\frac{\\text{cost_per_smoker}}{\\text{days} * \\text{n_packs}}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:29.773130Z",
     "start_time": "2018-11-02T02:22:29.770854Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Breaking even requires a tax of $23.63 per pack.\n"
     ]
    }
   ],
   "source": [
    "tax_required = cost_per_smoker / (days * n_packs)\n",
    "\n",
    "print(f'Breaking even requires a tax of ${tax_required:,.2f} per pack.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Revenue vs Tax with Constant Smoking Rate\n",
    "\n",
    "This plot shows the revenue versus tax per pack with no decrease in the number of smokers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:30.040990Z",
     "start_time": "2018-11-02T02:22:29.774484Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 60564 characters]\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "tax =  np.arange(0, 25, 0.1)\n",
    "revenue = [b_smokers * n_packs * days * x for x in tax]\n",
    "cost = [b_smokers * cost_per_smoker for _ in range(len(revenue))]\n",
    "\n",
    "plt.figure(figsize = (10, 8))\n",
    "plt.plot(tax, revenue, label = 'revenue', color = 'black')\n",
    "plt.axhline(y = cost[0], label = 'cost', color = 'red')\n",
    "plt.plot(tax_required, cost[0], marker = '*', ms = 50, color = 'gold', label = 'break even');\n",
    "plt.legend(prop = {'size': 18})\n",
    "ax = plt.gca()\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.yaxis.get_ticklocs()]\n",
    "ax.yaxis.set_ticklabels(tick_labels);\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.xaxis.get_ticklocs()]\n",
    "ax.xaxis.set_ticklabels(tick_labels);\n",
    "plt.xlabel(\"Tax Per Pack\"); plt.ylabel(None); plt.title('Cost and Revenue vs Tax with Constant Smoking Rate');\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In reality, the number of smokers declines as the tax per pack increases. We can account for this affect by finding the difference in smoking rates in Boston and Peoria and comparing this to the difference in tax rates between Boston and Peoria (assuming these two cities are indicative of the nation)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:30.246708Z",
     "start_time": "2018-11-02T02:22:30.042490Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For every $1.00 increase in revenue, there is a -2.29% decline in smokers.\n",
      "There are 0 smokers at a tax of $10.50 per pack.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39376 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "decline = (p_p - b_p) / (p_packtax - b_packtax)\n",
    "print(f'For every $1.00 increase in revenue, there is a {100 * decline:.2f}% decline in smokers.')\n",
    "rb_smokers = [b_adults * (b_p + (decline * (x - b_packtax))) for x in tax]\n",
    "rb_smokers = rb_smokers[:np.argmax(np.where(np.array(rb_smokers) > 0))]\n",
    "rtax =  tax[:len(rb_smokers)]\n",
    "\n",
    "print(f'There are 0 smokers at a tax of ${max(rtax):,.2f} per pack.')\n",
    "\n",
    "rrevenue = [rb_smokers[i] * n_packs * days * x for i, x in enumerate(rtax)]\n",
    "rcost = [rb_smokers[i] * cost_per_smoker for i in range(len(rrevenue))]\n",
    "\n",
    "\n",
    "plt.figure(figsize = (8, 6))\n",
    "plt.plot(rtax, rb_smokers, label = 'Number of Smokers')\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.xaxis.get_ticklocs()]\n",
    "ax.xaxis.set_ticklabels(tick_labels);\n",
    "plt.xlabel('Tax Per Pack'); plt.ylabel('Number of Smokers');\n",
    "plt.title('Number of Smokers vs Tax Per Pack');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This seems reasonable. What it means, is that at a certain point, there are no more smokers because of the tax per pack (probably not realistic)!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Revenue vs Tax Rate with Declining Rate of Smoking\n",
    "\n",
    "Now we can make the same plot accounting for the declining rate in smokers to see if there is a break-even point in this situation. This is a very simple model but can still be useful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-02T02:22:30.567805Z",
     "start_time": "2018-11-02T02:22:30.248241Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 65196 characters]\n",
      "text/plain": [
       "<Figure size 864x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize = (12, 10))\n",
    "\n",
    "plt.plot(rtax, rrevenue, label = 'Revenue', color = 'black')\n",
    "plt.plot(rtax, rcost, label = 'Cost', color = 'red')\n",
    "plt.legend(prop = {'size': 18})\n",
    "ax = plt.gca()\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.yaxis.get_ticklocs()]\n",
    "ax.yaxis.set_ticklabels(tick_labels);\n",
    "tick_labels = [f'${x:,.2f}' for x in ax.xaxis.get_ticklocs()]\n",
    "ax.xaxis.set_ticklabels(tick_labels);\n",
    "\n",
    "plt.ylabel(None); plt.xlabel('Tax Per Pack');\n",
    "plt.title('Revenue and Cost Versus Tax Per Pack with Declining Smoking Rate');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conclusions \n",
    "\n",
    "In the final plot, we see there is no break even point in terms of cost to society versus revenue from smoking. On the whole, we can conclude two things:\n",
    "\n",
    "* Smoking rates are significantly higher in my home town\n",
    "* Smoking is a negative to society \n",
    "\n",
    "Hopefully, this notebook has given you a small idea of what you can do by playing with the data yourself. Looking at facts is one thing, but trying to see how they are important in your life can make them stand out. \n",
    "\n",
    "![](images/national_smoking_time.png)\n",
    "(Source: https://news.gallup.com/poll/237908/smoking-rate-hits-new-low.aspx)\n",
    "\n",
    "National smoking rates over time have clearly declined! Just more evidence the world is improving. (Relevant Book: [Enlightenment Now](https://www.penguinrandomhouse.com/books/317051/enlightenment-now-by-steven-pinker/9780525427575/))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
