@@ -62,7 +62,8 @@ Here are a few lines of code that perform the task we set
6262import numpy as np
6363import matplotlib.pyplot as plt
6464
65- ϵ_values = np.random.randn(100)
65+ rng = np.random.default_rng()
66+ ϵ_values = rng.standard_normal(100)
6667plt.plot(ϵ_values)
6768plt.show()
6869```
@@ -135,7 +136,7 @@ print(np.__file__)
135136``` {index} single: Python; Subpackages
136137```
137138
138- Consider the line ` ϵ_values = np.random.randn(100 )` .
139+ Consider the line ` rng = np.random.default_rng( )` .
139140
140141Here ` 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
176177after the import statements are
177178
178179``` {code-cell} ipython
179- ϵ_values = np.random.randn (100)
180+ ϵ_values = rng.standard_normal (100)
180181plt.plot(ϵ_values)
181182plt.show()
182183```
@@ -207,7 +208,7 @@ ts_length = 100
207208ϵ_values = [] # empty list
208209
209210for i in range(ts_length):
210- e = np.random.randn ()
211+ e = rng.standard_normal ()
211212 ϵ_values.append(e)
212213
213214plt.plot(ϵ_values)
@@ -296,7 +297,7 @@ Now let's consider the `for` loop from {ref}`the program above <firstloopprog>`,
296297
297298``` {code-cell} python3
298299for 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 = []
373374i = 0
374375while i < ts_length:
375- e = np.random.randn ()
376+ e = rng.standard_normal ()
376377 ϵ_values.append(e)
377378 i = i + 1
378379plt.plot(ϵ_values)
@@ -479,9 +480,10 @@ Here's one solution.
479480T = 200
480481x = np.empty(T+1)
481482x[0] = 0
483+ rng = np.random.default_rng()
482484
483485for t in range(T):
484- x[t+1] = α * x[t] + np.random.randn ()
486+ x[t+1] = α * x[t] + rng.standard_normal ()
485487
486488plt.plot(x)
487489plt.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]
521523T = 200
522524x = np.empty(T+1)
525+ rng = np.random.default_rng()
523526
524527for α 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 = {α}$')
529532
530533plt.legend()
@@ -572,9 +575,10 @@ Here's one solution:
572575T = 200
573576x = np.empty(T+1)
574577x[0] = 0
578+ rng = np.random.default_rng()
575579
576580for 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 ()
578582
579583plt.plot(x)
580584plt.show()
@@ -627,13 +631,14 @@ Here's one way:
627631T = 200
628632x = np.empty(T+1)
629633x[0] = 0
634+ rng = np.random.default_rng()
630635
631636for 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 ()
637642
638643plt.plot(x)
639644plt.show()
@@ -646,10 +651,11 @@ Here's a shorter way to write the same thing:
646651T = 200
647652x = np.empty(T+1)
648653x[0] = 0
654+ rng = np.random.default_rng()
649655
650656for 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 ()
653659
654660plt.plot(x)
655661plt.show()
@@ -710,12 +716,13 @@ fraction that falls into the circle.
710716
711717``` {code-cell} python3
712718n = 1000000 # sample size for Monte Carlo simulation
719+ rng = np.random.default_rng()
713720
714721count = 0
715722for i in range(n):
716723
717724 # drawing random positions on the square
718- u, v = np.random. uniform(), np.random .uniform()
725+ u, v = rng. uniform(), rng .uniform()
719726
720727 # check whether the point falls within the boundary
721728 # of the unit circle centred at (0.5,0.5)
0 commit comments