Skip to content

Commit 63d440d

Browse files
committed
fix
1 parent a74fc48 commit 63d440d

File tree

1 file changed

+54
-70
lines changed

1 file changed

+54
-70
lines changed

lectures/olg.md

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -728,44 +728,38 @@ def simulate_ts(m, x0_values, ts_length):
728728
simulate_ts(m_crra, x0, ts_length)
729729
```
730730

731-
## Exercises
732731

733-
TODO Add exercise-solution environment
734732

733+
## Exercises
735734

736735

737-
### Exercise 1
736+
```{exercise}
737+
:label: olg_ex1
738738
739-
To solve the equilibrium of the CRRA case numerically using [](equilibrium_crra).
739+
Solve the equilibrium of the CRRA case numerically using [](equilibrium_crra).
740740
741741
Plot the equilibrium quantity and equilibrium price in the equilibrium plot with the CRRA utility (TODO label and refer the plot generated by the code).
742+
```
742743

743744

744-
745-
### Solution to exercise 1
746-
747-
To solve the equation we need to turn to Newton's method.
748-
749-
750-
751-
```{math}
752-
L (1-\alpha)(K_t / L)^{\alpha} \left [ 1 + \beta^{-1/\gamma} R_{t+1}^{(\gamma-1)/\gamma} \right ]^{-1} = L\left (\frac{R_{t+1}}{\alpha} \right )^{1/(\alpha - 1)}
745+
```{solution-start} olg_ex1
746+
:class: dropdown
753747
```
754748

749+
To solve the equation we need to turn to Newton's method. `find_Rstar` is used to find $R^*_{t+1}$ by finding the zero of equation [](equilibrium_crra) using the helper function `find_Rstar_newton` for a given value of $K_t$. Similary, `find_Kstar` finds the equilibrium quantity $K^*_{t+1}$ using the value of $R^*_{t+1}$.
750+
755751
```{code-cell} ipython3
756752
def find_Rstar_newton(x, K_prev, model):
757753
α, β, γ, L = model.α, model.β, model.u.__defaults__[0], model.L
758754
lhs = L * (1-α) * (K_prev / L)**α
759755
lhs /= (1 + β**(-1/γ) * x**((γ-1)/γ))
760-
# To should always non-negative for a real solution.
761-
assert x>=0, x
762756
rhs = L * (x / α)**(1/(α-1))
763757
return lhs - rhs
764758
```
765759

766760
```{code-cell} ipython3
767761
def find_Rstar(K_prev, model):
768-
return optimize.newton(find_Rstar_newton, K_prev*100, args=(K_prev, model))
762+
return optimize.newton(find_Rstar_newton, 0.5, args=(K_prev, model))
769763
770764
def find_Kstar(R_star, model):
771765
return model.L * (R_star / model.α)**(1/(model.α-1))
@@ -792,25 +786,27 @@ def plot_ks_rs(K_t_vals, model):
792786
```
793787

794788
```{code-cell} ipython3
795-
K_t_vals = np.linspace(500, 1000, 50)
789+
K_t_vals = np.linspace(0.1, 50, 50)
796790
m_crra = create_olg_model(u=crra)
797791
plot_ks_rs(K_t_vals, m_crra)
798792
```
799793

794+
```{solution-end}
795+
```
800796

801-
802-
### Exercise 2
803-
797+
```{exercise}
798+
:label: olg_ex2
804799
805800
Let's keep the model the same except for replacing the utility function $u(c)$ in {eq}`eq_crra` with a quasilinear form $u(c)=c + c^{\theta}$.
806801
807-
Like what we did in the crra case we don't have an analytical solution.
802+
Like what we did in the CRRA case we don't have an analytical solution.
808803
809804
Try to compute the time path capital $\{k_t\}$ in this case.
805+
```
810806

811-
+++
812-
813-
### Solution to exercise 2
807+
```{solution-start} olg_ex2
808+
:class: dropdown
809+
```
814810

815811
To get the time path capital $\{k_t\}$ first we need to solve the household's utility maximization problem for the optimal consumption and optimal saving.
816812

@@ -825,74 +821,62 @@ Let $k_t := K_t / L$.
825821

826822
Since [](aggregate_supply_capital_log_olg), [](wage_2) and [](interest_rate_2) the Euler equation becomes
827823
```{math}
828-
:label:
824+
:label: euler_quasilinear1
829825
1 + \theta ((1-\alpha)k^{\alpha}_t - k_{t+1})^{\theta-1} = \beta \alpha k^{\alpha - 1}_t + \beta (\alpha k^{\alpha - 1}_t)^{\theta} \theta k_{t+1}^{\theta - 1}
830826
```
831827

832828
Obviously $k_{t+1}$ cannot be solved by pencil and paper.
833829

834830
To solve for $k_{t+1}$ we need to turn to the newton's method.
835831

832+
Let's start by defining the utility function.
833+
836834
```{code-cell} ipython3
837-
# Hi Smit please fill in the trial answers here
835+
def u_quasilinear(c, θ=4):
836+
return c + c**θ
838837
```
839838

840839
```{code-cell} ipython3
841-
840+
def solve_for_k_t1(x, k_t, model):
841+
α, β, L, θ = model.α, model.β, model.L, model.u.__defaults__[0]
842+
l = 1 + θ * ((1 - α) * k_t**α - x)**(θ - 1)
843+
r = β * α * k_t**(α - 1)
844+
r += β * (α * k_t**(α - 1))**θ * θ * x**(θ - 1)
845+
return l - r
842846
```
843847

844848
```{code-cell} ipython3
845-
# Smit's previous code
846-
847-
# def u_prime(c, θ):
848-
# return 1 + θ * c ** (θ - 1)
849-
850-
# def solve_for_c(x, θ, model, k_t, k_t_1):
851-
# l = u_prime(x, θ)
852-
# R = model.α * k_t_1**(model.α-1)
853-
# r = model.β * R
854-
# y = R * ((1 - model.α) * k_t**model.α - x)
855-
# r = r * u_prime(y, θ)
856-
# return l - r
857-
858-
# def solve_for_k(x, θ, model, k_t):
859-
# c = optimize.newton(solve_for_c, k_t, args=(θ, model, k_t, x))
860-
# num = (1 - model.α) * k_t**model.α - c
861-
# den = (1 + model.n)
862-
# return x - num/den
863-
864-
# def k_next(model, θ, k_t):
865-
# return optimize.newton(solve_for_k, k_t, args=(θ, model, k_t), maxiter=200)
866-
867-
# ts_length = 10
868-
# θ = 1.5
869-
# x0 = np.array([1.2, 2.6])
870-
871-
# def simulate_ts(olg, θ, x0_values, ts_length):
849+
def find_k_next(k_t, model):
850+
return optimize.newton(solve_for_k_t1, k_t, args=(k_t, model))
851+
```
872852

873-
# fig, ax = plt.subplots(figsize=(10, 5))
853+
```{code-cell} ipython3
854+
def simulate_ts(k0_values, model, ts_length=10):
874855
875-
# ts = np.zeros(ts_length)
856+
fig, ax = plt.subplots(figsize=(10, 5))
876857
877-
# # simulate and plot time series
878-
# for x_init in x0_values:
879-
# ts[0] = x_init
880-
# for t in range(1, ts_length):
881-
# ts[t] = k_next(olg, θ, ts[t-1])
882-
# ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
883-
# label=r'$k_0=%g$' %x_init)
884-
# ax.plot(np.arange(ts_length), np.full(ts_length,k_star),
885-
# alpha=0.6, color='red', label=r'$k^*$')
886-
# ax.legend(fontsize=10)
858+
ts = np.zeros(ts_length)
887859
888-
# ax.set_xlabel(r'$t$', fontsize=14)
889-
# ax.set_ylabel(r'$k_t$', fontsize=14)
860+
# simulate and plot time series
861+
for x_init in k0_values:
862+
ts[0] = x_init
863+
for t in range(1, ts_length):
864+
ts[t] = find_k_next(ts[t-1], model)
865+
ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
866+
label=r'$k_0=%g$' %x_init)
867+
ax.legend(fontsize=10)
890868
891-
# plt.show()
869+
ax.set_xlabel(r'$t$', fontsize=14)
870+
ax.set_ylabel(r'$k_t$', fontsize=14)
892871
893-
# simulate_ts(olg, θ, x0, ts_length)
872+
plt.show()
894873
```
895874

896875
```{code-cell} ipython3
876+
k0_values = [0.2, 10, 50, 100]
877+
m_quasilinear = create_olg_model(u=u_quasilinear)
878+
simulate_ts(k0_values, m_quasilinear)
879+
```
897880

881+
```{solution-end}
898882
```

0 commit comments

Comments
 (0)