GitHub

@@ -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

```

67696870

## Review: likelihood ratio processes

@@ -150,7 +152,7 @@ g = jit(lambda x: p(x, G_a, G_b))

150152151153

```{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):

160162161163

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)

165167166168

return l_arr

@@ -614,14 +616,14 @@ T = 100

614616

N = 10000

615617616618

# 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))

619621620622

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())

625627626628

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([

770772771773

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, λ)

775777776778

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

1121112311221124

```{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))

1125112911261130

# 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))

1128113211291133

results_f = {}

11301134

results_g = {}

@@ -1241,6 +1245,8 @@ Here is one solution

12411245

T = 40

12421246

N = 1000

124312471248+

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

]

125512611256-

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))

1258126412591265

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

158815941595+

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))

1592160015931601

# 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

171417221723+

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))

1718172817191729

# Run simulations for both scenarios

17201730

results_f = simulate_three_model_allocation(

Read the original on github.com ↗