@@ -29,7 +29,7 @@ jupyter:
2929
3030In addition to what's in Anaconda, this lecture will need the following libraries:
3131
32- ``` python
32+ ``` {code-cell} python
3333:tags: ["hide-output"]
3434!pip install --upgrade quantecon
3535```
@@ -77,7 +77,7 @@ Such dynamics are consistent with experiences of many countries.
7777
7878Let's start with some imports:
7979
80- ``` python
80+ ``` {code-cell} python
8181import matplotlib.pyplot as plt
8282import numpy as np
8383import quantecon as qe
@@ -349,7 +349,7 @@ As we have in other places, we accelerate our code using Numba.
349349We define a class that will store parameters, grids and transition
350350probabilities.
351351
352- ``` python
352+ ``` {code-cell} python
353353class Arellano_Economy:
354354 " Stores data and creates primitives for the Arellano economy. "
355355
@@ -399,7 +399,7 @@ Jitted functions prefer simple arguments, since type inference is easier.
399399Here is the utility function.
400400
401401
402- ``` python
402+ ``` {code-cell} python
403403@njit
404404def u(c, γ):
405405 return c**(1-γ)/(1-γ)
@@ -408,7 +408,7 @@ def u(c, γ):
408408Here is a function to compute the bond price at each state, given $v_c$ and
409409$v_d$.
410410
411- ``` python
411+ ``` {code-cell} python
412412@njit
413413def compute_q(v_c, v_d, q, params, arrays):
414414 """
@@ -431,7 +431,7 @@ def compute_q(v_c, v_d, q, params, arrays):
431431
432432Next we introduce Bellman operators that updated $v_d$ and $v_c$.
433433
434- ``` python
434+ ``` {code-cell} python
435435@njit
436436def T_d(y_idx, v_c, v_d, params, arrays):
437437 """
@@ -478,7 +478,7 @@ def T_c(B_idx, y_idx, v_c, v_d, q, params, arrays):
478478
479479Here is a fast function that calls these operators in the right sequence.
480480
481- ``` python
481+ ``` {code-cell} python
482482@njit(parallel=True)
483483def update_values_and_prices(v_c, v_d, # Current guess of value functions
484484 B_star, q, # Arrays to be written to
@@ -518,7 +518,7 @@ In fact, one of the jobs of this function is to take an instance of
518518` Arellano_Economy ` , which is hard for the JIT compiler to handle, and strip it
519519down to more basic objects, which are then passed out to jitted functions.
520520
521- ``` python
521+ ``` {code-cell} python
522522def solve(model, tol=1e-8, max_iter=10_000):
523523 """
524524 Given an instance of Arellano_Economy, this function computes the optimal
@@ -558,7 +558,7 @@ def solve(model, tol=1e-8, max_iter=10_000):
558558Finally, we write a function that will allow us to simulate the economy once
559559we have the policy functions
560560
561- ``` python
561+ ``` {code-cell} python
562562def simulate(model, T, v_c, v_d, q, B_star, y_idx=None, B_idx=None):
563563 """
564564 Simulates the Arellano 2008 model of sovereign debt
@@ -703,17 +703,17 @@ To the extent that you can, replicate the figures shown above
703703
704704Compute the value function, policy and equilibrium prices
705705
706- ``` python
706+ ``` {code-cell} python
707707ae = Arellano_Economy()
708708```
709709
710- ``` python
710+ ``` {code-cell} python
711711v_c, v_d, q, B_star = solve(ae)
712712```
713713
714714Compute the bond price schedule as seen in figure 3 of Arellano (2008)
715715
716- ``` python
716+ ``` {code-cell} python
717717# Unpack some useful names
718718B_grid, y_grid, P = ae.B_grid, ae.y_grid, ae.P
719719B_grid_size, y_grid_size = len(B_grid), len(y_grid)
@@ -744,7 +744,7 @@ plt.show()
744744
745745Draw a plot of the value functions
746746
747- ``` python
747+ ``` {code-cell} python
748748v = np.maximum(v_c, np.reshape(v_d, (1, y_grid_size)))
749749
750750fig, ax = plt.subplots(figsize=(10, 6.5))
@@ -759,7 +759,7 @@ plt.show()
759759
760760Draw a heat map for default probability
761761
762- ``` python
762+ ``` {code-cell} python
763763xx, yy = B_grid, y_grid
764764zz = np.empty_like(v_c)
765765
@@ -780,7 +780,7 @@ plt.show()
780780
781781Plot a time series of major variables simulated from the model
782782
783- ``` python
783+ ``` {code-cell} python
784784T = 250
785785np.random.seed(42)
786786y_sim, y_a_sim, B_sim, q_sim, d_sim = simulate(ae, T, v_c, v_d, q, B_star)
0 commit comments