Chapter 12: Probability

Example Problem 12.1, Page Number 283

In [2]:
#Varible declaration
n = range(1,51,1)
Prob = 0
for x in n:
    Prob = 1./len(n) + Prob

#Results
print 'Probability of picking up any one ball is %3.1f'%Prob
Probability of picking up any one ball is 1.0

Example Problem 12.2, Page Number 284

In [3]:
from fractions import Fraction

#Variable Declaration
n = 52          #Total cards
nheart = 13     #Number of cards with hearts

#Calculations
Pe = Fraction(nheart,n)

#Results
print 'Probability of one (heart)card picked from a std. stack of %d cards is'%n,Pe
Probability of one (heart)card picked from a std. stack of 52 cards is 1/4

Example Problem 12.3, Page Number 285

In [4]:
#Variable Declaration
n = 52          #Total cards

#Calculations
TotalM = n*(n-1)*(n-2)*(n-3)*(n-4)
#Results
print 'Total number of Five card arrangment from a deck of 52 cards is %d'%TotalM
Total number of Five card arrangment from a deck of 52 cards is 311875200

Example Problem 12.4, Page Number 285

In [5]:
#Variable Declaration
n1 = 2         #Two spin states for 1st electron in orbit 1
n2 = 2         #Two spin states for 2nd electron in orbit 2

#Calculation
M = n1*n1

#Results
print 'Possible spin states for excited state are %2d'%M
Possible spin states for excited state are  4

Example Problem 12.5, Page Number 286

In [6]:
from math import factorial

#Variable Declaration
n = 12         #Total Number of players 
j = 5          #Number player those can play match

#Calculation
P = factorial(n)/factorial(n-j)

#Results
print 'Maximum Possible permutations for 5 player to play are %8d'%P
Maximum Possible permutations for 5 player to play are    95040

Example Problem 12.6, Page Number 287

In [7]:
from math import factorial

#Variable Declaration
n = 52         #Number of cards in std . pack
j = 5          #Number of cards in subset

#Calculation
C = factorial(n)/(factorial(j)*factorial(n-j))

#Results
print 'Maximum Possible 5-card combinations are %8d'%C
Maximum Possible 5-card combinations are  2598960

Example Problem 12.7, Page Number 288

In [8]:
from math import factorial

#Variable Declaration
x = 6          #Number of electrons
n = 2          #Number of states

#Calculation
P = factorial(x)/(factorial(n)*factorial(x-n))

#Results
print 'Total number of quantum states are %3d'%P
Total number of quantum states are  15

Example Problem 12.8, Page Number 289

In [9]:
from math import factorial
from fractions import Fraction

#Variable Declaration
n = 50          #Number of separate experiments
j1 = 25          #Number of sucessful expt with heads up
j2 = 10          #Number of sucessful expt with heads up

#Calculation
C25 = factorial(n)/(factorial(j1)*factorial(n-j1))
PE25 = Fraction(1,2)**j1
PEC25 = (1-Fraction(1,2))**(n-j1)
P25 = C25*PE25*PEC25

C10 = factorial(n)/(factorial(j2)*factorial(n-j2))
PE10 = Fraction(1,2)**j2
PEC10 = (1-Fraction(1,2))**(n-j2)
P10 = C10*PE10*PEC10

#Results
print 'Probability of getting 25 head out of 50 tossing is %4.3f'%(float(P25))
print 'Probability of getting 10 head out of 50 tossing is %4.3e'%(float(P10))
Probability of getting 25 head out of 50 tossing is 0.112
Probability of getting 10 head out of 50 tossing is 9.124e-06

Example Problem 12.9, Page Number 290

In [10]:
from math import factorial, log
#Variable Declaration
N = [10,50,100]       #Valures for N

#Calculations
print '  N        ln(N!)       ln(N!)sterling      Error'
for i in N:
    lnN = log(factorial(i))
    lnNs = i*log(i)-i
    err = abs(lnN-lnNs)
    print '%3d       %5.2f        %5.2f              %4.2f'%(i,lnN,lnNs, err)
  N        ln(N!)       ln(N!)sterling      Error
 10       15.10        13.03              2.08
 50       148.48        145.60              2.88
100       363.74        360.52              3.22

Example Problem 12.10, Page Number 293

In [11]:
from fractions import Fraction

#Variable Declaration
fi = 1       #Probability of receiving any card
n = 52       #Number od Cards

#Calculations
sum = 0
for i in range(52):
    sum = sum + fi

Pxi = Fraction(fi,sum)

#Results
print 'Probability of receiving any card', Pxi
Probability of receiving any card 1/52

Example Problem 12.11, Page Number 295

In [12]:
from math import exp
from scipy import integrate
#Variable Declaration

#Calculations
fun = lambda x: exp(-0.05*x)
Pt = 0
for i in range(0,101):
    Pt = Pt + fun(i)
    
Ptot = integrate.quad(fun, 0.0, 100.)

#Results
print 'Sum of Px considering it as discrete function %4.1f'%Pt
print 'Sum of Px considering it as contineous function %4.1f'%Ptot[0]
Sum of Px considering it as discrete function 20.4
Sum of Px considering it as contineous function 19.9

Example Problem 12.12, Page Number 296

In [13]:
from sympy import Symbol

#Variable Declaration
r = Symbol('r')      #Radius of inner circle
C = [5,2,0]
#Calculations
A1 = pi*r**2
A2 = pi*(2*r)**2 - A1
A3 = pi*(3*r)**2 - (A1 + A2)
At = A1 + A2 + A3
f1 = A1/At
f2 = A2/At
f3 = A3/At
sf = f1 + f2 + f3

ns = (f1*C[0]+f2*C[1]+f3*C[2])/sf

#Results
print 'A1, A2, A3: ', A1,', ', A2,', ', A3
print 'f1, f2, f3: ', f1,f2,f3
print 'Average payout $', round(float(ns),2)
A1, A2, A3:  3.14159265358979*r**2 ,  9.42477796076938*r**2 ,  15.707963267949*r**2
f1, f2, f3:  0.111111111111111 0.333333333333333 0.555555555555556
Average payout $ 1.22