{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "XeV1U7GkVNZY" }, "source": [ "## Importing necessary libraries" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "219CEYUFVNZn" }, "outputs": [], "source": [ "# Libraries to help with reading and manipulating data\n", "import pandas as pd\n", "import numpy as np\n", "\n", "# Libraries to help with data visualization\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "%matplotlib inline \n", "\n", "# Library to help with statistical analysis\n", "import scipy.stats as stats " ] }, { "cell_type": "markdown", "metadata": { "id": "cfuKFTnCDTeA" }, "source": [ "## Sampling Distribution" ] }, { "cell_type": "markdown", "metadata": { "id": "azEQu4DoC_Q5" }, "source": [ "**Q1. Suppose an automobile battery manufacturer claims that the mean lifetime of their battery is 60 months with a standard deviation of 6 months. Suppose the distribution of battery life is approximately normal. Find the probability that the mean lifetime of 40 randomly sampled batteries will be less than 58 months.**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AiWFb0LCC9Wp", "outputId": "6d0dd09c-a184-4aac-daba-8fd1f2ec4d3d" }, "outputs": [ { "data": { "text/plain": [ "0.0175" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# import the required function\n", "from scipy.stats import norm\n", "# declare the value of mean lifetime of battery in mu\n", "mu = 60\n", "# declare the value of standard deviation of battery\n", "sigma = 6\n", "# sample size\n", "n = 40\n", "# find the sample standard deviation\n", "s = sigma/np.sqrt(40)\n", "# find the probability\n", "round(norm.cdf(58, loc = mu, scale = s), 4)" ] }, { "cell_type": "markdown", "metadata": { "id": "BSBuNii8HHC0" }, "source": [ "**Insight:**\n", "\n", "* There is less than 2% chance that the mean lifetime of 40 randomly sampled batteries will be less than 58 months." ] }, { "cell_type": "markdown", "metadata": { "id": "1TFwIVHqBR8O" }, "source": [ "## Interval Estimation" ] }, { "cell_type": "markdown", "metadata": { "id": "hyfZcgYs_LMD" }, "source": [ "**Q2. A random sample of 40 households was selected as part of a study on electricity usage, and the number of kilowatt-hours (kWh) was recorded for each household in the sample for the first quarter of 2020. The average usage was found to be 310 kWh. In a very large study in the first quarter of the previous year, it was found that the standard deviation of the usage was 89 kWh.**\n", "\n", "**Assuming the standard deviation is unchanged and that the usage is normally distributed, provide an expression for calculating a 95% confidence interval for the mean usage in the first quarter of 2019.**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uyAHorce_KaM", "outputId": "a17aca72-2c15-42a8-993f-88393ea59489" }, "outputs": [ { "data": { "text/plain": [ "array([282.42, 337.58])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#import the required function\n", "from scipy.stats import norm\n", "\n", "#set the values of sample mean and sigma\n", "x_bar, sigma = 310, 89\n", "\n", "# set the value of sample size\n", "n = 40\n", "\n", "# construct the confidence interval\n", "np.round(norm.interval(0.95, loc = x_bar, scale = sigma/np.sqrt(n)), 2)" ] }, { "cell_type": "markdown", "metadata": { "id": "F1JpYY7JGGYY" }, "source": [ "**Insight:** \n", " \n", "* We are 95% confident that the mean usage in the first quarter of 2019 lies between 282.42 and 337.58 kWh." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hypothesis Testing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3. You are a manager of a Chinese restaurant. You want to determine whether the mean waiting time to place an order has changed in the past month from its previous population mean value of 4.5 minutes. State the null and alternative hypotheses.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The null hypothesis is that the mean waiting time has not changed from its previous value of 4.5 minutes. This is stated \n", "as\n", "\n", "$$H_0: \\mu = 4.5$$\n", "\n", "The alternative hypothesis is that the mean waiting time has been changed from its previous value of 4.5 minutes. This is stated as\n", "\n", "$$H_a: \\mu \\neq 4.5$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4. What is the p-value in a two-tailed z-test for one sample, where the computed test statistic (z-stat) is equal to +2.00?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For calculating the p-value for a two-tailed hypothesis test, first we will calculate the p-value for a one-tailed test and then multiply the p-value by 2 to obtain the result for a two-tailed test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The p-value for a one-tailed test is the area to the right of the computed test statistic in the distribution of the test statistic. That is, the probability $P(X>z\\_stat)$. This can be computed using the `cdf` function.\n", " * The function `norm.cdf(x, mu, sigma)` calculates the probability $P(X 29000$$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the description in the question, we are provided:\n", "\n", "$\\bar{x} = 30000$\n", "\n", "$\\mu = 29000$\n", "\n", "$\\sigma = 8000$\n", "\n", "$n = 400$\n", "\n", "The formula for computing the test statistic (z-stat) is given as:\n", "\n", " $z = \\frac{(\\bar{x}-\\mu)}{\\sigma/\\sqrt{n}}$ " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5\n" ] } ], "source": [ "# calculating the test statistic (z-stat)\n", "z = (30000 - 29000)/(8000/np.sqrt(400))\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the alternative hypothesis contains $>$ sign, it is a one-tailed test of greater than type. So, the p-value will be the area to the right of the computed test statistic in the distribution of the test statistic. That is, the probability $P(X>z)$" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.006209665325776159" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculating the p-value for the test statistic z = 2.5\n", "p_val = 1 - stats.norm.cdf(z)\n", "p_val" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Conclusion based on p-value**\n", "\n", "* As the p-value is less than $\\alpha$ (=0.05), we have enough evidence to reject the null hypothesis. \n", "\n", "* Hence, we have enough evidence to conclude that the mean income of the household is greater than 29000 rupees" ] } ], "metadata": { "colab": { "collapsed_sections": [ "g7pg-lWiVNZx", "l46Ul-hMd7DD" ], "name": "Practice_exercise -1 (1).ipynb", "provenance": [] }, "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.8.8" } }, "nbformat": 4, "nbformat_minor": 1 }