Skip to content

[newton_method] Some updates - #1073

Open
longye-tian wants to merge 7 commits into
mainfrom
newton_update
Open

longye-tian wants to merge 7 commits into
mainfrom
newton_update

Conversation

@longye-tian

@longye-tian longye-tian commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Hi @jstac
This PR updates the lecture newton_method according to #1063

Best,
Longye

@longye-tian longye-tian self-assigned this Sep 27, 2026
@longye-tian longye-tian added enhancement Improvement to existing material do-not-merge Approved-looking but must not be merged yet (hold / pin) labels Sep 27, 2026
@longye-tian longye-tian linked an issue Sep 27, 2026 that may be closed by this pull request
23 tasks done
@github-actions

github-actions Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

@longye-tian longye-tian removed the do-not-merge Approved-looking but must not be merged yet (hold / pin) label Sep 27, 2026
@longye-tian
longye-tian marked this pull request as ready for review September 28, 2026 00:29
@mmcky
mmcky requested a balanced review from Copilot October 8, 2026 23:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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)_i row products; computed k_star inside plot_trajectories; parameterized plot_excess_demand with A, b, c via two_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 scalar newton) 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 mmcky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 root uses 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 scalar newton is 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_0 and p_1, but the variables are still P1, P2. Renaming them to P0, P1 would 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$"} to plt.clabel would label them $e_0 = 0$ and $e_1 = 0$.
  • two_good_params makes the three plot cells safe to re-run after the 3,000-good cell reassigns A, b, c. The two-good root and newton_nd cells 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, $(4.5, 0.1, 4)$, actually converges in 8 iterations. The surrounding text implies that only the second guess converges.

@mmcky

mmcky commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@longye-tian let me know what you think of this Opus 5.5 review when you have time.

@longye-tian

Copy link
Copy Markdown
Contributor Author

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,
Longye

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing material

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[newton_method] Some suggestions on code and wording

3 participants