Refactor cross-section simulation: reverse loop structure for better performance#734
Merged
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 <noreply@anthropic.com>
Contributor
Author
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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