Commit ed8e89a
Unify K operator signature in cake_eating_egm_jax with cake_eating_egm (#732)
* Unify K operator signature in cake_eating_egm_jax with cake_eating_egm
Update the Coleman-Reffett operator K and solver functions in the JAX
implementation to match the signature from the NumPy version:
- K now takes (c_in, x_in, model) and returns (c_out, x_out)
- solve_model_time_iter now takes (c_init, x_init) and returns (c, x)
- Applied same changes to K_crra and solve_model_crra in exercises
The efficient JAX implementation is fully preserved:
- Vectorization with vmap
- JIT compilation
- Use of jax.lax.while_loop
Tested successfully with maximum deviation of 1.43e-06 from analytical
solution and execution time of ~0.009 seconds.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Remove unnecessary global alpha variable
The α parameter is already defined with a default value (0.4) in the
create_model function, so there's no need to set it as a global variable
and pass it explicitly.
Simplified:
- model = create_model() instead of α = 0.4; model = create_model(α=α)
- model_crra = create_model() in the CRRA exercise section
Tested successfully with same results as before.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Remove redundant alpha parameter in cake_eating_egm
The α parameter doesn't need to be passed explicitly to create_model
since it already has a default value of 0.4. The α = 0.4 line is still
needed for the lambda function closures (f and f_prime capture it).
Changed:
- create_model(u=u, f=f, α=α, ...)
+ create_model(u=u, f=f, ...)
Tested successfully with same convergence behavior.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Refactor f and f_prime to use explicit alpha parameter
Changed from closure-based approach to explicit parameter passing:
- f = lambda k, α: k**α (instead of f = lambda k: k**α with global α)
- f_prime = lambda k, α: α * k**(α - 1)
- Updated K operator to call f(s, α) and f_prime(s, α)
This makes the NumPy version consistent with the JAX implementation and
ensures the α stored in the Model is actually used in the K operator
(previously it was unpacked but unused).
Benefits:
- Consistency between NumPy and JAX versions
- Clearer function dependencies (α is an explicit parameter)
- Actually uses model.α instead of relying on closure
Tested successfully with same convergence behavior.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Rename grid to s_grid for consistency with NumPy version
Changed all references from 'grid' to 's_grid' to match the NumPy
implementation and clarify that this is the exogenous savings grid:
- Model.grid → Model.s_grid
- Updated comment: "state grid" → "exogenous savings grid"
- Updated all variable names throughout (K, K_crra, initializations)
- Also renamed loop variable from 'k' to 's' for consistency
This makes the JAX version consistent with the NumPy version's naming
conventions and makes it clearer that we're working with the exogenous
grid for savings (not the endogenous grid for wealth x).
Tested successfully with identical results.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Fix remaining k references to use s for savings
Changed remaining instances where k was used instead of s:
- Mathematical notation: x = k + σ(k) → x = s + σ(s)
- Added missing inline comment in K_crra: x_i = s_i + c_i
This completes the transition to using 's' for savings throughout,
maintaining consistency with the exogenous savings grid terminology.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c1d524a commit ed8e89a
2 files changed
+78
-66
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
| 246 | + | |
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | | - | |
270 | 269 | | |
271 | 270 | | |
272 | 271 | | |
273 | | - | |
274 | | - | |
| 272 | + | |
| 273 | + | |
275 | 274 | | |
276 | | - | |
| 275 | + | |
277 | 276 | | |
278 | 277 | | |
279 | 278 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
105 | | - | |
| 104 | + | |
| 105 | + | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
120 | 124 | | |
121 | 125 | | |
122 | 126 | | |
123 | 127 | | |
124 | 128 | | |
125 | 129 | | |
126 | 130 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 131 | + | |
131 | 132 | | |
132 | 133 | | |
133 | | - | |
| 134 | + | |
134 | 135 | | |
135 | 136 | | |
136 | | - | |
137 | | - | |
| 137 | + | |
| 138 | + | |
138 | 139 | | |
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
142 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
143 | 147 | | |
144 | | - | |
| 148 | + | |
145 | 149 | | |
146 | 150 | | |
147 | 151 | | |
| |||
160 | 164 | | |
161 | 165 | | |
162 | 166 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
| 167 | + | |
| 168 | + | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
171 | 173 | | |
172 | 174 | | |
173 | 175 | | |
174 | | - | |
| 176 | + | |
| 177 | + | |
175 | 178 | | |
176 | | - | |
| 179 | + | |
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
181 | 184 | | |
182 | | - | |
| 185 | + | |
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
190 | 193 | | |
191 | 194 | | |
192 | | - | |
| 195 | + | |
193 | 196 | | |
194 | 197 | | |
195 | | - | |
| 198 | + | |
196 | 199 | | |
197 | | - | |
| 200 | + | |
198 | 201 | | |
199 | 202 | | |
200 | 203 | | |
201 | 204 | | |
202 | 205 | | |
203 | | - | |
204 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
205 | 209 | | |
206 | 210 | | |
207 | 211 | | |
208 | 212 | | |
209 | 213 | | |
210 | | - | |
211 | | - | |
212 | 214 | | |
213 | 215 | | |
214 | | - | |
| 216 | + | |
215 | 217 | | |
216 | 218 | | |
217 | 219 | | |
| |||
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
227 | | - | |
| 229 | + | |
228 | 230 | | |
229 | 231 | | |
230 | 232 | | |
231 | 233 | | |
232 | 234 | | |
233 | 235 | | |
234 | 236 | | |
235 | | - | |
| 237 | + | |
| 238 | + | |
236 | 239 | | |
237 | 240 | | |
238 | 241 | | |
| |||
282 | 285 | | |
283 | 286 | | |
284 | 287 | | |
285 | | - | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
286 | 294 | | |
287 | 295 | | |
288 | 296 | | |
289 | 297 | | |
290 | 298 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 299 | + | |
295 | 300 | | |
296 | 301 | | |
297 | | - | |
| 302 | + | |
298 | 303 | | |
299 | 304 | | |
300 | | - | |
301 | | - | |
| 305 | + | |
| 306 | + | |
302 | 307 | | |
303 | 308 | | |
304 | 309 | | |
305 | 310 | | |
306 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
307 | 315 | | |
308 | | - | |
| 316 | + | |
309 | 317 | | |
310 | 318 | | |
311 | 319 | | |
312 | 320 | | |
313 | 321 | | |
314 | 322 | | |
315 | 323 | | |
316 | | - | |
| 324 | + | |
| 325 | + | |
317 | 326 | | |
318 | 327 | | |
319 | | - | |
| 328 | + | |
320 | 329 | | |
321 | 330 | | |
322 | 331 | | |
323 | 332 | | |
324 | 333 | | |
325 | | - | |
| 334 | + | |
326 | 335 | | |
327 | 336 | | |
328 | 337 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
333 | 342 | | |
334 | 343 | | |
335 | | - | |
| 344 | + | |
336 | 345 | | |
337 | 346 | | |
338 | | - | |
| 347 | + | |
339 | 348 | | |
340 | | - | |
| 349 | + | |
341 | 350 | | |
342 | 351 | | |
343 | 352 | | |
344 | 353 | | |
345 | 354 | | |
346 | 355 | | |
347 | 356 | | |
| 357 | + | |
348 | 358 | | |
349 | | - | |
| 359 | + | |
350 | 360 | | |
351 | 361 | | |
352 | | - | |
353 | | - | |
354 | | - | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
355 | 368 | | |
356 | 369 | | |
357 | 370 | | |
| |||
361 | 374 | | |
362 | 375 | | |
363 | 376 | | |
364 | | - | |
| 377 | + | |
365 | 378 | | |
366 | 379 | | |
367 | 380 | | |
| |||
377 | 390 | | |
378 | 391 | | |
379 | 392 | | |
380 | | - | |
| 393 | + | |
381 | 394 | | |
382 | 395 | | |
383 | 396 | | |
| |||
0 commit comments