Skip to content

Commit b5e5bd4

Browse files
Update rng usage in likelihood_ratio_process_2.md
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rs2tDEcDXLnfMnM88v8e1D
1 parent c10a3a8 commit b5e5bd4

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

lectures/likelihood_ratio_process_2.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import numpy as np
6363
from numba import vectorize, jit, prange
6464
from math import gamma
6565
from scipy.integrate import quad
66+
67+
rng = np.random.default_rng()
6668
```
6769

6870
## Review: likelihood ratio processes
@@ -150,7 +152,7 @@ g = jit(lambda x: p(x, G_a, G_b))
150152

151153
```{code-cell} ipython3
152154
@jit
153-
def simulate(a, b, T=50, N=500):
155+
def simulate(a, b, rng, T=50, N=500):
154156
'''
155157
Generate N sets of T observations of the likelihood ratio,
156158
return as N x T matrix.
@@ -160,7 +162,7 @@ def simulate(a, b, T=50, N=500):
160162
161163
for i in range(N):
162164
for j in range(T):
163-
w = np.random.beta(a, b)
165+
w = rng.beta(a, b)
164166
l_arr[i, j] = f(w) / g(w)
165167
166168
return l_arr
@@ -614,14 +616,14 @@ T = 100
614616
N = 10000
615617
616618
# Nature follows f, g, or mixture
617-
s_seq_f = np.random.beta(F_a, F_b, (N, T))
618-
s_seq_g = np.random.beta(G_a, G_b, (N, T))
619+
s_seq_f = rng.beta(F_a, F_b, (N, T))
620+
s_seq_g = rng.beta(G_a, G_b, (N, T))
619621
620622
h = jit(lambda x: 0.5 * f(x) + 0.5 * g(x))
621-
model_choices = np.random.rand(N, T) < 0.5
623+
model_choices = rng.random((N, T)) < 0.5
622624
s_seq_h = np.empty((N, T))
623-
s_seq_h[model_choices] = np.random.beta(F_a, F_b, size=model_choices.sum())
624-
s_seq_h[~model_choices] = np.random.beta(G_a, G_b, size=(~model_choices).sum())
625+
s_seq_h[model_choices] = rng.beta(F_a, F_b, size=model_choices.sum())
626+
s_seq_h[~model_choices] = rng.beta(G_a, G_b, size=(~model_choices).sum())
625627
626628
l_cum_f, c1_f = simulate_blume_easley(s_seq_f)
627629
l_cum_g, c1_g = simulate_blume_easley(s_seq_g)
@@ -770,7 +772,7 @@ for row, (f_belief, g_belief, label) in enumerate([
770772
771773
for col, nature_label in enumerate(nature_labels):
772774
params = nature_params[label][col]
773-
s_seq = np.random.beta(params[0], params[1], (1000, 200))
775+
s_seq = rng.beta(params[0], params[1], (1000, 200))
774776
_, c1 = simulate_blume_easley(s_seq, f_belief, g_belief, λ)
775777
776778
median_c1 = np.median(c1, axis=0)
@@ -1120,11 +1122,13 @@ We'll start with different initial priors $\pi^i_0 \in (0, 1)$ and widen the ga
11201122
Now we can run simulations for different scenarios
11211123

11221124
```{code-cell} ipython3
1125+
rng = np.random.default_rng()
1126+
11231127
# Nature follows f
1124-
s_seq_f = np.random.beta(F_a, F_b, (N, T))
1128+
s_seq_f = rng.beta(F_a, F_b, (N, T))
11251129
11261130
# Nature follows g
1127-
s_seq_g = np.random.beta(G_a, G_b, (N, T))
1131+
s_seq_g = rng.beta(G_a, G_b, (N, T))
11281132
11291133
results_f = {}
11301134
results_g = {}
@@ -1241,6 +1245,8 @@ Here is one solution
12411245
T = 40
12421246
N = 1000
12431247
1248+
rng = np.random.default_rng()
1249+
12441250
F_a, F_b = 2, 5
12451251
G_a, G_b = 5, 2
12461252
@@ -1253,8 +1259,8 @@ g = jit(lambda x: p(x, G_a, G_b))
12531259
(0.1, 0.9),
12541260
]
12551261
1256-
s_seq_f = np.random.beta(F_a, F_b, (N, T))
1257-
s_seq_g = np.random.beta(G_a, G_b, (N, T))
1262+
s_seq_f = rng.beta(F_a, F_b, (N, T))
1263+
s_seq_g = rng.beta(G_a, G_b, (N, T))
12581264
12591265
results_f = {}
12601266
results_g = {}
@@ -1586,9 +1592,11 @@ In the simulation below, agent 1 assigns positive probabilities only to $f$ and
15861592
T = 100
15871593
N = 1000
15881594
1595+
rng = np.random.default_rng()
1596+
15891597
# Generate sequences for nature f and g
1590-
s_seq_f = np.random.beta(F_a, F_b, (N, T))
1591-
s_seq_g = np.random.beta(G_a, G_b, (N, T))
1598+
s_seq_f = rng.beta(F_a, F_b, (N, T))
1599+
s_seq_g = rng.beta(G_a, G_b, (N, T))
15921600
15931601
# Run simulations
15941602
results_f = simulate_three_model_allocation(s_seq_f,
@@ -1712,9 +1720,11 @@ Now we can run the simulation
17121720
T = 1000
17131721
N = 1000
17141722
1723+
rng = np.random.default_rng()
1724+
17151725
# Generate sequences for different nature scenarios
1716-
s_seq_f = np.random.beta(F_a, F_b, (N, T))
1717-
s_seq_g = np.random.beta(G_a, G_b, (N, T))
1726+
s_seq_f = rng.beta(F_a, F_b, (N, T))
1727+
s_seq_g = rng.beta(G_a, G_b, (N, T))
17181728
17191729
# Run simulations for both scenarios
17201730
results_f = simulate_three_model_allocation(

0 commit comments

Comments
 (0)