Skip to content

Commit d15bf8b

Browse files
committed
apply suggestions
1 parent c63f294 commit d15bf8b

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lectures/newton_method.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ In other words, we seek a $k^* > 0$ such that $g(k^*) = k^*$.
127127

128128
Using pencil and paper to solve $g(k) = k$, you will be able to confirm that
129129

130-
$$ k^* = \left(\frac{s A}{\delta}\right)^{1/(1 - \alpha)} $$
130+
$$
131+
k^* = \left(\frac{s A}{\delta}\right)^{1/(1 - \alpha)}
132+
$$
131133

132134
### Implementation
133135

@@ -329,9 +331,9 @@ def plot_trajectories(
329331
params,
330332
k0_a=0.8, # first initial condition
331333
k0_b=3.1, # second initial condition
332-
n=20, # length of time series
333-
fs=14,
334-
): # fontsize
334+
n=20, # length of time series
335+
fs=14, # fontsize
336+
):
335337
336338
fig, axes = plt.subplots(2, 1, figsize=(10, 6))
337339
ax1, ax2 = axes
@@ -457,14 +459,14 @@ Let's apply this idea to the Solow problem
457459

458460
```{code-cell} ipython3
459461
params = create_solow_params()
460-
k_star_approx_newton = newton(f=lambda x: g(x, params) - x, x_0=0.8)
462+
k_star_approx_newton = newton(f = lambda x: g(x, params) - x, x_0=0.8)
461463
```
462464

463465
```{code-cell} ipython3
464466
k_star_approx_newton
465467
```
466468

467-
The result confirms the convergence we saw in the graphs above: a very accurate result is reached with only 5 iterations.
469+
The result confirms convergence we saw in the graphs above: a very accurate result is reached with only 5 iterations.
468470

469471

470472

@@ -600,6 +602,7 @@ To increase the efficiency of computation, we will use the power of vectorizatio
600602
```{code-cell} ipython3
601603
# Create vectorization on the first axis of p.
602604
e_vectorized_p_1 = jax.vmap(e, in_axes=(0, None, None, None))
605+
603606
# Create vectorization on the second axis of p.
604607
e_vectorized = jax.vmap(e_vectorized_p_1, in_axes=(0, None, None, None))
605608
```
@@ -611,8 +614,10 @@ We will use the following function to build the contour plots
611614
```{code-cell} ipython3
612615
def plot_excess_demand(ax, good=0, grid_size=100, grid_max=4, surface=True):
613616
p_grid = jnp.linspace(0, grid_max, grid_size)
617+
614618
# Create meshgrid for all combinations of p_1 and p_2
615619
P1, P2 = jnp.meshgrid(p_grid, p_grid, indexing="ij")
620+
616621
# Stack to create array of shape (grid_size, grid_size, 2)
617622
P = jnp.stack([P1, P2], axis=-1)
618623
@@ -627,7 +632,7 @@ def plot_excess_demand(ax, good=0, grid_size=100, grid_max=4, surface=True):
627632
ctr1 = ax.contour(p_grid, p_grid, z.T, levels=[0.0])
628633
ax.set_xlabel("$p_0$")
629634
ax.set_ylabel("$p_1$")
630-
ax.set_title(f"Excess Demand for Good {good}")
635+
ax.set_title(f"Excess demand for good {good}")
631636
plt.clabel(ctr1, inline=1, fontsize=13)
632637
```
633638

@@ -727,7 +732,10 @@ def jacobian_e(p, A, b, c):
727732
```{code-cell} ipython3
728733
%%time
729734
solution = root(
730-
lambda p: e(p, A, b, c), init_p, jac=lambda p: jacobian_e(p, A, b, c), method="hybr"
735+
lambda p: e(p, A, b, c),
736+
init_p,
737+
jac = lambda p: jacobian_e(p, A, b, c),
738+
method="hybr"
731739
)
732740
```
733741

@@ -847,7 +855,7 @@ With the same tolerance, we compare the runtime and accuracy of Newton's method
847855
solution = root(
848856
lambda p: e(p, A, b, c),
849857
init_p,
850-
jac=lambda p: jax.jacobian(e)(p, A, b, c),
858+
jac = lambda p: jax.jacobian(e)(p, A, b, c),
851859
method="hybr",
852860
tol=1e-5,
853861
)

0 commit comments

Comments
 (0)