Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lab-hypothesis-testing.ipynb #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 147 additions & 29 deletions lab-hypothesis-testing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -57,21 +57,7 @@
{
"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",
"<div><div id=151c5387-9920-4c01-afd7-5688dbd37e71 style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">See Full Dataframe in Mito</div> <script> if (window.commands?.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('151c5387-9920-4c01-afd7-5688dbd37e71').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
Expand Down Expand Up @@ -244,9 +230,7 @@
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>800 rows × 11 columns</p>\n",
"</div>"
"</table></div>"
],
"text/plain": [
" Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n",
Expand Down Expand Up @@ -297,11 +281,39 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Prueba t de Student (unilateral):\n",
"t-stat = 3.590444254130357, p-value (unilateral) = 0.00025679691501534816\n",
"\n",
"Conclusión: Rechazamos la hipótesis nula. Los Pokémon de tipo Dragon tienen, en promedio, más HP que los de tipo Grass.\n"
]
}
],
"source": [
"#code here"
"from scipy.stats import ttest_ind\n",
"\n",
"# Prueba t de Student (sin especificar 'alternative')\n",
"t_stat, p_value_two_tailed = ttest_ind(dragon_hp, grass_hp, equal_var=True)\n",
"\n",
"# Ajustar el p-valor para una prueba unilateral\n",
"p_value_one_tailed = p_value_two_tailed / 2 if t_stat > 0 else 1.0\n",
"\n",
"print(\"\\nPrueba t de Student (unilateral):\")\n",
"print(f\"t-stat = {t_stat}, p-value (unilateral) = {p_value_one_tailed}\")\n",
"\n",
"# Interpretación de resultados\n",
"alpha = 0.05\n",
"if p_value_one_tailed < alpha:\n",
" print(\"\\nConclusión: Rechazamos la hipótesis nula. Los Pokémon de tipo Dragon tienen, en promedio, más HP que los de tipo Grass.\")\n",
"else:\n",
" print(\"\\nConclusión: No hay suficiente evidencia para rechazar la hipótesis nula. No podemos afirmar que los Pokémon de tipo Dragon tienen más HP que los de tipo Grass.\")\n"
]
},
{
Expand All @@ -313,11 +325,89 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Analizando HP:\n",
"Prueba de normalidad - Legendary: p=0.008644964545965195, Non-Legendary: p=6.934747223262693e-22\n",
"Prueba de homogeneidad de varianzas: p=0.5758428070138398\n",
"Prueba t: t-stat=8.036124405043928, p-value=3.330647684846191e-15\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en HP entre Pokémon Legendarios y No Legendarios.\n",
"\n",
"Analizando Attack:\n",
"Prueba de normalidad - Legendary: p=0.03771796450018883, Non-Legendary: p=3.1845864612023433e-08\n",
"Prueba de homogeneidad de varianzas: p=0.9944645877795636\n",
"Prueba t: t-stat=10.397321023700622, p-value=7.827253003205333e-24\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en Attack entre Pokémon Legendarios y No Legendarios.\n",
"\n",
"Analizando Defense:\n",
"Prueba de normalidad - Legendary: p=0.0043097008019685745, Non-Legendary: p=1.892132637823198e-18\n",
"Prueba de homogeneidad de varianzas: p=0.3203429011757649\n",
"Prueba t: t-stat=7.181240122992339, p-value=1.5842226094427259e-12\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en Defense entre Pokémon Legendarios y No Legendarios.\n",
"\n",
"Analizando Sp. Atk:\n",
"Prueba de normalidad - Legendary: p=0.6240566372871399, Non-Legendary: p=1.0735815882123578e-11\n",
"Prueba de homogeneidad de varianzas: p=0.37014131900583735\n",
"Prueba t: t-stat=14.191406210846289, p-value=6.314915770427266e-41\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en Sp. Atk entre Pokémon Legendarios y No Legendarios.\n",
"\n",
"Analizando Sp. Def:\n",
"Prueba de normalidad - Legendary: p=0.00823843665421009, Non-Legendary: p=8.440728584829804e-13\n",
"Prueba de homogeneidad de varianzas: p=0.7764108095205825\n",
"Prueba t: t-stat=11.037751061205222, p-value=1.8439809580409333e-26\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en Sp. Def entre Pokémon Legendarios y No Legendarios.\n",
"\n",
"Analizando Speed:\n",
"Prueba de normalidad - Legendary: p=0.0022167644929140806, Non-Legendary: p=2.4617634153401013e-07\n",
"Prueba de homogeneidad de varianzas: p=0.0017862391470896439\n",
"Prueba t: t-stat=11.47504444631443, p-value=1.0490163118824507e-18\n",
"Conclusión: Rechazamos H0. Hay diferencias significativas en Speed entre Pokémon Legendarios y No Legendarios.\n"
]
}
],
"source": [
"#code here"
"#code here\n",
"from scipy.stats import ttest_ind, shapiro, levene\n",
"\n",
"# Seleccionar columnas relevantes\n",
"stats = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']\n",
"\n",
"# Separar datos por legendarios y no legendarios\n",
"legendary = df[df['Legendary'] == True]\n",
"non_legendary = df[df['Legendary'] == False]\n",
"\n",
"# Iterar sobre cada estadística\n",
"for stat in stats:\n",
" print(f\"\\nAnalizando {stat}:\")\n",
"\n",
" # Datos de cada grupo\n",
" legendary_stat = legendary[stat]\n",
" non_legendary_stat = non_legendary[stat]\n",
"\n",
" # Prueba de normalidad\n",
" normal_legendary = shapiro(legendary_stat)\n",
" normal_non_legendary = shapiro(non_legendary_stat)\n",
" print(f\"Prueba de normalidad - Legendary: p={normal_legendary.pvalue}, Non-Legendary: p={normal_non_legendary.pvalue}\")\n",
"\n",
" # Prueba de homogeneidad de varianzas\n",
" var_test = levene(legendary_stat, non_legendary_stat)\n",
" print(f\"Prueba de homogeneidad de varianzas: p={var_test.pvalue}\")\n",
"\n",
" # Prueba t de Student\n",
" t_stat, p_value = ttest_ind(legendary_stat, non_legendary_stat, equal_var=(var_test.pvalue > 0.05))\n",
" print(f\"Prueba t: t-stat={t_stat}, p-value={p_value}\")\n",
"\n",
" # Conclusión\n",
" if p_value < 0.05:\n",
" print(f\"Conclusión: Rechazamos H0. Hay diferencias significativas en {stat} entre Pokémon Legendarios y No Legendarios.\")\n",
" else:\n",
" print(f\"Conclusión: No hay evidencia suficiente para rechazar H0. No hay diferencias significativas en {stat} entre Pokémon Legendarios y No Legendarios.\")\n"
]
},
{
Expand Down Expand Up @@ -483,10 +573,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prueba t de Student:\n",
"t-stat = -2.2146147257665834, p-value (unilateral) = 1.0\n",
"\n",
"Conclusión: No hay suficiente evidencia para rechazar H0. No podemos afirmar que las casas cercanas a una escuela o un hospital son más caras.\n"
]
}
],
"source": [
"# Prueba t de Student (sin 'alternative')\n",
"t_stat, p_value_two_tailed = ttest_ind(close_prices, far_prices, equal_var=True)\n",
"\n",
"# Ajustar el p-valor para una prueba unilateral\n",
"p_value_one_tailed = p_value_two_tailed / 2 if t_stat > 0 else 1.0\n",
"\n",
"# Resultados\n",
"print(\"Prueba t de Student:\")\n",
"print(f\"t-stat = {t_stat}, p-value (unilateral) = {p_value_one_tailed}\")\n",
"\n",
"# Interpretación\n",
"alpha = 0.05\n",
"if p_value_one_tailed < alpha:\n",
" print(\"\\nConclusión: Rechazamos H0. Las casas cercanas a una escuela o un hospital son significativamente más caras.\")\n",
"else:\n",
" print(\"\\nConclusión: No hay suficiente evidencia para rechazar H0. No podemos afirmar que las casas cercanas a una escuela o un hospital son más caras.\")\n"
]
},
{
"cell_type": "code",
Expand All @@ -512,7 +630,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.7.9"
}
},
"nbformat": 4,
Expand Down