From 6e29e806087770d160981f3c2da39d92b87c647d Mon Sep 17 00:00:00 2001 From: Sergio Soutinho Date: Fri, 11 Aug 2023 15:27:01 +0100 Subject: [PATCH] lab done --- your-code/main.ipynb | 142 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 9 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 332f496..dea2b22 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -9,11 +9,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Libraries" + "# Libraries\n", + "import pandas as pd\n", + "import numpy as np\n", + "import scipy.stats as st" ] }, { @@ -30,13 +33,68 @@ "**Hint**: function `stats.norm.interval` from `scipy` can help you get through this exercise. " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "st.norm.interval - we use when we know the std of the population and n~of observation is greater than 30\n", + "st.t.interval - when we dont know the std of the population or nÂș of observation is less than 30" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(172.14308590115726, 174.79024743217607)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "# NORM DIST\n", + "\n", + "heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n", + "alpha = 0.80\n", + "std = 4\n", + "mean = np.mean(heights)\n", + "n = len(heights)\n", + "\n", + "st.norm.interval(0.80, loc = mean, scale = std/np.sqrt(n))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(170.9117270472475, 176.02160628608584)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# doing with T\n", + "\n", + "heights = np.array([167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195])\n", + "s = heights.std(ddof=1)\n", + "mean = heights.mean()\n", + "n = len(heights)\n", + "\n", + "st.t.interval(0.80, n-1, loc = mean, scale = s/np.sqrt(n))" ] }, { @@ -51,11 +109,77 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80%: [0.20248138545542083, 0.3118043288302934]\n", + "90%: [0.18698561776452813, 0.3273000965211861]\n" + ] + } + ], "source": [ - "# your code here" + "n = 105\n", + "losses = 27\n", + "p = losses / n\n", + "\n", + "z80 = st.norm.ppf(1-(1-0.8)/2)\n", + "moe80 = z80 * np.sqrt((p*(1-p))/n)\n", + "\n", + "\n", + "z90 = st.norm.ppf(1-(1-0.9)/2)\n", + "moe90 = z90 * np.sqrt((p*(1-p))/n)\n", + "\n", + "\n", + "CI80 = [p-moe80, p+moe80]\n", + "CI90 = [p-moe90, p+moe90]\n", + "print(\"80%: \", CI80)\n", + "print(\"90%: \", CI90)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0.20248138545542083, 0.3118043288302934)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# PYTHON WAY\n", + "st.norm.interval(0.80, loc = p, scale = np.sqrt((p*(1-p))/n))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0.1869856177645281, 0.3273000965211861)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# PYTHON WAY\n", + "st.norm.interval(0.90, loc = p, scale = np.sqrt((p*(1-p))/n))" ] }, { @@ -145,7 +269,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4,