From cedcbe2b39d2da6b77307e9a0ce780ad9c1f87e4 Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Thu, 3 Dec 2026 12:08:16 +1100 Subject: [PATCH 1/2] Update translation: lectures/newton_method.md --- lectures/newton_method.md | 142 +++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 54 deletions(-) diff --git a/lectures/newton_method.md b/lectures/newton_method.md index 2336dac4..16cb7087 100644 --- a/lectures/newton_method.md +++ b/lectures/newton_method.md @@ -18,15 +18,15 @@ translation: Fixed point computation using Newton's method::Implementation: 实现 Fixed point computation using Newton's method::Implementation::Successive approximation: 连续近似法 Fixed point computation using Newton's method::Implementation::Newton's method: 牛顿法 - Root-Finding in one dimension: 一维求根 - Root-Finding in one dimension::Newton's method for zeros: 牛顿法求零点 - Root-Finding in one dimension::Application to finding fixed points: 在寻找不动点中的应用 + Root-finding in one dimension: 一维求根 + Root-finding in one dimension::Newton's method for zeros: 牛顿法求零点 + Root-finding in one dimension::Application to finding fixed points: 在寻找不动点中的应用 Multivariate Newton's method: 多元牛顿法 - Multivariate Newton's method::A two-goods market equilibrium: 双商品市场均衡 - Multivariate Newton's method::A two-goods market equilibrium::A graphical exploration: 图形化探索 - Multivariate Newton's method::A two-goods market equilibrium::Using a multidimensional root finder: 使用多维根查找器 - Multivariate Newton's method::A two-goods market equilibrium::Adding gradient information: 添加梯度信息 - Multivariate Newton's method::A two-goods market equilibrium::Using Newton's method: 使用牛顿法 + Multivariate Newton's method::A two-good market equilibrium: 双商品市场均衡 + Multivariate Newton's method::A two-good market equilibrium::A graphical exploration: 图形化探索 + Multivariate Newton's method::A two-good market equilibrium::Using a multidimensional root finder: 使用多维根查找器 + Multivariate Newton's method::A two-good market equilibrium::Adding gradient information: 添加梯度信息 + Multivariate Newton's method::A two-good market equilibrium::Using Newton's method: 使用牛顿法 Multivariate Newton's method::A high-dimensional problem: 高维问题 Exercises: 练习 --- @@ -75,9 +75,9 @@ translation: 本讲将在一维和多维环境中应用牛顿法来解决不动点和零点计算问题。 -* 在寻找函数 $f$ 的不动点时,牛顿法通过求解对函数 $f$ 的线性近似的不动点,来更新已有的不动点猜测值。 +* 在寻找函数 $f$ 的某个不动点时,牛顿法通过求解对函数 $f$ 的线性近似的不动点,来更新已有的不动点猜测值。 -* 在寻找函数 $f$ 的零点时,牛顿法通过求解对函数 $f$ 的线性近似的零点,来更新已有的猜测值。 +* 在寻找函数 $f$ 的某个零点时,牛顿法通过求解对函数 $f$ 的线性近似的零点,来更新已有的猜测值。 为了建立直观认识,我们首先考虑一个简单的一维不动点问题,其中我们已知解,并使用连续近似和牛顿法来求解。 @@ -123,7 +123,7 @@ jax.config.update("jax_enable_x64", True) - $k_t$ 是人均资本存量 - $A, \alpha>0$ 是生产参数,$\alpha<1$ -- $s>0$ 是储蓄率 +- $s \in (0,1)$ 是储蓄率 - $\delta \in(0,1)$ 是折旧率 在这个例子中,我们希望计算资本运动规律$g$的唯一严格正不动点。 @@ -282,7 +282,7 @@ k_star ```{math} :label: motivation -\hat g(x) \approx g(x_0) + g'(x_0)(x - x_0) +\hat g(x) := g(x_0) + g'(x_0)(x - x_0) ``` 我们通过计算满足以下等式的$x_1$来求解$\hat g$的不动点: @@ -300,6 +300,8 @@ x_{t+1} = \frac{g(x_t) - g'(x_t) x_t}{ 1 - g'(x_t) }, \quad x_0 \text{ 给定} ``` +这个更新要求 $1 - g'(x_t) \neq 0$。 + 要实现牛顿法,我们观察到资本运动定律[](motion_law)的导数为: ```{math} @@ -335,6 +337,7 @@ def plot_trajectories( fs=14, # 字体大小 ): + k_star = exact_fixed_point(params) fig, axes = plt.subplots(2, 1, figsize=(10, 6)) ax1, ax2 = axes @@ -378,14 +381,14 @@ plot_trajectories(params) ### 牛顿法求零点 -假设我们想要找到一个 $x$ 使得对某个光滑函数 $f$ (从实数映射到实数)有 $f(x)=0$。 +假设我们想要找到一个 $x$ 使得对某个光滑函数 $f$(从实数映射到实数)有 $f(x)=0$。 假设我们有一个猜测值 $x_0$ 并且想要将其更新为新的点 $x_1$。 作为第一步,我们取 $f$ 在 $x_0$ 处的一阶近似: $$ -\hat f(x) \approx f\left(x_0\right)+f^{\prime}\left(x_0\right)\left(x-x_0\right) +\hat f(x) := f\left(x_0\right)+f^{\prime}\left(x_0\right)\left(x-x_0\right) $$ 现在我们求解 $\hat f$ 的零点。 @@ -405,6 +408,8 @@ x_{t+1} = x_t - \frac{ f(x_t) }{ f'(x_t) }, \quad x_0 \text{ 给定} ``` +每次更新都要求 $f'(x_t) \neq 0$。 + 以下代码实现了迭代公式 [](oneD-newton) (first_newton_attempt)= @@ -432,17 +437,17 @@ def newton(f, x_0, tol=1e-7, max_iter=100_000): return x.item() ``` -许多库都实现了一维牛顿法,包括SciPy,所以这里的代码仅作说明用途。 +许多库都实现了一维牛顿法,包括 SciPy,所以这里的代码仅作说明用途。 -(话虽如此,当我们想要使用自动微分或GPU加速等技术来应用牛顿法时,了解如何自己实现牛顿法会很有帮助。) +(话虽如此,当我们想要使用自动微分或 GPU 加速等技术来应用牛顿法时,了解如何自己实现牛顿法会很有帮助。) ### 在寻找不动点中的应用 -现在再次考虑索洛不动点计算,我们要求解满足$g(k) = k$的$k$值。 +现在再次考虑索洛不动点计算,我们要求解满足 $g(k) = k$ 的 $k$ 值。 -我们可以通过设定$f(x) := g(x)-x$将其转换为零点寻找问题。 +我们可以通过设定 $f(x) := g(x)-x$ 将其转换为零点寻找问题。 -显然,$f$的任何零点都是$g$的不动点。 +显然,$f$ 的任何零点都是 $g$ 的不动点。 让我们将这个想法应用到索洛问题中 @@ -455,13 +460,13 @@ k_star_approx_newton = newton(f = lambda x: g(x, params) - x, x_0=0.8) k_star_approx_newton ``` -结果证实了我们在上面图表中看到的收敛情况:仅需5次迭代就达到了非常精确的结果。 +结果证实了我们在上面图表中看到的收敛情况:仅需 5 次迭代就达到了非常精确的结果。 ## 多元牛顿法 在本节中,我们将介绍一个双商品问题,可视化问题,并使用`SciPy`中的零点查找器和牛顿法来求解这个双商品市场的均衡。 -然后,我们将这个概念扩展到一个包含5,000种商品的更大市场,并再次比较这两种方法的性能。 +然后,我们将这个概念扩展到一个包含3,000种商品的更大市场,并再次比较这两种方法的性能。 我们将看到使用牛顿法时能获得显著的性能提升。 @@ -487,7 +492,9 @@ $$ 这里的$c_i$、$b_i$和$a_{ij}$都是参数。 -例如,这两种商品可能是通常一起使用的计算机组件,在这种情况下它们是互补品。因此需求取决于两种组件的价格。 +例如,这两种商品可能是通常一起使用的计算机组件,在这种情况下它们是互补品。 + +因此需求取决于两种组件的价格。 超额需求函数为, @@ -564,6 +571,7 @@ $$ A = jnp.array([[0.5, 0.4], [0.8, 0.2]]) b = jnp.ones(2) c = jnp.ones(2) +two_good_params = (A, b, c) ``` 在价格水平 $p = (1, 0.5)$ 时,超额需求为 @@ -578,7 +586,9 @@ print( ) ``` -为了提高计算效率,我们将使用 [`jax.vmap`](https://docs.jax.dev/en/latest/_autosummary/jax.vmap.html) 提供的向量化能力。这比使用python循环快得多。 +为了提高计算效率,我们将使用 [`jax.vmap`](https://docs.jax.dev/en/latest/_autosummary/jax.vmap.html) 提供的向量化能力。 + +这比使用Python循环快得多。 ```{code-cell} ipython3 # 在p的第一个轴上创建向量化。 @@ -593,14 +603,16 @@ e_vectorized = jax.vmap(e_vectorized_p_1, in_axes=(0, None, None, None)) 我们将使用以下函数来构建等高线图 ```{code-cell} ipython3 -def plot_excess_demand(ax, good=0, grid_size=100, grid_max=4, surface=True): +def plot_excess_demand( + ax, A, b, c, good=0, grid_size=100, grid_max=4, surface=True +): p_grid = jnp.linspace(0, grid_max, grid_size) - # 为p_1和p_2的所有组合创建网格 - P1, P2 = jnp.meshgrid(p_grid, p_grid, indexing="ij") + # 为p_0和p_1的所有组合创建网格 + P0, P1 = jnp.meshgrid(p_grid, p_grid, indexing="ij") # 堆叠成形状为(grid_size, grid_size, 2)的数组 - P = jnp.stack([P1, P2], axis=-1) + P = jnp.stack([P0, P1], axis=-1) # 使用向量化函数一次性计算所有值 z_full = e_vectorized(P, A, b, c) @@ -610,18 +622,17 @@ def plot_excess_demand(ax, good=0, grid_size=100, grid_max=4, surface=True): cs1 = ax.contourf(p_grid, p_grid, z.T, alpha=0.5) plt.colorbar(cs1, ax=ax, format="%.6f") - ctr1 = ax.contour(p_grid, p_grid, z.T, levels=[0.0]) + ctr1 = ax.contour(p_grid, p_grid, z.T, levels=[0.0], colors="black") ax.set_xlabel("$p_0$") ax.set_ylabel("$p_1$") - ax.set_title(f'超额需求函数 {good}') - plt.clabel(ctr1, inline=1, fontsize=13) + plt.clabel(ctr1, inline=1, fontsize=13, fmt={0.0: f"$e_{good} = 0$"}) ``` 这是 $e_0$ 的图 ```{code-cell} ipython3 fig, ax = plt.subplots() -plot_excess_demand(ax, good=0) +plot_excess_demand(ax, *two_good_params, good=0) plt.show() ``` @@ -629,7 +640,7 @@ plt.show() ```{code-cell} ipython3 fig, ax = plt.subplots() -plot_excess_demand(ax, good=1) +plot_excess_demand(ax, *two_good_params, good=1) plt.show() ``` @@ -642,7 +653,7 @@ plt.show() ```{code-cell} ipython3 fig, ax = plt.subplots(figsize=(10, 5.7)) for good in (0, 1): - plot_excess_demand(ax, good=good, surface=False) + plot_excess_demand(ax, *two_good_params, good=good, surface=False) plt.show() ``` @@ -672,7 +683,9 @@ p = solution.x p ``` -这个结果看起来和我们从图中观察到的猜测很接近。我们可以把它代回到 $e$ 中验证 $e(p) \approx 0$ +这个结果看起来和我们从图中观察到的猜测很接近。 + +我们可以把它代回到 $e$ 中验证 $e(p) \approx 0$ ```{code-cell} ipython3 e_p = jnp.max(jnp.abs(e(p, A, b, c))) @@ -681,6 +694,12 @@ e_p.item() 这确实是一个很小的误差。 +下面是求解器计算$e$的次数: + +```{code-cell} ipython3 +solution.nfev +``` + #### 添加梯度信息 在许多情况下,对于应用于光滑函数的零点查找算法,提供函数的[雅可比矩阵](https://baike.baidu.com/item/%E9%9B%85%E5%8F%AF%E6%AF%94%E7%9F%A9%E9%98%B5?fromModule=lemma_search-box)可以带来更好的收敛性质。 @@ -700,10 +719,15 @@ def jacobian_e(p, A, b, c): p_0, p_1 = p a_00, a_01 = A[0, :] a_10, a_11 = A[1, :] - j_00 = -a_00 * jnp.exp(-a_00 * p_0) - (b[0] / 2) * p_0 ** (-1 / 2) - j_01 = -a_01 * jnp.exp(-a_01 * p_1) - j_10 = -a_10 * jnp.exp(-a_10 * p_0) - j_11 = -a_11 * jnp.exp(-a_11 * p_1) - (b[1] / 2) * p_1 ** (-1 / 2) + + exp_0 = jnp.exp(-(a_00 * p_0 + a_01 * p_1)) + exp_1 = jnp.exp(-(a_10 * p_0 + a_11 * p_1)) + + j_00 = -a_00 * exp_0 - (b[0] / 2) * p_0 ** (-1 / 2) + j_01 = -a_01 * exp_0 + j_10 = -a_10 * exp_1 + j_11 = -a_11 * exp_1 - (b[1] / 2) * p_1 ** (-1 / 2) + J = [[j_00, j_01], [j_10, j_11]] return jnp.array(J) ``` @@ -718,7 +742,7 @@ solution = root( ) ``` -现在的解更加精确了(尽管在这个低维问题中,差异非常小): +结果基本没有变化:在这个低维示例中,默认的有限差分近似已经表现得很好。 ```{code-cell} ipython3 p = solution.x @@ -726,6 +750,12 @@ e_p = jnp.max(jnp.abs(e(p, A, b, c))) e_p.item() ``` +提供雅可比矩阵确实减少了函数计算的次数,因为求解器不再需要额外计算$e$来用有限差分近似雅可比矩阵: + +```{code-cell} ipython3 +solution.nfev +``` + #### 使用牛顿法 现在让我们使用牛顿法来计算均衡价格,采用多变量版本的牛顿法 @@ -736,6 +766,8 @@ e_p.item() p_{n+1} = p_n - J_e(p_n)^{-1} e(p_n) ``` +这一更新步骤要求$J_e(p_n)$是非奇异的。 + 这是[](oneD-newton)的多变量版本 (这里的$J_e(p_n)$是在$p_n$处计算的$e$的雅可比矩阵。) @@ -747,7 +779,7 @@ p_{n+1} = p_n - J_e(p_n)^{-1} e(p_n) 只需稍作修改,我们就可以将[我们之前的尝试](first_newton_attempt)推广到多维问题 ```{code-cell} ipython3 -def newton(f, x_0, tol=1e-5, max_iter=10): +def newton_nd(f, x_0, tol=1e-5, max_iter=10): x = x_0 f_jac = jax.jacobian(f) @@ -775,7 +807,7 @@ def newton(f, x_0, tol=1e-5, max_iter=10): ```{code-cell} ipython3 %%time -p = newton(lambda p: e(p, A, b, c), init_p) +p = newton_nd(lambda p: e(p, A, b, c), init_p) ``` ```{code-cell} ipython3 @@ -817,7 +849,7 @@ init_p = jnp.ones(dim) ```{code-cell} ipython3 %%time -p = newton(lambda p: e(p, A, b, c), init_p) +p = newton_nd(lambda p: e(p, A, b, c), init_p) ``` ```{code-cell} ipython3 @@ -875,9 +907,9 @@ $$ $$ \begin{aligned} - k1_{0} &= (1, 1, 1) \\ - k2_{0} &= (3, 5, 5) \\ - k3_{0} &= (50, 50, 50) + k_0^{(1)} &= (1, 1, 1) \\ + k_0^{(2)} &= (3, 5, 5) \\ + k_0^{(3)} &= (50, 50, 50) \end{aligned} $$ @@ -902,7 +934,7 @@ A = \begin{bmatrix} k_0 = (1, 1, 1) ``` -结果应该收敛到[解析解](solved_k)。 +结果应该收敛到 $(k^*, k^*, k^*)^\top$,其中 $k^*$ 表示标量[解析解](solved_k)。 ```` ```{exercise-end} @@ -939,7 +971,7 @@ def multivariate_solow(k, A=A, s=s, α=α, δ=δ): attempt = 1 for init in initLs: print(f'Attempt {attempt}: Starting value is {init} \n') - %time k = newton(lambda k: multivariate_solow(k) - k, \ + %time k = newton_nd(lambda k: multivariate_solow(k) - k, \ init) print('-'*64) attempt += 1 @@ -972,7 +1004,7 @@ init = jnp.repeat(1.0, 3) ```{code-cell} ipython3 %%time -k = newton(lambda k: multivariate_solow(k, A=A, s=s, α=α, δ=δ) - k, init) +k = newton_nd(lambda k: multivariate_solow(k, A=A, s=s, α=α, δ=δ) - k, init) ``` 结果与真实值非常接近,但仍有细微差异。 @@ -980,7 +1012,7 @@ k = newton(lambda k: multivariate_solow(k, A=A, s=s, α=α, δ=δ) - k, init) ```{code-cell} ipython3 %%time -k = newton( +k = newton_nd( lambda k: multivariate_solow(k, A=A, s=s, α=α, δ=δ) - k, init, tol=1e-7 ) ``` @@ -1023,9 +1055,9 @@ $$ $$ \begin{aligned} - p1_{0} &= (5, 5, 5) \\ - p2_{0} &= (1, 1, 1) \\ - p3_{0} &= (4.5, 0.1, 4) + p_0^{(1)} &= (5, 5, 5) \\ + p_0^{(2)} &= (1, 1, 1) \\ + p_0^{(3)} &= (4.5, 0.1, 4) \end{aligned} $$ @@ -1055,7 +1087,7 @@ initLs = [jnp.repeat(5.0, 3), jnp.ones(3), jnp.array([4.5, 0.1, 4.0])] for attempt, init in enumerate(initLs, start=1): print(f"Attempt {attempt}: Starting value is {init} \n") - %time p = newton(lambda p: e(p, A, b, c), init, tol=1e-15, max_iter=15) + %time p = newton_nd(lambda p: e(p, A, b, c), init, tol=1e-15, max_iter=15) print("-" * 64) ``` @@ -1066,7 +1098,9 @@ for attempt, init in enumerate(initLs, start=1): 将结果代回公式中检验我们的结果,使用收敛的第二个初始猜测值 ```{code-cell} ipython3 -p_solution = newton(lambda p: e(p, A, b, c), initLs[1], tol=1e-15, max_iter=15) +p_solution = newton_nd( + lambda p: e(p, A, b, c), initLs[1], tol=1e-15, max_iter=15 +) e(p_solution, A, b, c) ``` From b6c2573a8abfa8fd3341183b2acb41e3eac34e8d Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Thu, 3 Dec 2026 12:08:18 +1100 Subject: [PATCH 2/2] Update translation: .translate/state/newton_method.md.yml --- .translate/state/newton_method.md.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.translate/state/newton_method.md.yml b/.translate/state/newton_method.md.yml index 5c5deb97..a55122a2 100644 --- a/.translate/state/newton_method.md.yml +++ b/.translate/state/newton_method.md.yml @@ -1,6 +1,6 @@ -source-sha: 96ede29937a77fb0cb26b731e1eecce5e62f6726 -synced-at: "2026-08-18" +source-sha: cc3b8563583c26f2385cdfc0db878d575ad628ca +synced-at: "2026-12-03" model: claude-sonnet-5 mode: UPDATE section-count: 5 -tool-version: 0.26.0 +tool-version: 0.34.0