Skip to content
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
154 changes: 145 additions & 9 deletions your_code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,67 @@
"Based on these results, we create a Poisson distribution with the sample mean parameter = 2.435. Is there any reason to believe that at a .05 level the number of scores is a Poisson variable?"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import scipy.stats as st\n",
"\n",
"from scipy.stats import poisson\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Power_divergenceResult(statistic=6.491310681109821, pvalue=0.4836889068537269)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#H0: does it fall into a poisson distribution?\n",
"\n",
"significance = 0.05\n",
"\n",
"mu = 2.435\n",
"\n",
"f_obs = np.array([35, 99, 104, 110, 62, 25, 10, 3])\n",
"\n",
"\n",
"poisson_dist = poisson(mu)\n",
"\n",
"\n",
"\n",
"#poisson_dist = poisson(mu)\n",
"\n",
"poisson_pmfs = np.array([poisson_dist.pmf(i) for i in range(0,7)])\n",
"\n",
"poisson_pmfs_last = np.append(poisson_pmfs, 1 - poisson_pmfs.sum())\n",
"\n",
"f_exp = poisson_pmfs_last*448\n",
"\n",
"st.chisquare(f_exp = f_exp, f_obs = f_obs)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# your answer here"
"# bevause p vaule is higher than significante level, we can NOT reject H0.\n"
]
},
{
Expand Down Expand Up @@ -60,13 +114,68 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"We can reject the null hypothesis\n"
]
},
{
"data": {
"text/plain": [
"0.015715783395950946"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your answer here"
"import scipy.stats as st\n",
"from scipy.stats import binom\n",
"import numpy as np\n",
"\n",
"\n",
"n = 10\n",
"p = 0.05\n",
"alpha = 0.05\n",
"\n",
"observed = np.array([138, 53, 9])\n",
"\n",
"population = 200\n",
"\n",
"binom_dist = binom(n, p)\n",
"\n",
"binom_pmfs = np.array([binom_dist.pmf(i) for i in range(0,2)])\n",
"\n",
"tail = 1 - binom_pmfs.sum()\n",
"\n",
"binom_with_tail = np.append(binom_pmfs, tail)\n",
"\n",
"exp = binom_with_tail * population\n",
"\n",
"chisquare_result = st.chisquare(f_obs = observed, f_exp = exp)\n",
"\n",
"if chisquare_result.pvalue < alpha:\n",
" print(\"We can reject the null hypothesis\")\n",
"else:\n",
" print(\"We can not reject the null hypothesis\")\n",
"\n",
"chisquare_result.pvalue "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -79,17 +188,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"Chi2ContingencyResult(statistic=10.712198008709638, pvalue=0.004719280137040844, dof=2, expected_freq=array([[24.08421053, 19.91578947],\n",
" [19.70526316, 16.29473684],\n",
" [ 8.21052632, 6.78947368]]))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#your answer here"
"\n",
"# H0: physical activity is independent of sugary drinks comsuption\n",
"# H1: physical activity is not independent of sugary drinks comsuption\n",
"\n",
"alpha = 0.05\n",
"\n",
"\n",
"children = [[32, 12],\n",
" [14, 22],\n",
" [6,9]]\n",
"\n",
"st.chi2_contingency(children)\n",
"\n",
"\n",
"# there pvalue is too low, we reject h0"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -103,7 +239,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down