|
74 | 74 | Let's use Python draw observations from the distribution and compare the sample mean and variance with the theoretical results. |
75 | 75 |
|
76 | 76 | ```{code-cell} ipython3 |
| 77 | +rng = np.random.default_rng() |
| 78 | +
|
77 | 79 | # specify parameters |
78 | 80 | p, n = 0.3, 1_000_000 |
79 | 81 |
|
80 | 82 | # draw observations from the distribution |
81 | | -x = np.random.geometric(p, n) |
| 83 | +x = rng.geometric(p, n) |
82 | 84 |
|
83 | 85 | # compute sample mean and variance |
84 | 86 | μ_hat = np.mean(x) |
|
126 | 128 | r, p, n = 10, 0.3, 1_000_000 |
127 | 129 |
|
128 | 130 | # draw observations from the distribution |
129 | | -x = np.random.negative_binomial(r, p, n) |
| 131 | +x = rng.negative_binomial(r, p, n) |
130 | 132 |
|
131 | 133 | # compute sample mean and variance |
132 | 134 | μ_hat = np.mean(x) |
@@ -217,7 +219,7 @@ In the below example, we set $\mu = 0, \sigma = 0.1$. |
217 | 219 | n = 1_000_000 |
218 | 220 |
|
219 | 221 | # draw observations from the distribution |
220 | | -x = np.random.normal(μ, σ, n) |
| 222 | +x = rng.normal(μ, σ, n) |
221 | 223 |
|
222 | 224 | # compute sample mean and variance |
223 | 225 | μ_hat = np.mean(x) |
@@ -259,7 +261,7 @@ a, b = 10, 20 |
259 | 261 | n = 1_000_000 |
260 | 262 |
|
261 | 263 | # draw observations from the distribution |
262 | | -x = a + (b-a)*np.random.rand(n) |
| 264 | +x = a + (b-a)*rng.random(n) |
263 | 265 |
|
264 | 266 | # compute sample mean and variance |
265 | 267 | μ_hat = np.mean(x) |
|
296 | 298 | Let's start by generating a random sample and computing sample moments. |
297 | 299 |
|
298 | 300 | ```{code-cell} ipython3 |
299 | | -x = np.random.rand(1_000_000) |
| 301 | +x = rng.random(1_000_000) |
300 | 302 | # x[x > 0.95] = 100*x[x > 0.95]+300 |
301 | | -x[x > 0.95] = 100*np.random.rand(len(x[x > 0.95]))+300 |
| 303 | +x[x > 0.95] = 100*rng.random(len(x[x > 0.95]))+300 |
302 | 304 | x[x <= 0.95] = 0 |
303 | 305 |
|
304 | 306 | μ_hat = np.mean(x) |
@@ -441,13 +443,13 @@ Let's check with `numpy`. |
441 | 443 | n, λ = 1_000_000, 0.3 |
442 | 444 |
|
443 | 445 | # draw uniform numbers |
444 | | -u = np.random.rand(n) |
| 446 | +u = rng.random(n) |
445 | 447 |
|
446 | 448 | # transform |
447 | 449 | x = -np.log(1-u)/λ |
448 | 450 |
|
449 | 451 | # draw geometric distributions |
450 | | -x_g = np.random.exponential(1 / λ, n) |
| 452 | +x_g = rng.exponential(1 / λ, n) |
451 | 453 |
|
452 | 454 | # plot and compare |
453 | 455 | plt.hist(x, bins=100, density=True) |
@@ -517,21 +519,21 @@ The exponential distribution is the continuous analog of geometric distribution. |
517 | 519 | n, λ = 1_000_000, 0.8 |
518 | 520 |
|
519 | 521 | # draw uniform numbers |
520 | | -u = np.random.rand(n) |
| 522 | +u = rng.random(n) |
521 | 523 |
|
522 | 524 | # transform |
523 | 525 | x = np.ceil(np.log(1-u)/np.log(λ) - 1) |
524 | 526 |
|
525 | 527 | # draw geometric distributions |
526 | | -x_g = np.random.geometric(1-λ, n) |
| 528 | +x_g = rng.geometric(1-λ, n) |
527 | 529 |
|
528 | 530 | # plot and compare |
529 | 531 | plt.hist(x, bins=150, density=True) |
530 | 532 | plt.show() |
531 | 533 | ``` |
532 | 534 |
|
533 | 535 | ```{code-cell} ipython3 |
534 | | -np.random.geometric(1-λ, n).max() |
| 536 | +rng.geometric(1-λ, n).max() |
535 | 537 | ``` |
536 | 538 |
|
537 | 539 | ```{code-cell} ipython3 |
|
0 commit comments