Skip to content

Conversation

@jstac
Copy link
Contributor

@jstac jstac commented Nov 5, 2025

Summary

This PR significantly improves the performance and code quality of the Aiyagari model lecture by switching from Howard Policy Iteration (HPI) to Value Function Iteration (VFI) as the primary solution method, with HPI moved to an advanced exercise.

Major Changes

Algorithm Changes

  • Primary method: Switched from HPI to VFI as the main solution algorithm
  • VFI implementation: Uses jax.lax.while_loop with @jax.jit compilation for optimal performance
  • HPI: Moved to Exercise 3 with complete JIT-compiled solution

Parameter Optimization

  • Reduced asset grid size from 200 to 100 points
  • Reduced asset grid maximum from 20 to 12.5 (better suited for equilibrium range)

Code Quality Improvements

  • JIT compilation strategy: Only use @jax.jit on top-level functions (not helper functions)
    • Helper functions are automatically inlined and optimized by JAX
    • Avoids double-compilation overhead
  • Naming: Use loop_state instead of state to avoid confusion with DP state variables
  • Consistency: Both VFI and HPI use compiled lax.while_loop

Performance Improvements

Configuration Time Speedup
Original (damped iteration) ~11.74 seconds Baseline
New VFI (bisection) ~0.68 seconds 17x faster
HPI in Exercise 3 ~0.48 seconds 24x faster

JIT Compilation Analysis

We tested four different JIT compilation strategies for HPI:

Strategy Time Notes
@jax.jit on helpers only ~1.02s Helpers compiled individually
No @jax.jit ~40.97s Pure Python (85x slower!)
@jax.jit on top-level only ~0.48s ✨ Best: single compilation unit
@jax.jit on all functions ~0.62s Double compilation overhead

Winner: Only JIT-compile the top-level function and let JAX optimize the entire call graph.

Educational Improvements

  • Better pedagogy: Students learn VFI first (simpler, more standard algorithm)
  • Advanced exercise: HPI presented as Exercise 3 for students who want to explore alternative methods
  • Verification task: Exercise asks students to confirm both methods produce the same equilibrium
  • Complete solution: Full JIT-compiled HPI implementation provided with timing comparisons

Testing

  • ✅ Code runs successfully with all tests passing
  • ✅ VFI and HPI produce nearly identical results (difference < 0.001)
  • ✅ All equilibrium computations complete successfully
  • ✅ Exercise 3 solution code is fully functional

Files Changed

  • lectures/aiyagari.md: 216 insertions(+), 103 deletions(-)

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

This commit significantly improves the performance and code quality of the
Aiyagari model lecture by switching from Howard Policy Iteration (HPI) to
Value Function Iteration (VFI) as the primary solution method, with HPI
moved to an exercise.

Major changes:
- Replace HPI with VFI using jax.lax.while_loop and @jax.jit compilation
- Reduce asset grid size from 200 to 100 points for efficiency
- Reduce asset grid maximum from 20 to 12.5 (better suited for equilibrium)
- Use 'loop_state' instead of 'state' in loops to avoid DP terminology confusion
- Remove redundant @jax.jit decorators from helper functions (only on top-level functions)
- Move HPI implementation to Exercise 3 with complete solution

Performance improvements:
- VFI equilibrium computation: ~0.68 seconds (was ~11+ seconds with damped iteration)
- HPI in Exercise 3: ~0.48 seconds with optimized JIT compilation
- 85x speedup compared to unoptimized Python loops

Code quality improvements:
- Cleaner JIT compilation strategy (only on ultimate calling functions)
- Both VFI and HPI use compiled lax.while_loop for consistency
- Helper functions automatically inlined and optimized by JAX
- Clear separation of main content (VFI) and advanced material (HPI exercise)

Educational improvements:
- Students learn VFI first (simpler, more standard algorithm)
- HPI presented as advanced exercise with guidance and complete solution
- Exercise asks students to verify both methods produce same equilibrium

Generated with Claude Code
Co-Authored-By: Claude <[email protected]>
@jstac
Copy link
Contributor Author

jstac commented Nov 5, 2025

Clarification: I switched the solver to VFI because it's simple and more familiar, so it won't act as a barrier to entry.

The HPI solver is now an exercise

…ic Programming book link

Replace the broken cross-reference to opt_savings_2 (which doesn't exist in this PR) with a direct link to the Dynamic Programming book at dp.quantecon.org where Howard policy iteration is discussed in detail.

This fixes the build warning:
aiyagari.md:689: WARNING: unknown document: 'opt_savings_2'

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-actions
Copy link

github-actions bot commented Nov 5, 2025

📖 Netlify Preview Ready!

Preview URL: https://pr-674--sunny-cactus-210e3e.netlify.app (341df07)

📚 Changed Lecture Pages: aiyagari, endogenous_lake

Updated the "Primitives and operators" section to correctly state that we solve the household problem using value function iteration (VFI), not Howard policy iteration (HPI). Removed the outdated reference to Ch 5 of Dynamic Programming book.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-actions
Copy link

github-actions bot commented Nov 5, 2025

📖 Netlify Preview Ready!

Preview URL: https://pr-674--sunny-cactus-210e3e.netlify.app (a3ccd27)

📚 Changed Lecture Pages: aiyagari, endogenous_lake

@jstac jstac merged commit 43a89ed into main Nov 5, 2025
1 check passed
@jstac jstac deleted the optimize-aiyagari-vfi branch November 5, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants