{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 35 : Sampling And Inference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.1, page no. 864" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Suppose the coin is unbiased\n", "Then probability of getting the head in a toss = 1/2 \n", "Then, expected no. of successes = a = 1/2∗400\n", "Observed no. of successes = 216\n", "The excess of observed value over expected value = 216\n", "S. D. of simple sampling = (n∗p∗q)ˆ0.5 = c \n", "Hence, z = (b−a)/c = 21.6\n", "As z<1.96, the hypothesis is accepted at 5% level of significance\n" ] } ], "source": [ "print \"Suppose the coin is unbiased\"\n", "print \"Then probability of getting the head in a toss = 1/2 \"\n", "print \"Then, expected no. of successes = a = 1/2∗400\"\n", "a = 1/2*400\n", "print \"Observed no. of successes = 216\"\n", "b = 216\n", "print \"The excess of observed value over expected value = \",b-a\n", "print \"S. D. of simple sampling = (n∗p∗q)ˆ0.5 = c \"\n", "c = (400*0.5*0.5)**0.5\n", "print \"Hence, z = (b−a)/c = \",(b-a)/c\n", "print \"As z<1.96, the hypothesis is accepted at 5% level of significance\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.2, page no. 865" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Suppose the die is unbiased \n", "Then probability of getting 5 or 6 with one die=1/3 \n", "Then, expected no. of successes = a = 1/3∗9000 \n", "Observed no. of successes = 3240\n", "The excess of observed value over expected value = 240.0\n", "S. D. of simple sampling = (n∗p∗q)ˆ0.5=c\n", "Hence, z = (b−a)/c = 5.366563146\n", "As z>2.58, the hypothesis has to be rejected at 1% level of significance\n" ] } ], "source": [ "print \"Suppose the die is unbiased \"\n", "print \"Then probability of getting 5 or 6 with one die=1/3 \"\n", "print \"Then, expected no. of successes = a = 1/3∗9000 \"\n", "a = 1./3*9000\n", "print \"Observed no. of successes = 3240\"\n", "b = 3240\n", "print \"The excess of observed value over expected value = \",b-a\n", "print \"S. D. of simple sampling = (n∗p∗q)ˆ0.5=c\"\n", "c = (9000*(1./3)*(2./3))**0.5\n", "print \"Hence, z = (b−a)/c = \",(b-a)/c\n", "print \"As z>2.58, the hypothesis has to be rejected at 1% level of significance\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.3, page no. 865" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "q = 1−p \n", "Standard error of the population of families having a monthly income of rs. 250 or less=(p∗q/n)ˆ0.5 = 0.0148442858929\n", "Hence taking 103/420 to be the estimate of families having a monthly income of rs. 250 or less, the limits are 20% and 29% approximately\n" ] } ], "source": [ "p = 206./840\n", "print \"q = 1−p \"\n", "q = 1-p\n", "n = 840\n", "print \"Standard error of the population of families having a monthly income of rs. 250 or less=(p∗q/n)ˆ0.5 = \",(p*q/n)**0.5\n", "print \"Hence taking 103/420 to be the estimate of families having a monthly income of rs. 250 or less, the limits are 20% and 29% approximately\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.4, page no. 866" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p=(n1∗p1+n2∗p2)/(n1+n2)\n", "0.1904\n", "q = 1−p \n", "0.8096\n", "e=(p∗q∗(1/n1+1/n2))ˆ0.5\n", "0.0163590274093\n", "z = 0.916924926202\n", "As z<1, the difference between the proportions is not significant. \n" ] } ], "source": [ "n1 = 900\n", "n2 = 1600\n", "p1 = 20./100\n", "p2 = 18.5/100\n", "print \"p=(n1∗p1+n2∗p2)/(n1+n2)\"\n", "p = (n1*p1+n2*p2)/(n1+n2)\n", "print p\n", "print \"q = 1−p \"\n", "q = 1-p\n", "print q\n", "print \"e=(p∗q∗(1/n1+1/n2))ˆ0.5\"\n", "e = (p*q*((1./n1)+(1./n2)))**0.5\n", "print e\n", "z = (p1-p2)/e\n", "print \"z = \",z\n", "print \"As z<1, the difference between the proportions is not significant. \"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.5, page no. 867" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "q1 = 1−p1 \n", "q2=1−p2\n", "e=((p1∗q1/n1)+(p2*q2/n2))ˆ0.5\n", "Hence, it is likely that real difference will be hidden.\n" ] } ], "source": [ "p1 = 0.3\n", "p2 = 0.25\n", "print \"q1 = 1−p1 \"\n", "q1 = 1-p1\n", "print \"q2=1−p2\"\n", "q2 = 1-p2\n", "n1 = 1200\n", "n2 = 900\n", "print \"e=((p1∗q1/n1)+(p2*q2/n2))ˆ0.5\"\n", "e = ((p1*q1/n1)+(p2*q2/n2))**0.5\n", "z = (p1-p2)/e\n", "print \"Hence, it is likely that real difference will be hidden.\" " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.6, page no. 868" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m and n represents mean and number of objects in sample respectively \n", "z=(m−M)/(d/(nˆ0.5)\n", "z = 2.7950310559\n", "As z>1.96, it cannot be regarded as a random sample\n" ] } ], "source": [ "print \"m and n represents mean and number of objects in sample respectively \"\n", "m = 3.4\n", "n = 900.\n", "M = 3.25\n", "d = 1.61\n", "print \"z=(m−M)/(d/(nˆ0.5)\"\n", "z = (m-M)/(d/(n**0.5))\n", "print \"z = \",z\n", "print \"As z>1.96, it cannot be regarded as a random sample\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.9, page no. 871" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m1 and n1 represents mean and no. of objects in sample 1\n", "m2 and n2 represents mean and no. of objects in sample 2\n", "On the hypothesis that the samples are drawn from the same population of d = 2.5, we get\n", "z = -5.16397779494\n", "Since |z|>1.96, thus samples cannot be regarded as drawn from the same population\n" ] } ], "source": [ "print \"m1 and n1 represents mean and no. of objects in sample 1\"\n", "print \"m2 and n2 represents mean and no. of objects in sample 2\"\n", "m1 = 67.5\n", "m2 = 68.\n", "n1 = 1000.\n", "n2 = 2000.\n", "d = 2.5\n", "print \"On the hypothesis that the samples are drawn from the same population of d = 2.5, we get\"\n", "z = (m1-m2)/(d*((1/n1)+(1/n2))**0.5)\n", "print \"z = \",z\n", "print \"Since |z|>1.96, thus samples cannot be regarded as drawn from the same population\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.10, page no. 872" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m1, d1 and n1 denotes mean, deviation and no.of objects in first sample \n", "m2, d2 and n2 denotes mean, deviation and no.of objects in second sample\n", "S. E. of the difference of the mean heights is 0.0703246933872\n", "-0.7\n", "|m1−m2|>10e, this is highly significant. hence, the data indicates that the sailors are on the average taller than the soldiers.\n" ] } ], "source": [ "print \"m1, d1 and n1 denotes mean, deviation and no.of objects in first sample \"\n", "m1 = 67.85\n", "d1 = 2.56\n", "n1 = 6400.\n", "print \"m2, d2 and n2 denotes mean, deviation and no.of objects in second sample\"\n", "m2 = 68.55\n", "d2 = 2.52\n", "n2 = 1600.\n", "print \"S. E. of the difference of the mean heights is \",\n", "e = ((d**2/n1)+(d2**2/n2))**0.5\n", "print e\n", "print m1-m2\n", "print \"|m1−m2|>10e, this is highly significant. hence, the data indicates that the sailors are on the average taller than the soldiers.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.12, page no. 874" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First of row denotes the different values of sample \n", "The second row denotes the corresponding deviation\n", "The third row denotes the corresponding square of deviation\n", "The sum of second row elements = 10.0\n", "The sum of third row elements = 66.0\n", "let m be the mean \n", "let d be the standard deviation\n", "1.84522581263\n" ] } ], "source": [ "import numpy\n", "\n", "A = numpy.zeros((3,9))\n", "n = 9\n", "print \"First of row denotes the different values of sample \"\n", "A[0,:] = [45,47,50,52,48,47,49,53,51]\n", "print \"The second row denotes the corresponding deviation\"\n", "for i in range(0,9):\n", " A[1,i] = A[0,i]-48\n", "print \"The third row denotes the corresponding square of deviation\"\n", "for i in range(0,9):\n", " A[2,i] = A[1,i]**2\n", "print \"The sum of second row elements = \",\n", "a =0\n", "for i in range(0,9):\n", " a = a+A[1,i]\n", "print a\n", "print \"The sum of third row elements = \",\n", "b = 0\n", "for i in range(0,9):\n", " b = b+A[2,i]\n", "print b\n", "print \"let m be the mean \"\n", "m = 48+a/n\n", "print \"let d be the standard deviation\"\n", "d = ((b/n)-(a/n)**2)**0.5\n", "t = (m-47.5)*(n-1)**0.5/d\n", "print t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 35.13, page no. 876" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d and n represents the deviation and no. objects in given sample\n", "Taking the hypothesis that the product is not inferior i.e. there is no significant difference between m and M\n", "3.15\n", "Degrees of freedom= \n", "9.0\n" ] } ], "source": [ "print \"d and n represents the deviation and no. objects in given sample\"\n", "n = 10.\n", "d = 0.04\n", "m = 0.742\n", "M = 0.700\n", "print \"Taking the hypothesis that the product is not inferior i.e. there is no significant difference between m and M\"\n", "t = (m-M)*(n-1)**0.5/d\n", "print t\n", "print \"Degrees of freedom= \"\n", "f = n-1\n", "print f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 34.15, page no. 878" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first row denotes the boy no. \n", "The second row denotes the marks in test I(x1)\n", "The third row denotes the marks in test I(x2)\n", "The fourth row denotes the difference of marks in two tests(d)\n", "The fifth row denotes the (d−1)\n", "The sixth row denotes the square of elements of fourth row \n", "[[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]\n", " [ 23. 20. 19. 21. 18. 20. 18. 17. 23. 16. 19.]\n", " [ 24. 19. 22. 18. 20. 22. 20. 20. 23. 20. 17.]\n", " [ 1. -1. 3. -3. 2. 2. 2. 3. 0. 4. -2.]\n", " [ 0. -2. 2. -4. 1. 1. 1. 2. -1. 3. -3.]\n", " [ 1. 1. 9. 9. 4. 4. 4. 9. 0. 16. 4.]]\n", "The sum of elements of fourth row=\n", "11.0\n", "The sum of elements of sixth row= \n", "61.0\n", "Standard deviation\n", "d = 2.46981780705\n", "t = 1.48063606712\n" ] } ], "source": [ "import numpy\n", "\n", "A = numpy.zeros((6,11))\n", "n = 11\n", "print \"The first row denotes the boy no. \"\n", "A[0,:] = [1,2,3,4,5,6,7,8,9,10,11]\n", "print \"The second row denotes the marks in test I(x1)\"\n", "A[1,:] = [23,20,19,21,18,20,18,17,23,16,19]\n", "print \"The third row denotes the marks in test I(x2)\"\n", "A[2,:] = [24,19,22,18,20,22,20,20,23,20,17]\n", "print \"The fourth row denotes the difference of marks in two tests(d)\"\n", "for i in range (0,11):\n", " A[3,i] = A[2,i]-A[1,i]\n", "print \"The fifth row denotes the (d−1)\"\n", "for i in range (0,11):\n", " A[4,i] = A[3,i]-1\n", "print \"The sixth row denotes the square of elements of fourth row \"\n", "for i in range(0,11):\n", " A[5,i] = A[3,i]**2\n", "print A\n", "a = 0\n", "print \"The sum of elements of fourth row=\"\n", "for i in range(0,11):\n", " a = a+A[3,i]\n", "print a\n", "b = 0\n", "print \"The sum of elements of sixth row= \"\n", "for i in range(0,11):\n", " b = b + A[5,i]\n", "print b\n", "print \"Standard deviation\"\n", "d = (b/(n-1))**0.5\n", "t = (1-0)*(n)**0.5/2.24\n", "print \"d = \",d\n", "print \"t = \",t" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }