-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab06.py
More file actions
288 lines (227 loc) · 5.45 KB
/
lab06.py
File metadata and controls
288 lines (227 loc) · 5.45 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 19 13:18:36 2015
@author: Sailung Yeung
email: yeungsl@bu.edu
"""
import math
import random
import matplotlib.pyplot as plt
## problem one
## (a)
##helper function
def facproductls(L):
p = 1
for i in range(len(L)):
p *= math.factorial(L[i])
return p
def multinomial(X, L):
# Given a random variable X = (R,P) and a list L = [x1, ...., xk] of multiplicities of the outcomes,
# return the probability that P(X1=x1, X2=x2, ..., Xk=xk) as in the definition.
(R, P) = X
N = sum (L)
multiconstant = math.factorial(N)/facproductls(L)
r = 1
for i in range(len(L)):
r *= P[i]**L[i]
r *= multiconstant
return r
"""
result:
In [34]: X = ([1,2,3,4,5,6] , [1/6,1/6,1/6,1/6,1/6,1/6])
In [35]: L = [1,1,1,1,2,2]
In [36]: multinomial(X,L)
Out[36]: 0.00600137174211248
"""
## (b)
##helper function
def C(N,K):
if (K < N/2):
K = N-K
row = [1] * (K+1)
nrow = N-K
for i in range(1, nrow+1):
above = row
row[i-1] = above[i]
for c in range(i, K+1):
row[c] = above[c] + row[c-1]
return row[-1]
def hypergeometrical(N,K,n,k):
# Return the probability that in n draws without replacement from an urn with N ball,
# of which K are red, you get k red ball
upper = C(K,k) * C((N-K),(n-k))
lower = C(N,n)
return(upper / lower)
"""
In [47]: hypergeometrical(52, 13, 5, 3)
Out[47]: 0.0815426170468187
"""
def getH(N,K,n):
# Return a list of the probabilty of the hypergeometrical distribution
# required by problem three
R = [0] * (n+1)
P = [0] * (n+1)
for i in range(n+1):
R[i] = i
P[i] = hypergeometrical(N,K,n,i)
X = (R,P)
return X
##(c)
def poisson(lamb, k):
# return P(X=k)
upper = (lamb ** k) * (math.e ** (-1 * lamb))
lower = math.factorial(k)
return(upper / lower)
"""
In [86]: poisson(7,2)
Out[86]: 0.02234110815608565
"""
## problem two
## (a)
def nextBernoulli(p):
ran = random.random()
if ran < p:
return True
if ran > p:
return False
## (b)
def nextBinomial(n,p):
counter = 0
for x in range(n):
r = nextBernoulli(p)
if r == True:
counter += 1
return counter
##(c)
def B(n, p):
c = 1000000
ls = [0]*(n+1)
for x in range(c + 1):
numofT = nextBinomial(n,p)
ls[numofT] += 1
return [r/c for r in ls]
def getBinomial(n,p):
lsr = []
lsp = []
for i in range(n+1):
lsp += [binomial(n,p,i)]
lsr += [i]
X = (lsr,lsp)
return X
def binomial(n,p,k):
P = (p**k) * ((1-p)**(n-k)) * C(n,k)
return P
def compare(n,p):
(R,P) = getBinomial(n,p)
A = B(n,p)
bins = [r - 0.5 for r in range(min(R),max(R)+2)]
plt.title("Comparing of the model and the Binomial distribution")
plt.ylabel("Probability")
plt.xlabel("Outcomes")
plt.hist(R,bins,histtype = "stepfilled", weights = P, color = "b",label = "Binominal")
plt.hist(R,bins,histtype = "stepfilled",normed = True,weights = A,color = "r",alpha = 0.5,label = "Actual")
plt.legend()
"""
In [246]: B(20, 0.7)
Out[246]:
[0.0,
0.0,
0.0,
0.0,
1.1e-05,
3.5e-05,
0.000225,
0.001026,
0.003839,
0.012003,
0.030782,
0.065199,
0.114367,
0.16476,
0.192034,
0.178865,
0.129985,
0.071252,
0.027972,
0.006889,
0.000757]
In [247]: getBinomial(20,0.7)
Out[247]:
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[3.4867844010000104e-11,
1.6271660538000045e-09,
3.606884752590009e-08,
5.049638653626012e-07,
5.00755833151246e-06,
3.738976887529303e-05,
0.00021810698510587597,
0.0010178325971607542,
0.003859281930901192,
0.012006654896137042,
0.030817080900085062,
0.06536956554563497,
0.11439673970486117,
0.16426198521723653,
0.1916389827534426,
0.17886305056987972,
0.1304209743738706,
0.07160367220526227,
0.02784587252426865,
0.006839337111223879,
0.0007979226629761189]
In [251]: compare(20,0.7)
"""
## problem Three
## (a)
def nextHypergeometrical(N,K,n):
# returns a number in {0,1,...,min(K,n)} with probability as stated
counter = 0
ls = [i for i in range(N)]
random.shuffle(ls)
for i in range(n):
if ls[i] < K:
counter += 1
return counter
def H(N,K,n):
# just to get the Hypergeometrical distribution by the data
c = 1000000
ls = [0]*(n+1)
for x in range(c + 1):
numofT = nextHypergeometrical(N,K,n)
ls[numofT] += 1
return [r/c for r in ls]
def compareH(N,K,n):
(R,P) = getH(N,K,n)
A = H(N,K,n)
bins = [r - 0.5 for r in range(min(R),max(R)+2)]
plt.title("Comparing of the model and the Binomial distribution")
plt.ylabel("Probability")
plt.xlabel("Outcomes")
plt.hist(R,bins,histtype = "stepfilled", weights = P, color = "b",label = "Binominal")
plt.hist(R,bins,histtype = "stepfilled",normed = True,weights = A,color = "r",alpha = 0.5,label = "Actual")
plt.legend()
"""
In [259]: H(20,12,8)
Out[259]:
[1.1e-05,
0.000717,
0.014647,
0.097917,
0.274273,
0.352754,
0.20567,
0.050286,
0.003726]
In [260]: getH(20,12,8)
Out[260]:
([0, 1, 2, 3, 4, 5, 6, 7, 8],
[7.938398031277288e-06,
0.0007620862110026197,
0.01467015956180043,
0.09780106374533619,
0.27506549178375805,
0.35208382948321026,
0.205382233865206,
0.050297689926172895,
0.003929507025482257]
"""