Repository navigation
Refactor cross-section simulation: reverse loop structure for better performance - #734
Conversation
…performance This commit refactors the cross-sectional agent simulation in both McCall model lectures to use a more efficient loop structure. Changes: - Replaced old approach (loop over time, vectorize over agents at each step) with new approach (vectorize over agents, loop over time per agent) - Added sim_agent() function that uses lax.fori_loop to simulate a single agent forward T time steps - Added sim_agents_vmap to vectorize sim_agent across multiple agents - Updated simulate_cross_section() to use the new implementation - Updated plot_cross_sectional_unemployment() to use sim_agents_vmap - Added explanatory text clarifying differences between simulate_employment_path() and sim_agent() Performance: The new approach has comparable or slightly better performance while being more modular and conceptually cleaner. Files modified: - mccall_model_with_sep_markov.md (discrete wage case) - mccall_fitted_vfi.md (continuous wage case) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Additional ContextThis refactoring was motivated by exploring different strategies for simulating cross-sections in JAX: Architecture ComparisonThe original implementation used: def update(t, loop_state):
# For each time step t:
# 1. Generate keys for all agents
# 2. Update all agents in parallel (vmap)
lax.fori_loop(0, T, update, ...) # outer loop over timeThe new implementation reverses this: def sim_agent(key, ...):
def update(t, state):
# For a single agent:
# Update state using fold_in for keys
lax.fori_loop(0, T, update, ...) # inner loop over time
sim_agents_vmap(...) # outer vectorization over agentsWhy This Matters
Interesting FindingRemoving Both implementations are correct and produce statistically equivalent results. The new one is cleaner and performs just as well (or slightly better). |
|
📖 Netlify Preview Ready! Preview URL: https://pr-734--sunny-cactus-210e3e.netlify.app (bc17309) 📚 Changed Lecture Pages: mccall_fitted_vfi, mccall_model_with_sep_markov |
Summary
This PR refactors the cross-sectional agent simulation in both McCall model lectures to use a more efficient and modular loop structure.
Key Changes
Previous approach: Loop over time steps, vectorize over all agents at each step
New approach: Vectorize over agents, with each agent looping over time internally
Implementation Details
sim_agent()function - Useslax.fori_loopto simulate a single agent forward T time steps withfold_infor key generationsim_agents_vmap- Vectorizessim_agentacross multiple agents usingjax.vmapsimulate_cross_section()- Now generates n_agents keys and passes each tosim_agentplot_cross_sectional_unemployment()- Usessim_agents_vmapdirectlysimulate_employment_path()(records full history for visualization) andsim_agent()(returns only final state for efficiency)Performance
Testing with 50,000 agents over 200 periods showed:
@jax.jitonsim_agent: New approach is ~1.07x faster and has lower varianceFiles Modified
mccall_model_with_sep_markov.md(discrete wage case)mccall_fitted_vfi.md(continuous wage case)Both files now use the same efficient pattern, with the only difference being continuous vs discrete wages.
Testing
Both notebooks have been converted to Python and run successfully, producing expected results that match the ergodic theorem (time-average ≈ cross-sectional average).
🤖 Generated with Claude Code