Skip to content

Commit 5f277a1

Browse files
committed
Update sources from lecture-python-advanced (ce9441dad2ec187b4c93cfca6225aa68818dd033) using sphinx-tomyst + adjustment
1 parent 7d072c7 commit 5f277a1

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

lectures/opt_tax_recur.md

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3131
tags: [hide-output]
3232
---
3333
!pip install --upgrade quantecon
34+
!pip install interpolation
3435
```
3536

3637
## Overview
@@ -70,6 +71,12 @@ Let's start with some standard imports:
7071
import numpy as np
7172
import matplotlib.pyplot as plt
7273
%matplotlib inline
74+
from scipy.optimize import root
75+
from quantecon import MarkovChain
76+
from quantecon.optimize.nelder_mead import nelder_mead
77+
from interpolation import interp
78+
from numba import njit, prange, float64
79+
from numba.experimental import jitclass
7380
```
7481

7582
## A Competitive Equilibrium with Distorting Taxes
@@ -717,7 +724,7 @@ $$
717724

718725
### Sequence Implementation
719726

720-
The above steps are implemented in a class called SequentialAllocation
727+
The above steps are implemented in a class called SequentialLS
721728

722729
```{code-cell} python3
723730
:load: _static/lecture_specific/opt_tax_recur/sequential_allocation.py
@@ -732,7 +739,7 @@ appears to be a purely “forward-looking” variable.
732739

733740
But $x_t(s^t)$ is a natural candidate for a state variable in
734741
a recursive formulation of the Ramsey problem, one that records history-dependence and so is
735-
``backward-looking''.
742+
`backward-looking`.
736743

737744
### Intertemporal Delegation
738745

@@ -1019,7 +1026,7 @@ through them, the value of initial government debt $b_0$.
10191026

10201027
### Recursive Implementation
10211028

1022-
The above steps are implemented in a class called `RecursiveAllocation`.
1029+
The above steps are implemented in a class called `RecursiveLS`.
10231030

10241031
```{code-cell} python3
10251032
:load: _static/lecture_specific/opt_tax_recur/recursive_allocation.py
@@ -1087,39 +1094,31 @@ We can now plot the Ramsey tax under both realizations of time $t = 3$ governme
10871094
* red when $g_3 = .2$
10881095

10891096
```{code-cell} python3
1090-
time_π = np.array([[0, 1, 0, 0, 0, 0],
1091-
[0, 0, 1, 0, 0, 0],
1092-
[0, 0, 0, 0.5, 0.5, 0],
1093-
[0, 0, 0, 0, 0, 1],
1094-
[0, 0, 0, 0, 0, 1],
1095-
[0, 0, 0, 0, 0, 1]])
1096-
1097-
time_G = np.array([0.1, 0.1, 0.1, 0.2, 0.1, 0.1])
1098-
# Θ can in principle be random
1099-
time_Θ = np.ones(6)
1100-
time_example = CRRAutility(π=time_π, G=time_G, Θ=time_Θ)
1097+
π = np.array([[0, 1, 0, 0, 0, 0],
1098+
[0, 0, 1, 0, 0, 0],
1099+
[0, 0, 0, 0.5, 0.5, 0],
1100+
[0, 0, 0, 0, 0, 1],
1101+
[0, 0, 0, 0, 0, 1],
1102+
[0, 0, 0, 0, 0, 1]])
1103+
1104+
g = np.array([0.1, 0.1, 0.1, 0.2, 0.1, 0.1])
1105+
crra_pref = CRRAutility()
11011106
11021107
# Solve sequential problem
1103-
time_allocation = SequentialAllocation(time_example)
1108+
seq = SequentialLS(crra_pref, π=π, g=g)
11041109
sHist_h = np.array([0, 1, 2, 3, 5, 5, 5])
11051110
sHist_l = np.array([0, 1, 2, 4, 5, 5, 5])
1106-
sim_seq_h = time_allocation.simulate(1, 0, 7, sHist_h)
1107-
sim_seq_l = time_allocation.simulate(1, 0, 7, sHist_l)
1108-
1109-
# Government spending paths
1110-
sim_seq_l[4] = time_example.G[sHist_l]
1111-
sim_seq_h[4] = time_example.G[sHist_h]
1111+
sim_seq_h = seq.simulate(1, 0, 7, sHist_h)
1112+
sim_seq_l = seq.simulate(1, 0, 7, sHist_l)
11121113
1113-
# Output paths
1114-
sim_seq_l[5] = time_example.Θ[sHist_l] * sim_seq_l[1]
1115-
sim_seq_h[5] = time_example.Θ[sHist_h] * sim_seq_h[1]
1116-
1117-
fig, axes = plt.subplots(3, 2, figsize=(12, 8))
1114+
fig, axes = plt.subplots(3, 2, figsize=(14, 10))
11181115
titles = ['Consumption', 'Labor Supply', 'Government Debt',
11191116
'Tax Rate', 'Government Spending', 'Output']
11201117
11211118
for ax, title, sim_l, sim_h in zip(axes.flatten(),
1122-
titles, sim_seq_l, sim_seq_h):
1119+
titles,
1120+
sim_seq_l[:6],
1121+
sim_seq_h[:6]):
11231122
ax.set(title=title)
11241123
ax.plot(sim_l, '-ok', sim_h, '-or', alpha=0.7)
11251124
ax.grid()
@@ -1227,18 +1226,16 @@ $t=0$ and time $t\geq1$ as functions of the initial government debt
12271226
above)
12281227

12291228
```{code-cell} python3
1230-
tax_sequence = SequentialAllocation(CRRAutility(G=0.15,
1231-
π=np.ones((1, 1)),
1232-
Θ=np.ones(1)))
1229+
tax_seq = SequentialLS(CRRAutility(), g=np.array([0.15]), π=np.ones((1, 1)))
12331230
12341231
n = 100
12351232
tax_policy = np.empty((n, 2))
12361233
interest_rate = np.empty((n, 2))
12371234
gov_debt = np.linspace(-1.5, 1, n)
12381235
12391236
for i in range(n):
1240-
tax_policy[i] = tax_sequence.simulate(gov_debt[i], 0, 2)[3]
1241-
interest_rate[i] = tax_sequence.simulate(gov_debt[i], 0, 3)[-1]
1237+
tax_policy[i] = tax_seq.simulate(gov_debt[i], 0, 2)[3]
1238+
interest_rate[i] = tax_seq.simulate(gov_debt[i], 0, 3)[-1]
12421239
12431240
fig, axes = plt.subplots(2, 1, figsize=(10,8), sharex=True)
12441241
titles = ['Tax Rate', 'Gross Interest Rate']
@@ -1298,18 +1295,16 @@ Ramsey planner and what a new Ramsey planner would choose for its
12981295
time $t=0$ tax rate
12991296

13001297
```{code-cell} python3
1301-
tax_sequence = SequentialAllocation(CRRAutility(G=0.15,
1302-
π=np.ones((1, 1)),
1303-
Θ=np.ones(1)))
1298+
tax_seq = SequentialLS(CRRAutility(), g=np.array([0.15]), π=np.ones((1, 1)))
13041299
13051300
n = 100
13061301
tax_policy = np.empty((n, 2))
13071302
τ_reset = np.empty((n, 2))
13081303
gov_debt = np.linspace(-1.5, 1, n)
13091304
13101305
for i in range(n):
1311-
tax_policy[i] = tax_sequence.simulate(gov_debt[i], 0, 2)[3]
1312-
τ_reset[i] = tax_sequence.simulate(gov_debt[i], 0, 1)[3]
1306+
tax_policy[i] = tax_seq.simulate(gov_debt[i], 0, 2)[3]
1307+
τ_reset[i] = tax_seq.simulate(gov_debt[i], 0, 1)[3]
13131308
13141309
fig, ax = plt.subplots(figsize=(10, 6))
13151310
ax.plot(gov_debt, tax_policy[:, 1], gov_debt, τ_reset, lw=2)
@@ -1368,38 +1363,32 @@ The figure below plots a sample path of the Ramsey tax rate
13681363
```{code-cell} python3
13691364
log_example = LogUtility()
13701365
# Solve sequential problem
1371-
seq_log = SequentialAllocation(log_example)
1366+
seq_log = SequentialLS(log_example)
13721367
13731368
# Initialize grid for value function iteration and solve
1374-
μ_grid = np.linspace(-0.6, 0.0, 200)
1369+
x_grid = np.linspace(-3., 3., 200)
1370+
13751371
# Solve recursive problem
1376-
bel_log = RecursiveAllocation(log_example, μ_grid)
1372+
rec_log = RecursiveLS(log_example, x_grid)
13771373
1378-
T = 20
1379-
sHist = np.array([0, 0, 0, 0, 0, 0, 0,
1380-
0, 1, 1, 0, 0, 0, 1,
1381-
1, 1, 1, 1, 1, 0])
1374+
T_length = 20
1375+
sHist = np.array([0, 0, 0, 0, 0,
1376+
0, 0, 0, 1, 1,
1377+
0, 0, 0, 1, 1,
1378+
1, 1, 1, 1, 0])
13821379
13831380
# Simulate
1384-
sim_seq = seq_log.simulate(0.5, 0, T, sHist)
1385-
sim_bel = bel_log.simulate(0.5, 0, T, sHist)
1381+
sim_seq = seq_log.simulate(0.5, 0, T_length, sHist)
1382+
sim_rec = rec_log.simulate(0.5, 0, T_length, sHist)
13861383
1387-
# Government spending paths
1388-
sim_seq[4] = log_example.G[sHist]
1389-
sim_bel[4] = log_example.G[sHist]
1390-
1391-
# Output paths
1392-
sim_seq[5] = log_example.Θ[sHist] * sim_seq[1]
1393-
sim_bel[5] = log_example.Θ[sHist] * sim_bel[1]
1394-
1395-
fig, axes = plt.subplots(3, 2, figsize=(10, 6))
1384+
fig, axes = plt.subplots(3, 2, figsize=(14, 10))
13961385
titles = ['Consumption', 'Labor Supply', 'Government Debt',
13971386
'Tax Rate', 'Government Spending', 'Output']
13981387
1399-
for ax, title, sim_s, sim_b in zip(axes.flatten(), titles, sim_seq, sim_bel):
1400-
ax.plot(sim_s, '-ob', sim_b, '-xk', alpha=0.7)
1401-
ax.set(title=title)
1402-
ax.grid()
1388+
for ax, title, sim_s, sim_b in zip(axes.flatten(), titles, sim_seq[:6], sim_rec[:6]):
1389+
ax.plot(sim_s, '-ob', sim_b, '-xk', alpha=0.7)
1390+
ax.set(title=title)
1391+
ax.grid()
14031392
14041393
axes.flatten()[0].legend(('Sequential', 'Recursive'))
14051394
fig.tight_layout()

0 commit comments

Comments
 (0)