GitHub

@@ -62,7 +62,8 @@ Here are a few lines of code that perform the task we set

6262

import numpy as np

6363

import matplotlib.pyplot as plt

646465-

ϵ_values = np.random.randn(100)

65+

rng = np.random.default_rng()

66+

ϵ_values = rng.standard_normal(100)

6667

plt.plot(ϵ_values)

6768

plt.show()

6869

```

@@ -135,7 +136,7 @@ print(np.__file__)

135136

```{index} single: Python; Subpackages

136137

```

137138138-

Consider the line `ϵ_values = np.random.randn(100)`.

139+

Consider the line `rng = np.random.default_rng()`.

139140140141

Here `np` refers to the package NumPy, while `random` is a **subpackage** of NumPy.

141142

@@ -176,7 +177,7 @@ Returning to our program that plots white noise, the remaining three lines

176177

after the import statements are

177178178179

```{code-cell} ipython

179-

ϵ_values = np.random.randn(100)

180+

ϵ_values = rng.standard_normal(100)

180181

plt.plot(ϵ_values)

181182

plt.show()

182183

```

@@ -207,7 +208,7 @@ ts_length = 100

207208

ϵ_values = [] # empty list

208209209210

for i in range(ts_length):

210-

e = np.random.randn()

211+

e = rng.standard_normal()

211212

ϵ_values.append(e)

212213213214

plt.plot(ϵ_values)

@@ -296,7 +297,7 @@ Now let's consider the `for` loop from {ref}`the program above <firstloopprog>`,

296297297298

```{code-cell} python3

298299

for i in range(ts_length):

299-

e = np.random.randn()

300+

e = rng.standard_normal()

300301

ϵ_values.append(e)

301302

```

302303

@@ -372,7 +373,7 @@ ts_length = 100

372373

ϵ_values = []

373374

i = 0

374375

while i < ts_length:

375-

e = np.random.randn()

376+

e = rng.standard_normal()

376377

ϵ_values.append(e)

377378

i = i + 1

378379

plt.plot(ϵ_values)

@@ -479,9 +480,10 @@ Here's one solution.

479480

T = 200

480481

x = np.empty(T+1)

481482

x[0] = 0

483+

rng = np.random.default_rng()

482484483485

for t in range(T):

484-

x[t+1] = α * x[t] + np.random.randn()

486+

x[t+1] = α * x[t] + rng.standard_normal()

485487486488

plt.plot(x)

487489

plt.show()

@@ -520,11 +522,12 @@ If you can, add a legend, to help distinguish between the three time series.

520522

α_values = [0.0, 0.8, 0.98]

521523

T = 200

522524

x = np.empty(T+1)

525+

rng = np.random.default_rng()

523526524527

for α in α_values:

525528

x[0] = 0

526529

for t in range(T):

527-

x[t+1] = α * x[t] + np.random.randn()

530+

x[t+1] = α * x[t] + rng.standard_normal()

528531

plt.plot(x, label=f'$\\alpha = {α}$')

529532530533

plt.legend()

@@ -572,9 +575,10 @@ Here's one solution:

572575

T = 200

573576

x = np.empty(T+1)

574577

x[0] = 0

578+

rng = np.random.default_rng()

575579576580

for t in range(T):

577-

x[t+1] = α * np.abs(x[t]) + np.random.randn()

581+

x[t+1] = α * np.abs(x[t]) + rng.standard_normal()

578582579583

plt.plot(x)

580584

plt.show()

@@ -627,13 +631,14 @@ Here's one way:

627631

T = 200

628632

x = np.empty(T+1)

629633

x[0] = 0

634+

rng = np.random.default_rng()

630635631636

for t in range(T):

632637

if x[t] < 0:

633638

abs_x = - x[t]

634639

else:

635640

abs_x = x[t]

636-

x[t+1] = α * abs_x + np.random.randn()

641+

x[t+1] = α * abs_x + rng.standard_normal()

637642638643

plt.plot(x)

639644

plt.show()

@@ -646,10 +651,11 @@ Here's a shorter way to write the same thing:

646651

T = 200

647652

x = np.empty(T+1)

648653

x[0] = 0

654+

rng = np.random.default_rng()

649655650656

for t in range(T):

651657

abs_x = - x[t] if x[t] < 0 else x[t]

652-

x[t+1] = α * abs_x + np.random.randn()

658+

x[t+1] = α * abs_x + rng.standard_normal()

653659654660

plt.plot(x)

655661

plt.show()

@@ -710,12 +716,13 @@ fraction that falls into the circle.

710716711717

```{code-cell} python3

712718

n = 1000000 # sample size for Monte Carlo simulation

719+

rng = np.random.default_rng()

713720714721

count = 0

715722

for i in range(n):

716723717724

# drawing random positions on the square

718-

u, v = np.random.uniform(), np.random.uniform()

725+

u, v = rng.uniform(), rng.uniform()

719726720727

# check whether the point falls within the boundary

721728

# of the unit circle centred at (0.5,0.5)

Read the original on github.com ↗