@@ -81,8 +81,6 @@ We begin with some imports.
8181import numpy as np
8282import matplotlib.pyplot as plt
8383from scipy.optimize import minimize
84-
85- np.random.seed(0)
8684```
8785
8886## Experiments and stochastic transformations
@@ -627,15 +625,17 @@ mystnb:
627625 caption: Sampled posterior points on the 2-simplex
628626 name: fig-blackwell-simplex-clouds
629627---
630- def sample_posteriors(μ_matrix, prior, n_draws=3000):
628+ def sample_posteriors(μ_matrix, prior, n_draws=3000, rng=None ):
631629 """
632630 Simulate n_draws observations from the experiment and compute
633631 the resulting posterior beliefs.
634632 Returns array of shape (n_draws, N).
635633 """
634+ if rng is None:
635+ rng = np.random.default_rng()
636636 N, M = μ_matrix.shape
637- states = np.random .choice(N, size=n_draws, p=prior)
638- signals = np.array([np.random .choice(M, p=μ_matrix[s]) for s in states])
637+ states = rng .choice(N, size=n_draws, p=prior)
638+ signals = np.array([rng .choice(M, p=μ_matrix[s]) for s in states])
639639 posteriors, _ = compute_posteriors(μ_matrix, prior)
640640 return posteriors[signals]
641641
@@ -648,9 +648,10 @@ def simplex_to_cart(pts):
648648 return pts @ corners
649649
650650
651- def plot_simplex_posteriors(μ_matrix, ν_matrix, prior3, n_draws=3000):
652- posts_μ = sample_posteriors(μ_matrix, prior3, n_draws)
653- posts_ν = sample_posteriors(ν_matrix, prior3, n_draws)
651+ def plot_simplex_posteriors(μ_matrix, ν_matrix, prior3, n_draws=3000, seed=0):
652+ rng = np.random.default_rng(seed)
653+ posts_μ = sample_posteriors(μ_matrix, prior3, n_draws, rng=rng)
654+ posts_ν = sample_posteriors(ν_matrix, prior3, n_draws, rng=rng)
654655
655656 cart_μ = simplex_to_cart(posts_μ)
656657 cart_ν = simplex_to_cart(posts_ν)
0 commit comments