Skip to content

Commit b7c6464

Browse files
jstacclaude
andauthored
Refactor optimal growth lectures into unified cake eating series (#679)
* Refactor optimal growth lectures into unified cake eating series This commit restructures and consolidates five lectures into a coherent cake eating series with consistent notation, modern Python patterns, and clearer pedagogical flow. ## File Changes ### Renamed Files - `cake_eating_problem.md` → `cake_eating.md` (Cake Eating I) - `optgrowth.md` → `cake_eating_stochastic.md` (Cake Eating III) - `coleman_policy_iter.md` → `cake_eating_time_iter.md` (Cake Eating IV) - `egm_policy_iter.md` → `cake_eating_egm.md` (Cake Eating V) ### Deleted Files - `optgrowth_fast.md` (content merged into Cake Eating III) ### Updated Files - `_toc.yml` - Updated all file references - `_static/lecture_specific/optgrowth/cd_analytical.py` - Changed variable names ## Major Changes ### 1. Consistent Notation (y → x) Changed state variable from `y` to `x` throughout all lectures to maintain consistency with Cake Eating I and II, which use `x` for cake size. ### 2. Reframed as Cake Eating Problem - Cake Eating III now explains the problem as a stochastic cake that regrows (like a harvest) when seeds are saved - Connected to stochastic growth theory without claiming to be a growth model - Updated all references from "optimal growth" to "cake eating" in text - Changed index entries and section headers accordingly ### 3. Modern Python with Type Hints Converted from traditional classes to typed NamedTuples: - `class Model(NamedTuple)` with full type annotations - `create_model()` factory function - Type hints on all functions using `Callable`, `np.ndarray`, etc. - Changed class methods to standalone functions ### 4. Consistent Naming - `OptimalGrowthModel` → `Model` - `og` → `model` (variable names) - All lectures now use the same model structure ### 5. Pedagogical Improvements **Cake Eating III (Stochastic Dynamics):** - Introduced as continuation of Cake Eating I and II - Uses harvest/regrowth metaphor for stochastic production - Maintained value function iteration approach **Cake Eating IV (Time Iteration):** - Clear introduction explaining time iteration concept - Explains it builds on Cake Eating III - Previews that Cake Eating V will be even more efficient - Defined model inline instead of loading external files **Cake Eating V (EGM):** - Builds naturally from Cake Eating IV - Shows efficiency gains from avoiding root-finding - Consistent model structure throughout ## Technical Details - All Python code uses consistent variable names (x instead of y) - Removed external file dependencies where possible - Inline function definitions for clarity - Updated cross-references between lectures - Preserved mathematical rigor while improving accessibility 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix cake eating lectures: inline external code and remove Numba decorators This commit fixes issues with the cake eating lecture series that prevented them from executing correctly after the conversion to NamedTuple with type hints. Changes made: 1. Inlined external code files into lectures: - Replaced :load: directives with actual code from analytical.py files - Inlined solve_model function from optgrowth/solve_model.py - Deleted now-unused external files 2. Fixed Numba incompatibility: - Removed @jit decorators from cake_eating_time_iter.md and cake_eating_egm.md - Switched from quantecon.optimize.brentq to scipy.optimize.brentq - Removed numba imports where no longer needed 3. Fixed function signatures: - Corrected parameter order in state_action_value() in cake_eating_stochastic.md - Removed [0] indexing from scipy's brentq (returns scalar, not tuple) 4. Added Python files for testing: - Generated .py files using jupytext for all cake eating lectures - Verified all lectures execute successfully without errors All five cake eating lectures now execute correctly as standalone Python scripts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Remove intermediate Python test files The .py files were used for testing lecture execution but should not be part of the repository. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix cross-references to renamed cake eating lectures Updated all doc references throughout the lecture series to reflect the renamed files from PR #679: - cake_eating_problem → cake_eating - optgrowth → cake_eating_stochastic - coleman_policy_iter → cake_eating_time_iter - egm_policy_iter → cake_eating_egm Also: - Removed references to the deleted optgrowth_fast lecture - Inlined solve_model_time_iter function in ifp.md - Updated terminology from "stochastic optimal growth model" to "cake eating model" in ifp.md This fixes the Jupyter Book build warnings about unknown documents. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Refactor cake_eating_numerical: Convert from OOP to functional style Modernized the cake eating numerical methods lecture by replacing class-based implementation with a functional programming approach using NamedTuple. Key changes: - Replaced `class CakeEating` with `Model = namedtuple('Model', ...)` - Created `create_cake_eating_model()` helper function for building instances - Converted class methods to standalone functions: - `u(c, γ)` for utility function - `u_prime(c, γ)` for utility derivative - `state_action_value(c, x, v_array, model)` for Bellman RHS - Updated Bellman operator `T(v, model)` to use functional style - Modified policy function `σ(model, v)` to accept model parameter - Refactored exercise solution from inheritance-based `class OptimalGrowth(CakeEating)` to `class ExtendedModel(NamedTuple)` with standalone functions - Updated all code examples throughout to use `model` parameter pattern - Generated and tested Python version using jupytext This aligns with the modern functional style used in cake_eating.md and reduces reliance on OOP patterns. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Use class-based NamedTuple for Model definition Updated Model definition to use modern class-based NamedTuple syntax with type annotations instead of collections.namedtuple, matching the style used for ExtendedModel in the exercise solution. Before: Model = namedtuple('Model', ('β', 'γ', 'x_grid')) After: class Model(NamedTuple): β: float γ: float x_grid: np.ndarray This provides better type hints and is more consistent with modern Python typing conventions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Remove test Python file The .py file was only for testing purposes and should not be tracked. Users can generate it locally using jupytext when needed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * misc * Add JAX-based cake eating EGM lecture and refine related lectures - Created new lecture cake_eating_egm_jax.md implementing EGM with JAX - Uses JAX's vmap for vectorization instead of for loops - JIT-compiled solver with jax.lax.while_loop - Global utility/production functions for JAX compatibility - Streamlined Model class to contain only arrays and scalars - Focuses on JAX implementation patterns, refers to cake_eating_egm for theory - Improved cake_eating_time_iter.md - Moved imports to top of file - Removed timing benchmarks, added clearer performance discussion - Explained why time iteration is faster (exploits differentiability/FOCs) - Referenced EGM as even faster variation - Enhanced cake_eating_egm.md - Removed default values from Model class (now only in create_model) - Aligned all comments in Model class definition - Replaced %%timeit with qe.Timer for consistency - Simplified timing discussion - Updated _toc.yml to include new lecture after cake_eating_egm 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Refine JAX EGM lecture: improve imports and add CRRA exercise - Moved all imports to top of file (NamedTuple with other imports) - Removed unused Callable import - Added block_until_ready() to timing for accurate JAX benchmarking - Improved error output formatting with print statement - Added comprehensive CRRA utility exercise demonstrating convergence Exercise improvements: - Uses correct CRRA form: u(c) = (c^(1-γ) - 1)/(1-γ) that converges to log - Focuses on γ values approaching 1 from above (1.05, 1.1, 1.2) - Plots γ=1 (log case) in black with clear labeling - Includes explanation of endogenous grid coverage differences - Shows numerical convergence with maximum deviation metrics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * misc --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent eca6a1f commit b7c6464

17 files changed

+1719
-1614
lines changed

lectures/_static/lecture_specific/cake_eating_numerical/analytical.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

lectures/_static/lecture_specific/optgrowth/cd_analytical.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

lectures/_static/lecture_specific/optgrowth/solve_model.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

lectures/_toc.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ parts:
7575
- caption: Household Problems
7676
numbered: true
7777
chapters:
78-
- file: cake_eating_problem
78+
- file: cake_eating
7979
- file: cake_eating_numerical
80-
- file: optgrowth
81-
- file: optgrowth_fast
82-
- file: coleman_policy_iter
83-
- file: egm_policy_iter
80+
- file: cake_eating_stochastic
81+
- file: cake_eating_time_iter
82+
- file: cake_eating_egm
83+
- file: cake_eating_egm_jax
8484
- file: ifp
8585
- file: ifp_advanced
8686
- caption: LQ Control
File renamed without changes.

0 commit comments

Comments
 (0)