Skip to content

Commit 69b7f21

Browse files
Update rng usage in time_series_with_matrices.md
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5ee005d commit 69b7f21

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

lectures/time_series_with_matrices.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ $$ (eq:eqma)
305305
Let’s try it out in Python.
306306
307307
```{code-cell} ipython3
308+
rng = np.random.default_rng()
309+
308310
σ_u = 2.
309-
u = np.random.normal(0, σ_u, size=T)
311+
u = rng.normal(0, σ_u, size=T)
310312
y = A_inv @ (b + u)
311313
```
312314
@@ -327,8 +329,8 @@ We can simulate $N$ paths.
327329
N = 100
328330
329331
for i in range(N):
330-
col = cm.viridis(np.random.rand()) # Choose a random color from viridis
331-
u = np.random.normal(0, σ_u, size=T)
332+
col = cm.viridis(rng.random()) # Choose a random color from viridis
333+
u = rng.normal(0, σ_u, size=T)
332334
y = A_inv @ (b + u)
333335
plt.plot(np.arange(T)+1, y, lw=0.5, color=col)
334336
@@ -345,8 +347,8 @@ steady state.
345347
N = 100
346348
347349
for i in range(N):
348-
col = cm.viridis(np.random.rand()) # Choose a random color from viridis
349-
u = np.random.normal(0, σ_u, size=T)
350+
col = cm.viridis(rng.random()) # Choose a random color from viridis
351+
u = rng.normal(0, σ_u, size=T)
350352
y_steady = A_inv @ (b_steady + u)
351353
plt.plot(np.arange(T)+1, y_steady, lw=0.5, color=col)
352354
@@ -432,12 +434,14 @@ class population_moments:
432434
433435
self.A, self.b, self.A_inv, self.σ_u, self.T = A, b, A_inv, σ_u, T
434436
435-
def sample_y(self, n):
437+
def sample_y(self, n, rng=None):
436438
"""
437439
Give a sample of size n of y.
438440
"""
441+
if rng is None:
442+
rng = np.random.default_rng()
439443
A_inv, σ_u, b, T = self.A_inv, self.σ_u, self.b, self.T
440-
us = np.random.normal(0, σ_u, size=[n, T])
444+
us = rng.normal(0, σ_u, size=[n, T])
441445
ys = np.vstack([A_inv @ (b + u) for u in us])
442446
443447
return ys
@@ -472,8 +476,8 @@ Let's begin by generating $N$ time realizations of $y$ plotting them together wi
472476
N = 100
473477
474478
for i in range(N):
475-
col = cm.viridis(np.random.rand()) # Choose a random color from viridis
476-
ys = series_process.sample_y(N)
479+
col = cm.viridis(rng.random()) # Choose a random color from viridis
480+
ys = series_process.sample_y(N, rng=rng)
477481
plt.plot(ys[i,:], lw=0.5, color=col)
478482
plt.plot(μ_y, color='red')
479483
@@ -649,7 +653,7 @@ print(B)
649653
650654
```{code-cell} ipython3
651655
σ_u = 0.
652-
u = np.random.normal(0, σ_u, size=T)
656+
u = rng.normal(0, σ_u, size=T)
653657
y = A_inv @ (b + u)
654658
y_steady = A_inv @ (b_steady + u)
655659
```

0 commit comments

Comments
 (0)