GitHub

@@ -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.

75757676

```{code-cell} ipython3

77+

rng = np.random.default_rng()

78+7779

# specify parameters

7880

p, n = 0.3, 1_000_000

79818082

# draw observations from the distribution

81-

x = np.random.geometric(p, n)

83+

x = rng.geometric(p, n)

82848385

# compute sample mean and variance

8486

μ_hat = np.mean(x)

@@ -126,7 +128,7 @@ $$

126128

r, p, n = 10, 0.3, 1_000_000

127129128130

# draw observations from the distribution

129-

x = np.random.negative_binomial(r, p, n)

131+

x = rng.negative_binomial(r, p, n)

130132131133

# 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

218220219221

# draw observations from the distribution

220-

x = np.random.normal(μ, σ, n)

222+

x = rng.normal(μ, σ, n)

221223222224

# compute sample mean and variance

223225

μ_hat = np.mean(x)

@@ -259,7 +261,7 @@ a, b = 10, 20

259261

n = 1_000_000

260262261263

# draw observations from the distribution

262-

x = a + (b-a)*np.random.rand(n)

264+

x = a + (b-a)*rng.random(n)

263265264266

# 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.

297299298300

```{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

303305304306

μ_hat = np.mean(x)

@@ -441,13 +443,13 @@ Let's check with `numpy`.

441443

n, λ = 1_000_000, 0.3

442444443445

# draw uniform numbers

444-

u = np.random.rand(n)

446+

u = rng.random(n)

445447446448

# transform

447449

x = -np.log(1-u)/λ

448450449451

# draw geometric distributions

450-

x_g = np.random.exponential(1 / λ, n)

452+

x_g = rng.exponential(1 / λ, n)

451453452454

# 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

518520519521

# draw uniform numbers

520-

u = np.random.rand(n)

522+

u = rng.random(n)

521523522524

# transform

523525

x = np.ceil(np.log(1-u)/np.log(λ) - 1)

524526525527

# draw geometric distributions

526-

x_g = np.random.geometric(1-λ, n)

528+

x_g = rng.geometric(1-λ, n)

527529528530

# plot and compare

529531

plt.hist(x, bins=150, density=True)

530532

plt.show()

531533

```

532534533535

```{code-cell} ipython3

534-

np.random.geometric(1-λ, n).max()

536+

rng.geometric(1-λ, n).max()

535537

```

536538537539

```{code-cell} ipython3

Read the original on github.com ↗