-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulateDist .py
352 lines (263 loc) · 9.1 KB
/
simulateDist .py
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
'''Shreyash Shrivastava'''
#-------------------------------------------------
#-------------------------------------------------
import random
import sys
import math
import operator as op
sample = sys.argv[1]
sample = int(sample)
def bernoulli(p):
generated_samples = []
for x in range(sample):
generated_samples.append(random.randint(0,1)) # Generating random samples of successes and faliures
no_of_1s = (p*sample)
no_of_0s = (1-p)*sample
no_of_0s = (int)(no_of_0s)
no_of_1s = (int)(no_of_1s)
distribution = []
for x in range(no_of_1s+1):
distribution.append(1)
for x in range(no_of_0s+1):
distribution.append(0)
# distribution list maintains p% 1s and (1-p)% 0s
random.shuffle(distribution) # Shuffles the distribution sample to eliminate bias
bernau = []
'''Picking random item after applying bernoulli distribution'''
# Selects the sample from the distribution lost where chance of picking 1 is p and chance of picking 0 us (1-p)
# The selection happens length of sample times
for x in range(sample):
bernau.append(random.choice(distribution))
print bernau
def binomial(n,p):
'''Sample size is given as sample'''
'''Binomial distribution is the probability of exactly x success in n trials'''
# The range of each sample element is taken as n
# elements in the random sample range from 0 to n
# Function to calculate nCr
def ncr(n, r):
r = min(r, n - r)
numer = reduce(op.mul, xrange(n, n - r, -1), 1)
denom = reduce(op.mul, xrange(1, r + 1), 1)
return numer // denom
generated_samples = []
for x in range(sample):
generated_samples.append((int)(random.uniform(0,n)))
print generated_samples
bino = []
for x in generated_samples:
combination = ncr(n,x)
succes = p**x
fails = (1-p) ** (n-x)
#The probability of exactly x success in n trials
bino.append(combination*succes * fails)
print 'The random generated x (0 < x < n) values: ', generated_samples
print 'The binomial distribution for x', bino
return
def geometric(p):
'''Sample size is given as sample'''
'''The range of sample is not provided'''
'''Geometric distribution calculates the probability of first success on the xth trial'''
# The range of each sample element is taken as 10
# elements in the random sample range from 1 to 10, since the range is not specified for each sample
generated_samples = []
for x in range(sample):
generated_samples.append(random.randint(1,10))
geo = []
for x in generated_samples:
#The probability of the first success on the xth trial
geo.append(((1-p)**(x-1)) * p)
print 'The random generated x (1 < x < 10) values: ', generated_samples
print 'The geometric distribution for x: ',geo
def neg_binomial(k,p):
'''Sample size is given as sample'''
'''The xth trial result in the kth success'''
'''x can vary from k to some number, x>k'''
# The range of each sample element is taken as 10
# elements in the random sample range from k to 10, since the range is not specified for each sample
generated_sample = []
for x in range(sample):
generated_sample.append(random.randint(k,10))
# Function to calculate nCr
def ncr(n, r):
r = min(r, n - r)
numer = reduce(op.mul, xrange(n, n - r, -1), 1)
denom = reduce(op.mul, xrange(1, r + 1), 1)
return numer // denom
neg = []
for x in generated_sample:
combination = ncr(x-1,k-1)
failure = (1-p) ** (x-k)
success = p ** k
neg.append(combination*success*failure)
print 'The random generated x (k < x < 10) values: ', generated_sample
print 'The negative binomial distribution for x ',neg
return
def poisson(lambda_):
'''Sample size is given as sample'''
'''Number of x rare events happen in lambda_ time'''
# The range of each sample element is taken as 10
# elements in the random sample range from 1 to 10, since the range is not specified for each sample
generated_sample = []
for x in range(sample):
generated_sample.append(random.randint(0,10))
# Function to calculate factorial
def fact(n):
f = 1
for x in range(1,n+1):
f *= x
return f
poi = []
for x in generated_sample:
exp = math.exp(-lambda_)
lam_power = lambda_ ** x
ft = fact(x)
poi.append(exp*lam_power/ft)
print 'The random generated x (1 < x < 10) values: ', generated_sample
print 'The poission distribution for x :', poi
return
def arb_discrete(l1):
for x in range(len(l1)):
l1[x] = float(l1[x])
inputs = []
num = 0
'''Adding elements to choose from'''
for x in range(len(l1)):
number_of_elements = l1[x] * sample
for y in range((int)(number_of_elements)):
inputs.append(num)
num +=1
'''Shuffeling the inputs array with assigned probabilities to eliminate bia'''
random.shuffle(inputs)
select = []
'''Selecting numbers for the final output'''
for x in range(sample):
select.append(random.choice(inputs))
print select
return
def uniform(a,b):
'''Sample size is given as sample'''
'''Uniform distribution is x/(b-a)'''
'''Calculates P(a<x<b)'''
a = float(a)
b = float(b)
generated_samples = []
# Calcualting x in the range a < x < b
for x in range(sample):
generated_samples.append(random.randint(a+1,b-1))
print generated_samples
uni = []
for x in generated_samples: # Calculating for each x
f_x = 0.0
f_x = x/(b-a)
uni.append(f_x)
print 'The random generated x (a < x < b) values: ',generated_samples
print 'The uniform distribution for P(a<x<b) ',uni
return
def exponential (lambda_):
'''Sample size is given as sample'''
'''Exponential dsitributon is F(x) = (1-e^(-lambda x) for x > 0'''
# The range of each sample element is taken as 10
# elements in the random sample range from 1 to 10, since the range is not specified for each sample
'''Calculates P(X<x)'''
generated_samples = []
lambda_ = float(lambda_)
for x in range(sample):
generated_samples.append(random.randint(1,10))
exp = []
for x in generated_samples:
exponent = 1 - math.exp(-lambda_*x)
exp.append(exponent)
print 'The random generated x (1 < x < 10) values: ', generated_samples
print 'The exponential distribution for x', exp
return
def gamma(alpha_,lambda_):
generated_samples = []
for x in range(sample):
generated_samples.append(random.randint(1,10))
gam = []
for x in generated_samples:
part1 = lambda_ ** alpha_ / (math.gamma(alpha_))
part2 = x ** (alpha_-1) * math.exp(-lambda_*x)
gam.append(part1*part2)
print 'The random generated x (1 < x < 10) values: ', generated_samples
print 'The gamma distribution for x :', gam
return
def normal(mu,sigma):
'''Sample size is given as sample'''
# elements in the random sample range from -2 to 2, since the range is not specified for each sample
generated_samples = []
for x in range(sample):
generated_samples.append(random.uniform(-2.0,2.0))
normal_dist = []
distribution_func = []
# for x in generated_samples:
# # part1 = 1/(sigma *(2*(math.pi)**(1/2)))
# # part2 = math.erf( (-(x-mu)**2) / 2*(sigma**2))
# z = (x -mu) / sigma
# normal_dist.append((1.0 + math.erf(z / math.sqrt(2.0))) / 2.0)
for x in generated_samples:
part1 = 1/(sigma *(2*(math.pi)**(1/2)))
part2 = math.exp((-(x-mu)**2) / 2*(sigma**2))
normal_dist.append(part1*part2)
print 'The random generated x (-2.0 < x < 2.0) values: ',generated_samples
print 'The normal distribution for P(X=x): ',normal_dist
if sys.argv[2] == 'bernoulli':
param = sys.argv[3:]
p = param[0]
p = float(p)
bernoulli(p)
if sys.argv[2] == 'binomial':
param = sys.argv[3:]
n = param[0]
n = int(n)
p = param[1]
p = (float)(p)
binomial(n,p)
if sys.argv[2] == 'geometric':
param = sys.argv[3:]
p = param[0]
p = float(p)
geometric(p)
if sys.argv[2] == 'neg_binomial':
param = sys.argv[3:]
k = param[0]
p = param[1]
k = (int)(k)
p = float(p)
neg_binomial(k,p)
if sys.argv[2] == 'poisson':
param = sys.argv[3:]
lam = param[0]
lam = float(lam)
poisson(lam)
if sys.argv[2] == 'arb_discrete':
l1 = sys.argv[3:]
map(float,l1)
arb_discrete(l1)
if sys.argv[2] == 'uniform':
param = sys.argv[3:]
a = param[0]
b = param[1]
a = float(a)
b = float(b)
uniform(a,b)
if sys.argv[2] == 'exponential':
param = sys.argv[3:]
lam = param[0]
lam = float(lam)
exponential(lam)
if sys.argv[2] == 'gamma':
param = sys.argv[3:]
a = param[0]
l = param[1]
a = float(a)
l = float(l)
gamma(a,l)
if sys.argv[2] == 'normal':
param = sys.argv[3:]
m = param[0]
s = param[1]
m = float(m)
s = float(s)
normal(m,s)