{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Gamma function\n",
    "from scipy.special import gamma\n",
    "\n",
    "# To calculate statistics\n",
    "from scipy.stats import norm\n",
    "from scipy.stats import hmean, trim_mean, iqr, median_abs_deviation, skew, kurtosis\n",
    "from scipy.stats.mstats import gmean, winsorize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reading and preprocessing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "metadata": {},
   "outputs": [],
   "source": [
    "DataWhr2024 = pd.read_csv(\"DataWhr2024.csv\")\n",
    "UnM49 = pd.read_csv(\"UnM49.csv\", sep=';')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "metadata": {},
   "outputs": [],
   "source": [
    "UnM49 = UnM49[['Country or Area', 'Sub-region Name', 'Region Name']]\n",
    "UnM49 = UnM49.rename({'Country or Area':'Country name', 'Sub-region Name':'Subregion', 'Region Name':'Continent'}, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [],
   "source": [
    "DataWhr2024.loc[DataWhr2024[\"Country name\"].str.startswith(\"Hong\"), \"Country name\"] = \"Hong Kong\"\n",
    "DataWhr2024.loc[DataWhr2024[\"Country name\"].str.startswith(\"Somaliland\"), \"Country name\"] = \"Somaliland\"\n",
    "DataWhr2024.loc[DataWhr2024[\"Country name\"].str.startswith(\"Taiwan\"), \"Country name\"] = \"Taiwan\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [],
   "source": [
    "UnM49.loc[97, \"Country name\"] = \"Bolivia\"\n",
    "UnM49.loc[33, \"Country name\"] = \"Congo (Brazzaville)\"\n",
    "UnM49.loc[34, \"Country name\"] = \"Congo (Kinshasa)\"\n",
    "UnM49.loc[124, \"Country name\"] = \"Hong Kong\"\n",
    "UnM49.loc[125, \"Country name\"] = \"Macao\"\n",
    "UnM49.loc[126, \"Country name\"] = \"North Korea\"\n",
    "UnM49.loc[145, \"Country name\"] = \"Iran\"\n",
    "UnM49.loc[46, \"Country name\"] = \"Ivory Coast\"\n",
    "UnM49.loc[133, \"Country name\"] = \"Laos\"\n",
    "UnM49.loc[129, \"Country name\"] = \"South Korea\"\n",
    "UnM49.loc[173, \"Country name\"] = \"Moldova\"\n",
    "UnM49.loc[217, \"Country name\"] = \"Netherlands\"\n",
    "UnM49.loc[175, \"Country name\"] = \"Russia\"\n",
    "UnM49.loc[164, \"Country name\"] = \"Syria\"\n",
    "UnM49.loc[26, \"Country name\"] = \"Tanzania\"\n",
    "UnM49.loc[116, \"Country name\"] = \"United States\"\n",
    "UnM49.loc[193, \"Country name\"] = \"United Kingdom\"\n",
    "UnM49.loc[111, \"Country name\"] = \"Venezuela\"\n",
    "UnM49.loc[140, \"Country name\"] = \"Vietnam\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = pd.DataFrame(\n",
    "    {\n",
    "        \"Country name\": [\"Kosovo\", \"Somaliland\", \"Taiwan\"],\n",
    "        \"Subregion\": [\"Southern Europe\", \"Sub-Saharan Africa\", \"Eastern Asia\"],\n",
    "        \"Continent\": [\"Europe\", \"Africa\", \"Asia\"],\n",
    "    }\n",
    ")\n",
    "\n",
    "UnM49 = pd.concat([UnM49, _], axis=0)\n",
    "UnM49 = UnM49.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data\n",
    "Dat = pd.merge(DataWhr2024, UnM49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data of 2023\n",
    "Dat2023 = Dat[Dat['year'] == 2023]\n",
    "Dat2023 = Dat2023.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Taiwan',\n",
       " 'Tajikistan',\n",
       " 'Tanzania',\n",
       " 'Thailand',\n",
       " 'Togo',\n",
       " 'Trinidad and Tobago',\n",
       " 'Tunisia',\n",
       " 'Turkmenistan',\n",
       " 'Türkiye']"
      ]
     },
     "execution_count": 303,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Countries that starts with the same letter that your name\n",
    "StartsWith = 'T' # The first letter of your name\n",
    "list(Dat[Dat['Country name'].str.startswith(StartsWith)]['Country name'].unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data of 2023 from the region selected\n",
    "CountrySelected = 'Iraq' # Change to the country that you selected\n",
    "SubregionSelected = Dat[Dat['Country name']==CountrySelected]['Subregion'].unique()[0]\n",
    "\n",
    "DatSelected = Dat2023[Dat2023['Subregion']==SubregionSelected]\n",
    "DatSelected = DatSelected.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "metadata": {},
   "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>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>138.0</td>\n",
       "      <td>138.000000</td>\n",
       "      <td>129.000000</td>\n",
       "      <td>138.000000</td>\n",
       "      <td>135.000000</td>\n",
       "      <td>136.000000</td>\n",
       "      <td>129.000000</td>\n",
       "      <td>131.000000</td>\n",
       "      <td>138.000000</td>\n",
       "      <td>138.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>5.620848</td>\n",
       "      <td>9.516690</td>\n",
       "      <td>0.790978</td>\n",
       "      <td>65.188148</td>\n",
       "      <td>0.790287</td>\n",
       "      <td>0.033597</td>\n",
       "      <td>0.721115</td>\n",
       "      <td>0.652101</td>\n",
       "      <td>0.293428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.139482</td>\n",
       "      <td>1.152052</td>\n",
       "      <td>0.129673</td>\n",
       "      <td>5.542482</td>\n",
       "      <td>0.120719</td>\n",
       "      <td>0.161931</td>\n",
       "      <td>0.175695</td>\n",
       "      <td>0.109047</td>\n",
       "      <td>0.088862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>1.446000</td>\n",
       "      <td>7.076000</td>\n",
       "      <td>0.368000</td>\n",
       "      <td>52.200000</td>\n",
       "      <td>0.228000</td>\n",
       "      <td>-0.268000</td>\n",
       "      <td>0.153000</td>\n",
       "      <td>0.261000</td>\n",
       "      <td>0.111000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>4.679750</td>\n",
       "      <td>8.620000</td>\n",
       "      <td>0.702250</td>\n",
       "      <td>60.700000</td>\n",
       "      <td>0.734750</td>\n",
       "      <td>-0.071000</td>\n",
       "      <td>0.662000</td>\n",
       "      <td>0.581250</td>\n",
       "      <td>0.229250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>5.863000</td>\n",
       "      <td>9.637000</td>\n",
       "      <td>0.829000</td>\n",
       "      <td>66.100000</td>\n",
       "      <td>0.803000</td>\n",
       "      <td>0.028000</td>\n",
       "      <td>0.769000</td>\n",
       "      <td>0.668500</td>\n",
       "      <td>0.285000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>6.487250</td>\n",
       "      <td>10.504000</td>\n",
       "      <td>0.889750</td>\n",
       "      <td>69.600000</td>\n",
       "      <td>0.876250</td>\n",
       "      <td>0.138000</td>\n",
       "      <td>0.838500</td>\n",
       "      <td>0.735500</td>\n",
       "      <td>0.357500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2023.0</td>\n",
       "      <td>7.699000</td>\n",
       "      <td>11.676000</td>\n",
       "      <td>0.979000</td>\n",
       "      <td>74.600000</td>\n",
       "      <td>0.965000</td>\n",
       "      <td>0.590000</td>\n",
       "      <td>0.948000</td>\n",
       "      <td>0.843000</td>\n",
       "      <td>0.516000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "count   138.0   138.000000          129.000000      138.000000   \n",
       "mean   2023.0     5.620848            9.516690        0.790978   \n",
       "std       0.0     1.139482            1.152052        0.129673   \n",
       "min    2023.0     1.446000            7.076000        0.368000   \n",
       "25%    2023.0     4.679750            8.620000        0.702250   \n",
       "50%    2023.0     5.863000            9.637000        0.829000   \n",
       "75%    2023.0     6.487250           10.504000        0.889750   \n",
       "max    2023.0     7.699000           11.676000        0.979000   \n",
       "\n",
       "       Healthy life expectancy at birth  Freedom to make life choices  \\\n",
       "count                        135.000000                    136.000000   \n",
       "mean                          65.188148                      0.790287   \n",
       "std                            5.542482                      0.120719   \n",
       "min                           52.200000                      0.228000   \n",
       "25%                           60.700000                      0.734750   \n",
       "50%                           66.100000                      0.803000   \n",
       "75%                           69.600000                      0.876250   \n",
       "max                           74.600000                      0.965000   \n",
       "\n",
       "       Generosity  Perceptions of corruption  Positive affect  Negative affect  \n",
       "count  129.000000                 131.000000       138.000000       138.000000  \n",
       "mean     0.033597                   0.721115         0.652101         0.293428  \n",
       "std      0.161931                   0.175695         0.109047         0.088862  \n",
       "min     -0.268000                   0.153000         0.261000         0.111000  \n",
       "25%     -0.071000                   0.662000         0.581250         0.229250  \n",
       "50%      0.028000                   0.769000         0.668500         0.285000  \n",
       "75%      0.138000                   0.838500         0.735500         0.357500  \n",
       "max      0.590000                   0.948000         0.843000         0.516000  "
      ]
     },
     "execution_count": 305,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dat2023.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "metadata": {},
   "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>Country name</th>\n",
       "      <th>year</th>\n",
       "      <th>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "      <th>Subregion</th>\n",
       "      <th>Continent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2023</td>\n",
       "      <td>1.446</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.368</td>\n",
       "      <td>55.2</td>\n",
       "      <td>0.228</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.738</td>\n",
       "      <td>0.261</td>\n",
       "      <td>0.460</td>\n",
       "      <td>Southern Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Albania</td>\n",
       "      <td>2023</td>\n",
       "      <td>5.445</td>\n",
       "      <td>9.689</td>\n",
       "      <td>0.691</td>\n",
       "      <td>69.2</td>\n",
       "      <td>0.872</td>\n",
       "      <td>0.068</td>\n",
       "      <td>0.855</td>\n",
       "      <td>0.597</td>\n",
       "      <td>0.314</td>\n",
       "      <td>Southern Europe</td>\n",
       "      <td>Europe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Argentina</td>\n",
       "      <td>2023</td>\n",
       "      <td>6.393</td>\n",
       "      <td>9.994</td>\n",
       "      <td>0.892</td>\n",
       "      <td>67.3</td>\n",
       "      <td>0.832</td>\n",
       "      <td>-0.129</td>\n",
       "      <td>0.846</td>\n",
       "      <td>0.720</td>\n",
       "      <td>0.301</td>\n",
       "      <td>Latin America and the Caribbean</td>\n",
       "      <td>Americas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Armenia</td>\n",
       "      <td>2023</td>\n",
       "      <td>5.679</td>\n",
       "      <td>9.730</td>\n",
       "      <td>0.819</td>\n",
       "      <td>68.2</td>\n",
       "      <td>0.819</td>\n",
       "      <td>-0.179</td>\n",
       "      <td>0.681</td>\n",
       "      <td>0.575</td>\n",
       "      <td>0.423</td>\n",
       "      <td>Western Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Australia</td>\n",
       "      <td>2023</td>\n",
       "      <td>7.025</td>\n",
       "      <td>10.846</td>\n",
       "      <td>0.896</td>\n",
       "      <td>71.2</td>\n",
       "      <td>0.876</td>\n",
       "      <td>0.187</td>\n",
       "      <td>0.482</td>\n",
       "      <td>0.731</td>\n",
       "      <td>0.248</td>\n",
       "      <td>Australia and New Zealand</td>\n",
       "      <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>Venezuela</td>\n",
       "      <td>2023</td>\n",
       "      <td>5.765</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.885</td>\n",
       "      <td>63.7</td>\n",
       "      <td>0.757</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.825</td>\n",
       "      <td>0.758</td>\n",
       "      <td>0.300</td>\n",
       "      <td>Latin America and the Caribbean</td>\n",
       "      <td>Americas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>Vietnam</td>\n",
       "      <td>2023</td>\n",
       "      <td>6.325</td>\n",
       "      <td>9.392</td>\n",
       "      <td>0.845</td>\n",
       "      <td>65.7</td>\n",
       "      <td>0.956</td>\n",
       "      <td>-0.159</td>\n",
       "      <td>0.655</td>\n",
       "      <td>0.710</td>\n",
       "      <td>0.120</td>\n",
       "      <td>South-eastern Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>Yemen</td>\n",
       "      <td>2023</td>\n",
       "      <td>3.532</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.825</td>\n",
       "      <td>56.6</td>\n",
       "      <td>0.583</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.771</td>\n",
       "      <td>0.447</td>\n",
       "      <td>0.341</td>\n",
       "      <td>Western Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>Zambia</td>\n",
       "      <td>2023</td>\n",
       "      <td>3.686</td>\n",
       "      <td>8.115</td>\n",
       "      <td>0.664</td>\n",
       "      <td>56.1</td>\n",
       "      <td>0.854</td>\n",
       "      <td>0.092</td>\n",
       "      <td>0.814</td>\n",
       "      <td>0.653</td>\n",
       "      <td>0.359</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>Africa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>2023</td>\n",
       "      <td>3.572</td>\n",
       "      <td>7.679</td>\n",
       "      <td>0.694</td>\n",
       "      <td>55.0</td>\n",
       "      <td>0.735</td>\n",
       "      <td>-0.069</td>\n",
       "      <td>0.757</td>\n",
       "      <td>0.610</td>\n",
       "      <td>0.179</td>\n",
       "      <td>Sub-Saharan Africa</td>\n",
       "      <td>Africa</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>138 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Country name  year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "0    Afghanistan  2023        1.446                 NaN           0.368   \n",
       "1        Albania  2023        5.445               9.689           0.691   \n",
       "2      Argentina  2023        6.393               9.994           0.892   \n",
       "3        Armenia  2023        5.679               9.730           0.819   \n",
       "4      Australia  2023        7.025              10.846           0.896   \n",
       "..           ...   ...          ...                 ...             ...   \n",
       "133    Venezuela  2023        5.765                 NaN           0.885   \n",
       "134      Vietnam  2023        6.325               9.392           0.845   \n",
       "135        Yemen  2023        3.532                 NaN           0.825   \n",
       "136       Zambia  2023        3.686               8.115           0.664   \n",
       "137     Zimbabwe  2023        3.572               7.679           0.694   \n",
       "\n",
       "     Healthy life expectancy at birth  Freedom to make life choices  \\\n",
       "0                                55.2                         0.228   \n",
       "1                                69.2                         0.872   \n",
       "2                                67.3                         0.832   \n",
       "3                                68.2                         0.819   \n",
       "4                                71.2                         0.876   \n",
       "..                                ...                           ...   \n",
       "133                              63.7                         0.757   \n",
       "134                              65.7                         0.956   \n",
       "135                              56.6                         0.583   \n",
       "136                              56.1                         0.854   \n",
       "137                              55.0                         0.735   \n",
       "\n",
       "     Generosity  Perceptions of corruption  Positive affect  Negative affect  \\\n",
       "0           NaN                      0.738            0.261            0.460   \n",
       "1         0.068                      0.855            0.597            0.314   \n",
       "2        -0.129                      0.846            0.720            0.301   \n",
       "3        -0.179                      0.681            0.575            0.423   \n",
       "4         0.187                      0.482            0.731            0.248   \n",
       "..          ...                        ...              ...              ...   \n",
       "133         NaN                      0.825            0.758            0.300   \n",
       "134      -0.159                      0.655            0.710            0.120   \n",
       "135         NaN                      0.771            0.447            0.341   \n",
       "136       0.092                      0.814            0.653            0.359   \n",
       "137      -0.069                      0.757            0.610            0.179   \n",
       "\n",
       "                           Subregion Continent  \n",
       "0                      Southern Asia      Asia  \n",
       "1                    Southern Europe    Europe  \n",
       "2    Latin America and the Caribbean  Americas  \n",
       "3                       Western Asia      Asia  \n",
       "4          Australia and New Zealand   Oceania  \n",
       "..                               ...       ...  \n",
       "133  Latin America and the Caribbean  Americas  \n",
       "134               South-eastern Asia      Asia  \n",
       "135                     Western Asia      Asia  \n",
       "136               Sub-Saharan Africa    Africa  \n",
       "137               Sub-Saharan Africa    Africa  \n",
       "\n",
       "[138 rows x 13 columns]"
      ]
     },
     "execution_count": 306,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dat2023"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "metadata": {},
   "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>Country name</th>\n",
       "      <th>year</th>\n",
       "      <th>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "      <th>Subregion</th>\n",
       "      <th>Continent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>2023</td>\n",
       "      <td>1.446</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.368</td>\n",
       "      <td>55.2</td>\n",
       "      <td>0.228</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.738</td>\n",
       "      <td>0.261</td>\n",
       "      <td>0.46</td>\n",
       "      <td>Southern Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Country name  year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "0  Afghanistan  2023        1.446                 NaN           0.368   \n",
       "\n",
       "   Healthy life expectancy at birth  Freedom to make life choices  Generosity  \\\n",
       "0                              55.2                         0.228         NaN   \n",
       "\n",
       "   Perceptions of corruption  Positive affect  Negative affect      Subregion  \\\n",
       "0                      0.738            0.261             0.46  Southern Asia   \n",
       "\n",
       "  Continent  \n",
       "0      Asia  "
      ]
     },
     "execution_count": 307,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].min()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "metadata": {},
   "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>Country name</th>\n",
       "      <th>year</th>\n",
       "      <th>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "      <th>Subregion</th>\n",
       "      <th>Continent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Finland</td>\n",
       "      <td>2023</td>\n",
       "      <td>7.699</td>\n",
       "      <td>10.808</td>\n",
       "      <td>0.947</td>\n",
       "      <td>71.3</td>\n",
       "      <td>0.943</td>\n",
       "      <td>-0.001</td>\n",
       "      <td>0.185</td>\n",
       "      <td>0.717</td>\n",
       "      <td>0.173</td>\n",
       "      <td>Northern Europe</td>\n",
       "      <td>Europe</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Country name  year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "38      Finland  2023        7.699              10.808           0.947   \n",
       "\n",
       "    Healthy life expectancy at birth  Freedom to make life choices  \\\n",
       "38                              71.3                         0.943   \n",
       "\n",
       "    Generosity  Perceptions of corruption  Positive affect  Negative affect  \\\n",
       "38      -0.001                      0.185            0.717            0.173   \n",
       "\n",
       "          Subregion Continent  \n",
       "38  Northern Europe    Europe  "
      ]
     },
     "execution_count": 308,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].max()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_health = Dat2023[\"Healthy life expectancy at birth\"].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "66.1\n"
     ]
    }
   ],
   "source": [
    "print(avg_health)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.22925)"
      ]
     },
     "execution_count": 311,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#p = [0.25, 0.5, 0.75]\n",
    "np.quantile(Dat2023[\"Negative affect\"], 0.25)\n",
    "\n",
    "#Dat[\"Negative affect\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.647 , 5.449 , 6.3235])"
      ]
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = [0.25, 0.5, 0.75]\n",
    "np.quantile(Dat[\"Life Ladder\"], p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.8629999999999995\n",
      "5.620847826086956\n",
      "5.484781886942988\n",
      "5.310494263379958\n",
      "5.690625\n",
      "5.768428571428572\n",
      "5.620847826086956\n"
     ]
    }
   ],
   "source": [
    "ll = Dat2023[\"Life Ladder\"]\n",
    "\n",
    "Mean = ll.mean()\n",
    "\n",
    "print(ll.median())\n",
    "print(Mean)\n",
    "print(gmean(ll))\n",
    "print(hmean(ll))\n",
    "print(trim_mean(ll, 0.1))\n",
    "print(trim_mean(ll, 0.25))\n",
    "print(winsorize(ll).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1353457170100019\n",
      "1.1394817806787827\n",
      "6.253\n",
      "1.8074999999999992\n",
      "0.7935000000000008\n",
      "0.9389300567107751\n"
     ]
    }
   ],
   "source": [
    "S2Biased = np.var(ll)\n",
    "S2Unbiased = np.var(ll, ddof=1)\n",
    "\n",
    "S1 = np.sqrt(S2Biased)\n",
    "S2 = np.sqrt(S2Unbiased)\n",
    "R = ll.max() - ll.min()\n",
    "IQR = iqr(ll)\n",
    "MAD = median_abs_deviation(ll)\n",
    "AAD = abs(ll-ll.mean()).mean()\n",
    "\n",
    "print(S1)\n",
    "print(S2)\n",
    "print(R)\n",
    "print(IQR)\n",
    "print(MAD)\n",
    "print(AAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "metadata": {},
   "outputs": [],
   "source": [
    "def an(n):\n",
    "    return np.sqrt((n-1)/2) * gamma((n-1)/2) / gamma(n/2)\n",
    "\n",
    "def c4(n):\n",
    "    return 1/an(n)\n",
    "\n",
    "# d2 for n in 2,...,25\n",
    "d2 = {2:1.128, 3:1.693, 4:2.059, 5:2.326, 6:2.534, 7:2.704, 8:2.847, 9:2.970, 10:3.078, 11:3.173, 12:3.258, 13:3.336, 14:3.407, 15:3.472, 16:3.532, 17:3.588, 18:3.640, 19:3.689, 20:3.735, 21:3.778, 22:3.819, 23:3.858, 24:3.895, 25:3.931}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "138\n",
      "0.9981768626225431\n"
     ]
    }
   ],
   "source": [
    "N = len(ll)\n",
    "\n",
    "print(N)\n",
    "print(c4(N))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1415630068653209\n",
      "1.3399017549744372\n",
      "1.1764448603841964\n",
      "1.1767743140260587\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "sigma_1 = S2 / c4(N)\n",
    "#sigma_2 = R / d2[N]\n",
    "sigma_3 = IQR / (2 * norm.ppf(0.75))\n",
    "sigma_4 = MAD / norm.ppf(0.75)\n",
    "sigma_5 = AAD * np.sqrt(np.pi/2)\n",
    "\n",
    "print(sigma_1)\n",
    "#print(sigma_2)\n",
    "print(sigma_3)\n",
    "print(sigma_4)\n",
    "print(sigma_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "metadata": {},
   "outputs": [],
   "source": [
    "m2 = S2Biased\n",
    "m3 = ((ll-Mean)**3).mean()\n",
    "\n",
    "k2 = S2Unbiased\n",
    "k3 = N**2 / ((N-1)*(N-2))*m3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "metadata": {},
   "outputs": [],
   "source": [
    "g1_byhand = m3 / m2**(3/2)\n",
    "g1 = skew(ll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(-0.6323), np.float64(-0.6323))"
      ]
     },
     "execution_count": 320,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1_byhand.round(4), g1.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "metadata": {},
   "outputs": [],
   "source": [
    "G1_byhand = k3 / k2**(3/2)\n",
    "G1 = skew(ll, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(-0.6393), np.float64(-0.6393))"
      ]
     },
     "execution_count": 322,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G1_byhand.round(4), G1.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "metadata": {},
   "outputs": [],
   "source": [
    "m4 = ((ll-Mean)**4).mean()\n",
    "\n",
    "k4 = N**2*((N+1)*m4 - 3*(N-1)*m2**2) / ((N-1)*(N-2)*(N-3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Coefficient of kurtosis based on central moments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "metadata": {},
   "outputs": [],
   "source": [
    "g2_byhand = m4/m2**2\n",
    "g2 = kurtosis(ll, fisher=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(3.1173), np.float64(3.1173))"
      ]
     },
     "execution_count": 325,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g2_byhand.round(4), g2.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "metadata": {},
   "outputs": [],
   "source": [
    "G2_byhand = k4/k2**2 + 3\n",
    "G2 = kurtosis(ll, fisher=False, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(3.1664), np.float64(3.1664))"
      ]
     },
     "execution_count": 327,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G2_byhand.round(4), G2.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "metadata": {},
   "outputs": [],
   "source": [
    "g2_excess_byhand = g2_byhand - 3\n",
    "g2_excess = kurtosis(ll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(0.1173), np.float64(0.1173))"
      ]
     },
     "execution_count": 329,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g2_excess_byhand.round(4), g2_excess.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "metadata": {},
   "outputs": [],
   "source": [
    "G2_excess_byhand = G2_byhand - 3\n",
    "G2_excess = kurtosis(ll, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(0.1664), np.float64(0.1664))"
      ]
     },
     "execution_count": 331,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G2_excess_byhand.round(4), G2_excess.round(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7th question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "metadata": {},
   "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>Country name</th>\n",
       "      <th>year</th>\n",
       "      <th>Life Ladder</th>\n",
       "      <th>Log GDP per capita</th>\n",
       "      <th>Social support</th>\n",
       "      <th>Healthy life expectancy at birth</th>\n",
       "      <th>Freedom to make life choices</th>\n",
       "      <th>Generosity</th>\n",
       "      <th>Perceptions of corruption</th>\n",
       "      <th>Positive affect</th>\n",
       "      <th>Negative affect</th>\n",
       "      <th>Subregion</th>\n",
       "      <th>Continent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>Thailand</td>\n",
       "      <td>2023</td>\n",
       "      <td>6.282</td>\n",
       "      <td>9.807</td>\n",
       "      <td>0.873</td>\n",
       "      <td>68.6</td>\n",
       "      <td>0.926</td>\n",
       "      <td>0.338</td>\n",
       "      <td>0.889</td>\n",
       "      <td>0.811</td>\n",
       "      <td>0.217</td>\n",
       "      <td>South-eastern Asia</td>\n",
       "      <td>Asia</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Country name  year  Life Ladder  Log GDP per capita  Social support  \\\n",
       "122     Thailand  2023        6.282               9.807           0.873   \n",
       "\n",
       "     Healthy life expectancy at birth  Freedom to make life choices  \\\n",
       "122                              68.6                         0.926   \n",
       "\n",
       "     Generosity  Perceptions of corruption  Positive affect  Negative affect  \\\n",
       "122       0.338                      0.889            0.811            0.217   \n",
       "\n",
       "              Subregion Continent  \n",
       "122  South-eastern Asia      Asia  "
      ]
     },
     "execution_count": 332,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dat2023.loc[Dat2023[\"Country name\"] == \"Thailand\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data of 2023 from the region selected\n",
    "CountrySelected = 'Thailand' # Change to the country that you selected\n",
    "SubregionSelected = Dat[Dat['Country name']==CountrySelected]['Subregion'].unique()[0]\n",
    "\n",
    "DatSelected = Dat2023[Dat2023['Subregion']==SubregionSelected]\n",
    "DatSelected = DatSelected.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.9  , 0.926, 0.956])"
      ]
     },
     "execution_count": 334,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = [0.25, 0.5, 0.75]\n",
    "np.quantile(DatSelected[\"Freedom to make life choices\"], p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.926\n",
      "0.8999999999999999\n",
      "0.8960830560432489\n",
      "0.891676454482847\n",
      "0.8999999999999999\n",
      "0.9236000000000001\n",
      "0.8999999999999999\n"
     ]
    }
   ],
   "source": [
    "ll = DatSelected[\"Freedom to make life choices\"]\n",
    "\n",
    "Mean = ll.mean()\n",
    "\n",
    "print(ll.median())\n",
    "print(Mean)\n",
    "print(gmean(ll))\n",
    "print(hmean(ll))\n",
    "print(trim_mean(ll, 0.1))\n",
    "print(trim_mean(ll, 0.25))\n",
    "print(winsorize(ll).mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.07912297039699942\n",
      "0.08392258337301112\n",
      "0.27\n",
      "0.05599999999999994\n",
      "0.029999999999999916\n",
      "0.05422222222222228\n"
     ]
    }
   ],
   "source": [
    "S2Biased = np.var(ll)\n",
    "S2Unbiased = np.var(ll, ddof=1)\n",
    "\n",
    "S1 = np.sqrt(S2Biased)\n",
    "S2 = np.sqrt(S2Unbiased)\n",
    "R = ll.max() - ll.min()\n",
    "IQR = iqr(ll)\n",
    "MAD = median_abs_deviation(ll)\n",
    "AAD = abs(ll-ll.mean()).mean()\n",
    "\n",
    "print(S1)\n",
    "print(S2)\n",
    "print(R)\n",
    "print(IQR)\n",
    "print(MAD)\n",
    "print(AAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {},
   "outputs": [],
   "source": [
    "def an(n):\n",
    "    return np.sqrt((n-1)/2) * gamma((n-1)/2) / gamma(n/2)\n",
    "\n",
    "def c4(n):\n",
    "    return 1/an(n)\n",
    "\n",
    "# d2 for n in 2,...,25\n",
    "d2 = {2:1.128, 3:1.693, 4:2.059, 5:2.326, 6:2.534, 7:2.704, 8:2.847, 9:2.970, 10:3.078, 11:3.173, 12:3.258, 13:3.336, 14:3.407, 15:3.472, 16:3.532, 17:3.588, 18:3.640, 19:3.689, 20:3.735, 21:3.778, 22:3.819, 23:3.858, 24:3.895, 25:3.931}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "0.9693106997139541\n"
     ]
    }
   ],
   "source": [
    "N = len(ll)\n",
    "\n",
    "print(N)\n",
    "print(c4(N))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.08657965232177553\n",
      "0.04151286211815681\n",
      "0.044478066555167936\n",
      "0.06795747766777387\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "sigma_1 = S2 / c4(N)\n",
    "#sigma_2 = R / d2[N]\n",
    "sigma_3 = IQR / (2 * norm.ppf(0.75))\n",
    "sigma_4 = MAD / norm.ppf(0.75)\n",
    "sigma_5 = AAD * np.sqrt(np.pi/2)\n",
    "\n",
    "print(sigma_1)\n",
    "#print(sigma_2)\n",
    "print(sigma_3)\n",
    "print(sigma_4)\n",
    "print(sigma_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "metadata": {},
   "outputs": [],
   "source": [
    "m2 = S2Biased\n",
    "m3 = ((ll-Mean)**3).mean()\n",
    "\n",
    "k2 = S2Unbiased\n",
    "k3 = N**2 / ((N-1)*(N-2))*m3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "metadata": {},
   "outputs": [],
   "source": [
    "g1_byhand = m3 / m2**(3/2)\n",
    "g1 = skew(ll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(-1.7826), np.float64(-1.7826))"
      ]
     },
     "execution_count": 342,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1_byhand.round(4), g1.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "metadata": {},
   "outputs": [],
   "source": [
    "G1_byhand = k3 / k2**(3/2)\n",
    "G1 = skew(ll, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(-2.1608), np.float64(-2.1608))"
      ]
     },
     "execution_count": 344,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G1_byhand.round(4), G1.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "metadata": {},
   "outputs": [],
   "source": [
    "m4 = ((ll-Mean)**4).mean()\n",
    "\n",
    "k4 = N**2*((N+1)*m4 - 3*(N-1)*m2**2) / ((N-1)*(N-2)*(N-3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Coefficient of kurtosis based on central moments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "metadata": {},
   "outputs": [],
   "source": [
    "g2_byhand = m4/m2**2\n",
    "g2 = kurtosis(ll, fisher=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(5.1354), np.float64(5.1354))"
      ]
     },
     "execution_count": 347,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g2_byhand.round(4), g2.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "metadata": {},
   "outputs": [],
   "source": [
    "G2_byhand = k4/k2**2 + 3\n",
    "G2 = kurtosis(ll, fisher=False, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(8.2103), np.float64(8.2103))"
      ]
     },
     "execution_count": 349,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G2_byhand.round(4), G2.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 350,
   "metadata": {},
   "outputs": [],
   "source": [
    "g2_excess_byhand = g2_byhand - 3\n",
    "g2_excess = kurtosis(ll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 351,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(2.1354), np.float64(2.1354))"
      ]
     },
     "execution_count": 351,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g2_excess_byhand.round(4), g2_excess.round(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 352,
   "metadata": {},
   "outputs": [],
   "source": [
    "G2_excess_byhand = G2_byhand - 3\n",
    "G2_excess = kurtosis(ll, bias=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 353,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(5.2103), np.float64(5.2103))"
      ]
     },
     "execution_count": 353,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "G2_excess_byhand.round(4), G2_excess.round(4)"
   ]
  }
 ],
 "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}