[newton_method] Exercise 2 solution stops after the first starting value

未关闭 适合新手
#1,115 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
markdown, python
领域
documentation

调研方向

阅读 lectures/newton_method.md 第 1082–1095 行附近:raises-exception 单元格中的循环及其后的一段。确认 #1073 是否已合入,以便多变量调用使用 newton_nd,然后将每次尝试包在 try/except 中,去掉标签,并改写文字,使第 2 次和第 3 次尝试被报告为收敛。还要将第 1061 行的容差修正为 $10^{-15}$ 或行内代码。当构建后的页面显示全部三次尝试、第 1 次尝试的失败以及匹配的文本时即完成。

由索引模型根据 Issue 内容生成。

描述

bug

While reviewing #1073 I noticed that the Exercise 2 solution only ever shows the first of its three starting values. The live page shows the same thing: the solution's output ends with the traceback from attempt 1.

What happens

The solution loops over the three initial price vectors in a cell tagged raises-exception (lines 1082–1090). Attempt 1, $p_0 = (5, 5, 5)$, fails as intended. Its first Newton step overshoots to $p \approx (-0.337, -0.337, -0.337)$, so $\sqrt{p}$ is NaN and newton raises "Solution not found with NaN generated". That exception ends the loop, so attempts 2 and 3 never run.

Here is what each starting value does when run on its own with the lecture's newton (tol=1e-15, max_iter=15):

Attempt Starting value Outcome
1 $(5, 5, 5)$ Fails at iteration 2, because the first step makes prices negative
2 $(1, 1, 1)$ Converges in 6 iterations to $(1.4974, 1.4974, 1.4974)$
3 $(4.5, 0.1, 4)$ Converges in 8 iterations to the same point

The text after the cell (lines 1091–1095) says it "may take a few initial guesses to achieve convergence", then checks "the second initial guess which converges". That reads as though the third guess failed too, but it converges. It just never runs.

Relation to #575

#575 asked about the exception in this cell. It was closed because the failure is deliberate: the exercise is meant to show that Newton's method can fail, and that should stay. This issue is narrower. The failure on attempt 1 shouldn't hide attempts 2 and 3. Making it "fail more gracefully" was offered as an option in #575.

Suggested change
  1. Catch the exception for each attempt, so all three run and the failure is still reported. Then drop the raises-exception tag:
for attempt, init in enumerate(initLs, start=1):
    print(f"Attempt {attempt}: Starting value is {init} \n")
    try:
        p = newton_nd(lambda p: e(p, A, b, c), init, tol=1e-15, max_iter=15)
    except Exception as err:
        print(f"Failed: {err}")
    print("-" * 64)

With the current IPython, %time passes the exception through, so it can stay inside the try if we want to keep the timings.

  1. Update the text after the cell to match. The first starting value fails because its first step takes prices negative, and the other two converge to the same equilibrium. The overshoot is also a useful teaching point about why Newton's method can fail.

  2. While there: line 1061 writes the tolerance as $1e-15$, which renders in math mode as "1e − 15". Use $10^{-15}$ or plain 1e-15 in backticks instead.

Timing

#1073 renames the multivariate newton to newton_nd in this cell (the snippet above already uses the new name), so this is easiest to do after #1073 merges.

主要语言
TeX
星标
129
派生
59
平均合并
2 天 10 小时
30 天内合并 PR
11

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

QuantEcon/lecture-python.myst 的其他 Issue

查看 QuantEcon/lecture-python.myst 的全部 Issue

相似的 Issue

更多 Documentation Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。