Skip to content

Commit 2944402

Browse files
Update rng usage in stats_examples.md (#873)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 30c5c43 commit 2944402

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

lectures/stats_examples.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ $$
7474
Let's use Python draw observations from the distribution and compare the sample mean and variance with the theoretical results.
7575

7676
```{code-cell} ipython3
77+
rng = np.random.default_rng()
78+
7779
# specify parameters
7880
p, n = 0.3, 1_000_000
7981
8082
# draw observations from the distribution
81-
x = np.random.geometric(p, n)
83+
x = rng.geometric(p, n)
8284
8385
# compute sample mean and variance
8486
μ_hat = np.mean(x)
@@ -126,7 +128,7 @@ $$
126128
r, p, n = 10, 0.3, 1_000_000
127129
128130
# draw observations from the distribution
129-
x = np.random.negative_binomial(r, p, n)
131+
x = rng.negative_binomial(r, p, n)
130132
131133
# compute sample mean and variance
132134
μ_hat = np.mean(x)
@@ -217,7 +219,7 @@ In the below example, we set $\mu = 0, \sigma = 0.1$.
217219
n = 1_000_000
218220
219221
# draw observations from the distribution
220-
x = np.random.normal(μ, σ, n)
222+
x = rng.normal(μ, σ, n)
221223
222224
# compute sample mean and variance
223225
μ_hat = np.mean(x)
@@ -259,7 +261,7 @@ a, b = 10, 20
259261
n = 1_000_000
260262
261263
# draw observations from the distribution
262-
x = a + (b-a)*np.random.rand(n)
264+
x = a + (b-a)*rng.random(n)
263265
264266
# compute sample mean and variance
265267
μ_hat = np.mean(x)
@@ -296,9 +298,9 @@ $$
296298
Let's start by generating a random sample and computing sample moments.
297299

298300
```{code-cell} ipython3
299-
x = np.random.rand(1_000_000)
301+
x = rng.random(1_000_000)
300302
# 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
302304
x[x <= 0.95] = 0
303305
304306
μ_hat = np.mean(x)
@@ -441,13 +443,13 @@ Let's check with `numpy`.
441443
n, λ = 1_000_000, 0.3
442444
443445
# draw uniform numbers
444-
u = np.random.rand(n)
446+
u = rng.random(n)
445447
446448
# transform
447449
x = -np.log(1-u)/λ
448450
449451
# draw geometric distributions
450-
x_g = np.random.exponential(1 / λ, n)
452+
x_g = rng.exponential(1 / λ, n)
451453
452454
# plot and compare
453455
plt.hist(x, bins=100, density=True)
@@ -517,21 +519,21 @@ The exponential distribution is the continuous analog of geometric distribution.
517519
n, λ = 1_000_000, 0.8
518520
519521
# draw uniform numbers
520-
u = np.random.rand(n)
522+
u = rng.random(n)
521523
522524
# transform
523525
x = np.ceil(np.log(1-u)/np.log(λ) - 1)
524526
525527
# draw geometric distributions
526-
x_g = np.random.geometric(1-λ, n)
528+
x_g = rng.geometric(1-λ, n)
527529
528530
# plot and compare
529531
plt.hist(x, bins=150, density=True)
530532
plt.show()
531533
```
532534

533535
```{code-cell} ipython3
534-
np.random.geometric(1-λ, n).max()
536+
rng.geometric(1-λ, n).max()
535537
```
536538

537539
```{code-cell} ipython3

0 commit comments

Comments
 (0)