{ "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
count138.0138.000000129.000000138.000000135.000000136.000000129.000000131.000000138.000000138.000000
mean2023.05.6208489.5166900.79097865.1881480.7902870.0335970.7211150.6521010.293428
std0.01.1394821.1520520.1296735.5424820.1207190.1619310.1756950.1090470.088862
min2023.01.4460007.0760000.36800052.2000000.228000-0.2680000.1530000.2610000.111000
25%2023.04.6797508.6200000.70225060.7000000.734750-0.0710000.6620000.5812500.229250
50%2023.05.8630009.6370000.82900066.1000000.8030000.0280000.7690000.6685000.285000
75%2023.06.48725010.5040000.88975069.6000000.8762500.1380000.8385000.7355000.357500
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 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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
..........................................
133Venezuela20235.765NaN0.88563.70.757NaN0.8250.7580.300Latin America and the CaribbeanAmericas
134Vietnam20236.3259.3920.84565.70.956-0.1590.6550.7100.120South-eastern AsiaAsia
135Yemen20233.532NaN0.82556.60.583NaN0.7710.4470.341Western AsiaAsia
136Zambia20233.6868.1150.66456.10.8540.0920.8140.6530.359Sub-Saharan AfricaAfrica
137Zimbabwe20233.5727.6790.69455.00.735-0.0690.7570.6100.179Sub-Saharan AfricaAfrica
\n", "

138 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", "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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": 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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
38Finland20237.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", "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
122Thailand20236.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", "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 }