-
-
Notifications
You must be signed in to change notification settings - Fork 53
Refactor optimal growth lectures into unified cake eating series #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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>
Resolved conflict in _toc.yml by: - Accepting the new "Optimal Growth" section from main - Using renamed file "cake_eating" instead of "cake_eating_problem" - Keeping Household Problems section with only cake eating lectures The main branch had moved Cass-Koopmans lectures to a separate "Optimal Growth" section, which is preserved in this merge. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Merge Conflict ResolvedThe conflict in Resolution details:
The PR is now mergeable and ready for review. |
…ators 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>
Code Verification and Bug FixesVerified all five cake eating lectures execute correctly by converting them to Python with jupytext and running them as standalone scripts. Fixed several issues discovered during testing: Issues Fixed
VerificationAll lectures now execute successfully:
Generated .py files are included in the commit for easy verification. |
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>
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>
Fixed Jupyter Book Build ErrorsFixed all cross-reference warnings that were causing the Jupyter Book build to fail. Changes Made1. Updated cross-references to renamed lectures:
2. Removed references to deleted lecture:
3. Inlined external code:
4. Updated terminology:
All 13 build warnings about unknown documents have been resolved. The book should now build successfully. |
|
@mmcky Just FYI I'm removing quite a few old "load" directives and inlining code. The reason is that I really need to be able to easily generate excutable code via |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (11e5850) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_numerical, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
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>
Refactored cake_eating_numerical to functional styleI've updated Main ChangesBefore (OOP style): class CakeEating:
def __init__(self, β=0.96, γ=1.5, ...):
self.β, self.γ = β, γ
self.x_grid = np.linspace(...)
def u(self, c):
return (c ** (1 - self.γ)) / (1 - self.γ)
def state_action_value(self, c, x, v_array):
...
ce = CakeEating()
v = T(v, ce)After (Functional style): Model = namedtuple('Model', ('β', 'γ', 'x_grid'))
def create_cake_eating_model(β=0.96, γ=1.5, ...):
x_grid = np.linspace(...)
return Model(β=β, γ=γ, x_grid=x_grid)
def u(c, γ):
return (c ** (1 - γ)) / (1 - γ)
def state_action_value(c, x, v_array, model):
...
model = create_cake_eating_model()
v = T(v, model)Exercise SolutionAlso refactored the exercise from inheritance-based Testing✅ Generated Python version with This reduces OOP complexity and makes the code more maintainable and easier to understand. |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (e5c45f4) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_numerical, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
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>
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>
|
Updated Model definition to use modern class-based Also removed the test Python file - it can be generated locally with jupytext when needed. |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (c916218) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_numerical, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (8f5d9bf) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_numerical, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
Roger that @jstac, I might take a note of which ones. You prefer the jupytext --to py workflow more than |
|
@jstac this re-organization looks great. The new structure makes a lot of sense. |
|
Thanks @mmcky
Yeah, i do, tbh, at least for intermediate steps. And Claude seems to like interacting directly with a py file. |
- 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>
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (10343f6) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_egm_jax, cake_eating_numerical, cake_eating_time_iter, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
- 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>
Latest Updates: JAX Lecture RefinementsJust pushed improvements to the new JAX EGM lecture ( Code Quality Improvements
New CRRA Utility ExerciseAdded a comprehensive exercise demonstrating convergence of CRRA utility to log utility: Key features:
Results demonstrate:
The monotonically increasing differences clearly show convergence to the log utility case as The exercise provides students with hands-on experience adapting the JAX code to different utility functions while understanding the mathematical relationship between CRRA and log utility. |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (6144e81) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_egm_jax, cake_eating_numerical, cake_eating_time_iter, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
|
@mmcky I'm going to go ahead and merge this, since it touches the _toc and a lot of other files. I've checked it pretty carefully and I think it's all in good shape. |
|
thanks @jstac -- this is a really nice change. |
|
Thanks @mmcky . when I worked through it all i realized what a mess it had become over time. disconnected logic, poor flow, repeated ideas, same ideas different names. it feels good to be starting to get it all back in shape again. |
|
@jstac do you want me to make this live? |
|
please hold off for now thanks @mmcky . there's a problem with the titles of the job search lectures that i'm fixing now. |
|
thanks @jstac -- I'll stop asking, just ping me when you need a release 😄 Let me know if you need a proofread. Happy to do so. I'm ducking out now but can take a look later today if you'd like. |
|
📖 Netlify Preview Ready! Preview URL: https://pr-679--sunny-cactus-210e3e.netlify.app (2016211) 📚 Changed Lecture Pages: cake_eating_egm, cake_eating_egm_jax, cake_eating_numerical, cake_eating_time_iter, ifp, ifp_advanced, lqcontrol, wald_friedman_2 |
Summary
This PR restructures five lectures into a coherent cake eating series with consistent notation, modern Python patterns, and improved pedagogical flow. The series now progresses logically from deterministic to stochastic problems, introducing increasingly sophisticated solution methods, culminating in a JAX-accelerated implementation.
Lecture Series Structure
cake_eating.md) - Introduction to Optimal Savingcake_eating_numerical.md) - Numerical Methodscake_eating_stochastic.md) - Stochastic Dynamics ✨ New framingcake_eating_time_iter.md) - Time Iteration ✨ Renamedcake_eating_egm.md) - The Endogenous Grid Method ✨ Renamedcake_eating_egm_jax.md) - EGM with JAX ✨ NewKey Changes
🚀 New JAX Implementation (Cake Eating VI)
Added a new lecture demonstrating high-performance implementation using JAX:
Key JAX features demonstrated:
vmap: Replaces for loops with efficient vectorized operations@jax.jitdecorator for compiled performancejax.lax.while_loopinstead of Python loopsImplementation highlights:
The lecture focuses on JAX implementation patterns while referring readers to Cake Eating V for algorithmic details.
🔤 Consistent Notation Throughout
Changed state variable from
y→xin all lectures (III, IV, V) to match the notation in Cake Eating I and II. This includes:🎯 Reframed as Cake Eating Problem
Cake Eating III now clearly presents the problem as stochastic cake eating:
🐍 Modern Python with Type Hints
Converted from traditional classes to typed
NamedTuple:All functions now have full type annotations.
📚 Improved Pedagogical Flow
Cake Eating III:
Cake Eating IV:
qe.Timerfor timingCake Eating V:
create_model)qe.Timerfor consistencyCake Eating VI:
🏗️ Structural Improvements
OptimalGrowthModel→Model,og→modelFile Changes
New
cake_eating_egm_jax.md- JAX implementation of EGMRenamed
cake_eating_problem.md→cake_eating.mdoptgrowth.md→cake_eating_stochastic.mdcoleman_policy_iter.md→cake_eating_time_iter.mdegm_policy_iter.md→cake_eating_egm.mdDeleted
optgrowth_fast.md(removed; content was redundant)Modified
_toc.yml- Updated all file references and added new lecture_static/lecture_specific/optgrowth/cd_analytical.py- Updated variable namesBenefits
Testing
_toc.ymlxinstead ofy) throughoutNotes
The lectures maintain mathematical rigor while improving accessibility. The cake eating framing makes the economic intuition clearer, especially for students new to dynamic programming. The new JAX lecture provides a bridge to modern high-performance computing for students ready to scale up their implementations.
🤖 Generated with Claude Code