diff --git a/lab3_confidence_intervals.ipynb b/lab3_confidence_intervals.ipynb new file mode 100644 index 0000000..b5013e8 --- /dev/null +++ b/lab3_confidence_intervals.ipynb @@ -0,0 +1,251 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "1y1Y7HzFTv8-" + }, + "source": [ + "# Confidence Intervals" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ltO2mrc5Tv9C" + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import scipy.stats as st" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UUlJf3hITv9D" + }, + "source": [ + "## Challenge 1\n", + "We want to estimate the average size of the men of a country with a confidence level of 80%. Assuming that the standard deviation of the sizes in the population is 4, get the confidence interval with a sample of men selected randomly, whose heights are:\n", + "\n", + "````\n", + "heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n", + "````\n", + "\n", + "**Hint**: function `stats.norm.interval` from `scipy` can help you get through this exercise." + ] + }, + { + "cell_type": "code", + "source": [ + "# so we know the std from the population, so I will use st.norm.interval\n", + "\n", + "samples_heights = pd.DataFrame([167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195], columns = [\"Heights\"])\n", + "\n", + "# 1. Sample = Population\n", + "samples_heights\n", + "\n", + "# 2. Mean\n", + "mean = samples_heights.Heights.mean()\n", + "\n", + "# 3.\n", + "n = 15\n", + "\n", + "# 4. std\n", + "std = 4\n", + "\n", + "heights_80_conf = st.norm.interval(0.80, loc = mean, scale = std/np.sqrt(15))\n", + "heights_80_conf" + ], + "metadata": { + "id": "zYxsDJ8_WMNm", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5fbcf45f-0d68-45b1-d5c1-3f5fe9ab6eed" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(172.14308590115726, 174.79024743217607)" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Tl05wHzJTv9E" + }, + "source": [ + "## Challenge 2\n", + "In a sample of 105 shops selected randomly from an area, we note that 27 of them have had losses in this month. Get an interval for the proportion of businesses in the area with losses to a confidence level of 80% and a confidence level of 90%.\n", + "\n", + "**Hint**: function `stats.norm.interval` from `scipy` can help you get through this exercise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "JZjNq1brTv9E", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4c69f9be-a4f1-46d8-b22c-4a159ac9daef" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Interval for the proportion of businesses in the area with losses to a confidence level of 80% is (0.20248138545542083, 0.3118043288302934)\n", + "Interval for the proportion of businesses in the area with losses to a confidence level of 90% is (0.1869856177645281, 0.3273000965211861)\n" + ] + } + ], + "source": [ + "## In a sample of size 𝑁 there are 𝑀 “successes” (say, people who clicked on an advertisement) and 𝑁−𝑀 “failures”\n", + "## (everyone else, who did not click on an advertisement).\n", + "\n", + "## The sample proportion is then: P = M/N\n", + "losses = 27\n", + "sample = 105\n", + "\n", + "## Sample proportion :\n", + "sample_p = 27/105\n", + "sample_p\n", + "\n", + "## N = sample\n", + "n = 105\n", + "\n", + "# to get the standard error\n", + "std_error = np.sqrt(sample_p * (1 - sample_p) / n)\n", + "std_error\n", + "\n", + "# the mean --- > μˆP = p # so the proportion of successes will actually work as my mean ---- i think so\n", + "\n", + "conf_at_80 = st.norm.interval(0.80, loc = sample_p, scale = std_error) # 104 because we actually have sample std\n", + "conf_at_90 = st.norm.interval(0.90, loc = sample_p, scale = std_error)\n", + "\n", + "print(\"Interval for the proportion of businesses in the area with losses to a confidence level of 80% is\",conf_at_80)\n", + "print(\"Interval for the proportion of businesses in the area with losses to a confidence level of 90% is\",conf_at_90)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k1MAs16CTv9F" + }, + "source": [ + "## Bonus Challenge\n", + "The error level or sampling error for the first challenge is given by the following expression:\n", + "$$Error = z_{\\frac{\\alpha}{2}}\\frac{\\sigma}{\\sqrt n}$$\n", + "Where z represents the value for N(0,1)\n", + "\n", + "\n", + "Suppose that with the previous data of challenge 1, and with a confidence level of\n", + "99% (that is, almost certainly) we want to estimate the average population size, so that the error level committed is not greater than half a centimeter.\n", + "\n", + "#### 1.- Determine what size the selected sample of men should be." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "nOtGr1YFTv9F" + }, + "outputs": [], + "source": [ + "# your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sz3nWefvTv9G" + }, + "source": [ + "#### 2.- For the second challenge, we have the following error:\n", + "$$ Error = z_{\\frac{\\alpha}{2}}\\sqrt{\\frac{p\\times q}{n}} $$\n", + "#### Determine the sample size required to not exceed an error of 1% with a confidence of 80%." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vrkUzGyATv9G" + }, + "outputs": [], + "source": [ + "# your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZAdJ8CzpTv9G" + }, + "source": [ + "## Bonus Challenge\n", + "\n", + "Let's consider the following problem:\n", + "\n", + "Build a confidence interval of 94% for the real difference between the durations of two brands of spotlights, if a sample of 40 spotlights taken randomly from the first mark gave an average duration of 418 hours, and a sample of 50 bulbs of another brand gave a duration average of 402 hours. The standard deviations of the two\n", + "populations are 26 hours and 22 hours, respectively.\n", + "\n", + "Sometimes, we will be interested in the difference of two different groups of random variables. We can also build a confidence interval for that! We have some different cases regarding the variance but for this specific case (the variance are different and known), we have that:\n", + "\n", + "$$\\overline{X} - \\overline{Y} \\sim N(\\mu_{X} - \\mu_{Y} , \\sqrt{\\frac{\\sigma_{X}^2}{n_X}+\\frac{\\sigma_{Y}^2}{n_Y}})$$\n", + "\n", + "Solve the problem with this information." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "inFcnAitTv9G" + }, + "outputs": [], + "source": [ + "# your code here" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/.gitignore b/your-code/.gitignore similarity index 100% rename from .gitignore rename to your-code/.gitignore diff --git a/README.md b/your-code/README.md similarity index 100% rename from README.md rename to your-code/README.md