{
"metadata": {
"name": "",
"signature": "sha256:1dd0a79ec872797b72ec8505541140b8f3aa9faf7b4e826202d80a4061f78ae9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 2 : Random Variables and Processes"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.1 Page No : 85"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#A & B are two events occured in sample space S, where P(A) & P(B) are their corresponding probability\n",
"P_S = 1\n",
"\n",
"#Given A&B are not mutually exclusive events, \n",
"#Probability of A is 0.2 = P_A\n",
"#Probability of B is 0.4 = P_B\n",
"#Probability of either A or B is 0.5 = P_AUB\n",
"P_A = 0.2\n",
"P_B = 0.4\n",
"P_AUB = 0.5\n",
"\n",
"#Probability of both of A&B jointly occur is P_AinterB = P_A+P_B-P_AUB where inter is intersection\n",
"P_AinterB = P_A+P_B-P_AUB\n",
"print 'Probability of both of A&B jointly occur is ',(P_AinterB)\n",
"\n",
"#Probability of none of AorB are occur is P_NOAB = Total occurence(P_S) - Probability of either AorB(P_AUB)\n",
"P_NOAB = P_S-P_AUB\n",
"print 'Probability of none of AorB are occur is ',(P_NOAB)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Probability of both of A&B jointly occur is 0.1\n",
"Probability of none of AorB are occur is 0.5\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.2 Page No : 86"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Probability that A will occur if B has already occurred(P_AB) = ratio of Probability of joint occurence of A&B (P_A_B) & Probability of B(P_B)\n",
"#P_A_B(robability of joint occurence) = Probability that A&B both occur(P_AinterB)\n",
"\n",
"#From given values P_AinterB = 0.1 implies P_A_B = 0.1 & P_B = 0.4\n",
"P_AinterB = 0.1\n",
"P_A_B = P_AinterB \n",
"P_B = 0.4\n",
"\n",
"P_AB = P_A_B/P_B\n",
"\n",
"#Similarly\n",
"#Probability that B will occur if A has already occurred(P_BA) = ratio of Probability of joint occurence of A&B (P_A_B) & Probability of B(P_A)\n",
"\n",
"#From given values P_A = 0.2\n",
"P_A = 0.2\n",
"\n",
"P_BA = P_A_B/P_A\n",
"\n",
"#Bayes theorem says that P_AB = (P_A/P_B)*P_BA\n",
"#After Calculating LHS & RHS if both are equal then bayes theorem is satisfying\n",
"\n",
"#Calculating LHS\n",
"LHS = P_AB\n",
"\n",
"#Calculating RHS\n",
"RHS = (P_A/P_B)*P_BA\n",
"\n",
"print 'P(A/B) = ',(P_AB);\n",
"\n",
"if LHS == RHS:\n",
" print ('LHS = RHS, Hence Bayes theorem is verified' );\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"P(A/B) = 0.25\n",
"LHS = RHS, Hence Bayes theorem is verified\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.5 Page No : 95"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Given, The probability that m0 is sent is 0.7 = P_m0\n",
"P_m0 = 0.7\n",
"\n",
"#The probability that m0 is sent is 0.3 = P_m1\n",
"P_m1 = 0.3\n",
"\n",
"#The probability that r0 is received given that m0 is sent is 0.9 = P_r0m0 where r is voltage & m is message\n",
"P_r0m0 = 0.9\n",
"\n",
"#the probability that r1 is received given that m0 is sent is 0.1 = P_r1m0 where r is voltage & m is message\n",
"P_r1m0 = 0.1\n",
"\n",
"#The probability that r1 is received given that m1 is sent is 0.6 = P_r1m1\n",
"P_r1m1 = 0.6\n",
"\n",
"#the probability that r0 is received given that m1 is sent is 0.4 = P_r0m1 where r is voltage & m is message\n",
"P_r0m1 = 0.4\n",
"\n",
"#With the given values check eqations P_r0m0*P_m0(P00) > P_r0m1*P_m1(P01)\n",
"P00 = P_r0m0*P_m0\n",
"P01 = P_r0m1*P_m1\n",
"\n",
"if P00>P01:\n",
" print ('as P(r0|m0)*P(m0) > P(r0|m1)*P(m1) is valid, we whould select m0 whenever r0 is received')\n",
"\n",
"#With the given values check eqations P_r1m1*P_m1(P11) > P_r1m0*P_m0(P10)\n",
"P11 = P_r1m1*P_m1\n",
"P10 = P_r1m0*P_m0\n",
"\n",
"if P11>P10:\n",
" print ('as P(r1|m1)*P(m1) > P(r1|m0)*P(m0) is valid, we whould select m1 whenever r1 is received')\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"as P(r0|m0)*P(m0) > P(r0|m1)*P(m1) is valid, we whould select m0 whenever r0 is received\n",
"as P(r1|m1)*P(m1) > P(r1|m0)*P(m0) is valid, we whould select m1 whenever r1 is received\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.6 Page No : 96"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Given, the probability that r0 is received given that m0 is sent is 0.9 = P_r0m0 where r is voltage & m is message\n",
"P_r0m0 = 0.9\n",
"\n",
"#The probability that m0 is sent is 0.7 = P_m0\n",
"P_m0 = 0.7\n",
"\n",
"#The probability that r1 is received given that m1 is sent is 0.6 = P_r1m1\n",
"P_r1m1 = 0.6\n",
"\n",
"#The probability that m0 is sent is 0.3 = P_m1\n",
"P_m1 = 0.3\n",
"\n",
"#The probability that the transmitted signal is correctly read at receiver is P(c)(P_c) = the probability that m0 was sent when r0 was read(P_r0m0*P_m0) + the probability that m1 was sent when r1 was read(P_r1m1*P_m1)\n",
"\n",
"P_c = P_r0m0*P_m0+P_r1m1*P_m1\n",
"\n",
"#P(e)(P_e) = 1-P(c)\n",
"P_e = 1-P_c\n",
"\n",
"print 'P(e) = ',P_e\n",
"print 'P(c) = ',P_c\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"P(e) = 0.19\n",
"P(c) = 0.81\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.7 Page No : 96"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Here P(ra|mb) is denoted as P_ramb where a is 0,1,2 & b is 0,1\n",
"#P(X) is denoted as P_X where X is m0, m1, C & E\n",
"\n",
"#From given values P_m0 = 0.6, P_m1 = 0.4, P_r0m0 = 0.6, P_r1m1 = 0.7, P_r0m1 = 0, P_r1m0 = 0.2, P_r2m0 = 0.2 & P_r2m1 = 0.3\n",
"P_m0 = 0.6\n",
"P_m1 = 0.4\n",
"P_r0m0 = 0.6\n",
"P_r1m1 = 0.7\n",
"P_r0m1 = 0\n",
"P_r1m0 = 0.2\n",
"P_r2m0 = 0.2\n",
"P_r2m1 = 0.3\n",
"\n",
"#(a)\n",
"#Comaparing P(r0|m0)*P(m0) & P(r0|m1)*P(m1) gives result\n",
"LHS = P_r0m0*P_m0\n",
"RHS = P_r0m1*P_m1\n",
"\n",
"print 'As P(r0|m0)*P(m0)[',LHS,'] > P(r0|m1)*P(m1)[',RHS,']'\n",
"print ('we select m0 whenever r0 is received')\n",
"\n",
"#Similarly compare P(r1|m1)*P(m1) & P(r1|m0)*P(m0)\n",
"LHS = P_r1m1*P_m1\n",
"RHS = P_r1m0*P_m0\n",
"\n",
"print 'As P(r1|m1)*P(m1)[',LHS,'] > P(r1|m0)*P(m0)[',RHS,']'\n",
"print ('we select m1 whenever r1 is received')\n",
"\n",
"#compare P(r2|m0)*P(m0) & P(r2|m1)*P(m1)\n",
"LHS = P_r2m0*P_m0\n",
"RHS = P_r2m1*P_m1\n",
"\n",
"print 'As P(r2|m0)*P(m0)[',LHS,'] = P(r2|m1)*P(m1)[',RHS,']'\n",
"print ('We can accordingly make either assignment and we arbitrarily associate r2 with m0')\n",
"\n",
"#(b)\n",
"#The probability of being correct is P(C) = P(r0|m0)*P(m0)+P(r1|m1)*P(m1)+P(r2|m0)*P(m0)\n",
"P_C = P_r0m0*P_m0+P_r1m1*P_m1+P_r2m0*P_m0\n",
"\n",
"#The probability of error is P(E) = 1-P(C)\n",
"P_E = 1 - P_C;\n",
"\n",
"print 'Probability of being correct is P(C) = ',(P_C)\n",
"print 'Probability of error is P(E) = ',(P_E)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"As P(r0|m0)*P(m0)[ 0.36 ] > P(r0|m1)*P(m1)[ 0.0 ]\n",
"we select m0 whenever r0 is received\n",
"As P(r1|m1)*P(m1)[ 0.28 ] > P(r1|m0)*P(m0)[ 0.12 ]\n",
"we select m1 whenever r1 is received\n",
"As P(r2|m0)*P(m0)[ 0.12 ] = P(r2|m1)*P(m1)[ 0.12 ]\n",
"We can accordingly make either assignment and we arbitrarily associate r2 with m0\n",
"Probability of being correct is P(C) = 0.76\n",
"Probability of error is P(E) = 0.24\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.8 Page No : 99"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math \n",
"from scipy.integrate import quad \n",
"\n",
"#Given, probability density function of X is fX_x where fX_x = a*e**(-0.2*x) for x greater than & equal to 0 & = 0 eleswhere\n",
"\n",
"#a = fX_x/(a*e**(-0.2*x))\n",
"#from definition integration of fX_x with limits -infinity to +infinity is 1\n",
"#As per given fX_x, integration of a*e**(-0.2*x) with limits 0 & +inffinity and obtained value be P\n",
"#a = 1/p\n",
"\n",
"\n",
"def f7(x): \n",
"\t return math.e**(-0.2*x)\n",
"\n",
"P = quad(f7,0,100)[0]\n",
"\n",
"a = 1./P\n",
"\n",
"print 'a = ',round(a,4)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a = 0.2\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.10 Page No : 105"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math \n",
"\n",
"#We know that, Probabilty of error(P_error) for the signal correpted by Gaussian channel variance sigma**2 where signal having voltage levels as 0&V is (1/2)*erfc(V/(2*math.sqrt(2)*sigma))\n",
"\n",
"#P_error for V = 4 & sigma**2 = 2\n",
"V = 4\n",
"sigma = math.sqrt(2)\n",
"P_error = (1./2)*math.erfc(V/(2*math.sqrt(2)*sigma))\n",
"\n",
"print 'Probabilty of error for V = 4 & sigma**2 = 2 is %.4f'%P_error\n",
"\n",
"#P_error for V = 2 & sigma**2 = 2\n",
"V = 2\n",
"sigma = math.sqrt(2)\n",
"P_error = (1./2)*math.erfc(V/(2*math.sqrt(2)*sigma))\n",
"\n",
"print 'Probabilty of error for V = 2 & sigma**2 = 2 is %.4f'%P_error\n",
"\n",
"#P_error for V = 4 & sigma**2 = 4\n",
"V = 4\n",
"sigma = math.sqrt(4)\n",
"P_error = (1./2)*math.erfc(V/(2*math.sqrt(2)*sigma))\n",
"\n",
"print 'Probabilty of error for V = 4 & sigma**2 = 4 is %.4f'%P_error\n",
"\n",
"#P_error for V = 8 & sigma**2 = 2\n",
"V = 8\n",
"sigma = math.sqrt(2)\n",
"P_error = (1./2)*math.erfc(V/(2*math.sqrt(2)*sigma))\n",
"\n",
"print 'Probabilty of error for V = 8 & sigma**2 = 2 is %.4f'%P_error\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Probabilty of error for V = 4 & sigma**2 = 2 is 0.0786\n",
"Probabilty of error for V = 2 & sigma**2 = 2 is 0.2398\n",
"Probabilty of error for V = 4 & sigma**2 = 4 is 0.1587\n",
"Probabilty of error for V = 8 & sigma**2 = 2 is 0.0023\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.11 Page No : 106"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math \n",
"\n",
"#(a)\n",
"#out of n attempts the probability of message reaching correctly for k times is given by binomial distribution pX(k) = nCk*(q**k)*(1-q)**(n-k) where q is probability of correctly reaching\n",
"\n",
"#Here n = 10, k = 1, q = 0.001\n",
"n = 10.\n",
"k = 1.\n",
"q = 0.001\n",
"\n",
"#pX(k) is denoted as p_X_1\n",
"#10C1 = 10\n",
"p_X_1 = 10*(q**k)*(1-q)**(n-k)\n",
"\n",
"print 'The probability that out of 10 transmissions 9 are corrent and 1 is incorrect is %.4f'%p_X_1\n",
"\n",
"#probability that more than two erroneous out of 100 transmissions(p_100_2) = 1-probability of less than or equal to two error in transmission\n",
"#p_100_2 = 1-pX(0)-pX(1)-pX(2)\n",
"#p_100_2 = 1-100C0*((0.001)**0)*((1-0.001)**100)-100C1*((0.001)**1)*((1-0.001)**99)-100C0*((0.001)**2)*((1-0.001)**98)\n",
"\n",
"#Since, calculation of above is cumbersome we may use Poisson ditribution to approximate above\n",
"#Poisson distribution = pX(k) = (alfa**k)*(e**-alfa)/k!, where alfa = n*T\n",
"\n",
"#Here n = 100 & q = 0.001\n",
"n = 100\n",
"q = 0.001\n",
"\n",
"alfa = n*q\n",
"\n",
"p_100_2 = 1-(alfa**0)*(math.e**-0.1)/math.factorial(0)-(alfa**1)*(math.e**-0.1)/math.factorial(1)-(alfa**2)*(math.e**-0.1)/math.factorial(2)\n",
"\n",
"print 'probability that more than two erroneous out of 100 transmissions is %.4f'%p_100_2\n",
"\n",
"#(c)\n",
"#from(b), required probability i.e probability of more than one are erroneous out of 100 transmission(p_100_1) is\n",
"p_100_1 = 1-(alfa**0)*(math.e**-0.1)/math.factorial(0)-(alfa**1)*(math.e**-0.1)/math.factorial(1)\n",
"\n",
"print 'probability that more than one erroneous out of 100 transmissions is %.4f'%p_100_1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The probability that out of 10 transmissions 9 are corrent and 1 is incorrect is 0.0099\n",
"probability that more than two erroneous out of 100 transmissions is 0.0002\n",
"probability that more than one erroneous out of 100 transmissions is 0.0047\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2.13 Page No : 115"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Given, Error probability is 10**-4 = P_e, no of ecperiments conducted = N = 4*10**5 & estimated probability of error p does not differ from P_e by more than 50%\n",
"P_e = 10.**-4\n",
"N = 4.*10**5\n",
"\n",
"#Tchebycheff's inequality is P(|p-Pe|> = E)< = P_e/(N*E**2)\n",
"#From given values we can find that E = 50*10**-4\n",
"E = 50.*10**-4\n",
"\n",
"#Here R.H.S of Tchebycheff's inequality is denoted as Tc_RHS\n",
"Tc_RHS = P_e/(N*E**2)\n",
"\n",
"#Tc_RHS in persentage is Tc_RHSper\n",
"Tc_RHSper = Tc_RHS/100\n",
"\n",
"#print (Tc_RHSper,Tc_RHS,'or P(|p-10**-4|> = 0.5*10**-2)< = ',Tc_RHS,'The probability of estimated probability of error p does not differ from P_e by more than 50% is less than equal to')\n",
"\n",
"#given solution has been computed wrong, obtaines solution is 10**-7\n",
"print 'The probability of estimated probability of error p does not differ from P_e by more \\\n",
"\\nthan 50% is less than equal to ',Tc_RHS,'or P(|p-10**-4|> = 0.5*10**-2)< = ',Tc_RHS,' = ',Tc_RHSper,'%'\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The probability of estimated probability of error p does not differ from P_e by more \n",
"than 50% is less than equal to 1e-05 or P(|p-10**-4|> = 0.5*10**-2)< = 1e-05 = 1e-07 %\n"
]
}
],
"prompt_number": 14
}
],
"metadata": {}
}
]
}