GitHub

@@ -29,7 +29,7 @@ jupyter:

29293030

In addition to what's in Anaconda, this lecture will need the following libraries:

313132-

```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.

77777878

Let's start with some imports:

797980-

```python

80+

```{code-cell} python

8181

import matplotlib.pyplot as plt

8282

import numpy as np

8383

import quantecon as qe

@@ -349,7 +349,7 @@ As we have in other places, we accelerate our code using Numba.

349349

We define a class that will store parameters, grids and transition

350350

probabilities.

351351352-

```python

352+

```{code-cell} python

353353

class 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.

399399

Here is the utility function.

400400401401402-

```python

402+

```{code-cell} python

403403

@njit

404404

def u(c, γ):

405405

return c**(1-γ)/(1-γ)

@@ -408,7 +408,7 @@ def u(c, γ):

408408

Here is a function to compute the bond price at each state, given $v_c$ and

409409

$v_d$.

410410411-

```python

411+

```{code-cell} python

412412

@njit

413413

def compute_q(v_c, v_d, q, params, arrays):

414414

"""

@@ -431,7 +431,7 @@ def compute_q(v_c, v_d, q, params, arrays):

431431432432

Next we introduce Bellman operators that updated $v_d$ and $v_c$.

433433434-

```python

434+

```{code-cell} python

435435

@njit

436436

def 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):

478478479479

Here is a fast function that calls these operators in the right sequence.

480480481-

```python

481+

```{code-cell} python

482482

@njit(parallel=True)

483483

def 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

519519

down to more basic objects, which are then passed out to jitted functions.

520520521-

```python

521+

```{code-cell} python

522522

def 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):

558558

Finally, we write a function that will allow us to simulate the economy once

559559

we have the policy functions

560560561-

```python

561+

```{code-cell} python

562562

def 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

703703704704

Compute the value function, policy and equilibrium prices

705705706-

```python

706+

```{code-cell} python

707707

ae = Arellano_Economy()

708708

```

709709710-

```python

710+

```{code-cell} python

711711

v_c, v_d, q, B_star = solve(ae)

712712

```

713713714714

Compute the bond price schedule as seen in figure 3 of Arellano (2008)

715715716-

```python

716+

```{code-cell} python

717717

# Unpack some useful names

718718

B_grid, y_grid, P = ae.B_grid, ae.y_grid, ae.P

719719

B_grid_size, y_grid_size = len(B_grid), len(y_grid)

@@ -744,7 +744,7 @@ plt.show()

744744745745

Draw a plot of the value functions

746746747-

```python

747+

```{code-cell} python

748748

v = np.maximum(v_c, np.reshape(v_d, (1, y_grid_size)))

749749750750

fig, ax = plt.subplots(figsize=(10, 6.5))

@@ -759,7 +759,7 @@ plt.show()

759759760760

Draw a heat map for default probability

761761762-

```python

762+

```{code-cell} python

763763

xx, yy = B_grid, y_grid

764764

zz = np.empty_like(v_c)

765765

@@ -780,7 +780,7 @@ plt.show()

780780781781

Plot a time series of major variables simulated from the model

782782783-

```python

783+

```{code-cell} python

784784

T = 250

785785

np.random.seed(42)

786786

y_sim, y_a_sim, B_sim, q_sim, d_sim = simulate(ae, T, v_c, v_d, q, B_star)

Read the original on github.com ↗