-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab07.py
More file actions
177 lines (145 loc) · 4.25 KB
/
lab07.py
File metadata and controls
177 lines (145 loc) · 4.25 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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 26 21:53:59 2015
@author: Sailung Yeung
email: yeungsl@bu.edu
"""
import random
import math
import matplotlib.pyplot as plt
## problem one
## (a)
def nextPoisson(lamb):
d = random.random()
sumr = 0.0
## controling k in 0- 20, since we long focus on the p in Rng[0...20]
for k in range(21):
sumr += ((lamb ** k) * (math.e ** (-1 * lamb)))/(math.factorial(k))
if (sumr > d):
return k
if k == 20:
return k
# c is the times of trials of getting the simulated poisson distribution
def Poi(lamb, c):
count = [0] * 21
for i in range(c + 1):
num = nextPoisson(lamb)
count[num] += 1
lsr = [i / c for i in count]
return lsr
## (b)
def poisson(lamb, k):
# return P(X=k)
upper = (lamb ** k) * (math.e ** (-1 * lamb))
lower = math.factorial(k)
return(upper / lower)
# c is the times of trials of getting the simulated poisson distribution
def comparePoi(lamb, c):
## controling k in 0- 20, since we long focus on the p in Rng[0...20]
R = [0] * 21
P = [0] * 21
for k in range(21):
R[k] = k
P[k] = poisson(lamb, k)
Ps = Poi(lamb, c)
bins = [r - 0.5 for r in range(min(R),max(R)+2)]
plt.title("Comparing of the simulated and the theoretical Poisson distribution")
plt.ylabel("Probability")
plt.xlabel("Outcomes")
plt.hist(R,bins,histtype = "stepfilled", weights = P, color = "b",label = "Poisson")
plt.hist(R,bins,histtype = "stepfilled",normed = True,weights = Ps, color = "r",alpha = 0.5,label = "Actual")
plt.legend()
## Problem Two
## (a)
def nextGeometrical(p):
d = random.random()
sumr = 0.0
## controling k in 0- 20, since we long focus on the p in Rng[0...20]
for k in range(21):
sumr += (((1-p) ** k) * p)
if (sumr > d):
return k
if k == 20:
return k
# c is the times of trials of getting the simulated poisson distribution
def Geo(p, c):
count = [0] * 21
for i in range(c + 1):
num = nextGeometrical(p)
count[num] += 1
lsr = [i / c for i in count]
return lsr
## (b)
def G(p, k):
# return P(X=k)
r = ((1-p) ** k) * p
return r
# c is the times of trials of getting the simulated poisson distribution
def compareG(p, c):
## controling k in 0- 20, since we long focus on the p in Rng[0...20]
R = [0] * 21
P = [0] * 21
for k in range(21):
R[k] = k
P[k] = G(p, k)
Ps = Geo(p, c)
bins = [r - 0.5 for r in range(min(R),max(R)+2)]
plt.title("Comparing of the simulated and the theoretical Geometrical distribution")
plt.ylabel("Probability")
plt.xlabel("Outcomes")
plt.hist(R,bins,histtype = "stepfilled", weights = P, color = "b",label = "Geometrical")
plt.hist(R,bins,histtype = "stepfilled",normed = True,weights = Ps, color = "r",alpha = 0.5,label = "Actual")
plt.legend()
## Problem Three
def ChiSquared(Obs,Exp):
# takes two histograms (lists of frequency counts) Obs and Exp and returns X2 statistic
X2 = (Obs - Exp) ** 2 / (Exp)
return X2
## helper function
## a function used to calculate the Choosing K number from N
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 binomial(n,p,k):
P = (p**k) * ((1-p)**(n-k)) * C(n,k)
return P
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 prob3(N, p):
##(1)
(r, Pb) = getBinomial(N,p)
Pp = []
for n in range(N+1):
Pp += [poisson(N*p,n)]
##(2)
O = Pp
E = Pb
##(3)
r = 0.0
for i in range(N+1):
r += ChiSquared(O[i],E[i])
return r
## Problem Four
def prob4(lamb, C):
E = [0] * 21
for k in range(21):
E[k] = poisson(lamb, k)
O = Poi(lamb, C)
r = 0.0
for i in range(21):
r += ChiSquared(O[i], E[i])
return r