-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrvgs.py
More file actions
313 lines (265 loc) · 8.25 KB
/
rvgs.py
File metadata and controls
313 lines (265 loc) · 8.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
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
#--------------------------------------------------------------------------
#This is an ANSI C library for generating random variates from six discrete
#distributions
#
# Generator Range (x) Mean Variance
#
# Bernoulli(p) x = 0,1 p p*(1-p)
# Binomial(n, p) x = 0,...,n n*p n*p*(1-p)
# Equilikely(a, b) x = a,...,b (a+b)/2 ((b-a+1)*(b-a+1)-1)/12
# Geometric(p) x = 0,... p/(1-p) p/((1-p)*(1-p))
# Pascal(n, p) x = 0,... n*p/(1-p) n*p/((1-p)*(1-p))
# Poisson(m) x = 0,... m m
#
#and seven continuous distributions
#
# Uniform(a, b) a < x < b (a + b)/2 (b - a)*(b - a)/12
# Exponential(m) x > 0 m m*m
# Erlang(n, b) x > 0 n*b n*b*b
# Normal(m, s) all x m s*s
# Lognormal(a, b) x > 0 see below
# Chisquare(n) x > 0 n 2*n
# Student(n) all x 0 (n > 1) n/(n - 2) (n > 2)
#
#For the a Lognormal(a, b) random variable, the mean and variance are
#
# mean = exp(a + 0.5*b*b)
# variance = (exp(b*b) - 1) #exp(2*a + b*b)
#
#Name : rvgs.c (Random Variate GeneratorS)
#Author : Steve Park & Dave Geyer
#Language : ANSI C
#Latest Revision : 10-28-98
#Translated by : Philip Steele
#Language : Python 3.3
#Latest Revision : 3/26/14
#
#--------------------------------------------------------------------------
from rngs import random
from math import log,sqrt,exp
def Bernoulli(p):
#========================================================
#Returns 1 with probability p or 0 with probability 1 - p.
#NOTE: use 0.0 < p < 1.0
#========================================================
if (random() < 1 - p):
return(0)
else:
return(1)
def Binomial(n,p):
#================================================================
#Returns a binomial distributed integer between 0 and n inclusive.
#NOTE: use n > 0 and 0.0 < p < 1.0
#================================================================
x = 0
for i in range(0,n):
x += Bernoulli(p)
return (x)
def Equilikely(a,b):
#===================================================================
#Returns an equilikely distributed integer between a and b inclusive.
#NOTE: use a < b
#===================================================================
return (a + int((b - a + 1) * random()))
def Geometric(p):
#====================================================
#Returns a geometric distributed non-negative integer.
#NOTE: use 0.0 < p < 1.0
#====================================================
#
return (int(log(1.0 - random()) / log(p)))
def Pascal(n,p):
#=================================================
#Returns a Pascal distributed non-negative integer.
#NOTE: use n > 0 and 0.0 < p < 1.0
#=================================================
#
x = 0
for i in range(0,n):
x += Geometric(p)
return (x)
def Poisson(m):
#==================================================
#Returns a Poisson distributed non-negative integer.
#NOTE: use m > 0
#==================================================
#
t = 0.0
x = 0
while (t < m):
t += Exponential(1.0)
x += 1
return (x - 1)
def Uniform(a,b):
#===========================================================
#Returns a uniformly distributed real number between a and b.
#NOTE: use a < b
#===========================================================
#
return (a + (b - a) * random())
def Exponential(m):
#=========================================================
#Returns an exponentially distributed positive real number.
#NOTE: use m > 0.0
#=========================================================
#
return (-m * log(1.0 - random()))
def Erlang(n,b):
#==================================================
#Returns an Erlang distributed positive real number.
#NOTE: use n > 0 and b > 0.0
#==================================================
#
x = 0.0
for i in range(0,n):
x += Exponential(b)
return (x)
def Normal(m,s):
#========================================================================
#Returns a normal (Gaussian) distributed real number.
#NOTE: use s > 0.0
#
#Uses a very accurate approximation of the normal idf due to Odeh & Evans,
#J. Applied Statistics, 1974, vol 23, pp 96-97.
#========================================================================
#
p0 = 0.322232431088
q0 = 0.099348462606
p1 = 1.0
q1 = 0.588581570495
p2 = 0.342242088547
q2 = 0.531103462366
p3 = 0.204231210245e-1
q3 = 0.103537752850
p4 = 0.453642210148e-4
q4 = 0.385607006340e-2
u = random()
if (u < 0.5):
t = sqrt(-2.0 * log(u))
else:
t = sqrt(-2.0 * log(1.0 - u))
p = p0 + t * (p1 + t * (p2 + t * (p3 + t * p4)))
q = q0 + t * (q1 + t * (q2 + t * (q3 + t * q4)))
if (u < 0.5):
z = (p / q) - t
else:
z = t - (p / q)
return (m + s * z)
def Lognormal(a,b):
# ====================================================
#Returns a lognormal distributed positive real number.
#NOTE: use b > 0.0
#====================================================
#
return (exp(a + b * Normal(0.0, 1.0)))
def Chisquare(n):
#=====================================================
#Returns a chi-square distributed positive real number.
#NOTE: use n > 0
#=====================================================
#
x = 0.0
for i in range(0,n):
z = Normal(0.0, 1.0)
x += z * z
return (x)
def Student(n):
#===========================================
#Returns a student-t distributed real number.
#NOTE: use n > 0
#===========================================
#
return (Normal(0.0, 1.0) / sqrt(Chisquare(n) / n))
def testFunctions():
#tests to ensure that all variates match what was produced by C version of program (with the same order and parameters)
#bernoulli
bern = []
for i in range(0,10):
bern.append(Bernoulli(.65))
if (bern == [0,0,1,0,1,0,1,1,1,1]):
print("Bernoulli test passed")
else:
print("FIX BERNOULLI!")
#binomial
bino = Binomial(50,.19)
if (bino == 7):
print("Binomial test passed")
else:
print("FIX BINOMIAL")
print(bino)
#equilikely
equi = Equilikely(32,108)
if (equi ==77):
print("Equilikely test passed")
else:
print("FIX EQUILIKELY")
#geo test
geo = []
for i in range(0,10):
geo.append(Geometric(.93))
if (geo == [0,8,4,21,7,3,17,5,14,1]):
print("Geo test passed")
else:
print("Fix geo!")
print(geo)
#pascal test
pas = Pascal(87,.93)
if (pas == 1407):
print("Pascal test passed!")
else:
print("FIX PASCAL")
#poisson test
pois = Poisson(13.3)
if (pois == 15):
print("Poisson test passed!")
else:
print("FIX POISSON!")
#uniform test
uni = Uniform(32,108)
if (round(uni,6) == 78.976127):
print("Uniform test passed!")
else:
print("FIX UNIFORM. Produced: ", uni)
print("Expected: 78.976127")
#exp test
exp = Exponential(4.7)
if (round(exp,6) == 4.796404):
print("Exponential test passed!")
else:
print("FIX EXP. Produced: ", exp)
print("expected: 4.796404")
#erlang test
erl = Erlang(41,.08)
if (round(erl,6) == 2.824873):
print("Erland test passed!")
else:
print("FIX ERLANG Produced: ", erl)
print("Expected: 2.824873")
#normal test
norm = Normal(8.9,4)
if (round(norm,6) == 8.374243):
print("Normal test passed!")
else:
print("FIX NORMAL Produced: ",norm)
print("Expected: 8.374243")
#lognormal test
lnorm = Lognormal(1.31,1.6)
if (round(lnorm,6) == 5.533064):
print("Lognormal test passed!")
else:
print("FIX LOGNORMAL Produced: ", lnorm)
print("Expected: 5.533064")
#chisq test
chisq = Chisquare(39)
if (round(chisq,6) == 33.634524):
print("Chisquare test passed!")
else:
print("FIX CHI-SQUARE - Produced: ", chisq)
print("Expected: 33.634524")
#t test
stu = Student(61)
if (round(stu,6) == -1.429058):
print("Student test passed!")
else:
print("FIX STUDENT - Produced: ", stu)
print("Expected: -1.429058")