In [9]:

```
from numpy import intersect1d, setdiff1d, union1d,inf
from __future__ import division
S=[1,2,3,4,5,6] #sample space for the rolling of a die
A=[2,4,6] #event that an even number occurs
B=[1,3,5] #event that an odd number occurs
C=[2,3,5] #event that a prime number occurs
print 'sample space for the event that an even or a prime number occurs = ',union1d(A,C)
print 'sample space for the event that an odd prime number occurs = ',intersect1d(B,C)
print 'sample space for the event that a prime number does not occur = ',setdiff1d(S,C) #It is the complement of the set C.
intersect1d(A,B) #It is a null set or null vector since there can't occur an even and an odd number simultaneously
H=0# #"head" face of a coin
T=1# #"tail" face of a coin
S=["000","001","010","011","100","101","110","111"] # #sample space for the toss of a coin three times
A=["000","001","100"]# #event that two more or more heads appear consecutively
B=["000","111"]# #event that all tosses are the same
print 'sample space for the event in which only heads appear = ',intersect1d(A,B)
print 'Experiment:tossing a coin until a head appears and then counting the number of times the coin is tossed'
S=[1,2,3,4,5,inf] #The sample space has infinite elements in it
print "Since every positive integer is an element of S,the sample space is infinite"
```

In [10]:

```
print 'Experiment:three coins are tossed and the number of heads are observed'
S=[0,1,2,3]# #the sample space for the experiment where 0 implies no heads,1 implies only one head out of the three coins and so on
print "the probability space is as follows "
P0=1/8# #probability of getting no head on any of the coins i.e TTT
P1=3/8# #probability of getting only one head on any of the coins, out of the three coins i.e HTT,THT,TTH
P2=3/8# #probability of getting two heads, out of the three coins i.e THH,HTH,HHT
P3=1/8# #probability of getting all the three heads i.e HHH
print "A is the event that atleast one head appears and B is the event that all heads or all tails appear "
A=[1,2,3]# # HHH,HHT,HTH,HTT,THH,THT,TTH
B=[0,3]# #HHH,TTT
PA=P1+P2+P3#
print 'probability of occurrence of event A = ',PA
PB=P0+P3#
print 'probability of occurrence of event B = ',PB
```

In [11]:

```
from fractions import Fraction
print "Experiment: a card is selected from a deck of 52 cards "
print "A is the event of the selected card being a spade "
print "B is the event of the selected card being a face card "
t=52 # #the total number of cards
s=13# #number of spades
PA= s/t#
print 'probability of selecting a spade = ',Fraction(PA).limit_denominator(100)
f=12# #number of face cards(jack,queen,king)
PB=f/t#
print 'probability of selecting a face card = ',Fraction(PB).limit_denominator(100)
sf=3# #number of spade face cards
Psf=sf/t#
print "probability of selecting a spade face card is:",Fraction(Psf).limit_denominator(100)
```

In [12]:

```
print "Experiment: selection of a student out of 100 students "
M=30# #no of students taking mathematics
C=20# #no of students taking chemistry
T=100# #total no. of students
PM = M/T #probability of the selected student taking mathematics
PC = C/T #probability of the selected student taking chemistry
MnC=10# #no of students taking mathematics and chemistry
PMnC = MnC/T #probability of the selected student taking mathematics and chemistry both
PMorC = PM+PC-PMnC#
print 'probability of the selected student taking mathematics or chemistry = ',Fraction(PMorC).limit_denominator(100)
```

In [13]:

```
print "A bag contains 12 items of which four are defective.Three items are drawn at random,one after the other"
s=12# #total itmes in the bag
d=4# #defective items in the bag
Pf=(s-d)/s # #probability that the first item drawn is non defective
Pe=Pf*((s-d-1)/(s-1))*((s-d-2)/(s-2))
print 'probability that all three items are non defective = ',Fraction(Pe).limit_denominator(100)
#after the first item is chosen,the second item is to be chosen from 1 less than the original number of items in the box and similarly the number of non defective items gets decreased by 1.Similarly ,for the third draw of item from the box
```

In [14]:

```
from numpy import intersect1d
H=1# #heads of a coin
T=2# #tails of the coin
S=[111,112,121,122,211,212,221,222] #sample space for the toss of a coin three times. 111 implies heads all three times,112 implies heads on first two tosses and tails on the third toss
A=[111,112,121,122]# #event that first toss is heads
B=[111,112,211,212]# #event that second toss is heads
C=[112,211]# #event that exactly two heads appear in a row
PA=len(A)/len(S)
print 'probability of A is = ',PA
PB=len(B)/len(S)
print 'probability of B is = ',PB
PC=len(C)/len(S)
print 'probability of C is = ',PC
AnB=intersect1d(A,B)
AnC=intersect1d(A,C)
BnC=intersect1d(B,C)
PAnB= len(AnB)/len(S)
print 'probability of the event AnB = ',PAnB
PAnC= len(AnC)/len(S)#
print 'probability of the event AnC = ',PAnC
PBnC= len(BnC)/len(S)
print 'probability of the event BnC = ',PBnC
if((PA*PB)==PAnB):
print "A and B are independent"
else:
print "A and B are dependent"
if((PA*PC)==PAnC):
print "A and C are independent"
else:
print "A and C are dependent"
if((PB*PC)==PBnC):
print "B and C are independent"
else:
print "B and C are dependent"
```

In [15]:

```
print "Experiment: A and B both shoot at a target"
PA=1/4# #given probability of A hitting the target
PB=2/5# #given probability of B hitting the target
print "A and B are independent events so PA*PB will be equal to probability of the event of A and B both hitting the target i.e PAnB"
PAnB=PA*PB#
PAorB=PA+PB-PAnB#
print'probability of atleast one of them hitting the target = ', PAorB
```

In [16]:

```
print "Experiment: Three horses race together twice"
Ph1=1/2# #probability of first horse winning the race
Ph2=1/3# #probability of second horse winning the race
Ph3=1/6# #probability of third horse winning the race
S=[11,12,13,21,22,23,31,32,33] #sample space where 11 implies first horse winning the first and second race both,12 implies first horse winning the first race and second horse winning the second race and so on
P11=Ph1*Ph1 #probability of first horse winning both races
P12=Ph1*Ph2 #probability of first horse winning the first race and second horse winning the second race
P13=Ph1*Ph3 #probability of first horse winning the first race and third horse winning the second race
P21=Ph2*Ph1 #probability of second horse winning the first race and first horse winning the second race
P22=Ph2*Ph2 #probability of second horse winning both the races
P23=Ph2*Ph3 #probability of second horse winning the first race and third horse winning the second race
P31=Ph3*Ph1 #probability of third horse winning the first race and first horse winning the second race
P32=Ph3*Ph2 #probability of third horse winning the first race and second horse winning the second race
P33=Ph3*Ph3 #probability of third horse winning both the races
print 'probability of third horse winning the first race and first horse winning the second race is = ',Fraction(P31).limit_denominator(100)
```

In [17]:

```
from scipy.misc import factorial
n=6# #number of times a fair coin is tossed and getting a heads is a success
p=1/2# #probability of getting a heads
q=1/2 # #probability of not getting a heads
P2=(factorial(6)/(factorial(6-2)*factorial(2)))*p**2*q**(6-2)#
print 'probability of getting exactly two heads (i.e k=2) = ',Fraction(P2).limit_denominator(100)
P4=(factorial(6)/(factorial(6-4)*factorial(4)))*p**4*q**(6-4)# #probabilty of getting four heads
P5=(factorial(6)/(factorial(6-5)*factorial(5)))*p**5*q**(6-5)# #probabilty of getting five heads
P6=(factorial(6)/(factorial(6-6)*factorial(6)))*p**6*q**(6-6)# #probabilty of getting five heads
PA=P4+P5+P6 #
print 'probability of getting atleast four heads(i.e k=4,5 or 6) = ',Fraction(PA).limit_denominator(100)
Pn=q**6 #probability of getting no heads
Pm=1-Pn#
print 'probability of getting one or more heads = ',Fraction(Pm).limit_denominator(100)
```

In [18]:

```
from scipy.misc import factorial
print "A box contains 12 items of which three are defective"
print "A sample of three items is selected from the box"
s=factorial(12)/(factorial(12-3)*factorial(3))#
print 'number of elements in the sample space where samples are of size 3',s
#X denotes the number of defective items in the sample
x=[0,1,2,3]# #range space of the random variable X
```

In [19]:

```
from numpy import mat,nditer
r=[1,2,3,4,5,6,5,4,3,2,1]
#number of outcomes whose sum is 2,3,4,5,6,7,8,9,10,11,12 respectively such that there is only 1 outcome i.e (1,1) whose sum is 2,two outcomes (1,2) and (2,1) whose sum is 3 and so on
t=36# #total number of elements in the sample space of the experiment of tossing a pair of dice
x=mat([[2,3,4,5,6,7,8,9,10,11,12]]) #range space of random variable X which assigns to each point in sample space the sum of the numbers
D=mat([[2,3,4,5,6,7,8,9,10,11,12],[0.0277778, 0.0555556 , 0.0833333, 0.1111111, 0.1388889 ,0.1666667, 0.1388889 ,0.1111111, 0.0833333, 0.0555556, 0.0277778]])
print '\n\ndistribution table of X where first row gives the range space and second row gives the respective probabilities is as follows : '
print "x(i)\t\t\tp(i)"
for x,y in nditer([D[0,:],D[1,:]]):
y=float(y)
print x,"\t\t\t",Fraction(y).limit_denominator(100)
```

In [20]:

```
from fractions import Fraction
from numpy import nditer,mat
from scipy.misc import factorial
print "a box contains 12 items of which three are defective"
print "A sample of three items is selected from the box"
r=factorial(9)/(factorial(9-3)*factorial(3)) #number of samples of size 3 with no defective items
t=220# #number of different samples of size 3 i.e the number of elements in the sample space
P0=r/t #probability of getting no defective item
r1=3*(factorial(9)/(factorial(9-2)*factorial(2))) #number of samples of size 3 getting 1 defective item
P1=r1/t #probability of getting 1 defective item
r2=9*(factorial(3)/(factorial(3-2)*factorial(2))) #number of samples of size 3 getting 2 defective item
P2=r2/t #probability of getting 2 defective item
r3=1# #number of samples of size 3 getting 3 defective item
P3=r3/t #probability of getting 3 defective item
x=[0,1,2,3]#
p=mat([[P0,P1,P2,P3]])
D=mat([[0,1,2,3],[P0,P1,P2,P3]])
print '\ndistribution table for random variable X the upper row being values of X\n:'
print "x(i)\t\t\tp(i)"
for x,y in nditer([D[0,:],D[1,:]]):
y=float(y)
print x,"\t\t\t",Fraction(y).limit_denominator(100)
```

In [21]:

```
print "A fair coin is tossed six times"
x=[0,1,2,3,4,5,6] #number of heads which can occur
p=[1/64,6/64,15/64,20/64,15/64,6/64,1/64]# #probability of occurring of heads where 1/64 is probability for occurrence of a single head,6/64 that of occurrence of two heads and so on.
r=0#
for i in range(0,7):
r = r + (x[i]*p[i])#
print 'mean or expected number of heads are = ',r
print "X is a random variable which gives possible number of defective items in a sample of size 3"
#Box contains 12 items of which three are defective
x=[0,1,2,3]# #possible number of defective items in a smaple of size 3
p=[84/220,108/220,27/220,1/220]# #probability of occurrence of each number in x respectively where 84/220 is the probability for getting no defective item,108/220 is that of getting 1 defective item and so on.
r=0#
for i in range(0,4):
r = r + (x[i]*p[i])#
print 'expected number of defective items in a sample of size 3 are = ',r
Ph1=1/2# #probability of winning the race by first horse
Ph2=1/3# #probability of winning the race by second horse
Ph3=1/6# #probability of winning the race by third horse
#X is the payoff function for the winning horse
X1=2# #X pays $2 as first horse wins the rac
X2=6# #X pays $6 as second horse wins the race
X3=9# #X pays $9 as third horse wins the race
E=X1*Ph1+X2*Ph2+X3*Ph3
print 'expected pay off for the race is = ',E
```

In [22]:

```
from numpy import sqrt
u=3 #mean of distribution of random variable X
x=[0,1,2,3,4,5,6] #values of X in the distribution as x where it is the number of times heads occurs when a coin is tossed six times
p=[1/64,6/64,15/64,20/64,15/64,6/64,1/64] #probabilities of occurrence of each value of X (x) in the distribution such that 1/64 gives the probability of occurrence of no heads at all,6/64 gives that of occurrence of heads for only one time and so on
k=0
for i in range(0,7):
k=k+((x[i]-u)**2)*p[i]
print 'Variance of X is = ',k
s=sqrt(k)#
print'Standard deviation of X is = ',round(s,4)
u=0.75# #mean
x=[0,1,2,3]# #values of random variable X as x in the probability distribution of X
p=[84/220,108/220,27/220,1/220]# #probability of values in x which appear in distribution table of X
g=0#
for i in range(0,4):
g=g+((x[i])**2)*p[i]
h=g-(u*u)
print 'variance of X is = ',round(h,4)
sd=sqrt(h)#
print 'Standard deviation for X = ',round(sd,4)
```

In [23]:

```
from __future__ import division
from numpy import sqrt
p=1/5# #probability of the man hitting a target
q=1-1/5# #probability of the man not hitting the target
n=100# #number of times the man fires
e=n*p#
print 'expected number of times the man will hit the target = ',e
r=sqrt(n*p*q)#
print 'Standard deviation = ',r
p=1/2# #probability of guessing the correct answer in a five question true-false exam
n=5# #number of questions in the exam
g=n*p#
print 'expected number of correct answers in the exam = ',g
```

In [24]:

```
from __future__ import division
u=75# #mean of a random variable X
n=5# #standard deviation of X
k=2# #for k=2
l1=u-k*n
l2=u+k*n
P1=1-(1/k)**2
print "thus the probability that a value of X lies between 65 and 85 is atleast 0.75 according to Chebyshev inequality"
k=3# #for k=3
l1=u-k*n
l2=u+k*n
P2=1-(1/k)**2
print "thus the probability that a value of X lies between 60 and 90 is atleast 0.8888889 according to Chebyshev Inequality"
```

In [25]:

```
from __future__ import division
print "a die is tossed 5 times with the following outcomes"
x1=3#
x2=4#
x3=6#
x4=1#
x5=4#
xmean=(x1+x2+x3+x4+x5)/5 #mean of the outcomes
print "for a fair die the mean is 3.5.So law of large numbers tells us that as number of outcomes increase for this experiment,there is a greater likelihood that themean will get closer to 3.5"
```