From 94930a44d0cb73670ccdba78609989e72feedec2 Mon Sep 17 00:00:00 2001 From: PedroFariaBlanc <82642165+PedroFariaBlanc@users.noreply.github.com> Date: Wed, 17 May 2023 11:36:46 +0100 Subject: [PATCH] [lab-confidence-intervals] Pedro Faria Blanc --- your-code/main.ipynb | 121 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 12 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 332f496..72d47f1 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -9,14 +10,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Libraries" + "# Libraries\n", + "\n", + "import numpy as np\n", + "import scipy.stats as st" ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -32,14 +37,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "172.59744399428203\n", + "174.3358893390513\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# confidence level of 80%\n", + "confidence_level = 0.80\n", + "\n", + "z_score = st.norm.ppf(confidence_level)\n", + "\n", + "heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n", + "\n", + "mean = np.mean(heights)\n", + "\n", + "# Assuming that the standard deviation of the sizes in the population is 4\n", + "std = 4\n", + "n = len(heights)\n", + "\n", + "left = mean - z_score * (std / np.sqrt(n))\n", + "right = mean + z_score * (std / np.sqrt(n))\n", + "\n", + "print(left)\n", + "print(right)" ] }, { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -51,14 +92,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.32793330928514125\n", + "0.18635240500057298\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# confidence level of 80%\n", + "confidence_level = 0.80\n", + "\n", + "# In a sample of 105 shops with 27 losses \n", + "shops = 105\n", + "shops_losses = 27\n", + "sample_proportion = shops_losses / shops\n", + "\n", + "z_score = st.norm.ppf(confidence_level)\n", + "\n", + "error_margin = z_score* np.sqrt((sample_proportion * (1 - sample_proportion)) / 27)\n", + "\n", + "positive = sample_proportion + error_margin\n", + "negative = sample_proportion - error_margin\n", + "\n", + "print(positive)\n", + "print(negative)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0.07983770947987007, 0.4344480048058441)\n" + ] + } + ], + "source": [ + "# for the 90% confidence level\n", + "confidence_level = 0.90\n", + "\n", + "z_score = st.norm.ppf(confidence_level)\n", + "\n", + "error_margin1 = z_score * np.sqrt((sample_proportion * (1 - sample_proportion)) / 27)\n", + "\n", + "ci = st.norm.interval(0.90, loc = sample_proportion, scale = error_margin1)\n", + "\n", + "print(ci)" ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -76,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -84,6 +179,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -94,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -102,6 +198,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -121,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -145,7 +242,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.3" } }, "nbformat": 4,