{
"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",
" year | \n",
" Life Ladder | \n",
" Log GDP per capita | \n",
" Social support | \n",
" Healthy life expectancy at birth | \n",
" Freedom to make life choices | \n",
" Generosity | \n",
" Perceptions of corruption | \n",
" Positive affect | \n",
" Negative affect | \n",
"
\n",
" \n",
" \n",
" \n",
" count | \n",
" 138.0 | \n",
" 138.000000 | \n",
" 129.000000 | \n",
" 138.000000 | \n",
" 135.000000 | \n",
" 136.000000 | \n",
" 129.000000 | \n",
" 131.000000 | \n",
" 138.000000 | \n",
" 138.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 2023.0 | \n",
" 5.620848 | \n",
" 9.516690 | \n",
" 0.790978 | \n",
" 65.188148 | \n",
" 0.790287 | \n",
" 0.033597 | \n",
" 0.721115 | \n",
" 0.652101 | \n",
" 0.293428 | \n",
"
\n",
" \n",
" std | \n",
" 0.0 | \n",
" 1.139482 | \n",
" 1.152052 | \n",
" 0.129673 | \n",
" 5.542482 | \n",
" 0.120719 | \n",
" 0.161931 | \n",
" 0.175695 | \n",
" 0.109047 | \n",
" 0.088862 | \n",
"
\n",
" \n",
" min | \n",
" 2023.0 | \n",
" 1.446000 | \n",
" 7.076000 | \n",
" 0.368000 | \n",
" 52.200000 | \n",
" 0.228000 | \n",
" -0.268000 | \n",
" 0.153000 | \n",
" 0.261000 | \n",
" 0.111000 | \n",
"
\n",
" \n",
" 25% | \n",
" 2023.0 | \n",
" 4.679750 | \n",
" 8.620000 | \n",
" 0.702250 | \n",
" 60.700000 | \n",
" 0.734750 | \n",
" -0.071000 | \n",
" 0.662000 | \n",
" 0.581250 | \n",
" 0.229250 | \n",
"
\n",
" \n",
" 50% | \n",
" 2023.0 | \n",
" 5.863000 | \n",
" 9.637000 | \n",
" 0.829000 | \n",
" 66.100000 | \n",
" 0.803000 | \n",
" 0.028000 | \n",
" 0.769000 | \n",
" 0.668500 | \n",
" 0.285000 | \n",
"
\n",
" \n",
" 75% | \n",
" 2023.0 | \n",
" 6.487250 | \n",
" 10.504000 | \n",
" 0.889750 | \n",
" 69.600000 | \n",
" 0.876250 | \n",
" 0.138000 | \n",
" 0.838500 | \n",
" 0.735500 | \n",
" 0.357500 | \n",
"
\n",
" \n",
" max | \n",
" 2023.0 | \n",
" 7.699000 | \n",
" 11.676000 | \n",
" 0.979000 | \n",
" 74.600000 | \n",
" 0.965000 | \n",
" 0.590000 | \n",
" 0.948000 | \n",
" 0.843000 | \n",
" 0.516000 | \n",
"
\n",
" \n",
"
\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",
" Country name | \n",
" year | \n",
" Life Ladder | \n",
" Log GDP per capita | \n",
" Social support | \n",
" Healthy life expectancy at birth | \n",
" Freedom to make life choices | \n",
" Generosity | \n",
" Perceptions of corruption | \n",
" Positive affect | \n",
" Negative affect | \n",
" Subregion | \n",
" Continent | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Afghanistan | \n",
" 2023 | \n",
" 1.446 | \n",
" NaN | \n",
" 0.368 | \n",
" 55.2 | \n",
" 0.228 | \n",
" NaN | \n",
" 0.738 | \n",
" 0.261 | \n",
" 0.460 | \n",
" Southern Asia | \n",
" Asia | \n",
"
\n",
" \n",
" 1 | \n",
" Albania | \n",
" 2023 | \n",
" 5.445 | \n",
" 9.689 | \n",
" 0.691 | \n",
" 69.2 | \n",
" 0.872 | \n",
" 0.068 | \n",
" 0.855 | \n",
" 0.597 | \n",
" 0.314 | \n",
" Southern Europe | \n",
" Europe | \n",
"
\n",
" \n",
" 2 | \n",
" Argentina | \n",
" 2023 | \n",
" 6.393 | \n",
" 9.994 | \n",
" 0.892 | \n",
" 67.3 | \n",
" 0.832 | \n",
" -0.129 | \n",
" 0.846 | \n",
" 0.720 | \n",
" 0.301 | \n",
" Latin America and the Caribbean | \n",
" Americas | \n",
"
\n",
" \n",
" 3 | \n",
" Armenia | \n",
" 2023 | \n",
" 5.679 | \n",
" 9.730 | \n",
" 0.819 | \n",
" 68.2 | \n",
" 0.819 | \n",
" -0.179 | \n",
" 0.681 | \n",
" 0.575 | \n",
" 0.423 | \n",
" Western Asia | \n",
" Asia | \n",
"
\n",
" \n",
" 4 | \n",
" Australia | \n",
" 2023 | \n",
" 7.025 | \n",
" 10.846 | \n",
" 0.896 | \n",
" 71.2 | \n",
" 0.876 | \n",
" 0.187 | \n",
" 0.482 | \n",
" 0.731 | \n",
" 0.248 | \n",
" Australia and New Zealand | \n",
" Oceania | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 133 | \n",
" Venezuela | \n",
" 2023 | \n",
" 5.765 | \n",
" NaN | \n",
" 0.885 | \n",
" 63.7 | \n",
" 0.757 | \n",
" NaN | \n",
" 0.825 | \n",
" 0.758 | \n",
" 0.300 | \n",
" Latin America and the Caribbean | \n",
" Americas | \n",
"
\n",
" \n",
" 134 | \n",
" Vietnam | \n",
" 2023 | \n",
" 6.325 | \n",
" 9.392 | \n",
" 0.845 | \n",
" 65.7 | \n",
" 0.956 | \n",
" -0.159 | \n",
" 0.655 | \n",
" 0.710 | \n",
" 0.120 | \n",
" South-eastern Asia | \n",
" Asia | \n",
"
\n",
" \n",
" 135 | \n",
" Yemen | \n",
" 2023 | \n",
" 3.532 | \n",
" NaN | \n",
" 0.825 | \n",
" 56.6 | \n",
" 0.583 | \n",
" NaN | \n",
" 0.771 | \n",
" 0.447 | \n",
" 0.341 | \n",
" Western Asia | \n",
" Asia | \n",
"
\n",
" \n",
" 136 | \n",
" Zambia | \n",
" 2023 | \n",
" 3.686 | \n",
" 8.115 | \n",
" 0.664 | \n",
" 56.1 | \n",
" 0.854 | \n",
" 0.092 | \n",
" 0.814 | \n",
" 0.653 | \n",
" 0.359 | \n",
" Sub-Saharan Africa | \n",
" Africa | \n",
"
\n",
" \n",
" 137 | \n",
" Zimbabwe | \n",
" 2023 | \n",
" 3.572 | \n",
" 7.679 | \n",
" 0.694 | \n",
" 55.0 | \n",
" 0.735 | \n",
" -0.069 | \n",
" 0.757 | \n",
" 0.610 | \n",
" 0.179 | \n",
" Sub-Saharan Africa | \n",
" Africa | \n",
"
\n",
" \n",
"
\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",
" Country name | \n",
" year | \n",
" Life Ladder | \n",
" Log GDP per capita | \n",
" Social support | \n",
" Healthy life expectancy at birth | \n",
" Freedom to make life choices | \n",
" Generosity | \n",
" Perceptions of corruption | \n",
" Positive affect | \n",
" Negative affect | \n",
" Subregion | \n",
" Continent | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Afghanistan | \n",
" 2023 | \n",
" 1.446 | \n",
" NaN | \n",
" 0.368 | \n",
" 55.2 | \n",
" 0.228 | \n",
" NaN | \n",
" 0.738 | \n",
" 0.261 | \n",
" 0.46 | \n",
" Southern Asia | \n",
" Asia | \n",
"
\n",
" \n",
"
\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",
" Country name | \n",
" year | \n",
" Life Ladder | \n",
" Log GDP per capita | \n",
" Social support | \n",
" Healthy life expectancy at birth | \n",
" Freedom to make life choices | \n",
" Generosity | \n",
" Perceptions of corruption | \n",
" Positive affect | \n",
" Negative affect | \n",
" Subregion | \n",
" Continent | \n",
"
\n",
" \n",
" \n",
" \n",
" 38 | \n",
" Finland | \n",
" 2023 | \n",
" 7.699 | \n",
" 10.808 | \n",
" 0.947 | \n",
" 71.3 | \n",
" 0.943 | \n",
" -0.001 | \n",
" 0.185 | \n",
" 0.717 | \n",
" 0.173 | \n",
" Northern Europe | \n",
" Europe | \n",
"
\n",
" \n",
"
\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",
" Country name | \n",
" year | \n",
" Life Ladder | \n",
" Log GDP per capita | \n",
" Social support | \n",
" Healthy life expectancy at birth | \n",
" Freedom to make life choices | \n",
" Generosity | \n",
" Perceptions of corruption | \n",
" Positive affect | \n",
" Negative affect | \n",
" Subregion | \n",
" Continent | \n",
"
\n",
" \n",
" \n",
" \n",
" 122 | \n",
" Thailand | \n",
" 2023 | \n",
" 6.282 | \n",
" 9.807 | \n",
" 0.873 | \n",
" 68.6 | \n",
" 0.926 | \n",
" 0.338 | \n",
" 0.889 | \n",
" 0.811 | \n",
" 0.217 | \n",
" South-eastern Asia | \n",
" Asia | \n",
"
\n",
" \n",
"
\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.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}