Skip to content

Commit 96ede29

Browse files
kp992Copilotmmcky
authored
update jax random key (#965)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mmcky <8263752+mmcky@users.noreply.github.com>
1 parent 40e15dc commit 96ede29

16 files changed

Lines changed: 32 additions & 32 deletions

lectures/aiyagari_egm.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ Let's test it:
472472
```{code-cell} ipython3
473473
household = create_household()
474474
prices = Prices(r=0.01, w=1.0)
475-
key = jax.random.PRNGKey(42)
475+
key = jax.random.key(42)
476476
477477
with qe.Timer():
478478
K_supply = capital_supply(household, prices, key)
@@ -529,7 +529,7 @@ Let's compute the equilibrium:
529529
```{code-cell} ipython3
530530
firm = Firm()
531531
household = create_household()
532-
key = jax.random.PRNGKey(42)
532+
key = jax.random.key(42)
533533
534534
with qe.Timer():
535535
K_star = compute_equilibrium(firm, household, key)

lectures/ar1_bayes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ NUTS_kernel = numpyro.infer.NUTS(AR1_model)
483483
484484
# Run MCMC
485485
mcmc = numpyro.infer.MCMC(NUTS_kernel, num_samples=50000, num_warmup=10000, progress_bar=False)
486-
mcmc.run(rng_key=random.PRNGKey(1), data=y)
486+
mcmc.run(rng_key=random.key(1), data=y)
487487
```
488488
489489
We plot the trace and posterior.
@@ -535,7 +535,7 @@ NUTS_kernel = numpyro.infer.NUTS(AR1_model_y0)
535535
536536
# Run MCMC
537537
mcmc2 = numpyro.infer.MCMC(NUTS_kernel, num_samples=50000, num_warmup=10000, progress_bar=False)
538-
mcmc2.run(rng_key=random.PRNGKey(1), data=y)
538+
mcmc2.run(rng_key=random.key(1), data=y)
539539
```
540540
541541
Again we plot the trace and posterior.

lectures/back_prop.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ N = 3 # Number of layers
393393
layer_sizes = [1, ] * (N + 1)
394394
param_scale = 0.1
395395
step_size = 0.01
396-
params = init_network_params(layer_sizes, random.PRNGKey(1))
396+
params = init_network_params(layer_sizes, random.key(1))
397397
```
398398
399399
```{code-cell} ipython3
@@ -496,12 +496,12 @@ f_val = f(grid)
496496
497497
```{code-cell} ipython3
498498
indices = jnp.arange(M)
499-
key = random.PRNGKey(0)
499+
key = random.key(0)
500500
501501
def train(params, grid, f_val, key, num_epochs=300):
502502
for epoch in range(num_epochs):
503503
key, _ = random.split(key)
504-
random_permutation = random.permutation(random.PRNGKey(1), indices)
504+
random_permutation = random.permutation(random.key(1), indices)
505505
for x, y in zip(grid[random_permutation], f_val[random_permutation]):
506506
params = update_la(params, x, y)
507507

lectures/ifp_advanced.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def create_ifp(
356356
assert β * ER < 1, "Stability condition failed."
357357
358358
# Generate random draws using JAX
359-
key = jax.random.PRNGKey(seed)
359+
key = jax.random.key(seed)
360360
subkey1, subkey2 = jax.random.split(key)
361361
η_draws = jax.random.normal(subkey1, (shock_draw_size,))
362362
ζ_draws = jax.random.normal(subkey2, (shock_draw_size,))
@@ -586,7 +586,7 @@ def compute_asset_stationary(
586586
z_idx_0_vector = jnp.zeros(num_households).astype(jnp.int32)
587587
588588
# Vectorize over many households
589-
key = jax.random.PRNGKey(seed)
589+
key = jax.random.key(seed)
590590
keys = jax.random.split(key, num_households)
591591
# Vectorize simulate_household in (key, a_0, z_idx_0)
592592
sim_all_households = jax.vmap(

lectures/ifp_egm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def compute_asset_stationary(
890890
z_idx_0_vector = jnp.zeros(num_households).astype(jnp.int32)
891891
892892
# Vectorize over many households
893-
key = jax.random.PRNGKey(seed)
893+
key = jax.random.key(seed)
894894
keys = jax.random.split(key, num_households)
895895
# Vectorize simulate_household in (key, a_0, z_idx_0)
896896
sim_all_households = jax.vmap(

lectures/ifp_egm_transient_shocks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def create_ifp(r=0.01,
411411
shock_draw_size=100,
412412
seed=1234):
413413
414-
key = jax.random.PRNGKey(seed)
414+
key = jax.random.key(seed)
415415
s = jnp.linspace(0, savings_grid_max, savings_grid_size)
416416
Π, z_grid = jnp.array(Π), jnp.array(z_grid)
417417
R = 1 + r
@@ -779,7 +779,7 @@ def compute_asset_stationary(
779779
z_idx_0_vector = jnp.zeros(num_households).astype(jnp.int32)
780780
781781
# Vectorize over many households
782-
key = jax.random.PRNGKey(seed)
782+
key = jax.random.key(seed)
783783
keys = jax.random.split(key, num_households)
784784
# Vectorize simulate_household in (key, a_0, z_idx_0)
785785
sim_all_households = jax.vmap(

lectures/lake_model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ P = jnp.array([[1 - λ, λ],
592592
xbar = rate_steady_state(model_markov)
593593
594594
# Simulate the Markov chain - we need a different approach for random updates
595-
key = jax.random.PRNGKey(0)
595+
key = jax.random.key(0)
596596
597597
def simulate_markov(P, initial_state, T, key):
598598
"""Simulate Markov chain for T periods"""

lectures/mccall_fitted_vfi.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def create_mccall_model(
282282
):
283283
"""Factory function to create a McCall model instance."""
284284
285-
key = jax.random.PRNGKey(seed)
285+
key = jax.random.key(seed)
286286
z_draws = jax.random.normal(key, (mc_size,))
287287
288288
# Discretize just to get a suitable wage grid for interpolation
@@ -529,7 +529,7 @@ def simulate_employment_path(
529529
Simulate employment path for T periods starting from unemployment.
530530
531531
"""
532-
key = jax.random.PRNGKey(seed)
532+
key = jax.random.key(seed)
533533
c, α, β, ρ, ν, γ, w_grid, z_draws = model
534534
535535
# Initial conditions: start unemployed with initial wage draw
@@ -678,7 +678,7 @@ def simulate_cross_section(
678678
"""
679679
c, α, β, ρ, ν, γ, w_grid, z_draws = model
680680
681-
key = jax.random.PRNGKey(seed)
681+
key = jax.random.key(seed)
682682
683683
# Solve for optimal reservation wage
684684
w_bar = get_reservation_wage(model)
@@ -750,7 +750,7 @@ def plot_cross_sectional_unemployment(
750750
c, α, β, ρ, ν, γ, w_grid, z_draws = model
751751
752752
# Get final employment state directly
753-
key = jax.random.PRNGKey(42)
753+
key = jax.random.key(42)
754754
w_bar = get_reservation_wage(model)
755755
756756
# Initialize arrays

lectures/mccall_model.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ class McCallModelContinuous(NamedTuple):
802802
def create_mccall_continuous(
803803
c=25, β=0.99, σ=0.5, μ=2.5, mc_size=1000, seed=1234
804804
):
805-
key = jax.random.PRNGKey(seed)
805+
key = jax.random.key(seed)
806806
s = jax.random.normal(key, (mc_size,))
807807
w_draws = jnp.exp(μ + σ * s)
808808
return McCallModelContinuous(c, β, σ, μ, w_draws)
@@ -970,7 +970,7 @@ def simulate_lifetime_value(key, model, w_bar, n_periods=100):
970970
971971
Parameters:
972972
-----------
973-
key : jax.random.PRNGKey
973+
key : jax.random.key
974974
Random key for JAX
975975
model : McCallModelContinuous
976976
The model containing parameters
@@ -1018,7 +1018,7 @@ def compute_mean_lifetime_value(model, w_bar, num_reps=10000, seed=1234):
10181018
Compute mean lifetime value across many simulations.
10191019
10201020
"""
1021-
key = jax.random.PRNGKey(seed)
1021+
key = jax.random.key(seed)
10221022
keys = jax.random.split(key, num_reps)
10231023
10241024
# Vectorize the simulation across all replications
@@ -1096,7 +1096,7 @@ def compute_stopping_time_continuous(w_bar, key, model):
10961096
-----------
10971097
w_bar : float
10981098
The reservation wage
1099-
key : jax.random.PRNGKey
1099+
key : jax.random.key
11001100
Random key for JAX
11011101
model : McCallModelContinuous
11021102
The model containing wage draws
@@ -1148,7 +1148,7 @@ def compute_mean_stopping_time_continuous(w_bar, model, num_reps=100000, seed=12
11481148
Average stopping time across all replications
11491149
"""
11501150
# Generate a key for each MC replication
1151-
key = jax.random.PRNGKey(seed)
1151+
key = jax.random.key(seed)
11521152
keys = jax.random.split(key, num_reps)
11531153
11541154
# Vectorize compute_stopping_time_continuous and evaluate across keys

lectures/mccall_model_with_sep_markov.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def simulate_employment_path(
605605
Simulate employment path for T periods starting from unemployment.
606606
607607
"""
608-
key = jax.random.PRNGKey(seed)
608+
key = jax.random.key(seed)
609609
# Unpack model
610610
n, w_vals, P, P_cumsum, β, c, α, γ = model
611611
@@ -813,7 +813,7 @@ def simulate_cross_section(
813813
814814
Returns the cross-sectional unemployment rate.
815815
"""
816-
key = jax.random.PRNGKey(seed)
816+
key = jax.random.key(seed)
817817
818818
# Solve for optimal reservation wage
819819
v_u = vfi(model)
@@ -848,7 +848,7 @@ def plot_cross_sectional_unemployment(
848848
849849
"""
850850
# Get final employment state directly
851-
key = jax.random.PRNGKey(42)
851+
key = jax.random.key(42)
852852
v_u = vfi(model)
853853
w_bar = get_reservation_wage(v_u, model)
854854

0 commit comments

Comments
 (0)