{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "worldwide-blood",
   "metadata": {},
   "source": [
    "# Introduction"
   ]
  },
  {
   "attachments": {
    "c4fa95ea-25d4-48b7-a126-87f7afdc8a6d.png": {
     "image/png": "[encoded data, 18352 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "understanding-numbers",
   "metadata": {},
   "source": [
    "The graph below shows a calculated probability of two people sharing a birthday given a sample number of people gathered together.\n",
    "\n",
    "![Birthday_Paradox.svg.png](attachment:c4fa95ea-25d4-48b7-a126-87f7afdc8a6d.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fd09807-f045-49e4-aac1-2442e9e42b63",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "greater-circular",
   "metadata": {},
   "source": [
    "## Overarching Question: What is the birthday paradox and how can we see it in action?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "appreciated-testimony",
   "metadata": {},
   "source": [
    "**Why this question?**\n",
    "\n",
    "The birthday \"paradox\" states that in a random group of 23 people, there is about a 50 percent chance that two people have the same birthday. In reality, the \"paradoxical\" nature of this problem comes from it being counterintuituve more than a true paradox. The percent chance obviously goes up with a larger group of people, but given that there are 365 days in a year, it was surprising to me that a group of just 75 people has a 99.9% chance to include two people with the same birthday.\n",
    "\n",
    "With information being provided that, to me, seems that striking, I felt that I had to find the pattern that must be the backbone of this phenomenon. If I can gather birth data over the course of a year, maybe there is some pattern, some \"most likely\" date of birth that forces this commonality among a group of people."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "permanent-pollution",
   "metadata": {},
   "source": [
    "# Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "technical-evans",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#Include any import statements you will need\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "overhead-sigma",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "file_name = \"US_births_2000-2014_SSA.csv\"\n",
    "dataset_path = \"data/\" + file_name\n",
    "\n",
    "df = pd.read_csv(dataset_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "heated-blade",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>date_of_month</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>births</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>9083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>8006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>11363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>13032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>12558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year  month  date_of_month  day_of_week  births\n",
       "0  2000      1              1            6    9083\n",
       "1  2000      1              2            7    8006\n",
       "2  2000      1              3            1   11363\n",
       "3  2000      1              4            2   13032\n",
       "4  2000      1              5            3   12558"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "continental-franklin",
   "metadata": {},
   "source": [
    "**What is this data about?**\n",
    "\n",
    "This dataset includes birth data for the dates 1/1/2000 through 12/31/2014. The column data presents the specific date day, month and year, as well as the day of the week (presented as a number 1-7, with 1 being Monday and 7 being Sunday) and the number of births occurring on that given date. The specific day of the week is included in the dataset because FiveThirtyEight (the source of the dataset) was using this particular column in an article about few babies being born on Friday the 13th than expected statistically, but will not be of particular use here.\n",
    "\n",
    "**Where is this data from?**\n",
    "\n",
    "The data is available through FiveThirtyEight, but the original data pulled comes from the United States' CDC, NCHS, and SSA. Generally, FiveThirtyEight has a repuation for being center/neutral politically and in this case, there isn't a whole lot of bias with the specific data presented. The data itself is coming from reporting numbers through various government agencies tracking birth data in the US and while it may not accound for some somehow off-the-grid birth occurrances, any baby with a birth certificate should be accounted for in this dataset."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infinite-instrument",
   "metadata": {},
   "source": [
    "# Methods and Results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "recognized-positive",
   "metadata": {},
   "source": [
    "## First Research Question: Is there a pattern of seasonality in data showing the number of births each day over a given year?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "graduate-palmer",
   "metadata": {},
   "source": [
    "### Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "endless-variation",
   "metadata": {},
   "source": [
    "In order to looks for specific patterns, including seasonality, of births throughout the year, we'll need to plot the births on a given day over the course of the year. We have access to the total births on each day in the year 2000, as well as to which day of the week (Monday-Friday) those dates correspond.\n",
    "\n",
    "We'll start by looking for any patterns in one given year's (2000) worth of data before moving on to looking for these patterns (if they exist) showing up consistently over multiple years and/or changing in some way.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "portuguese-japan",
   "metadata": {},
   "source": [
    "### Results "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "19412c8d-414f-47e7-8366-493c3426a744",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>date_of_month</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>births</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>9083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>8006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>11363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>13032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>12558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>361</th>\n",
       "      <td>2000</td>\n",
       "      <td>12</td>\n",
       "      <td>27</td>\n",
       "      <td>3</td>\n",
       "      <td>13252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>2000</td>\n",
       "      <td>12</td>\n",
       "      <td>28</td>\n",
       "      <td>4</td>\n",
       "      <td>13900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>2000</td>\n",
       "      <td>12</td>\n",
       "      <td>29</td>\n",
       "      <td>5</td>\n",
       "      <td>13607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>2000</td>\n",
       "      <td>12</td>\n",
       "      <td>30</td>\n",
       "      <td>6</td>\n",
       "      <td>9405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>365</th>\n",
       "      <td>2000</td>\n",
       "      <td>12</td>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>7892</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>366 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     year  month  date_of_month  day_of_week  births\n",
       "0    2000      1              1            6    9083\n",
       "1    2000      1              2            7    8006\n",
       "2    2000      1              3            1   11363\n",
       "3    2000      1              4            2   13032\n",
       "4    2000      1              5            3   12558\n",
       "..    ...    ...            ...          ...     ...\n",
       "361  2000     12             27            3   13252\n",
       "362  2000     12             28            4   13900\n",
       "363  2000     12             29            5   13607\n",
       "364  2000     12             30            6    9405\n",
       "365  2000     12             31            7    7892\n",
       "\n",
       "[366 rows x 5 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "years = {}\n",
    "for x in range(2000, 2015):\n",
    "    years[\"births{0}\".format(x)] = df[df.year == x]\n",
    "    \n",
    "years['births2000']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "0d65dec4-1240-47b5-a3f5-620fa85de935",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 52460 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Set the locator\n",
    "locator = mdates.MonthLocator()  # every month\n",
    "# Specify the format - %b gives us Jan, Feb...\n",
    "fmt = mdates.DateFormatter('%b')\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.hist(range(1, 367), bins=366, weights=years['births2000'].births, edgecolor='black', alpha=0.7)\n",
    "X = plt.gca().xaxis\n",
    "X.set_major_locator(locator)\n",
    "# Specify formatter\n",
    "X.set_major_formatter(fmt)\n",
    "plt.xticks(range(1, 366, 30))\n",
    "plt.xlabel('Month')\n",
    "plt.ylabel('Number of Births')\n",
    "plt.title('Distribution of Baby Births on Each Day of the Year 2000')\n",
    "\n",
    "plt.grid(True)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "cf904b01-a138-4c95-be78-0081ec1b4429",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "</style>\n",
       "<table id=\"T_7a8b2\">\n",
       "  <caption>Births by Day, Year 2000</caption>\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"blank level0\" >&nbsp;</th>\n",
       "      <th id=\"T_7a8b2_level0_col0\" class=\"col_heading level0 col0\" >BIRTHS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th class=\"index_name level0\" >day_of_week</th>\n",
       "      <th class=\"blank col0\" >&nbsp;</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row0\" class=\"row_heading level0 row0\" >Monday</th>\n",
       "      <td id=\"T_7a8b2_row0_col0\" class=\"data row0 col0\" >598752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row1\" class=\"row_heading level0 row1\" >Tuesday</th>\n",
       "      <td id=\"T_7a8b2_row1_col0\" class=\"data row1 col0\" >669284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row2\" class=\"row_heading level0 row2\" >Wednesday</th>\n",
       "      <td id=\"T_7a8b2_row2_col0\" class=\"data row2 col0\" >663638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row3\" class=\"row_heading level0 row3\" >Thursday</th>\n",
       "      <td id=\"T_7a8b2_row3_col0\" class=\"data row3 col0\" >662244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row4\" class=\"row_heading level0 row4\" >Friday</th>\n",
       "      <td id=\"T_7a8b2_row4_col0\" class=\"data row4 col0\" >651274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row5\" class=\"row_heading level0 row5\" >Saturday</th>\n",
       "      <td id=\"T_7a8b2_row5_col0\" class=\"data row5 col0\" >479641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_7a8b2_level0_row6\" class=\"row_heading level0 row6\" >Sunday</th>\n",
       "      <td id=\"T_7a8b2_row6_col0\" class=\"data row6 col0\" >424765</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fafa982df30>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "days_chart = pd.DataFrame(years['births2000'].groupby(\"day_of_week\").births.sum())\n",
    "\n",
    "days_chart.style \\\n",
    "  .format_index(str.upper, axis=1) \\\n",
    "  .set_caption(\"Births by Day, Year 2000\") \\\n",
    "  .relabel_index([\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"], axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "9cef60d9-4fe1-4467-ad22-aacc638222d4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "</style>\n",
       "<table id=\"T_b8213\">\n",
       "  <caption>Births by Month, Year 2000</caption>\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"blank level0\" >&nbsp;</th>\n",
       "      <th id=\"T_b8213_level0_col0\" class=\"col_heading level0 col0\" >BIRTHS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th class=\"index_name level0\" >month</th>\n",
       "      <th class=\"blank col0\" >&nbsp;</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row0\" class=\"row_heading level0 row0\" >January</th>\n",
       "      <td id=\"T_b8213_row0_col0\" class=\"data row0 col0\" >337739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row1\" class=\"row_heading level0 row1\" >February</th>\n",
       "      <td id=\"T_b8213_row1_col0\" class=\"data row1 col0\" >324046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row2\" class=\"row_heading level0 row2\" >March</th>\n",
       "      <td id=\"T_b8213_row2_col0\" class=\"data row2 col0\" >347824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row3\" class=\"row_heading level0 row3\" >April</th>\n",
       "      <td id=\"T_b8213_row3_col0\" class=\"data row3 col0\" >323357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row4\" class=\"row_heading level0 row4\" >May</th>\n",
       "      <td id=\"T_b8213_row4_col0\" class=\"data row4 col0\" >347950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row5\" class=\"row_heading level0 row5\" >June</th>\n",
       "      <td id=\"T_b8213_row5_col0\" class=\"data row5 col0\" >347899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row6\" class=\"row_heading level0 row6\" >July</th>\n",
       "      <td id=\"T_b8213_row6_col0\" class=\"data row6 col0\" >356131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row7\" class=\"row_heading level0 row7\" >August</th>\n",
       "      <td id=\"T_b8213_row7_col0\" class=\"data row7 col0\" >367893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row8\" class=\"row_heading level0 row8\" >September</th>\n",
       "      <td id=\"T_b8213_row8_col0\" class=\"data row8 col0\" >355981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row9\" class=\"row_heading level0 row9\" >October</th>\n",
       "      <td id=\"T_b8213_row9_col0\" class=\"data row9 col0\" >352368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row10\" class=\"row_heading level0 row10\" >November</th>\n",
       "      <td id=\"T_b8213_row10_col0\" class=\"data row10 col0\" >342494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_b8213_level0_row11\" class=\"row_heading level0 row11\" >December</th>\n",
       "      <td id=\"T_b8213_row11_col0\" class=\"data row11 col0\" >345916</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fafaa026f80>"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "months_chart = pd.DataFrame(years['births2000'].groupby(\"month\").births.sum())\n",
    "\n",
    "months_chart.style \\\n",
    "  .format_index(str.upper, axis=1) \\\n",
    "  .set_caption(\"Births by Month, Year 2000\") \\\n",
    "  .relabel_index([\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"], axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aada748-ded7-436b-b79f-3ce6952b8f5f",
   "metadata": {},
   "source": [
    "The plot above clearly shows some cyclic occurrance, which I thought might be related to the work-week. Turns out, there are upwards of 50% more babies born on a Monday through Friday day of the week than a weekend. There is also a notable (though to what extent is unclear) increase in the size of the spikes the later in the year you go, suggesting that more babies are born in the later months of the year."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "collectible-puppy",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Second Research Question: Does the pattern of seasonality shift over the course of a number of years?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "demographic-future",
   "metadata": {},
   "source": [
    "### Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "incorporate-roller",
   "metadata": {},
   "source": [
    "Having looked at the birth data over the course of a year, it would be worth looking at the same data for multiple years. The increase in baby births in July/August/September and again in November/December might be an anecdotal artifact, so checking for increases such as those might shed light on whether that pattern is consistent and seasonal or shifts around throughout the year over time. To see this, the same plots will be generated for the available data from 2000-2014."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "juvenile-creation",
   "metadata": {},
   "source": [
    "### Results "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "eb96236a-5a9c-45b9-b780-06f0c93f48ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 52460 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52204 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52648 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52772 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52592 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 53052 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 55432 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 54860 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 54800 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 54444 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52384 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52532 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 53328 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52728 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52468 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for year in years:\n",
    "    # Set the locator\n",
    "    locator = mdates.MonthLocator()  # every month\n",
    "    # Specify the format - %b gives us Jan, Feb...\n",
    "    fmt = mdates.DateFormatter('%b')\n",
    "\n",
    "    title_year = year[6:]\n",
    "    plt.figure(figsize=(10, 6))\n",
    "    if years[year].year.mean() % 4 == 0:\n",
    "        plt.hist(range(1, 367), bins=366, weights=years[year].births, edgecolor='black', alpha=0.7)\n",
    "        X = plt.gca().xaxis\n",
    "        X.set_major_locator(locator)\n",
    "        # Specify formatter\n",
    "        X.set_major_formatter(fmt)\n",
    "        plt.xticks(range(1, 366, 30))\n",
    "    else:\n",
    "        plt.hist(range(1, 366), bins=365, weights=years[year].births, edgecolor='black', alpha=0.7)\n",
    "        X = plt.gca().xaxis\n",
    "        X.set_major_locator(locator)\n",
    "        # Specify formatter\n",
    "        X.set_major_formatter(fmt)\n",
    "        plt.xticks(range(1, 365, 30))\n",
    "    plt.xlabel('Month')\n",
    "    plt.ylabel('Number of Births')\n",
    "    plt.title('Distribution of Baby Births on Each Day of the Year ' + title_year)\n",
    "    \n",
    "    plt.grid(True)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8615b8d7-84ce-4899-96de-e30e1996d080",
   "metadata": {},
   "source": [
    "The workweek peakes return in each of these plots, as expected. The late summer boom also occurs yearly, wavering only in the specific date range, but still falling in the same general season. Unfortunately, neither of these patterns seem to shed light directly on how the birthday paradox actually works."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e74d7aa-f950-47cf-8846-67dbc320fd1e",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Third Research Question: How does this data represent the birthday paradox related to the probability of two people having the same birthday?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bec7cfc-0c8f-4e61-b23f-41c059c48625",
   "metadata": {},
   "source": [
    "### Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b050efeb-803e-462c-adf4-25af965ca04a",
   "metadata": {},
   "source": [
    "The first thing to do here is to represent the birthday paradox mathematically. It is a probability problem based on the permutations of the total number of birthdays without repetitions and order matters versus the total number of birthdays with repetition and order matters. Taking the ratio of those two quantities, one can find the probability of two people having the same birthday within a group of some known size."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25be3254-44d8-48e0-89ba-913aaad798ba",
   "metadata": {},
   "source": [
    "### Results "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3c9c037-3cb0-4894-9657-2a238bdce17e",
   "metadata": {},
   "source": [
    "To find $V_{nr}$, the total number of birthdays without repetitions, we can use the formula:\n",
    "\n",
    "$$\n",
    "V_{nr} = \\frac{n!}{(n-k)!}\n",
    "$$\n",
    "\n",
    "where $n$ is the number of days in a year and $k$ is the number of people in the group being studied.\n",
    "\n",
    "For $V_{t}$, the total number of birthdays including any repetition, we use:\n",
    "\n",
    "$$\n",
    "V_{t} = n^k\n",
    "$$\n",
    "\n",
    "Then to find the probability of any two people in the group having the same birthday, we can take the ratio of these two values and subtract from $1$:\n",
    "\n",
    "$$\n",
    "P = 1 - \\frac{V_{nr}}{V_{t}}\n",
    "$$\n",
    "\n",
    "The subtraction is necessary because without it, we've actually found the probability of no two people in the group having the same birthday. This can be seen by looking at $k$ approaching $0$, where $V_{nr}$ gets smaller and smaller, forcing the ratio of $V_{nr}$ and $V_{t}$ to $0$. This is consistent with the fact that a group of 0 or 1 would have no shared birthdays, while a larger group of people would be more likely to have a shared birthday occur."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2f1490-e42a-4ff1-aeff-6dc2552be368",
   "metadata": {},
   "source": [
    "Using this method, the probability of having a shared birthday within a group of people can be seen below for a variety of group sizes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "08551a82-1060-4d84-9b1f-0cf0550267a3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def prob_shared_birthday(num_people):\n",
    "    Vnr = math.factorial(365) / math.factorial(365 - num_people)\n",
    "    Vt = 365**num_people\n",
    "    \n",
    "    return 1 - (Vnr / Vt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "380764d7-2f73-458f-b477-b6d70df1e017",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data = []\n",
    "num_people = []\n",
    "\n",
    "min_people = 0\n",
    "max_people = 100\n",
    "step_size = 2\n",
    "\n",
    "for i in range(min_people,max_people+1,step_size):\n",
    "    num_people.append(i)\n",
    "    data.append(prob_shared_birthday(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "781e0516-cfc2-425d-b2ee-1d663a7c4853",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Probability of at least 1 shared birthday in a group of people')"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 35344 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(num_people,data)\n",
    "plt.xlabel('Number of people in group')\n",
    "plt.ylabel('Probability')\n",
    "plt.title('Probability of at least 1 shared birthday in a group of people')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8cd965d-ec4c-43ed-b351-d182d954a111",
   "metadata": {},
   "source": [
    "It is interesting to note that it only takes 23 people to have a 50:50 chance of two people in the group sharing a birthday and once the group consists of about 70 people, the chance that two people share a birthday is about 99%."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infectious-symbol",
   "metadata": {},
   "source": [
    "# Discussion"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "furnished-camping",
   "metadata": {
    "code_folding": []
   },
   "source": [
    "## Considerations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bearing-stadium",
   "metadata": {},
   "source": [
    "After considering the historical birth data from 2000-2014, there are a few things to note:\n",
    "1) The frequency periodic peaks are not as unexpected as originally thought; there are 52 of them on each plot and they correspond to the 52 weeks in a year. Given that most babies are likely to be born during the typical workweek (being 5/7 of the days of a week) and any scheduled procedures also occurring during the workweek, those peaks, while shifting slightly on their date ranges year to year, are overall insignificant to the question of this study.\n",
    "\n",
    "2) The late summer/early fall increase in baby births occurs yearly, coupled by a second increase over a shorter period of time right before year's end.\n",
    "\n",
    "3) None of these observable patterns actually affect the probability in any way."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beneficial-invasion",
   "metadata": {},
   "source": [
    "## Summary"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "about-raise",
   "metadata": {},
   "source": [
    "Most surprising here was the lack of affect of the patterned data on the birthday paradox. It turns out, the reason the birthday paradox exists in the first place is the limited number of options for when a birthday can occur, not necessarily when people are specifically born.\n",
    "\n",
    "Changing the number of days in a year (for probability testing only, no changes to the current calendar have been adopted globally) from 365 to 400 reduces the probability of two people in a group of 20 sharing a birthday from about 50% to about 30%, which shows me that the available birthday options plays a huge role in why this \"paradox\" exists in the first place.\n",
    "\n",
    "Related to that finding, the reason for the \"paradox\" name does not have to do with the phenomenon being paradoxical in any way, but rather the system is counterintuitive to most, especially given that such a small group of people (23, for example) gives such a large chance (50%) of including two people with a shared birthday.\n",
    "\n",
    "As far as future impacts, this study was a test in whether I can control my own biases, as I wanted to find some answer to why the birthday paradox exists in the historical birth data that I had access to, and try as I might to find some pattern that explained what I was looking for, I had to accept the fact that no real effect existed and the system could be explained by mathematics."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_json": true,
   "text_representation": {
    "extension": ".Rmd",
    "format_name": "rmarkdown",
    "format_version": "1.2",
    "jupytext_version": "1.9.1"
   }
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "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": 5
}
