From 2ff62919c155da755afe838e200241daea69832e Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Sun, 22 Jun 2025 09:36:53 +0800 Subject: [PATCH 1/5] updates --- environment.yml | 2 +- lectures/cass_koopmans_1.md | 81 ++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/environment.yml b/environment.yml index 203de148f..8a57d898b 100644 --- a/environment.yml +++ b/environment.yml @@ -1,4 +1,4 @@ -name: quantecon +name: quantecon_new channels: - default dependencies: diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index 699def722..2e39fb066 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -4,7 +4,7 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.16.4 + jupytext_version: 1.17.2 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -614,7 +614,7 @@ tolerance bounds), we stop. def bisection(pp, c0, k0, T=10, tol=1e-4, max_iter=500, k_ter=0, verbose=True): # initial boundaries for guess c0 - c0_upper = pp.f(k0) + c0_upper = pp.f(k0) + (1 - pp.δ) * k0 c0_lower = 0 i = 0 @@ -648,7 +648,7 @@ def plot_paths(pp, c0, k0, T_arr, k_ter=0, k_ss=None, axs=None): if axs is None: fix, axs = plt.subplots(1, 3, figsize=(16, 4)) - ylabels = ['$c_t$', '$k_t$', '$\mu_t$'] + ylabels = ['$c_t$', '$k_t$', r'$\mu_t$'] titles = ['Consumption', 'Capital', 'Lagrange Multiplier'] c_paths = [] @@ -801,6 +801,77 @@ A rule of thumb for the planner is The planner accomplishes this by adjusting the saving rate $\frac{f(K_t) - C_t}{f(K_t)}$ over time. +```{exercise} +:label: ck1_ex1 + +Turnspike property is an property that is independent of the +initial condition $K_0$ as long as $T$ is large enough. + +Try to expand the function `plot_paths` to plot paths +of multiple initial points. +``` + +```{solution-start} ck1_ex1 +:class: dropdown +``` + +Here is one solution + +```{code-cell} ipython3 +def plot_multiple_paths(pp, c0, k0s, T_arr, k_ter=0, k_ss=None, axs=None): + if axs is None: + fig, axs = plt.subplots(1, 3, figsize=(16, 4)) + + ylabels = ['$c_t$', '$k_t$', r'$\mu_t$'] + titles = ['Consumption', 'Capital', 'Lagrange Multiplier'] + + colors = plt.cm.viridis(np.linspace(0, 1, len(k0s))) + + all_c_paths = [] + all_k_paths = [] + + for i, k0 in enumerate(k0s): + k0_c_paths = [] + k0_k_paths = [] + + for T in T_arr: + c_vec, k_vec = bisection(pp, c0, k0, T, k_ter=k_ter, verbose=False) + k0_c_paths.append(c_vec) + k0_k_paths.append(k_vec) + + μ_vec = pp.u_prime(c_vec) + paths = [c_vec, k_vec, μ_vec] + + for j in range(3): + axs[j].plot(paths[j], color=colors[i], + label=f'$k_0 = {k0:.2f}$' if j == 0 and T == T_arr[0] else "", alpha=0.7) + axs[j].set(xlabel='t', ylabel=ylabels[j], title=titles[j]) + + if k_ss is not None and i == 0 and T == T_arr[0]: + axs[1].axhline(k_ss, c='k', ls='--', lw=1) + + axs[1].axvline(T+1, c='k', ls='--', lw=1) + axs[1].scatter(T+1, paths[1][-1], s=80, color=colors[i]) + + all_c_paths.append(k0_c_paths) + all_k_paths.append(k0_k_paths) + + # Add legend if multiple initial points + if len(k0s) > 1: + axs[0].legend() + + return all_c_paths, all_k_paths +``` + +```{code-cell} ipython3 +_ = plot_multiple_paths(pp, 0.3, [k_ss*2, k_ss*3, k_ss/3], [250, 150, 75, 50], k_ss=k_ss) +``` + +We can see that the turnpike property holds for different initial capital stocks $K_0$. + +```{solution-end} +``` + Let's calculate and plot the saving rate. ```{code-cell} ipython3 @@ -1075,7 +1146,7 @@ studied in {doc}`Cass-Koopmans Competitive Equilibrium ` is a f ### Exercise ```{exercise} -:label: ck1_ex1 +:label: ck1_ex2 - Plot the optimal consumption, capital, and saving paths when the initial capital level begins at 1.5 times the steady state level @@ -1083,7 +1154,7 @@ studied in {doc}`Cass-Koopmans Competitive Equilibrium ` is a f - Why does the saving rate respond as it does? ``` -```{solution-start} ck1_ex1 +```{solution-start} ck1_ex2 :class: dropdown ``` From 88fa56c883db8d48d6c5c339e7b0303acda71f20 Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Sun, 22 Jun 2025 14:55:37 +0800 Subject: [PATCH 2/5] updates --- lectures/cass_koopmans_1.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index 2e39fb066..d55e289d0 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -804,11 +804,10 @@ over time. ```{exercise} :label: ck1_ex1 -Turnspike property is an property that is independent of the -initial condition $K_0$ as long as $T$ is large enough. +The turnpike property is a property that is independent of the initial condition +$K_0$ provided that $T$ is sufficiently large. -Try to expand the function `plot_paths` to plot paths -of multiple initial points. +Expand the `plot_paths` function so that it plots trajectories for multiple initial points using `k0s = [k_ss*2, k_ss*3, k_ss/3]`. ``` ```{solution-start} ck1_ex1 @@ -867,7 +866,7 @@ def plot_multiple_paths(pp, c0, k0s, T_arr, k_ter=0, k_ss=None, axs=None): _ = plot_multiple_paths(pp, 0.3, [k_ss*2, k_ss*3, k_ss/3], [250, 150, 75, 50], k_ss=k_ss) ``` -We can see that the turnpike property holds for different initial capital stocks $K_0$. +We see that the turnpike property holds for various initial values of $K_0$. ```{solution-end} ``` From ee82c3233ead2eeb25ab9df6f9db7f762c3ead63 Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Sun, 22 Jun 2025 14:58:22 +0800 Subject: [PATCH 3/5] remove changes in env file --- environment.yml | 2 +- lectures/cass_koopmans_1.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/environment.yml b/environment.yml index 8a57d898b..203de148f 100644 --- a/environment.yml +++ b/environment.yml @@ -1,4 +1,4 @@ -name: quantecon_new +name: quantecon channels: - default dependencies: diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index d55e289d0..8efeef1ea 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -804,7 +804,7 @@ over time. ```{exercise} :label: ck1_ex1 -The turnpike property is a property that is independent of the initial condition +The turnpike property is independent of the initial condition $K_0$ provided that $T$ is sufficiently large. Expand the `plot_paths` function so that it plots trajectories for multiple initial points using `k0s = [k_ss*2, k_ss*3, k_ss/3]`. From d2b4c99fff6cc893cc537b1a70e7d202386676f5 Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Sun, 22 Jun 2025 15:30:34 +0800 Subject: [PATCH 4/5] add quantecon install --- lectures/cass_koopmans_1.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index 8efeef1ea..72e9818c8 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -62,6 +62,15 @@ The lecture uses important ideas including long but finite-horizon economies. - A **stable manifold** and a **phase plane** +In addition to what's in Anaconda, this lecture will need the following libraries: + +```{code-cell} ipython +--- +tags: [hide-output] +--- +!pip install quantecon +``` + Let's start with some standard imports: ```{code-cell} ipython3 From 9e2e24ef59b335aebe5db2440da523a59a51cf53 Mon Sep 17 00:00:00 2001 From: Humphrey Yang <39026988+HumphreyYang@users.noreply.github.com> Date: Wed, 2 Jul 2025 15:31:32 +0800 Subject: [PATCH 5/5] Update lectures/cass_koopmans_1.md Co-authored-by: Matt McKay --- lectures/cass_koopmans_1.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index 72e9818c8..30b9cbdb0 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -65,9 +65,7 @@ The lecture uses important ideas including In addition to what's in Anaconda, this lecture will need the following libraries: ```{code-cell} ipython ---- -tags: [hide-output] ---- +:tags: [hide-output] !pip install quantecon ```