Added mre MAT-203 stuff

This commit is contained in:
Win 2024-11-19 23:03:48 +07:00
parent 039f333a8c
commit bd1c9f8db8
4 changed files with 428 additions and 76 deletions

View File

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

View File

@ -4,7 +4,19 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'numpy'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#Numerical Python\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# To plot\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'numpy'"
]
}
],
"source": [ "source": [
"#Numerical Python\n", "#Numerical Python\n",
"import numpy as np\n", "import numpy as np\n",
@ -810,7 +822,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.12.4" "version": "3.13.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 278,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -22,7 +22,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 279,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -32,7 +32,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 280,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -49,7 +49,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 281,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -59,7 +59,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 282,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -70,7 +70,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 283,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -79,7 +79,7 @@
"['Iceland', 'India', 'Indonesia', 'Iraq', 'Ireland', 'Israel', 'Italy']" "['Iceland', 'India', 'Indonesia', 'Iraq', 'Ireland', 'Israel', 'Italy']"
] ]
}, },
"execution_count": 26, "execution_count": 283,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -92,7 +92,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 284,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -106,7 +106,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 285,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -283,7 +283,7 @@
"max 0.590000 0.948000 0.843000 0.516000 " "max 0.590000 0.948000 0.843000 0.516000 "
] ]
}, },
"execution_count": 28, "execution_count": 285,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -294,7 +294,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 286,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -571,7 +571,7 @@
"[121 rows x 13 columns]" "[121 rows x 13 columns]"
] ]
}, },
"execution_count": 29, "execution_count": 286,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -582,7 +582,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 287,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -623,18 +623,18 @@
" </thead>\n", " </thead>\n",
" <tbody>\n", " <tbody>\n",
" <tr>\n", " <tr>\n",
" <th>13</th>\n", " <th>0</th>\n",
" <td>Afghanistan</td>\n", " <td>Afghanistan</td>\n",
" <td>2022</td>\n", " <td>2023</td>\n",
" <td>1.281</td>\n", " <td>1.446</td>\n",
" <td>NaN</td>\n", " <td>NaN</td>\n",
" <td>0.228</td>\n",
" <td>54.875</td>\n",
" <td>0.368</td>\n", " <td>0.368</td>\n",
" <td>55.2</td>\n",
" <td>0.228</td>\n",
" <td>NaN</td>\n", " <td>NaN</td>\n",
" <td>0.733</td>\n", " <td>0.738</td>\n",
" <td>0.206</td>\n", " <td>0.261</td>\n",
" <td>0.576</td>\n", " <td>0.46</td>\n",
" <td>Southern Asia</td>\n", " <td>Southern Asia</td>\n",
" <td>Asia</td>\n", " <td>Asia</td>\n",
" </tr>\n", " </tr>\n",
@ -643,31 +643,31 @@
"</div>" "</div>"
], ],
"text/plain": [ "text/plain": [
" Country name year Life Ladder Log GDP per capita Social support \\\n", " Country name year Life Ladder Log GDP per capita Social support \\\n",
"13 Afghanistan 2022 1.281 NaN 0.228 \n", "0 Afghanistan 2023 1.446 NaN 0.368 \n",
"\n", "\n",
" Healthy life expectancy at birth Freedom to make life choices \\\n", " Healthy life expectancy at birth Freedom to make life choices Generosity \\\n",
"13 54.875 0.368 \n", "0 55.2 0.228 NaN \n",
"\n", "\n",
" Generosity Perceptions of corruption Positive affect Negative affect \\\n", " Perceptions of corruption Positive affect Negative affect Subregion \\\n",
"13 NaN 0.733 0.206 0.576 \n", "0 0.738 0.261 0.46 Southern Asia \n",
"\n", "\n",
" Subregion Continent \n", " Continent \n",
"13 Southern Asia Asia " "0 Asia "
] ]
}, },
"execution_count": 40, "execution_count": 287,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"Dat.loc[Dat[\"Life Ladder\"] == Dat[\"Life Ladder\"].min()]" "Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].min()]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 288,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -708,18 +708,18 @@
" </thead>\n", " </thead>\n",
" <tbody>\n", " <tbody>\n",
" <tr>\n", " <tr>\n",
" <th>497</th>\n", " <th>35</th>\n",
" <td>Denmark</td>\n", " <td>Finland</td>\n",
" <td>2005</td>\n", " <td>2023</td>\n",
" <td>8.019</td>\n", " <td>7.699</td>\n",
" <td>10.849</td>\n", " <td>10.808</td>\n",
" <td>0.972</td>\n", " <td>0.947</td>\n",
" <td>68.3</td>\n", " <td>71.3</td>\n",
" <td>0.971</td>\n", " <td>0.943</td>\n",
" <td>NaN</td>\n", " <td>-0.001</td>\n",
" <td>0.237</td>\n", " <td>0.185</td>\n",
" <td>0.777</td>\n", " <td>0.717</td>\n",
" <td>0.154</td>\n", " <td>0.173</td>\n",
" <td>Northern Europe</td>\n", " <td>Northern Europe</td>\n",
" <td>Europe</td>\n", " <td>Europe</td>\n",
" </tr>\n", " </tr>\n",
@ -728,47 +728,47 @@
"</div>" "</div>"
], ],
"text/plain": [ "text/plain": [
" Country name year Life Ladder Log GDP per capita Social support \\\n", " Country name year Life Ladder Log GDP per capita Social support \\\n",
"497 Denmark 2005 8.019 10.849 0.972 \n", "35 Finland 2023 7.699 10.808 0.947 \n",
"\n", "\n",
" Healthy life expectancy at birth Freedom to make life choices \\\n", " Healthy life expectancy at birth Freedom to make life choices \\\n",
"497 68.3 0.971 \n", "35 71.3 0.943 \n",
"\n", "\n",
" Generosity Perceptions of corruption Positive affect Negative affect \\\n", " Generosity Perceptions of corruption Positive affect Negative affect \\\n",
"497 NaN 0.237 0.777 0.154 \n", "35 -0.001 0.185 0.717 0.173 \n",
"\n", "\n",
" Subregion Continent \n", " Subregion Continent \n",
"497 Northern Europe Europe " "35 Northern Europe Europe "
] ]
}, },
"execution_count": 43, "execution_count": 288,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"Dat.loc[Dat[\"Life Ladder\"] == Dat[\"Life Ladder\"].max()]" "Dat2023.loc[Dat2023[\"Life Ladder\"] == Dat2023[\"Life Ladder\"].max()]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 48, "execution_count": 289,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"avg_health = Dat[\"Healthy life expectancy at birth\"].median()" "avg_health = Dat2023[\"Healthy life expectancy at birth\"].median()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": 290,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"65.22\n" "66.3\n"
] ]
} }
], ],
@ -778,7 +778,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": 291,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -787,7 +787,7 @@
"np.float64(0.231)" "np.float64(0.231)"
] ]
}, },
"execution_count": 56, "execution_count": 291,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -801,7 +801,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 292,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -810,7 +810,7 @@
"array([4.6125, 5.42 , 6.3215])" "array([4.6125, 5.42 , 6.3215])"
] ]
}, },
"execution_count": 57, "execution_count": 292,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -822,34 +822,373 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 64, "execution_count": 293,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"5.42\n", "5.868\n",
"5.4660082404265635\n", "5.601454545454545\n",
"5.3416056329049155\n", "5.46003534802306\n",
"5.207292342092034\n", "5.277807689332831\n",
"5.446561471442401\n", "5.670474226804124\n",
"1.7089999999999996\n", "5.738901639344262\n",
"5.4660082404265635\n" "5.601454545454545\n"
] ]
} }
], ],
"source": [ "source": [
"ll = Dat[\"Life Ladder\"]\n", "ll = Dat2023[\"Life Ladder\"]\n",
"\n",
"Mean = ll.mean()\n",
"\n", "\n",
"print(ll.median())\n", "print(ll.median())\n",
"print(ll.mean())\n", "print(Mean)\n",
"print(gmean(ll))\n", "print(gmean(ll))\n",
"print(hmean(ll))\n", "print(hmean(ll))\n",
"print(trim_mean(ll, 0.1))\n",
"print(trim_mean(ll, 0.25))\n", "print(trim_mean(ll, 0.25))\n",
"print(iqr(ll).mean())\n",
"print(winsorize(ll).mean())" "print(winsorize(ll).mean())"
] ]
},
{
"cell_type": "code",
"execution_count": 294,
"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": 295,
"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": 296,
"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": 297,
"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": 298,
"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": 299,
"metadata": {},
"outputs": [],
"source": [
"g1_byhand = m3 / m2**(3/2)\n",
"g1 = skew(ll)"
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(np.float64(-0.6037), np.float64(-0.6037))"
]
},
"execution_count": 300,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g1_byhand.round(4), g1.round(4)"
]
},
{
"cell_type": "code",
"execution_count": 301,
"metadata": {},
"outputs": [],
"source": [
"G1_byhand = k3 / k2**(3/2)\n",
"G1 = skew(ll, bias=False)"
]
},
{
"cell_type": "code",
"execution_count": 302,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(np.float64(-0.6113), np.float64(-0.6113))"
]
},
"execution_count": 302,
"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(4.8753), np.float64(4.8753))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g2_byhand.round(4), g2.round(4)"
]
},
{
"cell_type": "code",
"execution_count": 303,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'k4' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[303], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m G2_byhand \u001b[38;5;241m=\u001b[39m \u001b[43mk4\u001b[49m\u001b[38;5;241m/\u001b[39mk2\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[1;32m 2\u001b[0m G2 \u001b[38;5;241m=\u001b[39m kurtosis(ll, fisher\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
"\u001b[0;31mNameError\u001b[0m: name 'k4' is not defined"
]
}
],
"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(7.2795), np.float64(7.2795))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"G2_byhand.round(4), G2.round(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'g2_byhand' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[251], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m g2_excess_byhand \u001b[38;5;241m=\u001b[39m \u001b[43mg2_byhand\u001b[49m \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[1;32m 2\u001b[0m g2_excess \u001b[38;5;241m=\u001b[39m kurtosis(ll)\n",
"\u001b[0;31mNameError\u001b[0m: name 'g2_byhand' is not defined"
]
}
],
"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(1.8753), np.float64(1.8753))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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(4.2795), np.float64(4.2795))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"G2_excess_byhand.round(4), G2_excess.round(4)"
]
} }
], ],
"metadata": { "metadata": {
@ -868,7 +1207,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.12.6" "version": "3.13.0"
} }
}, },
"nbformat": 4, "nbformat": 4,