{ "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affect
count121.0121.000000114.000000121.000000120.000000119.000000114.000000114.000000121.000000121.000000
mean2023.05.6014559.5273770.79253765.2833330.7892270.0278510.7224470.6504880.295818
std0.01.1593811.1514620.1319825.5808700.1247320.1629590.1825860.1133650.085531
min2023.01.4460007.1470000.36800052.2000000.228000-0.2680000.1530000.2610000.114000
25%2023.04.6090008.6125000.70300060.7000000.734500-0.0887500.6632500.5790000.231000
50%2023.05.8680009.6700000.83100066.3000000.8070000.0220000.7810000.6610000.289000
75%2023.06.48200010.4962500.89500069.7750000.8770000.1225000.8445000.7380000.358000
max2023.07.69900011.6760000.97900074.6000000.9650000.5900000.9480000.8430000.516000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
0Afghanistan20231.446NaN0.36855.20.228NaN0.7380.2610.460Southern AsiaAsia
1Albania20235.4459.6890.69169.20.8720.0680.8550.5970.314Southern EuropeEurope
2Argentina20236.3939.9940.89267.30.832-0.1290.8460.7200.301Latin America and the CaribbeanAmericas
3Armenia20235.6799.7300.81968.20.819-0.1790.6810.5750.423Western AsiaAsia
4Australia20237.02510.8460.89671.20.8760.1870.4820.7310.248Australia and New ZealandOceania
..........................................
116Uruguay20236.66210.1220.90867.50.904-0.0500.6620.7530.265Latin America and the CaribbeanAmericas
117Uzbekistan20236.3859.0260.90965.90.9270.2470.6500.7520.202Central AsiaAsia
118Yemen20233.532NaN0.82556.60.583NaN0.7710.4470.341Western AsiaAsia
119Zambia20233.6868.1150.66456.10.8540.0920.8140.6530.359Sub-Saharan AfricaAfrica
120Zimbabwe20233.5727.6790.69455.00.735-0.0690.7570.6100.179Sub-Saharan AfricaAfrica
\n", "

121 rows × 13 columns

\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
0Afghanistan20231.446NaN0.36855.20.228NaN0.7380.2610.46Southern AsiaAsia
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
35Finland20237.69910.8080.94771.30.943-0.0010.1850.7170.173Northern EuropeEurope
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
109Thailand20236.2829.8070.87368.60.9260.3380.8890.8110.217South-eastern AsiaAsia
\n", "
" ], "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 }