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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
{
"metadata": {
"name": "",
"signature": "sha256:2cbc1da90c4c7b54c1033f2cfac603ed9480419c909b33f66bc0d4ea974287c3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"2 Permutations, combinations, and discrete probability "
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 01:page 69"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"This is to find the number of ways to schedule three exams in five days such that no two exams fall on the same day\"\n",
"first_exam=5#number of available days for the first exam\n",
"second_exam=4#number of available days for the second exam\n",
"third_exam=3#number of available days for the third exam\n",
"total=first_exam*second_exam*third_exam#number of combinations\n",
"print \"Total number of ways is :\",total\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This is to find the number of ways to schedule three exams in five days such that no two exams fall on the same day\n",
"Total number of ways is : 60\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 02:Page 69"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"To find the number of ways in which seven rooms can be allocated to four programmers\"\n",
"a=7 # The number of possible rooms for first programmer\n",
"b=6 # The number of possible rooms for second programmer\n",
"c=5 # The number of possible rooms for third programmer\n",
"d=4 # The number of possible rooms for fourth programmer\n",
"res=a*b*c*d\n",
"print \"The assignment can be made in \",res,\"different ways\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To find the number of ways in which seven rooms can be allocated to four programmers\n",
"The assignment can be made in 840 different ways\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 03:Page 69"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Number of four digit decimal numbers which have no repeated digits\"\n",
"thousands_place=9 # Only nine digits can be placed there. zero cannot be placed there as it will become a three digit number\n",
"hundreds_place=9 #As one slot is already filled with a number, the remaining nine digits can only be placed here\n",
"tens_place=8 # As two digits have already been used, there are only eight more digits for this place\n",
"ones_place=7 # Three digits have occupied three places and only seven digits remain more\n",
"total=thousands_place*hundreds_place*tens_place*ones_place\n",
"print total,\" possible four digit decimal numbers can be formed without any repetition of digits\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number of four digit decimal numbers which have no repeated digits\n",
"4536 possible four digit decimal numbers can be formed without any repetition of digits\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 04:Page 70"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"print \"To find the number of ways in which we can make up strings of four distinct letters followed by three distinct digits\"\n",
"def permutations(n,r):\n",
" res=math.factorial(n)/math.factorial(n-r)#Mathematical formula for permutations\n",
" return res\n",
"print \"Total number of possible ways are\",permutations(26,4)*permutations(10,3)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To find the number of ways in which we can make up strings of four distinct letters followed by three distinct digits\n",
"Total number of possible ways are 258336000\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 05:Page 70"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"To schedule three examinations within a five day period with no restrictions on the number of examinations scheduled each day\"\n",
"print \"The total number of ways are\", pow(5,3)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To schedule three examinations within a five day period with no restrictions on the number of examinations scheduled each day\n",
"The total number of ways are 125\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 08:Page 71"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"We print all five-digit numbers on slips of paper with one number on each slip.\"\n",
"print \"We have\",pow(10,5),\"distinct five-digit numbers\" \n",
"print \"Among these numbers\",pow(5,5),\"of them can be read either right side up or upside down\"#Since the digits 0,1,6,8,9 become 0,1,9,8,6 when they are read upside down, there are pairs of numbers that can share the same slip if the slips are read right side up or upside down\n",
"print \"There are\",pow(5,5)-(3*pow(5,2)),\"numbers that can be read either right side up or upside down but will read differently.So,hey can be divided into pairs so that every pair of numbers can share one slip.\"\n",
"print \"The total number of distinct slips we need is\",pow(10,5)-((pow(5,5)-3*pow(5,2))/2)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"We print all five-digit numbers on slips of paper with one number on each slip.\n",
"We have 100000 distinct five-digit numbers\n",
"Among these numbers 3125 of them can be read either right side up or upside down\n",
"There are 3050 numbers that can be read either right side up or upside down but will read differently.So,hey can be divided into pairs so that every pair of numbers can share one slip.\n",
"The total number of distinct slips we need is 98475\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 09:Page 72"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"There are\",pow(4,7),\"different schedules for a seven day period with four subjects\"\n",
"print \"To find the number of schedules which devote at least one day to each subject namely mathematics, physics, chemistry,and economics.\"\n",
"print \"A1 denote the set of schedules in which mathematics is never included\"\n",
"print \"A2 denote the set of schedules in which physics is never included\"\n",
"print \"A3 denote the set of schedules in which chemistry is never included\"\n",
"print \"A4 denote the set of schedules in which economics is never included\"\n",
"print \"A1_union_A2_union_A3_union_A4 is the set of schedules in which one or more of the subjects is not included. We obtain |A1 U A2 U A3 U A4|=\",\n",
"A1=A2=A3=A4=pow(3,7)\n",
"A1_intersection_A2=A1_intersection_A3=A1_intersection_A4=A2_intersection_A3=A2_intersection_A4=A3_intersection_A4=pow(2,7)\n",
"A1_intersection_A2_intersection_A3=A1_intersection_A2_intersection_A4=A1_intersection_A3_intersection_A4=A2_intersection_A3_intersection_A4=1\n",
"A1_intersection_A2_intersection_A3_intersection_A4=0\n",
"A1_union_A2_union_A3_union_A4=A1+A2+A3+A4-A1_intersection_A2-A1_intersection_A3-A1_intersection_A4-A2_intersection_A3-A2_intersection_A4-A3_intersection_A4+A1_intersection_A2_intersection_A3+A1_intersection_A2_intersection_A4+A1_intersection_A3_intersection_A4+A2_intersection_A3_intersection_A4\n",
"print A1_union_A2_union_A3_union_A4\n",
"print \"The number of schedules in which all subjects will be included\",pow(4,7)-A1_union_A2_union_A3_union_A4\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"There are 16384 different schedules for a seven day period with four subjects\n",
"To find the number of schedules which devote at least one day to each subject namely mathematics, physics, chemistry,and economics.\n",
"A1 denote the set of schedules in which mathematics is never included\n",
"A2 denote the set of schedules in which physics is never included\n",
"A3 denote the set of schedules in which chemistry is never included\n",
"A4 denote the set of schedules in which economics is never included\n",
"A1_union_A2_union_A3_union_A4 is the set of schedules in which one or more of the subjects is not included. We obtain |A1 U A2 U A3 U A4|= 7984\n",
"The number of schedules in which all subjects will be included 8400\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 10:Page 73"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"num_office=12 # Total number of offices to be painted\n",
"num_of_green=3 # Number of offices to be painted green\n",
"num_of_pink=2 # Number of offices to be painted pink\n",
"num_of_yellow=2 # Number of offices to be painted yellow\n",
"num_of_white=5 # Number of offices to be painted white\n",
"res=math.factorial(num_office)/(math.factorial(num_of_green)*math.factorial(num_of_pink)*math.factorial(num_of_yellow)*math.factorial(num_of_white))#Mathematical implementation of permutation with similarities\n",
"print \"Total number of ways to paint 12 offices so that 3 of them are green,2 of them are pink,2 of them are yellow and the remaining with white are \", res\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total number of ways to paint 12 offices so that 3 of them are green,2 of them are pink,2 of them are yellow and the remaining with white are 166320\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 11:Page 73"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"length=5#Total length of the message\n",
"num_of_dashes=3#Number of dashes in message \n",
"num_of_dots=2#Number of dots in the message\n",
"res=math.factorial(length)/(math.factorial(num_of_dashes)*math.factorial(num_of_dots))\n",
"print \"The number of different messages that can be represented by a sequence of three dashes and two dots :\",res\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The number of different messages that can be represented by a sequence of three dashes and two dots : 10\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 12:Page 74"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"days=7 # Number of days in a week\n",
"spaghetti=3 # Number of days a housekeeper wants to serve speghetti in a week\n",
"res=math.factorial(days)/(math.factorial(spaghetti)*math.factorial(days-spaghetti))#Mathematical implementation of permutation swith similarities\n",
"print \"Number of possible ways in which a house keeper can serve spagetti dinner three times each week is\",res\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number of possible ways in which a house keeper can serve spagetti dinner three times each week is 35\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13:Page 74"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"length=32 # Total length of binary sequence\n",
"num_of_one=7 # Total number of places to be filled with one\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))\n",
" return res\n",
"print \"Total number of combinations of binary sequences of length 32 in each of which there are exactly seven 1s is \",c(length,num_of_one)\n",
"# Because we can view the problem as placing 7 ones in 32 numbered boxes(and then fill the empty boxes with 0s)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total number of combinations of binary sequences of length 32 in each of which there are exactly seven 1s is 3365856\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 14:Page 74"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"import math\n",
"\n",
"total_senators=11 # Total number of senators\n",
"committee_mem=5 # Number of members in the committee\n",
"\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))\n",
" return res\n",
"\n",
"#Finding number of combinations for selecting a committee of 5 members from 11 senators\n",
"print \"The number of ways to select a committee of 5 members among 11 senators are\",c(total_senators,committee_mem);\n",
"\n",
"# Finding number of combinations to select a committee of five members so that a particular senator , senator A is always included\n",
"print \"The number of ways to select a committee of five members so that a particular senator , senator A is always included are\", c(total_senators-1,committee_mem-1);\n",
"# Since senator A is always included in the committee, we need to find combinations for the remaining 4 using remaining 10 senators\n",
"\n",
"# Finding number of combinations to select a committee of five members so that a particular senator , senator A is always excluded\n",
"print \"The number of ways to select a committee of five members so that a particular senator , senator A is always excluded are\", c(total_senators-1,committee_mem);\n",
"# Since senator A is alway excluded from the committee, the committee members are selected from the remaining 10 senators\n",
"\n",
"\n",
"print \"To find number of combinations to select a committee of five members so that at least one of senator A and senator B will be included\"\n",
"\n",
"both_A_and_B=c(total_senators-2,committee_mem-2)\n",
"print \"Number of selections including both senator A and senator B \",both_A_and_B\n",
"# Since both senator A and senator B are already included, we need to find combinations for the remaining 3 committee members using remaining 9 senators\n",
"\n",
"#Number of ways to select a committee of five members including senator A and excluding senator B\n",
"A_but_not_B=c(total_senators-2,committee_mem-1)\n",
"print \"Number of selections including senator A and excluding senator B \",A_but_not_B\n",
"# Since senator A is included and senator B is always excluded, find combinations for remaining 4 committee members using remaining 9 senators\n",
"\n",
"#Finding number of combinations to select a committee of five members including senator B and excluding senator A\n",
"B_but_not_A=c(total_senators-2,committee_mem-1)\n",
"print \"Number of selections including senator B and excluding senator A \",B_but_not_A\n",
"# Since senator B is included and senator A is always excluded, find combinations for remaining 4 committee members using remaining 9 senators\n",
"\n",
"res=both_A_and_B+A_but_not_B+B_but_not_A\n",
"print \"Number of ways to select a committee of five members so that at least one of senator A and senator B will be included\",res\n",
"\n",
"print \"Alternative method\"\n",
"\n",
"print \"Number of ways to select a committee of five members so that at least one of senator A and senator B will be included\",c(total_senators,committee_mem)-c(total_senators-2,committee_mem)\n",
"\n",
"\n",
"print \"By applying principle of inclusion and exclusion\"\n",
"#A1 denotes the set of ways of selection that include senator A\n",
"#A2 denotes the set of ways of selection that include senator B\n",
"A1=c(total_senators-1,committee_mem-1)\n",
"A2=c(total_senators-1,committee_mem-1)\n",
"A1_intersection_A2=c(total_senators-2,committee_mem-2)\n",
"print \"|A1 U A2|\",A1+A2-A1_intersection_A2\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The number of ways to select a committee of 5 members among 11 senators are 462\n",
"The number of ways to select a committee of five members so that a particular senator , senator A is always included are 210\n",
"The number of ways to select a committee of five members so that a particular senator , senator A is always excluded are 252\n",
"To find number of combinations to select a committee of five members so that at least one of senator A and senator B will be included\n",
"Number of selections including both senator A and senator B 84\n",
"Number of selections including senator A and excluding senator B 126\n",
"Number of selections including senator B and excluding senator A 126\n",
"Number of ways to select a committee of five members so that at least one of senator A and senator B will be included 336\n",
"Alternative method\n",
"Number of ways to select a committee of five members so that at least one of senator A and senator B will be included 336\n",
"By applying principle of inclusion and exclusion\n",
"|A1 U A2| 336\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 15:Page 75"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"print \"To find the total number of line segments the diagonals are divided into by their intersections\"\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))\n",
" return res\n",
"nodes=10#Decagon has 10 vertex points\n",
"pair=2#Denotes pair of edges to form a disgonal\n",
"num_of_diagonals=(c(nodes,pair)-nodes)\n",
"print \"Number of diagonals :\",num_of_diagonals\n",
"#There are c(10,2) straight lines joining c(10,2) pairs of vertices. But since 10 of them are sides of the decagon, we subtract 10\n",
"num_of_intersections=c(nodes,4)\n",
"print \"Total number of intersections\",num_of_intersections\n",
"# Since for every 4 vertices we can count exactly one intersection between the diagonals\n",
"print \"Total number of line segments\",(num_of_diagonals+(2*num_of_intersections))\n",
"#Since a diagonal is divided into k+1 line segments when there are k intersecting points lying on it, and since each intersecting point lies on two diagonals\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To find the total number of line segments the diagonals are divided into by their intersections\n",
"Number of diagonals : 35\n",
"Total number of intersections 210\n",
"Total number of line segments 455\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 16:Page 76"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations\n",
" return res\n",
"days=7#Total days in a week\n",
"choice=3#Number of days to be selected\n",
"print \"The number of ways to choose three out of seven days(with repititions allowed) is c(7+3-1,3)=c(9,3)=\", c(days+choice-1,choice)\n",
"print \"The number of ways to choose seven out of three days(with repititions necessarily allowed) is c(3+7-1,7)=c(9,7)=\", c(choice+days-1,days)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The number of ways to choose three out of seven days(with repititions allowed) is c(7+3-1,3)=c(9,3)= 84\n",
"The number of ways to choose seven out of three days(with repititions necessarily allowed) is c(3+7-1,7)=c(9,7)= 36\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 17:Page 76"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"total=7#Total number of diferent markings in a domino\n",
"squares=2#Number of squares in a domino\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))\n",
" return res\n",
"print \"The number of distinct dominoes in a set is C(7+2-1,2)=C(8,2)=\",c(total+squares-1,squares)\n",
"#Equivalent to selecting two subjects from seven objects \"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"blank\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The number of distinct dominoes in a set is C(7+2-1,2)=C(8,2)= 28\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18:page 76"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"dice=3#Number of dice being rolled\n",
"num=6#Total number of outcomes in one dice\n",
"def C(n,r):\n",
" return math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Formula to calculate combinations\n",
"print \"When three dice are rolled, the number of different outcomes are C(6+3-1,3)=C(8,3)=\",C(num+dice-1,dice),\"because rolling three dice is equivalent to selecting three numbers from six numbers 1,2,3,4,5,6, with repetitions allowed\"\n",
"#Since it is equivalent to selecting 3 numbers from 6 numbers namely 1,2,3,4,5,6 with repetitions allowed\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"When three dice are rolled, the number of different outcomes are C(6+3-1,3)=C(8,3)= 56 because rolling three dice is equivalent to selecting three numbers from six numbers 1,2,3,4,5,6, with repetitions allowed\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 19:Page 77"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"def C(n,r):\n",
" res= math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Formula to calculate combinations\n",
" return res\n",
"moves=14#total possible moves in both directions\n",
"eastward=7#Possible number of eastward moves in total\n",
"northward=7#Possible number of northward moves in total\n",
"print \"The number of different paths for a rook to move from the southwest corner of a chessboard to the northwest corner by moving eastward and northward only.\"\n",
"print \"0 denote an eastward step and 1 denote a northward step, the number of paths is equal to the number of ways of arranging seven 0s and seven 1s, which is\",math.factorial(14)/(math.factorial(7)*math.factorial(7))\n",
"#Formula of permutations involving indistinguishable objects ie.,7 similar eastward moves and 7 similar northward moves\n",
"#This is the same as that of placing seven indistinguishable balls in four distinct boxes with no box left empty.\n",
"east_move=4#Number of northward moves\n",
"north_move=3#Number of eastward moves\n",
"num=C(east_move+north_move-1,north_move)#Number of ways of making up a path with four eastward moves\n",
"num1=C(north_move+east_move-1,east_move)#Number of ways of making up a path with three northward moves\n",
"print \"Number of ways of making up a path with four eastward moves is \",num#Same as that of placing seven indistinguishable balls in four boxes with no box left empty\n",
"print \"Number of ways of making up a path with three northward moves is \",num1\n",
"print \"Therefore, the answer to our question is \",num,\"*\",num1,\"=\",num*num1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The number of different paths for a rook to move from the southwest corner of a chessboard to the northwest corner by moving eastward and northward only.\n",
"0 denote an eastward step and 1 denote a northward step, the number of paths is equal to the number of ways of arranging seven 0s and seven 1s, which is 3432\n",
"Number of ways of making up a path with four eastward moves is 20\n",
"Number of ways of making up a path with three northward moves is 15\n",
"Therefore, the answer to our question is 20 * 15 = 300\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 20:Page 77"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"chairs=12#Total number of chairs\n",
"boys=5#Number of boys to be seated\n",
"objects=6#Number of differenr objects\n",
"def C(n,r):\n",
" return math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations\n",
"print \"To determine the number of ways to seat five boys in a row of 12 chairs.The number of arrangements would be\",math.factorial(chairs)/math.factorial(chairs-boys)#Because it is similar to arranging 12 objects which are of 6 different types ie.five boys are of different kind each and the remaining unoccupied chairs are of a kind\n",
"print \"Alternative way:\"\n",
"print \"Suppose, we arrange the first five boys in a row and then distribute the seven unoccupied chairs arbitrarily either between any two boys or at the two ends. The distribution problem then becomes that of placing seven balls of the same color in six boxes. Thus, the number of ways to do is 5!*C(6+7-1,7)=5!*(12!/7!*5!)=12!/7!=\",math.factorial(boys)*C(objects+(chairs-boys)-1,(chairs-boys))\n",
"print \"Suppose, we want to seat the boys so that no two boys are next to each other.\"\n",
"print \"5!*C(6+3-1,3)=5!*(8!/3!*5!)=\",math.factorial(5)*C(6+3-1,3)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To determine the number of ways to seat five boys in a row of 12 chairs.The number of arrangements would be 95040\n",
"Alternative way:\n",
"Suppose, we arrange the first five boys in a row and then distribute the seven unoccupied chairs arbitrarily either between any two boys or at the two ends. The distribution problem then becomes that of placing seven balls of the same color in six boxes. Thus, the number of ways to do is 5!*C(6+7-1,7)=5!*(12!/7!*5!)=12!/7!= 95040\n",
"Suppose, we want to seat the boys so that no two boys are next to each other.\n",
"5!*C(6+3-1,3)=5!*(8!/3!*5!)= 6720\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 22:page 83"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from fractions import Fraction\n",
"print \"For the experiment of rolling a dice, the sample space consists of six samples. We suppose the probability of occurence of each of these samples is 1/6.\"\n",
"print \"probability of getting an odd number is\", (Fraction(1,6)+Fraction(1,6)+Fraction(1,6)) # Since out of the six samples, three of them are odd numbers and the probability of getting each of them is 1/6\n",
"print \"On the other hand, suppose that we have a 'crooked' die such that the probability of getting a 1 is 1/3 and the probability of getting each of the remaining numbers is 2/15.\"\n",
"print \"The probability of getting an odd number is\",(Fraction(1,3)+Fraction(2,15)+Fraction(2,15)) # Since out of the six samples, three of them are odd numbers and the probability of getting a 1 is 1/3 and each of the remaining is 2/15\n",
"print \"The probability of getting an even number is\",(Fraction(2,15)+Fraction(2,15)+Fraction(2,15)) # Since out of the six samples, three of them are even numbers and the probability of getting each of them is 2/15\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"For the experiment of rolling a dice, the sample space consists of six samples. We suppose the probability of occurence of each of these samples is 1/6.\n",
"probability of getting an odd number is 1/2\n",
"On the other hand, suppose that we have a 'crooked' die such that the probability of getting a 1 is 1/3 and the probability of getting each of the remaining numbers is 2/15.\n",
"The probability of getting an odd number is 3/5\n",
"The probability of getting an even number is 2/5\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 23:Page 84"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from fractions import Fraction\n",
"print \"For the experiment of rolling two dice, the sample space consists of 36 samples. Suppose we want to find the probability of getting a sum of 9 in this experiment. We can get the sum 9 in four different ways such as {(3,6),(4,5),(5,4),(6,3)}\"\n",
"print \"probability of getting a sum 9 is\",(Fraction(1,36)+Fraction(1,36)+Fraction(1,36)+Fraction(1,36)) # Since the probability of occurence of each sample is 1/36 and we have four samples which give a sum of 9\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"For the experiment of rolling two dice, the sample space consists of 36 samples. Suppose we want to find the probability of getting a sum of 9 in this experiment. We can get the sum 9 in four different ways such as {(3,6),(4,5),(5,4),(6,3)}\n",
"probability of getting a sum 9 is 1/9\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 24:Page 84"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from decimal import *\n",
"import math\n",
"def c(n,r):\n",
" res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations\n",
" return res\n",
"\n",
"cards=52#Total number of cards in a deck\n",
"poker_cards=5#5 cards make up a poker hand\n",
"aces=48.0#Number of outcomes with 4 aces\n",
"print \"Consider the problem of dealing a poker hand out of a deck of 52 cards.\"\n",
"print \"The sample space consists of\",c(cards,poker_cards),\"sample points corresponding to the c(52,5) different hands that can be dealt\"\n",
"print \"Assume that the outcomes have equal probabilities;that is,the probability that a particular hand was dealt is equal to 1/c(52,5)=\",1.0/(c(cards,poker_cards))\n",
"getcontext().prec=3#Decides the number of decimal digits\n",
"print \"To determine the probability of geting four aces, we note that 48 of the c(52,5) possible outcomes contain four aces;thus,the probability is 48/c(52,5)=\",Decimal(aces)/Decimal(c(cards,poker_cards))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Consider the problem of dealing a poker hand out of a deck of 52 cards.\n",
"The sample space consists of 2598960 sample points corresponding to the c(52,5) different hands that can be dealt\n",
"Assume that the outcomes have equal probabilities;that is,the probability that a particular hand was dealt is equal to 1/c(52,5)= 3.84769292332e-07\n",
"To determine the probability of geting four aces, we note that 48 of the c(52,5) possible outcomes contain four aces;thus,the probability is 48/c(52,5)= 0.0000185\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 25:Page 84"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"from decimal import *\n",
"def P(n,r):\n",
" res=math.factorial(n)/math.factorial(n-r)#Mathematical implementation of permutations\n",
" return res\n",
"print \"Consider 23 people out of which the chance is less than 50-50 that no two of them will have the same birthday. Sample space consists of 366^23 samples corresponding to all possible distributions of birthdays of 23 people.\"\n",
"print \"Assume: The distributions are equiprobable.\"\n",
"getcontext().prec=3#Decides the number of decimal digits\n",
"print \"Since out of the 366^23 samples, P(366,23) of them correspond to distributions of birthdays such that no two of the 23 people have the same birthday, the probability that no two people have the same birthday is P(366,23)/366^23=\",(Decimal(P(366,23))/Decimal(pow(366,23)))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Consider 23 people out of which the chance is less than 50-50 that no two of them will have the same birthday. Sample space consists of 366^23 samples corresponding to all possible distributions of birthdays of 23 people.\n",
"Assume: The distributions are equiprobable.\n",
"Since out of the 366^23 samples, P(366,23) of them correspond to distributions of birthdays such that no two of the 23 people have the same birthday, the probability that no two people have the same birthday is P(366,23)/366^23= 0.494\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 26:Page 84"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from decimal import *\n",
"import math\n",
"students=8#Total number of students\n",
"freshmen=2#Total number of freshmen\n",
"sophomores=2#Total number of sophomores\n",
"juniors=2#Total number of juniors\n",
"seniors=2#Total number of seniors\n",
"sample_space=pow(4,8)#Total number of samples\n",
"print \"Eight students are standing in line for an interview. We want to determine the probability that there are exactly two fishermen, two sophomores, two juniors, and two seniors in the line. The sample space consists of 4^8 samples corresponding to all possibilities of classes the students are from. \"\n",
"print \"Assume: All the samples are equiprobable\"\n",
"getcontext().prec=3#Decides the number of decimal digits\n",
"print \"There are 8!/2!2!2!2! samples corresponding to the case in which there are two students from each class. Thus the probability is \",(Decimal(math.factorial(students))/(Decimal(math.factorial(freshmen)*math.factorial(sophomores)*math.factorial(juniors)*math.factorial(seniors)*sample_space)))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Eight students are standing in line for an interview. We want to determine the probability that there are exactly two fishermen, two sophomores, two juniors, and two seniors in the line. The sample space consists of 4^8 samples corresponding to all possibilities of classes the students are from. \n",
"Assume: All the samples are equiprobable\n",
"There are 8!/2!2!2!2! samples corresponding to the case in which there are two students from each class. Thus the probability is 0.0385\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 28:Page 85 "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"To find the probability of various number of buffers being filled by digital data from a remote site\"\n",
"A=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]# A denotes that initial 16 buffers are full\n",
"B=[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31]#B denotes that odd buffers are full\n",
"A_inter_B=[1,3,5,7,9,11,13,15]#set representing A intersection B\n",
"P_A=0.0#initial probability corresponding to set A is zero\n",
"P_A_and_B=0.0#initial probability of set A intersection B is zero\n",
"P_B=0.0#initial probability of set B is zero\n",
"for q in A:\n",
" P_A+=(1.0/561.0)*(33.0-q)\n",
"print \"Probability of buffers in set A being filled is\",round(P_A,3)\n",
"for q in B:\n",
" P_B+=(1.0/561.0)*(33.0-q)\n",
"print \"Probability of buffers in set B being filled is\",round(P_B,3)\n",
"for q in A_inter_B:\n",
" P_A_and_B+=(1.0/561.0)*(33.0-q)\n",
"print \"Probability of buffers in set A intersection B being filled is\",round(P_A_and_B,3)\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To find the probability of various number of buffers being filled by digital data from a remote site\n",
"Probability of buffers in set A being filled is 0.758\n",
"Probability of buffers in set B being filled is 0.485\n",
"Probability of buffers in set A intersection B being filled is 0.357\n"
]
}
],
"prompt_number": 25
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 29:Page 86"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random. We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.\"\n",
"P_fb=0.090 # Probability of selecting a bald female \n",
"P_fh=0.425 # Probabbility of selecting a female with hair\n",
"P_mb=0.302 # Probability of selecting a bald male\n",
"p_mh=0.183 # Probability of selecting a male with hair\n",
"print \"Given:\"\n",
"print \"P(fb)=0.090\"\n",
"print \"P(fh)=0.425\"\n",
"print \"P(mb)=0.302\"\n",
"print \"P(mh)=0.183\"\n",
"\n",
"print \"Let A denote the event that a bald person was chosen\"\n",
"print \"Let B denote the event that a female was chosen\"\n",
"print \"Then A intersection B is the event that a bald female was chosen, A union B the event that a bald person or a female was chosen,A EXOR B the event that a female with hair or a bald male was chosen and B-A the event that a female with hair was chosen.\"\n",
"P_A=P_fb+P_mb # Includes both male bald and female bald\n",
"P_B=P_fb+P_fh # Includes all females\n",
"P_A_intersection_B=P_fb # iIncludes all females who are bald\n",
"P_A_union_B=P_fb+P_fh+P_mb # Includes all females and bald male\n",
"P_A_EXOR_B=P_fh+P_mb # Includes female with hair and bald male\n",
"P_B_minus_A=P_fh # Includes only female with hair\n",
"print \"P(A)=\",P_A\n",
"print \"P(B)\",P_B\n",
"print \"P(A intersection B)=\",P_A_intersection_B\n",
"print \"P(A union B)=\",P_A_union_B\n",
"print \"P(A EXOR B)=\",P_A_EXOR_B\n",
"print \"P(B-A)=\",P_B_minus_A\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random. We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.\n",
"Given:\n",
"P(fb)=0.090\n",
"P(fh)=0.425\n",
"P(mb)=0.302\n",
"P(mh)=0.183\n",
"Let A denote the event that a bald person was chosen\n",
"Let B denote the event that a female was chosen\n",
"Then A intersection B is the event that a bald female was chosen, A union B the event that a bald person or a female was chosen,A EXOR B the event that a female with hair or a bald male was chosen and B-A the event that a female with hair was chosen.\n",
"P(A)= 0.392\n",
"P(B) 0.515\n",
"P(A intersection B)= 0.09\n",
"P(A union B)= 0.817\n",
"P(A EXOR B)= 0.727\n",
"P(B-A)= 0.425\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 31:Page 88"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random.We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.\"\n",
"print \"Let A denote the event that a bald person was chosen, B the event that a female was chosen, and C the event that a male was chosen.\"\n",
"P_fb=0.090 # Probability of selecting a bald female \n",
"P_fh=0.425 # Probabbility of selecting a female with hair\n",
"P_mb=0.302 # Probability of selecting a bald male\n",
"P_mh=0.183 # Probability of selecting a male with hair\n",
"print \"Given:\"\n",
"print \"P(fb)=0.090\"\n",
"print \"P(fh)=0.425\"\n",
"print \"P(mb)=0.302\"\n",
"print \"P(mh)=0.183\"\n",
"P_A=P_fb+P_mb # Includes both male bald and female bald\n",
"P_B=P_fb+P_fh # Includes all female\n",
"P_C=P_mb+P_mh # Includes all male\n",
"P_A_intersection_C=P_mb # Includes all males who are bald\n",
"P_C_intersection_A=P_mb # Since A intersection C and C intersection A are the same, this also includes all males who are bald\n",
"P_A_intersection_B=P_fb # Includes all females who are bald\n",
"P_B_intersection_A=P_fb # Since A intersection B and B intersection A are the same,this also includes all females who are bald\n",
"\n",
"P_A_given_B=P_A_intersection_B/P_B # By conditional probability\n",
"print \"P(A|B)=\",round(P_A_given_B,3);\n",
"print \"This is less than P(A) which is 0.392\"\n",
"P_A_given_C=P_A_intersection_C/P_C # By conditional probability\n",
"\n",
"print \"P(A|C)=\",round(P_A_given_C,3)\n",
"print \"This is quite a bit greater than P(A)\"\n",
"P_B_given_A=P_B_intersection_A/P_A# By conditional probability\n",
"print \"P(B|A)=\",round(P_B_given_A,2)\n",
"P_C_given_A=P_C_intersection_A/P_A# By conditional probability\n",
"print \"P(C|A)=\",round(P_C_given_A,2)\n",
"print \"Indeed, although P(B) is slightly larger than P(C),P(B|A) is much smaller than P(C|A)\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random.We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.\n",
"Let A denote the event that a bald person was chosen, B the event that a female was chosen, and C the event that a male was chosen.\n",
"Given:\n",
"P(fb)=0.090\n",
"P(fh)=0.425\n",
"P(mb)=0.302\n",
"P(mh)=0.183\n",
"P(A|B)= 0.175\n",
"This is less than P(A) which is 0.392\n",
"P(A|C)= 0.623\n",
"This is quite a bit greater than P(A)\n",
"P(B|A)= 0.23\n",
"P(C|A)= 0.77\n",
"Indeed, although P(B) is slightly larger than P(C),P(B|A) is much smaller than P(C|A)\n"
]
}
],
"prompt_number": 27
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 32:Page 89"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from fractions import Fraction\n",
"from decimal import *\n",
"print \"A coin was chosen at random and tossed.\"\n",
"p_fc_hd=Fraction('1/3') # Probability that a fair coin was chosen and head shows\n",
"p_fc_tl=Fraction('1/3') # Probability that a fair coin was chosen and tail shows\n",
"p_ufc_hd=Fraction('1/12') # Probability that a unfair coin was chosen and head shows\n",
"p_ufc_tl=Fraction('1/4') # Probability that a unfair coin was chosen and head shows\n",
"\n",
"print \"The probability that head shows is\",(Fraction(p_fc_hd)+Fraction(p_ufc_hd)) # Probability of head in both fair and unfair coins\n",
"print \"The probability that an unfair coin was chosen is\",(Fraction(p_ufc_hd)+Fraction(p_ufc_tl)) # Probability of head and tail in unfair coin\n",
"p_head=(Fraction(p_fc_hd)+Fraction(p_ufc_hd))\n",
"print \"The conditional probability that an unfair coin was chosen given that head shows is\",(Fraction(p_ufc_hd)/Fraction(p_head))# By conditional probability, probability of head in unfair coin to the probabilty of head in both fair and unfair coins\n",
"print \"The conditional probability that head shows given that an unfair coin was chosen is\",(Fraction(p_ufc_hd)/(Fraction(p_ufc_hd)+Fraction(p_ufc_tl))) # By conditional probability,probability of head in unfair coin to the probability of head and tail in unfair coin\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"A coin was chosen at random and tossed.\n",
"The probability that head shows is 5/12\n",
"The probability that an unfair coin was chosen is 1/3\n",
"The conditional probability that an unfair coin was chosen given that head shows is 1/5\n",
"The conditional probability that head shows given that an unfair coin was chosen is 1/4\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 33:Page 89"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"from fractions import Fraction\n",
"def P(n,r):\n",
" res=math.factorial(n)/math.factorial(n-r)\n",
" return res\n",
"faces=6#Number of faces in a dice\n",
"dice=3#Number of dice that were rolled\n",
"print \"Three dice were rolled.\"\n",
"print \"Given: No two faces were the same.\"\n",
"print \"Let A denote the event that there was an ace.\"\n",
"print \"Let B denote the event that no two faces were the same.\"\n",
"P_B=Fraction(P(faces,dice))/Fraction(pow(faces,dice)) # Sample space is pow(6,3) sice three dice are rolled.\n",
"P_A_intersection_B=Fraction(dice*P(faces-1,dice-1))/Fraction(pow(faces,dice))#Since A has occured, it cannot repeat again. So faces and dice are reduced by 1\n",
"P_A_given_B=Fraction(P_A_intersection_B)/Fraction(P_B) #By conditional probability\n",
"print \"P(A|B)=\",P_A_given_B\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Three dice were rolled.\n",
"Given: No two faces were the same.\n",
"Let A denote the event that there was an ace.\n",
"Let B denote the event that no two faces were the same.\n",
"P(A|B)= 1/2\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 34:Page 90"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from fractions import Fraction\n",
"print \"We define events and probabilities as follows.\"\n",
"print \"E:Introduction of computer education\"\n",
"print \"A1:Mr.X is selected as chairman\"\n",
"print \"A2:Mr.Y is selected as chairman\"\n",
"print \"A3:Mr.Z is selected as chairman\"\n",
"P_A1=Fraction('4/9') # Probability of Mr.X being selected\n",
"P_A2=Fraction('2/9') # Probability of Mr.Y being selected\n",
"P_A3=Fraction('3/9') # Probability of Mr.Z being selected\n",
"P_E_given_A1=Fraction('3/10') # probability that there was computer education in the college when Mr.X is selected as a chairman\n",
"P_E_given_A2=Fraction('5/10') # probability that there was computer education in the college when Mr.Y is selected as a chairman\n",
"P_E_given_A3=Fraction('8/10') # probability that there was computer education in the college when Mr.Z is selected as a chairman\n",
"print \"P(E)=P[(E INTERSECTION A1) UNION (E INTERSECTION A2) UNION (E INTERSECTION A3)]\"\n",
"print \"=P(E INTERSECTION A1) + P(E INTERSECTION A2) + P(E INTERSECTION A3)\"\n",
"print \"=P(A1)P(E|A1)+P(A2)P(E|A2)+P(A3)P(E|A3)\" # Since by conditional probability, P(E|A1)=P(E intersection A1)/P(A1)\n",
"print \"=\",P_A1,\"*\",P_E_given_A1,\"+\",P_A2,\"*\",P_E_given_A2,\"+\",P_A3,\"*\",P_E_given_A3,\"=\",\n",
"P_E=(P_A1*P_E_given_A1)+(P_A2*P_E_given_A2)+(P_A3*P_E_given_A3) # P_E denotes the probability of computer education in the college\n",
"print P_E\n",
"print \"So, the probability that there was computer education in the college is\",P_E\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"We define events and probabilities as follows.\n",
"E:Introduction of computer education\n",
"A1:Mr.X is selected as chairman\n",
"A2:Mr.Y is selected as chairman\n",
"A3:Mr.Z is selected as chairman\n",
"P(E)=P[(E INTERSECTION A1) UNION (E INTERSECTION A2) UNION (E INTERSECTION A3)]\n",
"=P(E INTERSECTION A1) + P(E INTERSECTION A2) + P(E INTERSECTION A3)\n",
"=P(A1)P(E|A1)+P(A2)P(E|A2)+P(A3)P(E|A3)\n",
"= 4/9 * 3/10 + 2/9 * 1/2 + 1/3 * 4/5 = 23/45\n",
"So, the probability that there was computer education in the college is 23/45\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 35:Page 91"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"A1: Event that the selected item was produced by machine M1\"\n",
"print \"A2: Event that the selected item was produced by machine M2\"\n",
"print \"A3: Event that the selected item was produced by machine M3\"\n",
"print \"E: Event that the selected item is defective\"\n",
"P_A1=0.2 # The probability that an item selected at random from the entire batch was produced by machine M1\n",
"P_A2=0.3 # The probability that an item selected at random from the entire batch was produced by machine M2\n",
"P_A3=0.5 # The probability that an item selected at random from the entire batch was produced by machine M3\n",
"P_E_given_A1=0.01 # Probability that an item produced by machine M1 will be defective\n",
"P_E_given_A2=0.02 # Probability that an item produced by machine M2 will be defective\n",
"P_E_given_A3=0.03 # Probability that an item produced by machine M3 will be defective\n",
"print \"We require to calculate the conditional probability P(A3|E)\"\n",
"print \"Using bayes' theorem\"\n",
"P_A3_given_E=(P_A3*P_E_given_A3)/((P_A1*P_E_given_A1)+(P_A2*P_E_given_A2)+(P_A3*P_E_given_A3))\n",
"print \"The probability that this item was produced by machine M3 is\",round(P_A3_given_E,3)\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"A1: Event that the selected item was produced by machine M1\n",
"A2: Event that the selected item was produced by machine M2\n",
"A3: Event that the selected item was produced by machine M3\n",
"E: Event that the selected item is defective\n",
"We require to calculate the conditional probability P(A3|E)\n",
"Using bayes' theorem\n",
"The probability that this item was produced by machine M3 is 0.652\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 36:Page 95"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from fractions import Fraction\n",
"import math\n",
"from decimal import *\n",
"print \"Estimating the likelihood that there will be a 1 hour examination when the professor is scheduled to get out of town\"\n",
"print \"Sample space={x1,x2,x3,x4}\"\n",
"P_X1=Fraction('1/2')#X1 denotes professor out of town and exam given\n",
"P_X2=Fraction('1/16')#X2 denotes professor out of town and exam not given\n",
"P_X3=Fraction('3/16')#X3 denotes professor in town and exam given\n",
"P_X4=Fraction('1/4')#X4 denotes professor in town and exam not given\n",
"print \"A denotes the event that exam is given and B denote the event that professor is out of town\"\n",
"P_A=P_X1+P_X3#Probability of A\n",
"P_A_numerator=P_A.numerator#Separating numerator and denominators in all P(A)\n",
"P_A_denominator=P_A.denominator\n",
"P_A_given_B=P_X1/(P_X1+P_X2)#Probability of P(A|B)\n",
"P_A_given_B_numerator=P_A_given_B.numerator#Separating numerators and denominators in P(A|B)\n",
"P_A_given_B_denominator=P_A_given_B.denominator\n",
"print \"Probability that the exam is given is \",P_A\n",
"print \"Probability of exam given that the profession is out of town is \",P_A_given_B\n",
"log_P_A_numerator=round(math.log(P_A_numerator,2),2)#Logrithm of 11\n",
"log_P_A_denominator=math.log(P_A_denominator,2)#Logrithm of 16\n",
"neg_log_P_A=-log_P_A_numerator+log_P_A_denominator#Negative logrithm of P(A).SInce log(a/b)=lob(a)-log(b)\n",
"getcontext().prec=2#Decides the number of digits after decimal point\n",
"print \"The needed information to determine that an examination will be given is \",neg_log_P_A,\"bits\"\n",
"log_P_A_given_B_numerator=math.log(P_A_given_B_numerator,2)#Logrithm of \n",
"log_P_A_given_B_denominator=round(math.log(P_A_given_B_denominator,2),2)#Logrithm of 9\n",
"print \"Information provided by the fact that the professor is out of town on the fact that examination is given is I(A,B)=\",(-log_P_A_numerator+log_P_A_denominator+log_P_A_given_B_numerator-log_P_A_given_B_denominator),\"bits\"\n",
"print \"C denotes the event that the professor is in town\"\n",
"P_A_given_C=P_X3/(P_X3+P_X4)\n",
"print \"Probability that the examination is given when the professor is in town is \",P_A_given_C\n",
"P_A_given_C_numerator=P_A_given_C.numerator#Separating numerators and denominators in P(A|C)\n",
"P_A_given_C_denominator=P_A_given_C.denominator\n",
"log_P_A_given_C_numerator=round(math.log(P_A_given_C_numerator,2),2)\n",
"log_P_A_given_C_denominator=math.log(P_A_given_C_denominator,2)\n",
"print \"I(A,C)=\",round((-log_P_A_numerator+log_P_A_denominator+log_P_A_given_C_numerator-log_P_A_given_C_denominator),2)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Estimating the likelihood that there will be a 1 hour examination when the professor is scheduled to get out of town\n",
"Sample space={x1,x2,x3,x4}\n",
"A denotes the event that exam is given and B denote the event that professor is out of town\n",
"Probability that the exam is given is 11/16\n",
"Probability of exam given that the profession is out of town is 8/9\n",
"The needed information to determine that an examination will be given is 0.54 bits\n",
"Information provided by the fact that the professor is out of town on the fact that examination is given is I(A,B)= 0.37 bits\n",
"C denotes the event that the professor is in town\n",
"Probability that the examination is given when the professor is in town is 3/7\n",
"I(A,C)= -0.69\n"
]
}
],
"prompt_number": 32
}
],
"metadata": {}
}
]
}
|