839839Mutability leads to the following behavior (which can be shocking to MATLAB programmers...)
840840
841841``` {code-cell} python3
842- a = np.random.randn(3)
842+ rng = np.random.default_rng()
843+ a = rng.standard_normal(3)
843844a
844845```
845846
@@ -869,7 +870,7 @@ It is of course possible to make `b` an independent copy of `a` when required.
869870This can be done using ` np.copy `
870871
871872``` {code-cell} python3
872- a = np.random.randn (3)
873+ a = rng.standard_normal (3)
873874a
874875```
875876
@@ -945,7 +946,7 @@ def f(x):
945946The NumPy function ` np.where ` provides a vectorized alternative:
946947
947948``` {code-cell} python3
948- x = np.random.randn (4)
949+ x = rng.standard_normal (4)
949950x
950951```
951952
@@ -1020,11 +1021,12 @@ z[z > 3]
10201021NumPy provides some additional functionality related to scientific programming
10211022through its sub-packages.
10221023
1023- We've already seen how we can generate random variables using np.random
1024+ We've already seen how we can generate random variables using NumPy's
1025+ [ random ` Generator ` ] ( https://numpy.org/doc/stable/reference/random/generator.html#random-generator ) .
10241026
10251027``` {code-cell} python3
1026- z = np.random.randn (10000) # Generate standard normals
1027- y = np.random .binomial(10, 0.5, size=1000) # 1,000 draws from Bin(10, 0.5)
1028+ z = rng.standard_normal (10000) # Generate standard normals
1029+ y = rng .binomial(10, 0.5, size=1000) # 1,000 draws from Bin(10, 0.5)
10281030y.mean()
10291031```
10301032
@@ -1064,7 +1066,7 @@ It takes a few seconds to run.
10641066n = 20
10651067m = 1000
10661068for i in range(n):
1067- X = np.random.randn( m, m)
1069+ X = rng.standard_normal(( m, m) )
10681070 λ = np.linalg.eigvals(X)
10691071```
10701072
@@ -1203,28 +1205,28 @@ Here's our first pass at a solution:
12031205
12041206``` {code-cell} python3
12051207from numpy import cumsum
1206- from numpy.random import uniform
12071208
12081209class DiscreteRV:
12091210 """
12101211 Generates an array of draws from a discrete random variable with vector of
12111212 probabilities given by q.
12121213 """
12131214
1214- def __init__(self, q):
1215+ def __init__(self, q, seed=None ):
12151216 """
12161217 The argument q is a NumPy array, or array like, nonnegative and sums
12171218 to 1
12181219 """
12191220 self.q = q
12201221 self.Q = cumsum(q)
1222+ self.rng = np.random.default_rng(seed)
12211223
12221224 def draw(self, k=1):
12231225 """
12241226 Returns k draws from q. For each such draw, the value i is returned
12251227 with probability q[i].
12261228 """
1227- return self.Q.searchsorted(uniform(0, 1, size=k))
1229+ return self.Q.searchsorted(self.rng. uniform(0, 1, size=k))
12281230```
12291231
12301232The logic is not obvious, but if you take your time and read it slowly,
@@ -1353,7 +1355,8 @@ Here's an example of usage
13531355
13541356``` {code-cell} python3
13551357fig, ax = plt.subplots()
1356- X = np.random.randn(1000)
1358+ rng = np.random.default_rng()
1359+ X = rng.standard_normal(1000)
13571360F = ECDF(X)
13581361F.plot(ax)
13591362```
@@ -1374,9 +1377,9 @@ In this exercise, try to use `for` loops to replicate the result of the followin
13741377
13751378``` {code-cell} python3
13761379
1377- np.random.seed (123)
1378- x = np.random.randn( 4, 4)
1379- y = np.random.randn (4)
1380+ rng = np.random.default_rng (123)
1381+ x = rng.standard_normal(( 4, 4) )
1382+ y = rng.standard_normal (4)
13801383A = x / y
13811384```
13821385
@@ -1404,9 +1407,9 @@ Now we can import the quantecon package.
14041407
14051408``` {code-cell} python3
14061409
1407- np.random.seed (123)
1408- x = np.random.randn( 1000, 100, 100)
1409- y = np.random.randn (100)
1410+ rng = np.random.default_rng (123)
1411+ x = rng.standard_normal(( 1000, 100, 100) )
1412+ y = rng.standard_normal (100)
14101413
14111414with qe.Timer("Broadcasting operation"):
14121415 B = x / y
@@ -1431,9 +1434,9 @@ print(B)
14311434** Part 1 Solution**
14321435
14331436``` {code-cell} python3
1434- np.random.seed (123)
1435- x = np.random.randn( 4, 4)
1436- y = np.random.randn (4)
1437+ rng = np.random.default_rng (123)
1438+ x = rng.standard_normal(( 4, 4) )
1439+ y = rng.standard_normal (4)
14371440
14381441C = np.empty_like(x)
14391442n = len(x)
@@ -1461,9 +1464,9 @@ print(np.array_equal(A, C))
14611464
14621465``` {code-cell} python3
14631466
1464- np.random.seed (123)
1465- x = np.random.randn( 1000, 100, 100)
1466- y = np.random.randn (100)
1467+ rng = np.random.default_rng (123)
1468+ x = rng.standard_normal(( 1000, 100, 100) )
1469+ y = rng.standard_normal (100)
14671470
14681471with qe.Timer("For loop operation"):
14691472 D = np.empty_like(x)
0 commit comments