diff --git a/lab-hypothesis-testing.ipynb b/lab-hypothesis-testing.ipynb index 0cc26d5..d81a022 100644 --- a/lab-hypothesis-testing.ipynb +++ b/lab-hypothesis-testing.ipynb @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -278,7 +278,7 @@ "[800 rows x 11 columns]" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -297,11 +297,52 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#code here\n", + "#H0: dragon_hp <= grass_hp\n", + "#H1: dragon_hp > grass_hp \n", + "# significance: 0.05\n", + "# one tailed" + ] + }, + { + "cell_type": "code", + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "#code here" + "df_dragon = df[df[\"Type 1\"]==\"Dragon\"][\"HP\"]\n", + "df_grass = df[df[\"Type 1\"]==\"Grass\"][\"HP\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "TtestResult(statistic=np.float64(3.590444254130357), pvalue=np.float64(0.0005135938300306962), df=np.float64(100.0))" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.ttest_ind(df_dragon, df_grass)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can reject the null hypothesis, since the pvalue is 0.0005, stadistically lower than our significance value." ] }, { @@ -313,11 +354,55 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "HP\n", + "F_onewayResult(statistic=np.float64(64.5792954533426), pvalue=np.float64(3.330647684845447e-15))\n", + "\n", + "Attack\n", + "F_onewayResult(statistic=np.float64(108.10428446988696), pvalue=np.float64(7.827253003204001e-24))\n", + "\n", + "Defense\n", + "F_onewayResult(statistic=np.float64(51.57020970407507), pvalue=np.float64(1.584222609442392e-12))\n", + "\n", + "Sp. Atk\n", + "F_onewayResult(statistic=np.float64(201.3960102412466), pvalue=np.float64(6.314915770426125e-41))\n", + "\n", + "Sp. Def\n", + "F_onewayResult(statistic=np.float64(121.83194848913693), pvalue=np.float64(1.8439809580406472e-26))\n", + "\n", + "Speed\n", + "F_onewayResult(statistic=np.float64(95.35980155754147), pvalue=np.float64(2.354075443689382e-21))\n" + ] + } + ], + "source": [ + "#code here\n", + "#H0: legendary_pokemons = non_legendary_pokemons\n", + "#H1: legendary_pokemons != non_legendary_pokemons\n", + "# significance: 0.05\n", + "\n", + "stats = [\"HP\", \"Attack\", \"Defense\", \"Sp. Atk\", \"Sp. Def\", \"Speed\"]\n", + "\n", + "for stat in stats:\n", + " legendary_pokemon = df[df[\"Legendary\"] == True][stat]\n", + " non_legendary_pokemon = df[df[\"Legendary\"] == False][stat]\n", + "\n", + " print(f\"\\n{stat}\")\n", + " print(st.f_oneway(legendary_pokemon, non_legendary_pokemon))\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "#code here" + "We can reject the null hypothesis, for the pvalues are below our significance value." ] }, { @@ -337,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -453,7 +538,7 @@ "4 624.0 262.0 1.9250 65500.0 " ] }, - "execution_count": 5, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -483,22 +568,161 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import numpy as np\n", + "\n", + "def euclidean_distance(x1, y1, x2, y2):\n", + " return np.sqrt((x2 - x1)**2 + (y2 - y1)**2)" + ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], + "source": [ + "# Distance to the school\n", + "df[\"distance_school\"] = df.apply(\n", + " lambda row: euclidean_distance(\n", + " row[\"longitude\"],\n", + " row[\"latitude\"],\n", + " -118,\n", + " 34\n", + " ),\n", + " axis=1\n", + ")\n", + "\n", + "# Distance to the hospital\n", + "df[\"distance_hospital\"] = df.apply(\n", + " lambda row: euclidean_distance(\n", + " row[\"longitude\"],\n", + " row[\"latitude\"],\n", + " -122,\n", + " 37\n", + " ),\n", + " axis=1\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 8.187319\n", + "1 7.966235\n", + "2 8.143077\n", + "3 8.154416\n", + "4 8.183508\n", + " ... \n", + "16995 4.233675\n", + "16996 4.332320\n", + "16997 5.358694\n", + "16998 5.322593\n", + "16999 4.249012\n", + "Name: distance_hospital, Length: 17000, dtype: float64" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[\"distance_hospital\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "close\n", + "False 10171\n", + "True 6829\n", + "Name: count, dtype: int64" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[\"close\"] = (\n", + " (df[\"distance_school\"] < 0.50) |\n", + " (df[\"distance_hospital\"] < 0.50)\n", + ")\n", + "\n", + "df[\"close\"].value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "#H0: close_houses <= far_houses\n", + "#H1: close_houses > far_houses\n", + "#significance: 0.05" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "close_houses = df[df[\"close\"] == True][\"median_house_value\"]\n", + "far_houses = df[df[\"close\"] == False][\"median_house_value\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "TtestResult(statistic=np.float64(38.04632342033554), pvalue=np.float64(4.817835891327844e-304), df=np.float64(16998.0))" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.ttest_ind(close_houses, far_houses)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can say that if we were potential real state customers interested in buying a house close to a school or a hospital, this result is statistically significant." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -512,7 +736,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.13.9" } }, "nbformat": 4,