Repository navigation
Expand file tree
/
Copy pathpdf.py
More file actions
335 lines (228 loc) · 9.51 KB
/
Copy pathpdf.py
File metadata and controls
335 lines (228 loc) · 9.51 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Tests and examples for the Monte Carlo Random Number Generator library (MCRand)
import numpy as np
import matplotlib.pyplot as plt
import time
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), '..')))
from mcrand import sample # noqa 402
def gaussian(x, mu, sigma):
return (1/(np.sqrt(2*np.pi*sigma**2))) * np.exp(-(x-mu)**2/(2*sigma**2))
def exponential(x):
return np.exp(-x)
def cauchy(x, x0, gamma):
return 1 / (np.pi * gamma * (1 + ((x-x0)/(gamma))**2))
def rayleigh(x, sigma):
return (x*np.exp(-(x**2)/(2*sigma**2))) / (sigma**2)
def maxwell_boltzmann(x, sigma):
return (np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2))) / (sigma**3)
def symmetric_maxwell_boltzmann(x, sigma):
return 0.5*(np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2))) / (sigma**3)
def invented(x, sigma):
return (x**2*np.exp(-(x**2)/(2*sigma**2))) / (2.506628*sigma**2)
current_milli_time = lambda: time.time() * 1000 # noqa 731
def generate_distribution_plot(title, subtitle, numpy_rand, f, x0, xf, N, *args):
print('Numpy exponential random generator mean: ', np.mean(numpy_rand))
print('NumPy std error:', np.std(numpy_rand) / np.sqrt(N) * 100, '%')
t0 = current_milli_time()
rand = sample(f, x0, xf, N, *args)
tf = current_milli_time()
print('\nMC exponential random generator mean: ', np.mean(rand))
print('MCRand std error:', np.std(rand) / np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf - t0, ))
x = np.linspace(x0, xf, N)
plt.hist(numpy_rand, bins=30, density=True, color=(0, 0, 1, 0.8), label='NumPy sample')
plt.hist(rand, bins=30, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, exponential(x), color='r', label='PDF')
plt.text(2, 0.95, subtitle, size=15)
plt.title(title)
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.legend()
plt.show()
# Gaussian distribution
def gaussian_test():
print('---------- GAUSSIAN TEST ----------\n')
x0 = -5
xf = 5
N = 50000
sigma = 1
mu = 0
print('sigma=%.2f, mu=%.2f\n' % (sigma, mu))
t0 = current_milli_time()
numpy_rand = np.random.normal(mu, sigma, N)
tf = current_milli_time()
print('NumPy gaussian random generator mean: ', np.mean(numpy_rand))
print('NumPy std error:', np.std(numpy_rand)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
t0 = current_milli_time()
rand = sample(gaussian, x0, xf, N, mu, sigma)
tf = current_milli_time()
print('\nMC gaussian random generator mean: ', np.mean(rand))
print('MCRand std error:', np.std(rand)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.figure(figsize=(9, 6))
plt.hist(numpy_rand, bins=30, density=True, color=(0, 0, 1, 0.8), label='NumPy sample')
plt.hist(rand, bins=30, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, gaussian(x, mu, sigma), color='r', label=r'Gaussian PDF $\mu=%.2f$, $\sigma=%.2f$' % (mu, sigma))
plt.text(-5.3, 0.45, r'PDF(x)=$\frac{1}{\sqrt{2\pi\sigma^2}}\cdot e^{-\frac{(x-\mu)^2}{2\sigma^2}}$', size=15)
# (1/(np.sqrt(2*np.pi*sigma**2))) * np.exp(-(x-mu)**2/(2*sigma**2))
plt.title('Gaussian distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.ylim(0, 0.5)
plt.legend()
plt.show()
# Exponential distribution
def exponential_test():
print('\n\n---------- EXPONENTIAL DISTRIBUTION TEST ----------\n')
x0 = 0
xf = 10
N = 10**5
numpy_rand = np.random.exponential(size=N)
generate_distribution_plot(
'Exponential Distribution',
r'$PDF(x)=e^{-x}$',
numpy_rand,
exponential,
x0, xf, N
)
# Cauchy distribution
def cauchy_test():
print('\n\n---------- CAUCHY DISTRIBUTION TEST ----------\n')
x0 = -10
xf = 10
N = 10**5
x0_cauchy = 0
gamma = 1
t0 = current_milli_time()
s = np.random.standard_cauchy(size=N)
numpy_cauchy = s[(s > -10) & (s < 10)] # truncate distribution so it plots well
tf = current_milli_time()
print('NumPy Cauchy random generator mean: ', np.mean(numpy_cauchy))
print('NumPy std error:', np.std(numpy_cauchy)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
t0 = current_milli_time()
rand = sample(cauchy, x0, xf, N, x0_cauchy, gamma)
tf = current_milli_time()
print('\nMC Cauchy random generator mean: ', np.mean(rand))
print('MCRand std error:', np.std(rand)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.figure(figsize=(9, 6))
plt.hist(numpy_cauchy, bins=50, density=True, color=(0, 0, 1, 0.8), label='NumPy sample')
plt.hist(rand, bins=50, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, cauchy(x, x0_cauchy, gamma), color='r', label=r'Cauchy PDF $γ=%.2f$, $x_0=%.2f$' % (gamma, x0_cauchy))
plt.text(-10, 0.34, r'PDF(x)=$\frac{1}{\pi\gamma[1+(\frac{x-x_0}{\gamma})^2]}$', size=15)
plt.title('Cauchy distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.xlim(-11, 11)
plt.ylim(0, 0.38)
plt.legend(loc='upper right')
plt.show()
# Rayleigh distribution
def rayleigh_test():
print('\n\n---------- RAYLEIGH DISTRIBUTION TEST ----------\n')
x0 = 0
xf = 4
sigma = 1
N = 10**5
t0 = current_milli_time()
numpy_rand = np.random.rayleigh(scale=sigma, size=N)
tf = current_milli_time()
print('Numpy rayleigh random generator mean: ', np.mean(numpy_rand))
print('NumPy std error:', np.std(numpy_rand)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
t0 = current_milli_time()
rand = sample(rayleigh, x0, xf, N, sigma)
tf = current_milli_time()
print('\nMC rayleigh random generator mean: ', np.mean(rand))
print('MCRand std error:', np.std(rand)/np.sqrt(N) * 100, '%')
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.hist(numpy_rand, bins=30, density=True, color=(0, 0, 1, 0.8), label='NumPy sample')
plt.hist(rand, bins=30, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, rayleigh(x, sigma), color='r', label=r'Rayleigh PDF $\sigma=%.2f$' % sigma)
plt.text(2.5, 0.3, r'$PDF(x)=\frac{x\cdot\exp(-\frac{x^2}{2\sigma^2})}{\sigma^2}$', size=15)
plt.title('Rayleigh distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.legend()
plt.show()
# Maxwell-Boltzmann distribution
def maxwell_boltzmann_test():
print('\n\n---------- MAXWELL-BOLTZMANN DISTRIBUTION TEST ----------\n')
x0 = 0
xf = 10
sigma = 2
N = 10**5
t0 = current_milli_time()
rand = sample(maxwell_boltzmann, x0, xf, N, sigma)
tf = current_milli_time()
print('\nMC Maxwell-Boltzmann random generator mean: ', np.mean(rand))
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.hist(rand, bins=30, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, maxwell_boltzmann(x, sigma), color='r', label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$' % sigma)
plt.text(5, 0.15, r'$PDF(x)=\sqrt{\frac{2}{\pi}}\cdot\frac{x^2\exp(-\frac{x^2}{2\sigma^2})}{\sigma^3}$', size=15)
plt.title('Maxwell-Boltmann distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.legend()
plt.show()
# Symmetric Maxwell-Boltzmann distribution
def symmetric_maxwell_boltzmann_test():
print('\n\n---------- SYMMETRIC MAXWELL-BOLTZMANN DISTRIBUTION TEST ----------\n')
x0 = -10
xf = 10
sigma = 2
N = 10**5
t0 = current_milli_time()
rand = sample(symmetric_maxwell_boltzmann, x0, xf, N, sigma)
tf = current_milli_time()
print('\nMC Symmetric Maxwell-Boltzmann random generator mean: ', np.mean(rand))
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.figure(figsize=(12, 6))
plt.hist(rand, bins=40, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, symmetric_maxwell_boltzmann(x, sigma), color='r', label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$' % sigma)
plt.text(-10.5, 0.135, r'$PDF(x)=\sqrt{\frac{1}{2\pi}}\cdot\frac{x^2\exp(-\frac{x^2}{2\sigma^2})}{\sigma^3}$',
size=15)
plt.title('Symmetric Maxwell-Boltmann distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.legend()
plt.show()
# Invented distribution
def invented_test():
print('\n\n---------- MODIFIED RAYLEIGH DISTRIBUTION TEST ----------\n')
x0 = -4
xf = 4
sigma = 1
N = 10**5
t0 = current_milli_time()
rand = sample(invented, x0, xf, N, sigma)
tf = current_milli_time()
print('\nMC invented random generator mean: ', np.mean(rand))
print('Elapsed time: %.4f ms' % (tf-t0, ))
x = np.linspace(x0, xf, N)
plt.figure(figsize=(12, 5))
plt.hist(rand, bins=40, density=True, color=(0, 1, 0, 0.5), label='MCRand sample')
plt.plot(x, invented(x, sigma), color='r', label=r'Modified Rayleigh PDF $\sigma=%.2f$' % sigma)
plt.text(-4.5, 0.3, r'$PDF(x)=\frac{x^2\exp(-\frac{x^2}{2\sigma^2})}{2.506628\cdot\sigma^2}$', size=15)
plt.title('Modified Rayleigh distribution test')
plt.xlabel('Sample number')
plt.ylabel('Probability')
plt.xlim(-5, 5)
plt.ylim(0, 0.35)
plt.legend()
plt.show()
gaussian_test()
exponential_test()
cauchy_test()
rayleigh_test()
maxwell_boltzmann_test()
symmetric_maxwell_boltzmann_test()
invented_test()