Repository navigation
[newton_method] Some updates - #1073
longye-tian wants to merge 7 commits into
Conversation
📖 Netlify Preview Ready!Preview URL: https://pr-1073--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
There was a problem hiding this comment.
🟢 Approval recommended
The changes are limited to one lecture file, and all code renames, the Jacobian correction, and parameter threading were traced and verified to be consistent and correct.
0 open findings
What changed in this PR
This PR revises the newton_method lecture to improve wording, notation, and code clarity/correctness in response to issue #1063. It fits into the QuantEcon intermediate Python lecture series as an educational content and code-quality improvement, with no structural or build changes.
Changes:
- Wording/notation polish: non-uniqueness phrasing ("a fixed point"/"a zero"), sentence-case headings,
:=definitions, explicit non-degeneracy conditions (1-g'(x_t)≠0,f'(x_t)≠0, nonsingular Jacobian), clearer per-sentence paragraphs, and corrected initial-vector notation ($k_0^{(i)}$ ,$p_0^{(i)}$ ). - Code correctness/clarity: fixed the hand-computed Jacobian to use full
(Ap)_irow products; computedk_starinsideplot_trajectories; parameterizedplot_excess_demandwithA, b, cviatwo_good_params; set the zero contour color to black and removed the misleading embedded title. - Renamed the multivariate Newton routine to
newton_nd(distinct from the scalarnewton) and updated all downstream calls; aligned "3,000 goods" prose with the code.
| File | Description |
|---|---|
| lectures/newton_method.md | Wording/notation refinements, a corrected hand-coded Jacobian, parameterized plotting, and a newton → newton_nd rename with consistent downstream call updates. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
mmcky
left a comment
There was a problem hiding this comment.
Thanks @longye-tian, this is a careful implementation of #1063: all 23 items are here and nothing else changed. I ran both this branch and main locally and compared every cell's output. Everything lines up except one spot, and that needs a small wording change before merge.
One change needed: the sentence after the corrected Jacobian
The corrected jacobian_e is right. It matches jax.jacobian(e) to machine precision at several test points, while the old version was off by about 0.05–0.18 per entry.
The problem is the sentence right after the root(..., jac=...) cell, at line 765: "Now the solution is even more accurate (although, in this low-dimensional problem, the difference is quite small)". With the correct Jacobian this is no longer true:
root(..., method="hybr") |
max abs residual | function evaluations |
|---|---|---|
| No Jacobian | 2.04e-13 | 12 |
Old (incorrect) Jacobian, on main |
1.11e-15 | 11 |
| Corrected Jacobian, this PR | 2.04e-13 | 10 |
The extra accuracy on main came from the bug. With the correct Jacobian the result is identical to the no-Jacobian case. The only gain is the two evaluations root no longer spends on a finite-difference approximation. Something along these lines would keep the text accurate; the wording is yours to choose, but I'd avoid quoting numbers because they can shift slightly between CPU and GPU builds:
The result is essentially unchanged: in this low-dimensional problem the finite-difference approximation that
rootuses by default already works well, so supplying the exact Jacobian mainly saves a few function evaluations.
(Line 765 is outside the diff, so I couldn't attach this as a suggestion on that line.)
Verified
-
newton→newton_nd: all seven multivariate call sites are updated. The scalarnewtonis still used once (line 472) before the multivariate definition, so nothing breaks. - The other places where the text describes output still match it:
- "5 iterations" and "terminates in 4 steps" are both correct.
- The Exercise 1 hint converges to
$(k^*, k^*, k^*)$ with$k^* \approx 1.78467$ . - "3,000 goods" matches
dim = 3000.
- The contour plots render cleanly with the black zero lines and no embedded titles.
Optional nits (non-blocking)
- The meshgrid comment now says
p_0andp_1, but the variables are stillP1, P2. Renaming them toP0, P1would make the two match. - In the combined plot both zero contours are black and labelled "0", so readers can't tell which curve is which. This isn't a regression, since both lines were the same colour before. Passing
fmt={0.0: f"$e_{good} = 0$"}toplt.clabelwould label them$e_0 = 0$ and$e_1 = 0$ . -
two_good_paramsmakes the three plot cells safe to re-run after the 3,000-good cell reassignsA, b, c. The two-goodrootandnewton_ndcells still read the globals, though. It's fine to leave this as is.
Out of scope, for reference
This one is already on main, so it doesn't belong in this PR. In the Exercise 2 solution, the loop over starting values raises on attempt 1 (NaN), and the cell is tagged raises-exception, so attempts 2 and 3 never display. Attempt 3,
|
@longye-tian let me know what you think of this Opus 5.5 review when you have time. |
|
Many thanks, @mmcky, for your review. It was really helpful in catching that the accuracy statement no longer held after correcting the Jacobian. I’ve updated the wording and made the two suggested changes to the meshgrid names and contour labels. I also noticed that the latest build failed. The Newton lecture executed successfully, but the PDF build reported an unresolved jax:wealth_dynamics reference in wealth_dynamics.md. The logs also show a separate execution warning for bayes_nonconj.md, although the traceback doesn’t appear to have been uploaded. I’ll address #1115 as a follow-up once this PR is merged, as you suggested. Best, |
Hi @jstac
This PR updates the lecture
newton_methodaccording to #1063Best,
Longye