summaryrefslogtreecommitdiff
path: root/sample_notebooks/ArshadKhan/Chap10.ipynb
blob: fed824df340c0efa3fc51bf5672b482bfdd4c765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter - 10 : Introduction to theory of probability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 437 Ex 10.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability of each outcome=0.12\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# referred to fig 10.1 on the page no. 435\n",
    "# the occurance of each outcome is essumed to be equal.\n",
    "n=8 # number of outcomes\n",
    "p=1/n#\n",
    "print \"probability of each outcome=%0.2f\"%p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 438 Ex no 10.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability = 0.1667\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "m=6 # enter the number of faces \n",
    "n=2# enter the number of dice \n",
    "l=m**n ## total number of outcomes = 36\n",
    "a=7 #\"enter the number which is to be obtained as the sum of dice\n",
    "c=0 # # counter value for favorable outcome\n",
    "for i in range(1,7):\n",
    "    for j in range(1,7):\n",
    "        if (i+j==a):\n",
    "            c=c+1#\n",
    "        else :\n",
    "            continue\n",
    "   \n",
    "  \n",
    "\n",
    "p=c/l#\n",
    "print \"probability = %0.4f\"%p\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 438 Ex no 10.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability = 0.4075\n"
     ]
    }
   ],
   "source": [
    "from numpy.random import gamma\n",
    "from __future__ import division\n",
    "m=2# the number of faces \n",
    "n=4 #the number of tosses\n",
    "l=m**n ## l is total number of outcomes = 16\n",
    "a=2 #exact no of heads\n",
    "p=gamma (n+1)/(gamma(n+1-a) * gamma (a+1))## to find combination\n",
    "print \"probability = %0.4f\"%(p/l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 440 Ex no 10.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total probability = 0.0023\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "a=52# # total no of cards in a deck\n",
    "b=6# the no of cards to be drawn\n",
    "pA1= b/a## probability of getting first red ace =pA1\n",
    "#the cards are drawn in succession without replacement, therefore the probability that the 2nd card will be the red ace = pA2\n",
    "pA2=1/(a-1)#\n",
    "p= pA1*pA2\n",
    "print \"total probability = %0.4f\"%p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 441 Ex no 10.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability = 1.04e-10\n"
     ]
    }
   ],
   "source": [
    "from numpy.random import gamma\n",
    "from __future__ import division\n",
    "\n",
    "# This problem is based on Bernoulli Trials formula which is P( k successes in n trials ) = n!*p**k *(1-p)**(n-k)/k!*(n-k)!22\n",
    "# hence the probability of finding 2 digits wrong in a sequence of 8 digits is\n",
    "\n",
    "k=2 # no. of successes\n",
    "p= 1e-5# probability of success\n",
    "n=8 #\"no. of trials\n",
    "A=gamma (n+1)* (p**k)*((1-p)**(n-k))/(gamma(k)*gamma(n+1-k))#\n",
    "print \"probability = %0.2e\"%A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 446 Ex no 10.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability =  0.0231481481481\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "m=6 # enter the number of faces\n",
    "n=3 # enter the number of dice\n",
    "l=m**n ## l is total number of outcomes \n",
    "a=8# the number which is to be obtained as the sum of dice\n",
    "c=0 # # counter value for favorable outcome\n",
    "for i in range(1,7):\n",
    "    for j in range(1,7):\n",
    "        if(i+j==a):\n",
    "            c=c+1\n",
    "        else :\n",
    "            continue\n",
    "   \n",
    "p=c/l#\n",
    "print \"probability = \",p\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 447 Ex no 10.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Py(1) =  0.59235668\n",
      "Py0= 0.40764332\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "Pe=0.6898 # error probability\n",
    "Q= 0.2567 # the probability of transmitting 1 #Hence probability of transmitting zero is 1-Q = P\n",
    "P=1-Q#\n",
    "Px_1=Q#\n",
    "Px0=P#\n",
    "# If x and y are transmitted digit and received digit then for BSC P(y=0/x=1) = P(y=1/x=0) = Pe , P(y=0/x=10) = P(y=1/x=1) = 1-Pe\n",
    "# to find the probability of receiving 1 is Py(1) = Px(0)*P(y=1/x=0) + Px(1)*P(y=1/x=1)\n",
    "Py_1= ((1-Q)* Pe) + (Q *(1-Pe))#\n",
    "print \"Py(1) = \",Py_1\n",
    "Py0=((1-Q)*(1-Pe)) + (Q*Pe)\n",
    "print \"Py0=\",Py0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 448 Ex 10.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "probability of error =  6.04e-05\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "Px0=.4\n",
    "Px1=.6 \n",
    "PE0=10**-6 \n",
    "PE1=10**-4 ## given\n",
    "PE=(Px0*PE0) + (Px1*PE1)# formula for probability of error\n",
    "print \"probability of error = \",PE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 472 Ex no 10.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "width or spread of Gaussian PDF = 0.985977359605\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import pi,sqrt,exp\n",
    "#Gaussian PDF: Q(x)= %e**((-x**2)/2)/ (x*sqrt(2*pi))\n",
    "x=2.5 #  input for the function Q \n",
    "Q_x = (exp(-(x**2)/2))/ (x*sqrt(2*pi))\n",
    "P=1-(2*Q_x)#\n",
    "print \"width or spread of Gaussian PDF =\",P\n",
    "## P gives the width or spread of Gaussian PDF"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## page no 479 Ex no 10.21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SNR improvement = 12.90 dB\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log\n",
    "# formula for estimate error E is E = mk** - mk = a1* mk-1 +a2* mk-2 -mk\n",
    "#given: various values of correlation (mk*mk)'= (m**2)',(mk*mk-1)'= .825* (m**2)',(mk*mk-2)'= .562*(m**2)',(mk*mk-3)'= .825*(m**2)' , R02=.562(m**2)', a1=1.1314, a2= -0.3714\n",
    "# mean square error is given by I=(E**2)'=[1-((.825*a1)+(.562*a2))]*(m**2)'= .2753*(m**2)'\n",
    "\n",
    "m=1#\n",
    "I=.2753*(m**2)#\n",
    "S=10*log ((m**2)/I)#\n",
    "print \"SNR improvement = %0.2f dB\"%S"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}