File size: 10,838 Bytes
532ef6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{
 "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<x)$ or $P(X<=x)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.02275013194817921"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#import the required function\n",
    "from scipy.stats import norm\n",
    "# provided value of the test statistic is +2.00\n",
    "z_stat = 2\n",
    "# calculating the p-value for a one-tailed test\n",
    "p_val = 1 - norm.cdf(2)\n",
    "p_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.04550026389635842\n"
     ]
    }
   ],
   "source": [
    "# calculating the p-value for a two-tailed test\n",
    "print(p_val*2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Insight:**\n",
    "* in a two-tailed z-test for one sample, where the computed test statistic (z-stat) is equal to +2.00, the p-value is 0.0455"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Q5. Samy, Product Manager of K2 Jeans, wants to launch a product line into a new market area. A Survey of a random sample of 400 households in that market showed a mean income per household of 30000 rupees. The standard deviation based on an earlier pilot study of households is 8000 rupees. Samy strongly believes the product line will be adequately profitable only in markets where the mean household income is greater than 29000 rupees. Samy wants our help in deciding whether the product line should be introduced in the new market. Perform statistical analysis and based on that draw a conclusion.**\n",
    "\n",
    "Assume a level of significance ($\\alpha$) of 5%"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Null Hypothesis: Mean income of the household is less than or equal to 29000 rupees. This can be written as:\n",
    "\n",
    "$$H_0: \\mu \\leq 29000$$ \n",
    "\n",
    "Alternative Hypothesis: Mean income of the household is greater than 29000 rupees. This can be written as:\n",
    "\n",
    "$$H_a: \\mu > 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",
    "<font size = 5> $z = \\frac{(\\bar{x}-\\mu)}{\\sigma/\\sqrt{n}}$ </font>"
   ]
  },
  {
   "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
}