finished SYS-102 - working on HCD-201

This commit is contained in:
Win 2024-11-20 18:06:44 +07:00
parent d9b5e5c861
commit 44ceb89ddd
9 changed files with 535 additions and 52 deletions

View File

@ -0,0 +1 @@
,winsdominoes,Fons-iMac-2.local,20.11.2024 18:02,file:///Users/winsdominoes/Library/Application%20Support/LibreOffice/4;

View File

@ -1 +0,0 @@
,winsdominoes,Fons-iMac-2.local,19.11.2024 23:03,file:///Users/winsdominoes/Library/Application%20Support/LibreOffice/4;

View File

@ -808,7 +808,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 304,
"execution_count": 542,
"metadata": {},
"outputs": [],
"source": [
@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 305,
"execution_count": 543,
"metadata": {},
"outputs": [],
"source": [
@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 306,
"execution_count": 544,
"metadata": {},
"outputs": [],
"source": [
@ -49,7 +49,7 @@
},
{
"cell_type": "code",
"execution_count": 307,
"execution_count": 545,
"metadata": {},
"outputs": [],
"source": [
@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"execution_count": 308,
"execution_count": 546,
"metadata": {},
"outputs": [],
"source": [
@ -70,29 +70,35 @@
},
{
"cell_type": "code",
"execution_count": 309,
"execution_count": 547,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Iceland', 'India', 'Indonesia', 'Iraq', 'Ireland', 'Israel', 'Italy']"
"['Tajikistan',\n",
" 'Thailand',\n",
" 'Togo',\n",
" 'Trinidad and Tobago',\n",
" 'Tunisia',\n",
" 'Turkmenistan',\n",
" 'Türkiye']"
]
},
"execution_count": 309,
"execution_count": 547,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Countries that starts with the same letter that your name\n",
"StartsWith = 'I' # The first letter of 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": 310,
"execution_count": 548,
"metadata": {},
"outputs": [],
"source": [
@ -106,7 +112,7 @@
},
{
"cell_type": "code",
"execution_count": 311,
"execution_count": 549,
"metadata": {},
"outputs": [
{
@ -283,7 +289,7 @@
"max 0.590000 0.948000 0.843000 0.516000 "
]
},
"execution_count": 311,
"execution_count": 549,
"metadata": {},
"output_type": "execute_result"
}
@ -294,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": 312,
"execution_count": 550,
"metadata": {},
"outputs": [
{
@ -571,7 +577,7 @@
"[121 rows x 13 columns]"
]
},
"execution_count": 312,
"execution_count": 550,
"metadata": {},
"output_type": "execute_result"
}
@ -582,7 +588,7 @@
},
{
"cell_type": "code",
"execution_count": 313,
"execution_count": 551,
"metadata": {},
"outputs": [
{
@ -656,7 +662,7 @@
"0 Asia "
]
},
"execution_count": 313,
"execution_count": 551,
"metadata": {},
"output_type": "execute_result"
}
@ -667,7 +673,7 @@
},
{
"cell_type": "code",
"execution_count": 314,
"execution_count": 552,
"metadata": {},
"outputs": [
{
@ -741,7 +747,7 @@
"35 Northern Europe Europe "
]
},
"execution_count": 314,
"execution_count": 552,
"metadata": {},
"output_type": "execute_result"
}
@ -752,7 +758,7 @@
},
{
"cell_type": "code",
"execution_count": 315,
"execution_count": 553,
"metadata": {},
"outputs": [],
"source": [
@ -761,7 +767,7 @@
},
{
"cell_type": "code",
"execution_count": 316,
"execution_count": 554,
"metadata": {},
"outputs": [
{
@ -778,7 +784,7 @@
},
{
"cell_type": "code",
"execution_count": 317,
"execution_count": 555,
"metadata": {},
"outputs": [
{
@ -787,7 +793,7 @@
"np.float64(0.231)"
]
},
"execution_count": 317,
"execution_count": 555,
"metadata": {},
"output_type": "execute_result"
}
@ -801,7 +807,7 @@
},
{
"cell_type": "code",
"execution_count": 318,
"execution_count": 556,
"metadata": {},
"outputs": [
{
@ -810,7 +816,7 @@
"array([4.6125, 5.42 , 6.3215])"
]
},
"execution_count": 318,
"execution_count": 556,
"metadata": {},
"output_type": "execute_result"
}
@ -822,7 +828,7 @@
},
{
"cell_type": "code",
"execution_count": 319,
"execution_count": 557,
"metadata": {},
"outputs": [
{
@ -855,7 +861,7 @@
},
{
"cell_type": "code",
"execution_count": 320,
"execution_count": 558,
"metadata": {},
"outputs": [
{
@ -892,7 +898,7 @@
},
{
"cell_type": "code",
"execution_count": 321,
"execution_count": 559,
"metadata": {},
"outputs": [],
"source": [
@ -908,7 +914,7 @@
},
{
"cell_type": "code",
"execution_count": 322,
"execution_count": 560,
"metadata": {},
"outputs": [
{
@ -929,7 +935,7 @@
},
{
"cell_type": "code",
"execution_count": 323,
"execution_count": 561,
"metadata": {},
"outputs": [
{
@ -961,7 +967,7 @@
},
{
"cell_type": "code",
"execution_count": 324,
"execution_count": 562,
"metadata": {},
"outputs": [],
"source": [
@ -974,7 +980,7 @@
},
{
"cell_type": "code",
"execution_count": 325,
"execution_count": 563,
"metadata": {},
"outputs": [],
"source": [
@ -984,7 +990,7 @@
},
{
"cell_type": "code",
"execution_count": 326,
"execution_count": 564,
"metadata": {},
"outputs": [
{
@ -993,7 +999,7 @@
"(np.float64(-0.6037), np.float64(-0.6037))"
]
},
"execution_count": 326,
"execution_count": 564,
"metadata": {},
"output_type": "execute_result"
}
@ -1004,7 +1010,7 @@
},
{
"cell_type": "code",
"execution_count": 327,
"execution_count": 565,
"metadata": {},
"outputs": [],
"source": [
@ -1014,7 +1020,7 @@
},
{
"cell_type": "code",
"execution_count": 328,
"execution_count": 566,
"metadata": {},
"outputs": [
{
@ -1023,7 +1029,7 @@
"(np.float64(-0.6113), np.float64(-0.6113))"
]
},
"execution_count": 328,
"execution_count": 566,
"metadata": {},
"output_type": "execute_result"
}
@ -1034,7 +1040,7 @@
},
{
"cell_type": "code",
"execution_count": 329,
"execution_count": 567,
"metadata": {},
"outputs": [],
"source": [
@ -1052,7 +1058,7 @@
},
{
"cell_type": "code",
"execution_count": 330,
"execution_count": 568,
"metadata": {},
"outputs": [],
"source": [
@ -1062,7 +1068,7 @@
},
{
"cell_type": "code",
"execution_count": 331,
"execution_count": 569,
"metadata": {},
"outputs": [
{
@ -1071,7 +1077,7 @@
"(np.float64(3.0748), np.float64(3.0748))"
]
},
"execution_count": 331,
"execution_count": 569,
"metadata": {},
"output_type": "execute_result"
}
@ -1082,7 +1088,7 @@
},
{
"cell_type": "code",
"execution_count": 332,
"execution_count": 570,
"metadata": {},
"outputs": [],
"source": [
@ -1092,7 +1098,7 @@
},
{
"cell_type": "code",
"execution_count": 333,
"execution_count": 571,
"metadata": {},
"outputs": [
{
@ -1101,7 +1107,7 @@
"(np.float64(3.1292), np.float64(3.1292))"
]
},
"execution_count": 333,
"execution_count": 571,
"metadata": {},
"output_type": "execute_result"
}
@ -1112,7 +1118,7 @@
},
{
"cell_type": "code",
"execution_count": 334,
"execution_count": 572,
"metadata": {},
"outputs": [],
"source": [
@ -1122,7 +1128,7 @@
},
{
"cell_type": "code",
"execution_count": 335,
"execution_count": 573,
"metadata": {},
"outputs": [
{
@ -1131,7 +1137,7 @@
"(np.float64(0.0748), np.float64(0.0748))"
]
},
"execution_count": 335,
"execution_count": 573,
"metadata": {},
"output_type": "execute_result"
}
@ -1142,7 +1148,7 @@
},
{
"cell_type": "code",
"execution_count": 336,
"execution_count": 574,
"metadata": {},
"outputs": [],
"source": [
@ -1152,7 +1158,7 @@
},
{
"cell_type": "code",
"execution_count": 337,
"execution_count": 575,
"metadata": {},
"outputs": [
{
@ -1161,7 +1167,484 @@
"(np.float64(0.1292), np.float64(0.1292))"
]
},
"execution_count": 337,
"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"
}
@ -1173,7 +1656,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},

Binary file not shown.