From 0cd2e1ab33174a432abdcd809f6f94da50099dd7 Mon Sep 17 00:00:00 2001 From: Dulce-04 <136611956+Dulce-04@users.noreply.github.com> Date: Fri, 11 Aug 2023 15:28:12 +0100 Subject: [PATCH] Confidence Intervals --- your-code/main.ipynb | 153 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 134 insertions(+), 19 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 332f496..b66b54c 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -9,11 +9,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Libraries" + "import pandas as pd\n", + "import numpy as np\n", + "import scipy.stats as st" ] }, { @@ -32,11 +34,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(172.14308590115726, 174.79024743217607)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n", + "\n", + "mean = np.mean(heights)\n", + "alpha = 0.80\n", + "std = 4\n", + "n = len(heights)\n", + "\n", + "st.norm.interval(0.80, loc=mean, scale=std/np.sqrt(n))" ] }, { @@ -51,11 +71,82 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(0.2463369513350275, 0.2679487629506867)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "sample_size = 105\n", + "losses = 27\n", + "n = 105\n", + "\n", + "p = losses/ sample_size\n", + "\n", + "z_value = st.norm.ppf(1 - (0.80)/2)\n", + "\n", + "margin_of_error = z_value * np.sqrt((p * (1 - p))/n)\n", + "\n", + "lower_bound = p - margin_of_error\n", + "upper_bound = p + margin_of_error\n", + "\n", + "print(f'Our CI for proportion is [{lower_bound}, {upper_bound}]')\n", + "\n", + "st.norm.interval(1 - confidence_level_80, loc= mean, scale=std / np.sqrt(n))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Our CI for proportion is [0.25178307737433503, 0.2625026369113792]\n" + ] + }, + { + "data": { + "text/plain": [ + "(173.4176134996416, 173.51571983369175)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sample_size = 105\n", + "losses = 27\n", + "n = 105\n", + "\n", + "p = losses/ sample_size\n", + "\n", + "z_value = st.norm.ppf(1 - (0.90)/2)\n", + "\n", + "se = np.sqrt((p * (1 - p))/n) \n", + "\n", + "margin_of_error = z_value * se\n", + "\n", + "lower_bound = p - margin_of_error\n", + "upper_bound = p + margin_of_error\n", + "\n", + "print(f'Our CI for proportion is [{lower_bound}, {upper_bound}]')\n", + "\n", + "confidence_level_90 = 0.90\n", + "st.norm.interval(1 - confidence_level_90, loc= mean, scale=std / np.sqrt(n))" ] }, { @@ -76,11 +167,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2.0537489106318225" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "z = st.norm.ppf(0.98) \n", + "z" ] }, { @@ -94,11 +197,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sample Size: 4106\n" + ] + } + ], "source": [ - "# your code here" + "confidence_level = 0.80\n", + "desired_error = 0.01 \n", + "\n", + "z_alpha_over_2 = stats.norm.ppf(1 - (1 - confidence_level) / 2)\n", + "\n", + "sample_size = ((z_alpha_over_2**2 * 0.5 * 0.5) / desired_error**2)\n", + "print(\"Sample Size:\", round(sample_size))" ] }, { @@ -124,14 +241,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -145,7 +260,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4,