From 188f918ba0a211d8a294a223a07eee1c082db884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cdanielmdepaoli=E2=80=9D?= <“danielmdepaoli@gmail.com”> Date: Mon, 14 Aug 2023 09:03:15 +0100 Subject: [PATCH] Lab Done --- .../{main.ipynb => GoodnessFitLab.ipynb} | 113 ++++++++++++++++-- 1 file changed, 103 insertions(+), 10 deletions(-) rename your_code/{main.ipynb => GoodnessFitLab.ipynb} (52%) diff --git a/your_code/main.ipynb b/your_code/GoodnessFitLab.ipynb similarity index 52% rename from your_code/main.ipynb rename to your_code/GoodnessFitLab.ipynb index 7810ccf..9377d30 100644 --- a/your_code/main.ipynb +++ b/your_code/GoodnessFitLab.ipynb @@ -14,11 +14,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "import scipy.stats as st\n", + "import numpy as np\n", + "from scipy.stats import geom\n", + "from scipy.stats import poisson" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Power_divergenceResult(statistic=nan, pvalue=nan)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = 2.435\n", + "\n", + "geom_dist = geom(p)\n", + "\n", + "geom_pmfs = np.array([geom_dist.pmf(i) for i in range(0, 7)])\n", + "\n", + "geom_pmfs_tail = np.append(geom_pmfs, 1 - geom_dist.cdf(7)) \n", + "\n", + "f_exp = geom_pmfs_tail* 200 # WHAT IS THIS 200???\n", + "\n", + "f_obs = np.array([35, 99, 104, 110, 62, 25, 10, 3])\n", + "\n", + "st.chisquare(f_obs=f_obs, f_exp=f_exp)" ] }, { @@ -60,11 +95,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We CAN reject the null hypothesis\n", + "p-value: 0.011503557379296871\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/d9/783c_j055nj0b0y037qm_1zc0000gn/T/ipykernel_37151/92150982.py:13: DeprecationWarning: 'binom_test' is deprecated in favour of 'binomtest' from version 1.7.0 and will be removed in Scipy 1.12.0.\n", + " p_value = st.binom_test(defective_tires, n, p)\n" + ] + } + ], "source": [ - "# your answer here" + "n = 10\n", + "p = 0.05\n", + "\n", + "# σ = sqrt(n * p * (1 - p)) = sqrt(10 * 0.05 * (1 - 0.05)) ≈ 0.71\n", + "# z = (x - μ) / σ\n", + "# Expected number of defective tires (μ) = p * n = 0.05 * 10 = 0.5\n", + "# The observed number of defective tires x = 14 (recorded over 200 days).\n", + "\n", + "z = (14 - 0.5) / 0.71\n", + "\n", + "defective_tires = 3\n", + "\n", + "p_value = st.binom_test(defective_tires, n, p)\n", + "if p_value < alpha:\n", + " print(\"We CAN reject the null hypothesis\")\n", + "else:\n", + " print(\"We CAN NOT reject the null hypothesis\")\n", + "print(\"p-value:\", p_value)" ] }, { @@ -79,17 +148,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, + "metadata": {}, + "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": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kids_table = [[32, 12], [14, 22], [6, 9]]\n", + "\n", + "alpha = 0.05\n", + "\n", + "st.chi2_contingency(kids_table)" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "#your answer here" + "Hypothesis Rejected " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -103,7 +196,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.9" } }, "nbformat": 4,