{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd7359f8-9382-4897-87c7-ed50e6c1ae74",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f626ed14db5b7eda",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Gruppenprojekt Psychologie - Analysing Extramarital Affairs Data\n",
    "\n",
    "<div style=\"display:flex;\">\n",
    "    <div style=\"text-align: left\">\n",
    "        Willkommen zum Gruppenprojekt Psychologie - Programmierübung Einführung in Python 3.\n",
    "    </div>\n",
    "    <img style=\"float: right; margin: 0px 15px 15px 0px\" src=\"https://www.python.org/static/img/python-logo-large.c36dccadd999.png?1576869008\" width=\"100\" />\n",
    "</div>\n",
    "\n",
    "Wenn Sie Fragen oder Verbesserungsvorschläge zum Inhalt oder Struktur der Notebooks haben, dann können sie eine E-Mail an Phil Keier ([p.keier@hbk-bs.de](mailto:p.keier@hbk-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) oder Martin Le ([martin.le@tu-bs.de](mailto:martin.le@tu-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) schreiben.\n",
    "\n",
    "Link zu einem Python Spickzettel: [hier](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/PythonForDataScience.pdf)\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c02f55d-1ce9-4f76-b148-de90e475e146",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f88646e5559e3089",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## About Dataset\n",
    "\n",
    "Enclosed with this notebook is the dataset `affairs.csv`. Collected in 1969 by means of a survey by the magazine *Psychology Today*.\n",
    "\n",
    "The significance of the data collected is shown in the following table:\n",
    "\n",
    "| Variable      | Description                                                                                                                                                                                                     |\n",
    "|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n",
    "| affairs       | How often engaged in extramarital sexual intercourse during the past year?                                                                                                                                      |\n",
    "| gender        | Gender                                                                                                                                                                                                          |\n",
    "| age           | Age in years. Coding: 17.5 = under 20, 22 = 20–24, 27 = 25–29, 32 = 30–34, 37 = 35–39, 42 = 40–44, 47 = 45–49, 52 = 50–54, 57 = 55 or over.                                                                     |\n",
    "| yearsmarried  | Number of years married. Coding: 0.125 = 3 months or less, 0.417 = 4–6 months, 0.75 = 6 months–1 year, 1.5 = 1–2 years, 4 = 3–5 years, 7 = 6–8 years, 10 = 9–11 years, 15 = 12 or more years.                   |\n",
    "| children      | Are there children in the marriage?                                                                                                                                                                             |\n",
    "| religiousness | Religiousness level. Coding: 1 = anti, 2 = not at all, 3 = slightly, 4 = somewhat, 5 = very.                                                                                                                    |\n",
    "| education     | Level of education. Coding: 9 = grade school, 12 = high school graduate,  14 = some college, 16 = college graduate, 17 = some graduate work, 18 =  master's degree, 20 = Ph.D., M.D., or other advanced degree. |\n",
    "| occupation    | Occupation according to [Hollingshead classification](http://fcon_1000.projects.nitrc.org/indi/enhanced/assessments/ses-child.html) (reverse numbering)                                                                                                                                         |\n",
    "| rating        | Self-rating of marriage. Coding: 1 = very unhappy, 2 = somewhat unhappy, 3 = average, 4 = happier than average, 5 = very happy.                                                                                 |\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0eccb03-5480-4512-b7c6-cc2c5ae355b6",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3bdfe5a3bd2f8017",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Your Task\n",
    "\n",
    "Prove / Disprove the following 5 Hypothesis:\n",
    "\n",
    "1. __Women cheat more often than men.__\n",
    "2. __People who are not religious have more affairs.__\n",
    "3. __As long as men have a happy marriage, they have fewer affairs.__\n",
    "4. __Educated persons rate their marriage higher.__\n",
    "5. __People with a high income have fewer children, are not religious and have more affairs.__\n",
    "\n",
    "You're free to alter the dataset through your needs.\n",
    "\n",
    "Explain your approach for each thesis, find a suitable presentation and interpret your result in context.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12431fef-aa6d-4eff-b8c3-8911c99d3b91",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-645b377cd74cf1b6",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "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>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>affairs</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>gender</th>\n",
       "      <td>male</td>\n",
       "      <td>female</td>\n",
       "      <td>female</td>\n",
       "      <td>male</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>age</th>\n",
       "      <td>37.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>yearsmarried</th>\n",
       "      <td>10.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>children</th>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>religiousness</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>education</th>\n",
       "      <td>18</td>\n",
       "      <td>14</td>\n",
       "      <td>12</td>\n",
       "      <td>18</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>occupation</th>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rating</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  0       1       2     3     4\n",
       "affairs           0       0       0     0     0\n",
       "gender         male  female  female  male  male\n",
       "age            37.0    27.0    32.0  57.0  22.0\n",
       "yearsmarried   10.0     4.0    15.0  15.0  0.75\n",
       "children         no      no     yes   yes    no\n",
       "religiousness     3       4       1     5     2\n",
       "education        18      14      12    18    17\n",
       "occupation        7       6       1     6     6\n",
       "rating            4       4       4     5     3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Here you can Import everthing you need\n",
    "### BEGIN SOLUTION\n",
    "\n",
    "# Link to Dataset: https://www.kaggle.com/datasets/utkarshx27/fairs-extramarital-affairs-data\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('https://github.com/dhaitz/matplotlib-stylesheets/raw/master/pitayasmoothie-dark.mplstyle') # Using my favourite Style\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "df = pd.read_csv(\"affairs.csv\")\n",
    "df.head().T\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11c2c2fc-0beb-4f11-aa39-bd92dd65bea4",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ed301460b403c979",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 1 - Women cheat more often than men"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8dad5720-10e8-49fe-8a75-7bd5c697ff18",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-4062eab5a30510d9",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23668 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Filter \n",
    "male = df.loc[df['gender'] == \"male\"]['affairs'].sum()\n",
    "female = df.loc[df['gender'] == \"female\"]['affairs'].sum()\n",
    "\n",
    "# All\n",
    "all = df['affairs'].sum()\n",
    "\n",
    "# Calc relatives\n",
    "rel_male = male / all\n",
    "rel_female = female / all\n",
    "\n",
    "# Check\n",
    "assert rel_male + rel_female == 1.0\n",
    "\n",
    "plt.title(\"Relative distribution of Gender Affairs\")\n",
    "plt.pie([rel_male, rel_female], labels=[\"Male\", \"Female\"], autopct='%1.1f%%', startangle=90)\n",
    "plt.show()\n",
    "\n",
    "# As shown: the Females have more affairs the then males\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f283bb5b-114c-4d1b-8866-bf339b52a073",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-1a3888f580ab1d8d",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "10cc7841-8437-48cf-9889-76680c5e4455",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-5ec94944d1291488",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 2 - People who are not religious have more affairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "e6f48473-8741-4739-8004-39405d4a2967",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-6d99761f45b1f44f",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30932 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Filter religious People (3 or higher) and non religious People (2 or lower) and count the affairs they have\n",
    "reli = df.loc[df['religiousness'] > 2]['affairs'].sum()\n",
    "anti = df.loc[df['religiousness'] < 3]['affairs'].sum()\n",
    "\n",
    "# All Affairs\n",
    "all = df['affairs'].sum()\n",
    "\n",
    "# Calc relative Affairs\n",
    "rel_reli = reli.sum() / all\n",
    "rel_anti = anti.sum() / all\n",
    "\n",
    "# Check\n",
    "assert rel_reli + rel_anti == 1.0\n",
    "\n",
    "# Plot\n",
    "plt.title(\"Relative distribution of religious Affairs\")\n",
    "plt.pie([rel_reli, rel_anti], labels=[\"Religious (3 or higher)\", \"Non-religious (2 or lower)\"], autopct='%1.1f%%', startangle=90)\n",
    "plt.show()\n",
    "\n",
    "# As shown: Non-religious People have less affairs than Religious\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d070b5-9de5-4a3a-828e-183e6ea4a28f",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b686dfdb0eeee32d",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "cd952f86-b978-430b-ab26-fe9a552bc0de",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-571621a9b96d835e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 3 - As long as men have a happy marriage, they have fewer affairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "6907e8a1-9701-4d23-86d5-a85d8cffe946",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-c9b83b08f5101afb",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18064 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 32080 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "\n",
    "# Filter Gender\n",
    "male = df.loc[df['gender'] == \"male\"]\n",
    "\n",
    "colors = [\"purple\", \"magenta\", \"blue\", \"cyan\", \"red\"]\n",
    "colors.reverse()\n",
    "\n",
    "plt.title(\"Men Marriage Happyness\")\n",
    "plt.bar(male[\"rating\"].value_counts().keys(), male[\"rating\"].value_counts()/male[\"rating\"].value_counts().sum(), color=colors)\n",
    "plt.show()\n",
    "\n",
    "happy = male.loc[male['rating'] > 3]\n",
    "sad = male.loc[male['rating'] < 4]\n",
    "\n",
    "plt.title(\"Men Affairs in Marriage\")\n",
    "plt.pie([happy['affairs'].sum(), sad['affairs'].sum()],\n",
    "        labels=[\"Happy Marriage (Rating above 3)\", \"Sad Marriage (Rating lower 2)\"],\n",
    "        autopct='%1.1f%%', startangle=90)\n",
    "plt.show()\n",
    "\n",
    "# As shown: Men in happier Mariagges have more Affairs\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "417bd98c-a41c-4e52-808e-0c4c65f6f119",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-398bd0934015aef0",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9175418f-4304-4ab3-be16-2ddc87297c78",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c6e827a96111dbcf",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 4 - Educated persons rate their marriage higher"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "id": "3c52f118-c9e3-4402-b9d0-397ee088d86b",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-68f5ecbc2a8a9a29",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21292 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 17136 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 32892 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Analyse Education Distribution\n",
    "values = df['education'].value_counts()\n",
    "labels = df['education'].value_counts().keys()\n",
    "\n",
    "plt.title(\"Education Distribution\")\n",
    "plt.barh(labels, values)\n",
    "plt.yticks(labels, labels)\n",
    "plt.ylabel(\"Level of Education\")\n",
    "plt.xlabel(\"Number of people\")\n",
    "plt.show()\n",
    "\n",
    "# Analyse Marriage Happyness\n",
    "values = df['rating'].value_counts()\n",
    "labels = df['rating'].value_counts().keys()\n",
    "\n",
    "plt.title(\"Marriage Distribution\")\n",
    "plt.barh(labels, values)\n",
    "plt.yticks(labels, labels)\n",
    "plt.ylabel(\"Happyness\")\n",
    "plt.xlabel(\"Number of people\")\n",
    "plt.show()\n",
    "\n",
    "# Getting the mean rating per education level\n",
    "# This is a true abomination pls don't try this at home\n",
    "mean_rating_per_edu = {\n",
    "    str(i): df.loc[df[\"education\"] == i]['rating'].mean()\n",
    "    for i in df['education'].value_counts().keys().sort_values()\n",
    "}\n",
    "\n",
    "cum_mean_rating_per_edu = np.array([*mean_rating_per_edu.values()]).sum()\n",
    "\n",
    "rel_mean_rating_per_edu = {\n",
    "    key: value / cum_mean_rating_per_edu *100\n",
    "    for key, value in mean_rating_per_edu.items()\n",
    "}\n",
    "\n",
    "# Analyse the relationship\n",
    "plt.title(\"Mean Rating per level of Education\")\n",
    "plt.plot(mean_rating_per_edu.keys(), rel_mean_rating_per_edu.values(), \"-o\")\n",
    "plt.xlabel(\"Level of Education\")\n",
    "plt.yticks(np.arange(0,19,2))\n",
    "plt.ylabel(\"Mean Marriage Rating (in %)\")\n",
    "plt.show()\n",
    "\n",
    "# Conclusion: Sort of\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b1593e6-3d2a-4e63-83cc-f88f8cf9ffa9",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-f3005123d89aa194",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6ad22099-c134-4e4c-87e1-9f68c8645499",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-59b21027ba8e6bfc",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Hypothesis 5 - People with a high income have fewer children, are not religious and have more affairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 458,
   "id": "a5b960d9-df2c-4b2a-bf04-0419b15f86bc",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-928c0be71a47530a",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29564 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 26356 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52532 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22068 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Following the Hollingshead Index\n",
    "# 7 = Graduate / Professional Training\n",
    "# 6 = Standard College or University\n",
    "# 5 = Partial College\n",
    "# 4 = High School Graduate\n",
    "# 3 = Partial High School\n",
    "# 2 = Junior High School\n",
    "# 1 = Less than 7th Grade\n",
    "\n",
    "# Append Correct Orderd occupation values to DF\n",
    "occ = {\"occupation unreversed\": []}\n",
    "\n",
    "for v in df['occupation']:\n",
    "    match v:\n",
    "        case 1:\n",
    "            occ[\"occupation unreversed\"].append(7)\n",
    "        case 2:\n",
    "            occ[\"occupation unreversed\"].append(6)\n",
    "        case 3:\n",
    "            occ[\"occupation unreversed\"].append(5)\n",
    "        case 4:\n",
    "            occ[\"occupation unreversed\"].append(4)\n",
    "        case 5:\n",
    "            occ[\"occupation unreversed\"].append(3)\n",
    "        case 6:\n",
    "            occ[\"occupation unreversed\"].append(2)\n",
    "        case 7:\n",
    "            occ[\"occupation unreversed\"].append(1)\n",
    "        case _:\n",
    "            raise \"Unexpected Value\"\n",
    "\n",
    "df[\"occupation unreversed\"] = occ[\"occupation unreversed\"]\n",
    "\n",
    "# Analyse Distribution \n",
    "plt.title(\"Socioeconomic Status - Hollingshead Index\")\n",
    "plt.bar(df[\"occupation unreversed\"].value_counts().keys(), df['occupation unreversed'].value_counts())\n",
    "plt.xlabel(\"Socioeconomic Status (Higher = More Income)\")\n",
    "plt.ylabel(\"Number of People\")\n",
    "plt.show()\n",
    "\n",
    "# Filter High Income Peoples; Assuming High Income > 3\n",
    "high_income = df.loc[df['occupation unreversed'] > 3]\n",
    "low_income = df.loc[df['occupation unreversed'] < 4]\n",
    "\n",
    "# Count Childrens\n",
    "hi_children = high_income.loc[high_income['children'] == 'yes']['children'].value_counts()\n",
    "li_children = low_income.loc[low_income['children'] == 'yes']['children'].value_counts()\n",
    "\n",
    "# Analyse Children Distribution\n",
    "plt.title(\"Children Distribution against parental income\")\n",
    "plt.pie([hi_children[0], li_children[0]], labels=['High Income', 'Low Income'], autopct='%1.1f%%', startangle=90)\n",
    "plt.show()\n",
    "# High Income People have less Children\n",
    "\n",
    "# Analyse Religiousness\n",
    "labels = high_income['religiousness'].value_counts().keys().sort_values()\n",
    "all = df['occupation unreversed'].value_counts().sum()\n",
    "plt.title(\"Relative Religiousness per Income Group\")\n",
    "plt.plot(labels, high_income['religiousness'].value_counts() / all, \"-o\", label=\"High Income\")\n",
    "plt.plot(labels, low_income['religiousness'].value_counts() / all, \"-o\", label=\"Low Income\")\n",
    "plt.xticks(np.arange(1,6))\n",
    "plt.xlabel(\"Religiousness (Higher means More)\")\n",
    "plt.ylabel(\"Relative Percentage\")\n",
    "plt.legend()\n",
    "plt.show()\n",
    "# High Income People are less Religious\n",
    "\n",
    "# Analyse Affairs\n",
    "hi_affairs = high_income['affairs'].value_counts().sum() / all\n",
    "li_affairs = low_income['affairs'].value_counts().sum() / all\n",
    "assert hi_affairs + li_affairs == 1.0\n",
    "\n",
    "plt.title(\"Affairs per Income\")\n",
    "plt.pie([hi_affairs, li_affairs], labels=['High Income', 'Low Income'], autopct='%1.1f%%', startangle=90)\n",
    "plt.show()\n",
    "\n",
    "# Pearson Test Correlation between the 3 Parameters\n",
    "import seaborn as sns\n",
    "children = [1 if v == 'yes' else 0 for v in high_income['children']]\n",
    "high_income['children normalized'] = children\n",
    "corr = high_income[['children normalized', 'religiousness', 'affairs']].corr('pearson')\n",
    "plt.title(\"Pearson Correlation Test\")\n",
    "sns.heatmap(corr, annot=True)\n",
    "plt.show()\n",
    "\n",
    "# No Linear Correlation between Parameters\n",
    "### END SOLUTIOn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1337ccda-cda9-4937-a762-4ec9fb9f2abd",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-7783f04ac192c079",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "source": []
  }
 ],
 "metadata": {
  "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.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
