{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 2 : Finite Sample Spaces\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.1 , Page number : 21" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Probability of p1 is : 4/7\n", "Probability of p2 is : 2/7\n", "Probability of p3 is : 1/7\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "# p1,p2 and p3 be the probabilities of events a1,a2 and a3\n", "# Probalitity of p1+p2+p3=1 and p1=2*p2 , p2=2*p3\n", " \n", "\n", "#calculation\n", "p2= 1.0/(2.0+1.0+1.0/2.0) #p1+p2+p3=1 hence 2p2+p2+p3/3=1 \n", " #p2(2+1+1/3)=1\n", "p1=2.0*p2\n", "p3=1.0/2*p2\n", "\n", "#converstion of floating point value into Fraction and Result\n", "print \"Probability of p1 is :\",Fraction(p1).limit_denominator(1000)\n", "print \"Probability of p2 is :\",Fraction(p2).limit_denominator(1000)\n", "print \"Probability of p3 is :\",Fraction(p3).limit_denominator(1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.2 , Page number : 22" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Probability of number greater than 4 in single throw of die : 1/3\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "S = {1,2,3,4,5,6} #Sample space for single throw of die\n", "A = { } #Event occurance of number >4 initially empty\n", "\n", "count=0\n", "\n", "for i in S: #counting the number of elements greater than 4\n", " if i>4:\n", " count=count+1\n", "\n", "x= float (count)/len(S) # Probability of A\n", "\n", "#Result\n", "print \"Probability of number greater than 4 in single throw of die : \",Fraction(x).limit_denominator(1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.3 , Page number : 22" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Probability of number of Heads are 1 in single throw of two coins : 1/2\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "mylist=[0,0,0] #Initially posiions of list represents number of occurances of H in event\n", "count=0 #to count total number of possible events\n", "for i in range(0,2): #method to generate the 2 elemets unordered pairs H and T and 0 represents Head and 1 represents TAIL\n", " for j in range(0,2):\n", " count=count+1 #counting the total number of order pairs\n", " if i+j==0:\n", " mylist[0]=mylist[0]+1\n", " if i+j==1:\n", " mylist[1]=mylist[1]+1\n", " if i+j==2:\n", " mylist[2]=mylist[2]+1\n", "\n", "x= float(mylist[1])/count #probability of number of heads are 1\n", "\n", "#Result\n", "print \"Probability of number of Heads are 1 in single throw of two coins : \",Fraction(x).limit_denominator(1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.4 , Page number : 24" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of ways to select 5 items from 20 items : 15504.0\n", "number of ways to select 5 items from 80 items : 24040016.0\n", "number of ways to select 10 items from 100 items : 1.73103094564e+13\n", "Required probability is : 1/719598\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "#lot has 20 defective and 80 non defective items\n", "#ten items choosen withour replacement such half 5 are defective and 5 are non defective\n", "\n", "def factorial(value): #Function to calculate factorial\n", " result=1\n", " for i in range(1,value+1):\n", " result=result*i\n", " return result\n", " \n", "x=factorial(20);\n", "y=factorial(15);\n", "z=factorial(5);\n", "\n", "slection_from_20=float(x)/(y*z) #calculating 20!/(15!*5!)\n", " \n", "x=factorial(80);\n", "y=factorial(75);\n", "z=factorial(5);\n", "\n", "slection_from_80=float(x)/(y*z) #calculating 80!/(75!*5!)\n", "\n", "x=factorial(100);\n", "y=factorial(90);\n", "z=factorial(10);\n", "\n", "slection_from_100=float(x)/(y*z) # calculating 100!/(90!*10!)\n", "print \"number of ways to select 5 items from 20 items : \",slection_from_20\n", "print \"number of ways to select 5 items from 80 items : \",slection_from_80\n", "print \"number of ways to select 10 items from 100 items : \",slection_from_100\n", "\n", "probability= (slection_from_20+slection_from_80)/slection_from_100\n", "\n", "#Result\n", "print \"Required probability is : \",Fraction(probability).limit_denominator(1000000)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.5 , Page number : 24" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of ways to select 5 items from 20 items : 15504.0\n", "number of ways to select 5 items from 80 items : 24040016.0\n", "number of ways to select 10 items from 100 items : 1.73103094564e+13\n", "Required probability is : 1/719598\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "#lot has 20 defective and 80 non defective items\n", "#ten items choosen withour replacement such half 5 are defective and 5 are non defective\n", "\n", "def factorial(value): #Function to calculate factorial\n", " result=1\n", " for i in range(1,value+1):\n", " result=result*i\n", " return result\n", " \n", "x=factorial(20);\n", "y=factorial(15);\n", "z=factorial(5);\n", "\n", "selection_from_20=float(x)/(y*z) # calculating 20!/(15!*5!)\n", " \n", "x=factorial(80);\n", "y=factorial(75);\n", "z=factorial(5);\n", "\n", "selection_from_80=float(x)/(y*z) # calculating 80!/(75!*5!)\n", "\n", "x=factorial(100);\n", "y=factorial(90);\n", "z=factorial(10);\n", "\n", "selection_from_100=float(x)/(y*z) # calculating 100!/(90!*10!)\n", "probability= (selection_from_20+selection_from_80)/selection_from_100\n", "\n", "#Result\n", "print \"number of ways to select 5 items from 20 items : \",selection_from_20\n", "print \"number of ways to select 5 items from 80 items : \",selection_from_80\n", "print \"number of ways to select 10 items from 100 items : \",selection_from_100\n", "print \"Required probability is : \",Fraction(probability).limit_denominator(1000000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.7 , Page number : 28" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subproblem 1 : Number of ways of forming committee of 3 members is : 56\n", "Subproblem 2 : Numberof signals made up of 3 flags are : 336\n", "Subproblem 3 : Total number of committes formed are : 30\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "def factorial(value):\t\t\t#Function to calculate factorial\n", " result=1\n", " for i in range(1,value+1):\n", " result=result*i\n", " return result\n", " \n", "#subproblem a Committee selection problem\n", "\n", "x=factorial(8);\t\n", "y=factorial(5);\t\n", "z=factorial(3);\n", "selecting_3_from_8 = x/(y*z)\n", "#Result\n", "print \"Subproblem 1 : Number of ways of forming committee of 3 members is : \",selecting_3_from_8\n", "\n", "\n", "\n", "#subproblem b flag problem\n", "\n", "x=factorial(8);\t\n", "y=factorial(5);\t\n", "\n", "number_of_ways = x/y\n", "#Result\n", "print \"Subproblem 2 : Numberof signals made up of 3 flags are : \",number_of_ways\n", "\n", "\n", "#Sub problem 3 : committee\n", "x=factorial(5);\t\n", "y=factorial(3);\t\n", "z=factorial(2);\n", "selecting_2_men_from_5 = x/(y*z)\n", "\n", "x=factorial(3);\t\n", "y=factorial(1);\t\n", "z=factorial(2);\n", "selecting_1_women_from_3 = x/(y*z)\n", "\n", "total_committes=selecting_2_men_from_5*selecting_1_women_from_3\n", "\n", "#Result\n", "print \"Subproblem 3 : Total number of committes formed are : \",total_committes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2.9, Page number : 29" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Listing the number of ways of selecting 2 objects from 4 objects without repetition or ordered \n", "a b\n", "a c\n", "a d\n", "b c\n", "b d\n", "c d\n", "Probability of object c is choosen without repetition is : 1/2\n", "Listing the number of ways of selecting 2 objects from 4 objects with repetition OR unordered\n", "a a\n", "a b\n", "a c\n", "a d\n", "b a\n", "b b\n", "b c\n", "b d\n", "c a\n", "c b\n", "c c\n", "c d\n", "d a\n", "d b\n", "d c\n", "d d\n", "Probability of object c is choosen without repetition is : 7/16\n" ] } ], "source": [ "from fractions import Fraction\n", "\n", "mylist=['a','b','c','d'] #Objects in sample space\n", "c_count_with_repeat=0\n", "c_count_without_repeat=0\n", "totalways=0\n", "\n", "print \"Listing the number of ways of selecting 2 objects from 4 objects without repetition or ordered \"\n", "for i in range(0,4):\n", " for j in range (i+1,4):\n", " totalways=totalways+1 #counting number of ways of orderd listing\n", " print mylist[i],mylist[j] #Listing ordered pairs\n", " if(mylist[i]=='c' or mylist[j]=='c'): #checking the object c selected or not while selection atleast once\n", " c_count_without_repeat=c_count_without_repeat+1\n", "\n", "probability_c_without_repeat=float( c_count_without_repeat)/totalways #calculating probability of object c selected without repetition \n", "\n", "#Result\n", "print \"Probability of object c is choosen without repetition is : \",Fraction(probability_c_without_repeat).limit_denominator(1000)\n", "\n", "\n", "totalways=0\n", "print \"Listing the number of ways of selecting 2 objects from 4 objects with repetition OR unordered\"\n", "for i in range(0,4):\n", " for j in range (0,4):\n", " totalways=totalways+1 #counting number of ways of unorderd listing\n", " print mylist[i],mylist[j] #Listing unordered pairs\n", " if(mylist[i]=='c' or mylist[j]=='c'): #checking the object c selected or not while selection atleast once\n", " c_count_with_repeat=c_count_with_repeat+1\n", "\n", "probability_c_with_repeat=float( c_count_with_repeat)/totalways #calculating probability of object c selected with repetition\n", "\n", "#Result\n", "print \"Probability of object c is choosen without repetition is : \",Fraction(probability_c_with_repeat).limit_denominator(1000)" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }