summaryrefslogtreecommitdiff
path: root/sample_notebooks/MaheshkumarEaga/Chapter_2.ipynb
blob: cf246c3c1331a41bcc5afbcf2e83020636548c7e (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
{
 "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
}