Skip to content

Commit fea6b58

Browse files
Chihiro2000GitHubclaudeHumphreyYang
authored
[blackwell_kihlstrom.md] Update np.random → Generator API (#890)
* Update rng usage in blackwell_kihlstrom.md Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Align sample_posteriors RNG with sibling functions --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Humphrey Yang <39026988+HumphreyYang@users.noreply.github.com>
1 parent cb3f056 commit fea6b58

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

lectures/blackwell_kihlstrom.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ We begin with some imports.
8181
import numpy as np
8282
import matplotlib.pyplot as plt
8383
from 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

Comments
 (0)