Skip to content

Commit a962a11

Browse files
authored
LECT: Solow - Fix exercise (#65)
* Fix solow exercise * add sympy code
1 parent 7bf60c9 commit a962a11

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

lectures/solow.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,36 @@ k_star = ((s_grid * A) / delta)**(1/(1 - alpha))
460460
c_star = (1 - s_grid) * A * k_star ** alpha
461461
```
462462

463-
Let's find the value of $s$ that maximizes $c^*$.
463+
Let's find the value of $s$ that maximizes $c^*$ using [scipy.optimize.minimize_scalar](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#scipy.optimize.minimize_scalar).
464+
We will use $-c^*(s)$ since `minimize_scalar` finds the minimum value.
464465

465466
```{code-cell} ipython3
466-
c_max_index = np.argmax(c_star)
467-
s_star_max = s_grid[c_max_index]
467+
from scipy.optimize import minimize_scalar
468+
```
469+
470+
```{code-cell} ipython3
471+
def calc_c_star(s):
472+
k = ((s * A) / delta)**(1/(1 - alpha))
473+
return - (1 - s) * A * k ** alpha
474+
```
475+
476+
```{code-cell} ipython3
477+
return_values = minimize_scalar(calc_c_star, bounds=(0, 1))
478+
s_star_max = return_values.x
479+
c_star_max = -return_values.fun
480+
print(f"Function is maximized at s = {round(s_star_max, 4)}")
481+
```
468482

483+
```{code-cell} ipython3
469484
x_s_max = np.array([s_star_max, s_star_max])
470-
y_s_max = np.array([0, c_star[c_max_index]])
485+
y_s_max = np.array([0, c_star_max])
471486
472487
fig, ax = plt.subplots()
473488
474-
fps = (c_star[c_max_index],)
489+
fps = (c_star_max,)
475490
476491
# Highlight the maximum point with a marker
477-
ax.plot((s_star_max, ), (c_star[c_max_index],), 'go', ms=8, alpha=0.6)
478-
492+
ax.plot((s_star_max, ), (c_star_max,), 'go', ms=8, alpha=0.6)
479493
480494
ax.annotate(r'$s^*$',
481495
xy=(s_star_max, c_star[c_max_index]),
@@ -484,15 +498,35 @@ ax.annotate(r'$s^*$',
484498
textcoords='offset points',
485499
fontsize=12,
486500
arrowprops=dict(arrowstyle="->"))
487-
ax.plot(s_grid, c_star, label=r'$c^*(s)$')
501+
ax.plot(s_grid, c_star, label=r'$c*(s)$')
488502
ax.plot(x_s_max, y_s_max, alpha=0.5, ls='dotted')
489503
ax.set_xlabel(r'$s$')
490-
ax.set_ylabel(r'$C^*(s)$')
504+
ax.set_ylabel(r'$c^*(s)$')
491505
ax.legend()
492506
493507
plt.show()
494508
```
495509

510+
One can also try to solve this mathematically by differentiating $c^*(s)$ and solve for $\frac{d}{ds}c^*(s)=0$ using [sympy](https://www.sympy.org/en/index.html).
511+
512+
```{code-cell} ipython3
513+
from sympy import solve, Symbol
514+
```
515+
516+
```{code-cell} ipython3
517+
s_symbol = Symbol('s', real=True)
518+
k = ((s_symbol * A) / delta)**(1/(1 - alpha))
519+
c = (1 - s_symbol) * A * k ** alpha
520+
```
521+
522+
Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve)
523+
524+
```{code-cell} ipython3
525+
# Solve using sympy
526+
s_star = solve(c.diff())[0]
527+
print(f"s_star = {s_star}")
528+
```
529+
496530
Incidentally, the rate of savings which maximizes steady state level of per capita consumption is called the [Golden Rule savings rate](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate).
497531

498532
```{solution-end}

0 commit comments

Comments
 (0)