1679 lines
45 KiB
Plaintext
1679 lines
45 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 542,
|
||
"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": "code",
|
||
"execution_count": 543,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"DataWhr2024 = pd.read_csv(\"../DataWhr2024.csv\")\n",
|
||
"UnM49 = pd.read_csv(\"./UnM49.csv\", sep=';')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 544,
|
||
"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": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"---"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 545,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Data\n",
|
||
"Dat = pd.merge(DataWhr2024, UnM49)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 546,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Data of 2023\n",
|
||
"Dat2023 = Dat[Dat['year'] == 2023]\n",
|
||
"Dat2023 = Dat2023.reset_index(drop=True)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 547,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['Tajikistan',\n",
|
||
" 'Thailand',\n",
|
||
" 'Togo',\n",
|
||
" 'Trinidad and Tobago',\n",
|
||
" 'Tunisia',\n",
|
||
" 'Turkmenistan',\n",
|
||
" 'Türkiye']"
|
||
]
|
||
},
|
||
"execution_count": 547,
|
||
"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": 548,
|
||
"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": 549,
|
||
"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>121.0</td>\n",
|
||
" <td>121.000000</td>\n",
|
||
" <td>114.000000</td>\n",
|
||
" <td>121.000000</td>\n",
|
||
" <td>120.000000</td>\n",
|
||
" <td>119.000000</td>\n",
|
||
" <td>114.000000</td>\n",
|
||
" <td>114.000000</td>\n",
|
||
" <td>121.000000</td>\n",
|
||
" <td>121.000000</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>mean</th>\n",
|
||
" <td>2023.0</td>\n",
|
||
" <td>5.601455</td>\n",
|
||
" <td>9.527377</td>\n",
|
||
" <td>0.792537</td>\n",
|
||
" <td>65.283333</td>\n",
|
||
" <td>0.789227</td>\n",
|
||
" <td>0.027851</td>\n",
|
||
" <td>0.722447</td>\n",
|
||
" <td>0.650488</td>\n",
|
||
" <td>0.295818</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>std</th>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>1.159381</td>\n",
|
||
" <td>1.151462</td>\n",
|
||
" <td>0.131982</td>\n",
|
||
" <td>5.580870</td>\n",
|
||
" <td>0.124732</td>\n",
|
||
" <td>0.162959</td>\n",
|
||
" <td>0.182586</td>\n",
|
||
" <td>0.113365</td>\n",
|
||
" <td>0.085531</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>min</th>\n",
|
||
" <td>2023.0</td>\n",
|
||
" <td>1.446000</td>\n",
|
||
" <td>7.147000</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.114000</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>25%</th>\n",
|
||
" <td>2023.0</td>\n",
|
||
" <td>4.609000</td>\n",
|
||
" <td>8.612500</td>\n",
|
||
" <td>0.703000</td>\n",
|
||
" <td>60.700000</td>\n",
|
||
" <td>0.734500</td>\n",
|
||
" <td>-0.088750</td>\n",
|
||
" <td>0.663250</td>\n",
|
||
" <td>0.579000</td>\n",
|
||
" <td>0.231000</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>50%</th>\n",
|
||
" <td>2023.0</td>\n",
|
||
" <td>5.868000</td>\n",
|
||
" <td>9.670000</td>\n",
|
||
" <td>0.831000</td>\n",
|
||
" <td>66.300000</td>\n",
|
||
" <td>0.807000</td>\n",
|
||
" <td>0.022000</td>\n",
|
||
" <td>0.781000</td>\n",
|
||
" <td>0.661000</td>\n",
|
||
" <td>0.289000</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>75%</th>\n",
|
||
" <td>2023.0</td>\n",
|
||
" <td>6.482000</td>\n",
|
||
" <td>10.496250</td>\n",
|
||
" <td>0.895000</td>\n",
|
||
" <td>69.775000</td>\n",
|
||
" <td>0.877000</td>\n",
|
||
" <td>0.122500</td>\n",
|
||
" <td>0.844500</td>\n",
|
||
" <td>0.738000</td>\n",
|
||
" <td>0.358000</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 121.0 121.000000 114.000000 121.000000 \n",
|
||
"mean 2023.0 5.601455 9.527377 0.792537 \n",
|
||
"std 0.0 1.159381 1.151462 0.131982 \n",
|
||
"min 2023.0 1.446000 7.147000 0.368000 \n",
|
||
"25% 2023.0 4.609000 8.612500 0.703000 \n",
|
||
"50% 2023.0 5.868000 9.670000 0.831000 \n",
|
||
"75% 2023.0 6.482000 10.496250 0.895000 \n",
|
||
"max 2023.0 7.699000 11.676000 0.979000 \n",
|
||
"\n",
|
||
" Healthy life expectancy at birth Freedom to make life choices \\\n",
|
||
"count 120.000000 119.000000 \n",
|
||
"mean 65.283333 0.789227 \n",
|
||
"std 5.580870 0.124732 \n",
|
||
"min 52.200000 0.228000 \n",
|
||
"25% 60.700000 0.734500 \n",
|
||
"50% 66.300000 0.807000 \n",
|
||
"75% 69.775000 0.877000 \n",
|
||
"max 74.600000 0.965000 \n",
|
||
"\n",
|
||
" Generosity Perceptions of corruption Positive affect Negative affect \n",
|
||
"count 114.000000 114.000000 121.000000 121.000000 \n",
|
||
"mean 0.027851 0.722447 0.650488 0.295818 \n",
|
||
"std 0.162959 0.182586 0.113365 0.085531 \n",
|
||
"min -0.268000 0.153000 0.261000 0.114000 \n",
|
||
"25% -0.088750 0.663250 0.579000 0.231000 \n",
|
||
"50% 0.022000 0.781000 0.661000 0.289000 \n",
|
||
"75% 0.122500 0.844500 0.738000 0.358000 \n",
|
||
"max 0.590000 0.948000 0.843000 0.516000 "
|
||
]
|
||
},
|
||
"execution_count": 549,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Dat2023.describe()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 550,
|
||
"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>116</th>\n",
|
||
" <td>Uruguay</td>\n",
|
||
" <td>2023</td>\n",
|
||
" <td>6.662</td>\n",
|
||
" <td>10.122</td>\n",
|
||
" <td>0.908</td>\n",
|
||
" <td>67.5</td>\n",
|
||
" <td>0.904</td>\n",
|
||
" <td>-0.050</td>\n",
|
||
" <td>0.662</td>\n",
|
||
" <td>0.753</td>\n",
|
||
" <td>0.265</td>\n",
|
||
" <td>Latin America and the Caribbean</td>\n",
|
||
" <td>Americas</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>117</th>\n",
|
||
" <td>Uzbekistan</td>\n",
|
||
" <td>2023</td>\n",
|
||
" <td>6.385</td>\n",
|
||
" <td>9.026</td>\n",
|
||
" <td>0.909</td>\n",
|
||
" <td>65.9</td>\n",
|
||
" <td>0.927</td>\n",
|
||
" <td>0.247</td>\n",
|
||
" <td>0.650</td>\n",
|
||
" <td>0.752</td>\n",
|
||
" <td>0.202</td>\n",
|
||
" <td>Central Asia</td>\n",
|
||
" <td>Asia</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>118</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>119</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>120</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>121 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",
|
||
"116 Uruguay 2023 6.662 10.122 0.908 \n",
|
||
"117 Uzbekistan 2023 6.385 9.026 0.909 \n",
|
||
"118 Yemen 2023 3.532 NaN 0.825 \n",
|
||
"119 Zambia 2023 3.686 8.115 0.664 \n",
|
||
"120 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",
|
||
"116 67.5 0.904 \n",
|
||
"117 65.9 0.927 \n",
|
||
"118 56.6 0.583 \n",
|
||
"119 56.1 0.854 \n",
|
||
"120 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",
|
||
"116 -0.050 0.662 0.753 0.265 \n",
|
||
"117 0.247 0.650 0.752 0.202 \n",
|
||
"118 NaN 0.771 0.447 0.341 \n",
|
||
"119 0.092 0.814 0.653 0.359 \n",
|
||
"120 -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",
|
||
"116 Latin America and the Caribbean Americas \n",
|
||
"117 Central Asia Asia \n",
|
||
"118 Western Asia Asia \n",
|
||
"119 Sub-Saharan Africa Africa \n",
|
||
"120 Sub-Saharan Africa Africa \n",
|
||
"\n",
|
||
"[121 rows x 13 columns]"
|
||
]
|
||
},
|
||
"execution_count": 550,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Dat2023"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 551,
|
||
"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": 551,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].min()]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 552,
|
||
"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>35</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",
|
||
"35 Finland 2023 7.699 10.808 0.947 \n",
|
||
"\n",
|
||
" Healthy life expectancy at birth Freedom to make life choices \\\n",
|
||
"35 71.3 0.943 \n",
|
||
"\n",
|
||
" Generosity Perceptions of corruption Positive affect Negative affect \\\n",
|
||
"35 -0.001 0.185 0.717 0.173 \n",
|
||
"\n",
|
||
" Subregion Continent \n",
|
||
"35 Northern Europe Europe "
|
||
]
|
||
},
|
||
"execution_count": 552,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].max()]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 553,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"avg_health = Dat2023[\"Healthy life expectancy at birth\"].median()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 554,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"66.3\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(avg_health)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 555,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"np.float64(0.231)"
|
||
]
|
||
},
|
||
"execution_count": 555,
|
||
"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": 556,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([4.6125, 5.42 , 6.3215])"
|
||
]
|
||
},
|
||
"execution_count": 556,
|
||
"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": 557,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"5.868\n",
|
||
"5.601454545454545\n",
|
||
"5.46003534802306\n",
|
||
"5.277807689332831\n",
|
||
"5.670474226804124\n",
|
||
"5.738901639344262\n",
|
||
"5.601454545454545\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": 558,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"1.1545804575939485\n",
|
||
"1.1593812286445444\n",
|
||
"6.253\n",
|
||
"1.8730000000000002\n",
|
||
"0.8130000000000006\n",
|
||
"0.9629526671675434\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": 559,
|
||
"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": 560,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"121\n",
|
||
"0.9979188593597667\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"N = len(ll)\n",
|
||
"\n",
|
||
"print(N)\n",
|
||
"print(c4(N))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 561,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"1.1617990959589308\n",
|
||
"1.3884569776304965\n",
|
||
"1.2053556036450552\n",
|
||
"1.2068821913267496\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": 562,
|
||
"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": 563,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g1_byhand = m3 / m2**(3/2)\n",
|
||
"g1 = skew(ll)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 564,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(-0.6037), np.float64(-0.6037))"
|
||
]
|
||
},
|
||
"execution_count": 564,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g1_byhand.round(4), g1.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 565,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G1_byhand = k3 / k2**(3/2)\n",
|
||
"G1 = skew(ll, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 566,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(-0.6113), np.float64(-0.6113))"
|
||
]
|
||
},
|
||
"execution_count": 566,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"G1_byhand.round(4), G1.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 567,
|
||
"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": 568,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g2_byhand = m4/m2**2\n",
|
||
"g2 = kurtosis(ll, fisher=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 569,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(3.0748), np.float64(3.0748))"
|
||
]
|
||
},
|
||
"execution_count": 569,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g2_byhand.round(4), g2.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 570,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G2_byhand = k4/k2**2 + 3\n",
|
||
"G2 = kurtosis(ll, fisher=False, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 571,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(3.1292), np.float64(3.1292))"
|
||
]
|
||
},
|
||
"execution_count": 571,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"G2_byhand.round(4), G2.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 572,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g2_excess_byhand = g2_byhand - 3\n",
|
||
"g2_excess = kurtosis(ll)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 573,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(0.0748), np.float64(0.0748))"
|
||
]
|
||
},
|
||
"execution_count": 573,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g2_excess_byhand.round(4), g2_excess.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 574,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G2_excess_byhand = G2_byhand - 3\n",
|
||
"G2_excess = kurtosis(ll, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 575,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(0.1292), np.float64(0.1292))"
|
||
]
|
||
},
|
||
"execution_count": 575,
|
||
"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": 576,
|
||
"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>109</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",
|
||
"109 Thailand 2023 6.282 9.807 0.873 \n",
|
||
"\n",
|
||
" Healthy life expectancy at birth Freedom to make life choices \\\n",
|
||
"109 68.6 0.926 \n",
|
||
"\n",
|
||
" Generosity Perceptions of corruption Positive affect Negative affect \\\n",
|
||
"109 0.338 0.889 0.811 0.217 \n",
|
||
"\n",
|
||
" Subregion Continent \n",
|
||
"109 South-eastern Asia Asia "
|
||
]
|
||
},
|
||
"execution_count": 576,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Dat2023.loc[Dat2023[\"Country name\"] == \"Thailand\"]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 577,
|
||
"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": 578,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([0.8805, 0.926 , 0.9465])"
|
||
]
|
||
},
|
||
"execution_count": 578,
|
||
"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": 579,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"0.926\n",
|
||
"0.8914285714285715\n",
|
||
"0.8867207399346544\n",
|
||
"0.8814869334699901\n",
|
||
"0.8914285714285713\n",
|
||
"0.916\n",
|
||
"0.8914285714285715\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": 580,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"0.08674899281894888\n",
|
||
"0.09369962138460887\n",
|
||
"0.27\n",
|
||
"0.06599999999999995\n",
|
||
"0.03499999999999992\n",
|
||
"0.06481632653061224\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": 581,
|
||
"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": 582,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"7\n",
|
||
"0.959368788699833\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"N = len(ll)\n",
|
||
"\n",
|
||
"print(N)\n",
|
||
"print(c4(N))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 583,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"0.09766799012879454\n",
|
||
"0.048925873210684825\n",
|
||
"0.051891077647695946\n",
|
||
"0.08123521836967404\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": 584,
|
||
"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": 585,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g1_byhand = m3 / m2**(3/2)\n",
|
||
"g1 = skew(ll)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 586,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(-1.4801), np.float64(-1.4801))"
|
||
]
|
||
},
|
||
"execution_count": 586,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g1_byhand.round(4), g1.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 587,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G1_byhand = k3 / k2**(3/2)\n",
|
||
"G1 = skew(ll, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 588,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(-1.9184), np.float64(-1.9184))"
|
||
]
|
||
},
|
||
"execution_count": 588,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"G1_byhand.round(4), G1.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"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": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g2_byhand = m4/m2**2\n",
|
||
"g2 = kurtosis(ll, fisher=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(3.9011), np.float64(3.9011))"
|
||
]
|
||
},
|
||
"execution_count": 535,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g2_byhand.round(4), g2.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G2_byhand = k4/k2**2 + 3\n",
|
||
"G2 = kurtosis(ll, fisher=False, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(6.9626), np.float64(6.9626))"
|
||
]
|
||
},
|
||
"execution_count": 537,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"G2_byhand.round(4), G2.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"g2_excess_byhand = g2_byhand - 3\n",
|
||
"g2_excess = kurtosis(ll)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(0.9011), np.float64(0.9011))"
|
||
]
|
||
},
|
||
"execution_count": 539,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"g2_excess_byhand.round(4), g2_excess.round(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"G2_excess_byhand = G2_byhand - 3\n",
|
||
"G2_excess = kurtosis(ll, bias=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(np.float64(3.9626), np.float64(3.9626))"
|
||
]
|
||
},
|
||
"execution_count": 541,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"G2_excess_byhand.round(4), G2_excess.round(4)"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": ".venv",
|
||
"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.13.0"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|