11---
2- jupyter :
3- jupytext :
4- text_representation :
5- extension : .md
6- format_name : markdown
7- format_version : ' 1.3'
8- jupytext_version : 1.13.7
9- kernelspec :
10- display_name : Python 3
11- language : python
12- name : python3
2+ jupytext :
3+ text_representation :
4+ extension : .md
5+ format_name : myst
6+ kernelspec :
7+ display_name : Python 3
8+ language : python
9+ name : python3
1310---
1411
1512(arellano)=
@@ -29,7 +26,7 @@ jupyter:
2926
3027In addition to what's in Anaconda, this lecture will need the following libraries:
3128
32- ``` python
29+ ``` {code-cell} python
3330:tags: ["hide-output"]
3431!pip install --upgrade quantecon
3532```
@@ -77,7 +74,7 @@ Such dynamics are consistent with experiences of many countries.
7774
7875Let's start with some imports:
7976
80- ``` python
77+ ``` {code-cell} python
8178import matplotlib.pyplot as plt
8279import numpy as np
8380import quantecon as qe
@@ -349,7 +346,7 @@ As we have in other places, we accelerate our code using Numba.
349346We define a class that will store parameters, grids and transition
350347probabilities.
351348
352- ``` python
349+ ``` {code-cell} python
353350class Arellano_Economy:
354351 " Stores data and creates primitives for the Arellano economy. "
355352
@@ -399,7 +396,7 @@ Jitted functions prefer simple arguments, since type inference is easier.
399396Here is the utility function.
400397
401398
402- ``` python
399+ ``` {code-cell} python
403400@njit
404401def u(c, γ):
405402 return c**(1-γ)/(1-γ)
@@ -408,7 +405,7 @@ def u(c, γ):
408405Here is a function to compute the bond price at each state, given $v_c$ and
409406$v_d$.
410407
411- ``` python
408+ ``` {code-cell} python
412409@njit
413410def compute_q(v_c, v_d, q, params, arrays):
414411 """
@@ -431,7 +428,7 @@ def compute_q(v_c, v_d, q, params, arrays):
431428
432429Next we introduce Bellman operators that updated $v_d$ and $v_c$.
433430
434- ``` python
431+ ``` {code-cell} python
435432@njit
436433def T_d(y_idx, v_c, v_d, params, arrays):
437434 """
@@ -478,7 +475,7 @@ def T_c(B_idx, y_idx, v_c, v_d, q, params, arrays):
478475
479476Here is a fast function that calls these operators in the right sequence.
480477
481- ``` python
478+ ``` {code-cell} python
482479@njit(parallel=True)
483480def update_values_and_prices(v_c, v_d, # Current guess of value functions
484481 B_star, q, # Arrays to be written to
@@ -518,7 +515,7 @@ In fact, one of the jobs of this function is to take an instance of
518515` Arellano_Economy ` , which is hard for the JIT compiler to handle, and strip it
519516down to more basic objects, which are then passed out to jitted functions.
520517
521- ``` python
518+ ``` {code-cell} python
522519def solve(model, tol=1e-8, max_iter=10_000):
523520 """
524521 Given an instance of Arellano_Economy, this function computes the optimal
@@ -558,7 +555,7 @@ def solve(model, tol=1e-8, max_iter=10_000):
558555Finally, we write a function that will allow us to simulate the economy once
559556we have the policy functions
560557
561- ``` python
558+ ``` {code-cell} python
562559def simulate(model, T, v_c, v_d, q, B_star, y_idx=None, B_idx=None):
563560 """
564561 Simulates the Arellano 2008 model of sovereign debt
@@ -703,17 +700,17 @@ To the extent that you can, replicate the figures shown above
703700
704701Compute the value function, policy and equilibrium prices
705702
706- ``` python
703+ ``` {code-cell} python
707704ae = Arellano_Economy()
708705```
709706
710- ``` python
707+ ``` {code-cell} python
711708v_c, v_d, q, B_star = solve(ae)
712709```
713710
714711Compute the bond price schedule as seen in figure 3 of Arellano (2008)
715712
716- ``` python
713+ ``` {code-cell} python
717714# Unpack some useful names
718715B_grid, y_grid, P = ae.B_grid, ae.y_grid, ae.P
719716B_grid_size, y_grid_size = len(B_grid), len(y_grid)
@@ -744,7 +741,7 @@ plt.show()
744741
745742Draw a plot of the value functions
746743
747- ``` python
744+ ``` {code-cell} python
748745v = np.maximum(v_c, np.reshape(v_d, (1, y_grid_size)))
749746
750747fig, ax = plt.subplots(figsize=(10, 6.5))
@@ -759,7 +756,7 @@ plt.show()
759756
760757Draw a heat map for default probability
761758
762- ``` python
759+ ``` {code-cell} python
763760xx, yy = B_grid, y_grid
764761zz = np.empty_like(v_c)
765762
@@ -780,7 +777,7 @@ plt.show()
780777
781778Plot a time series of major variables simulated from the model
782779
783- ``` python
780+ ``` {code-cell} python
784781T = 250
785782np.random.seed(42)
786783y_sim, y_a_sim, B_sim, q_sim, d_sim = simulate(ae, T, v_c, v_d, q, B_star)
0 commit comments