In this notebook we replicate the baseline model exposed in
From Sudden Stops to Fisherian Deflation, Quantitative Theory and Policy
by Anton Korinek and Enrique G. Mendoza
The file sudden_stop.yaml
which is printed below, describes the model, and must be included in the same directory as this notebook.
%pylab inline
Populating the interactive namespace from numpy and matplotlib
from dolo import *
from dolo.algos import time_iteration
from dolo.algos import plot_decision_rule, simulate
pwd
'/home/pablo/Mobilhome/econforge/dolo/examples/notebooks'
# filename = 'https://raw.githubusercontent.com/EconForge/dolo/master/examples/models/compat/sudden_stop.yaml'
filename = '../models/sudden_stop.yaml'
# the model file is coded in a separate file called sudden_stop.yaml
# note how the borrowing constraint is implemented as complementarity condition
pcat(filename)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | # This file adapts the model described in # "From Sudden Stops to Fisherian Deflation, Quantitative Theory and Policy" # by Anton Korinek and Enrique G. Mendoza name: Sudden Stop (General) symbols: exogenous: [y] states: [l] controls: [b, lam] values: [V, Vc] parameters: [beta, R, sigma, a, mu, kappa, delta_y, pi, lam_inf] definitions: c: 1 + y + l*R - b equations: transition: - l = b(-1) arbitrage: - lam = b/c - 1 - beta*(c(1)/c)^(-sigma)*R | lam_inf <= lam <= inf value: - V = c^(1.0-sigma)/(1.0-sigma) + beta*V(1) - Vc = c^(1.0-sigma)/(1.0-sigma) calibration: beta: 0.95 R: 1.03 sigma: 2.0 a: 1/3 mu: 0.8 kappa: 1.3 delta_y: 0.03 pi: 0.05 lam_inf: -0.2 y: 1.0 c: 1.0 + y b: 0.0 l: 0.0 lam: 0.0 V: c^(1.0-sigma)/(1.0-sigma)/(1.0-beta) Vc: c^(1.0-sigma)/(1.0-sigma) exogenous: !MarkovChain values: [[ 1.0-delta_y ], # bad state [ 1.0 ]] # good state transitions: [[ 0.5, 1-0.5 ], # probabilities [p(L|L), p(H|L)] [ 0.5, 0.5 ]] # probabilities [p(L|H), p(H|H)] options: grid: !Cartesian a: [-1.0] b: [ 1.0] orders: [10] |
Note, that residuals, are not zero at the calibration we supply. This is because the representative agent is impatient and we have $\beta<1/R$. In this case it doesn't matter.
By default, the calibrated value for endogenous variables are used as a (constant) starting point for the decision rules.
model = yaml_import(filename)
model
Model | |
name | Sudden Stop (General) |
type | dtcc |
filename | ../models/sudden_stop.yaml |
Type | Equation | Residual |
transition | $l_{t} = b_{t-1}$ | 0.000 |
arbitrage | $\lambda_{t} = \frac{b_{t}}{c_{t}}$ | 0.000 |
$1 - \beta \; \left(\frac{c_{t+1}}{c_{t}}\right)^{- \sigma} \; R$ | 0.0215 | |
value | $V_{t} = \frac{c_{t}^{1.0 - \sigma}}{1.0 - \sigma} + \beta \; V_{t+1}$ | |
$Vc_{t} = \frac{c_{t}^{1.0 - \sigma}}{1.0 - \sigma}$ | ||
definitions | $c_{t} = 1 + y_{t} + l_{t} \; R - b_{t}$ |
# to avoid numerical glitches we choose a relatively high number of grid points
mdr = time_iteration(model, verbose=True, grid={'orders':[1000]})
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-55c9a50a524d> in <module>() 1 # to avoid numerical glitches we choose a relatively high number of grid points ----> 2 mdr = time_iteration(model, verbose=True, grid={'orders':[1000]}) /home/pablo/Mobilhome/econforge/dolo/dolo/algos/time_iteration.py in time_iteration(model, initial_guess, with_complementarities, verbose, grid, maxit, inner_maxit, tol, hook) 76 n_s = len(model.symbols['states']) 77 ---> 78 endo_grid = model.get_grid(**grid) 79 80 interp_type = 'cubic' TypeError: get_grid() got an unexpected keyword argument 'orders'
# produce the plots
n_steps = 100
figure(figsize(10,6))
subplot(121)
plot_decision_rule(model, mdr, 'l', 'b', i0=0, n_steps=n_steps, label='$b_t$ (bad state)' )
plot_decision_rule(model, mdr, 'l', 'b', i0=1, n_steps=n_steps, label='$b_t$ (good state)' )
plot_decision_rule(model, mdr, 'l', 'l', i0=1, n_steps=n_steps, linestyle='--', color='black' )
#plot(df['l'], df['l'], linestyle='--', color='black')
# to plot the borrowing limit, we produce a dataframe df which contains all series
# (note that we don't supply a variable name to plot, only the state 'l')
lam_inf = model.get_calibration('lam_inf')
df = plot_decision_rule(model, mdr, 'l', i0=0, n_steps=n_steps)
plot(df['l'], lam_inf*df['c'], linestyle='--', color='black')
xlabel('$l_t$')
legend(loc= 'upper left')
subplot(122)
plot_decision_rule(model, mdr, 'l', 'c', i0=0, n_steps=n_steps, label='$c_t$ (bad state)' )
plot_decision_rule(model, mdr, 'l', 'c', i0=1, n_steps=n_steps, label='$c_t$ (good state)' )
legend(loc= 'lower right')
xlabel('$l_t$')
suptitle("Decision Rules")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-12-1c2de05bc895> in <module>() 4 figure(figsize(10,6)) 5 subplot(121) ----> 6 plot_decision_rule(model, mdr, 'l', 'b', i0=0, n_steps=n_steps, label='$b_t$ (bad state)' ) 7 plot_decision_rule(model, mdr, 'l', 'b', i0=1, n_steps=n_steps, label='$b_t$ (good state)' ) 8 plot_decision_rule(model, mdr, 'l', 'l', i0=1, n_steps=n_steps, linestyle='--', color='black' ) NameError: name 'mdr' is not defined
## stochastic simulations
i_0 = 1 # we start from the good state
sim = simulate(model, mdr, i_0, s0=0.5, n_exp=1, horizon=100) # markov_indices=markov_indices)
subplot(211)
plot(sim['y'])
subplot(212)
plot(sim['b'])
[<matplotlib.lines.Line2D at 0x7fa1bb111860>]
Here we want to compare the saving behaviour as a function of risk aversion $\sigma$. We contrast the baseline $\sigma=2$ with the high aversion scenario $\sigma=16$.
# we solve the model with sigma=16
model.set_calibration(sigma=16.0)
mdr_high_gamma = time_iteration(model, verbose=True, grid={'orders':[1000]})
Solving WITH complementarities. ------------------------------------------------ | N | Error | Gain | Time | nit | ------------------------------------------------
UserWarning:/home/pablo/Programming/econforge/dolo/dolo/numeric/optimize/newton.py:150
Did not converge
| 1 | 5.133e-01 | nan | 0.475 | 10 | | 2 | 1.703e-01 | 0.332 | 0.744 | 8 | | 3 | 8.435e-02 | 0.495 | 0.369 | 7 | | 4 | 5.005e-02 | 0.593 | 0.406 | 7 | | 5 | 3.292e-02 | 0.658 | 0.405 | 7 | | 6 | 2.313e-02 | 0.703 | 0.390 | 7 | | 7 | 1.702e-02 | 0.736 | 0.464 | 7 | | 8 | 1.295e-02 | 0.761 | 0.362 | 7 | | 9 | 1.011e-02 | 0.780 | 0.376 | 7 | | 10 | 8.045e-03 | 0.796 | 0.372 | 7 | | 11 | 6.501e-03 | 0.808 | 0.326 | 7 | | 12 | 5.316e-03 | 0.818 | 0.425 | 7 | | 13 | 4.387e-03 | 0.825 | 0.384 | 6 | | 14 | 3.647e-03 | 0.831 | 0.386 | 7 | | 15 | 3.048e-03 | 0.836 | 0.362 | 7 | | 16 | 2.558e-03 | 0.839 | 0.385 | 6 | | 17 | 2.206e-03 | 0.863 | 0.318 | 6 | | 18 | 2.010e-03 | 0.911 | 0.489 | 6 | | 19 | 1.842e-03 | 0.916 | 0.243 | 5 | | 20 | 1.699e-03 | 0.922 | 0.305 | 5 | | 21 | 1.580e-03 | 0.930 | 0.280 | 5 | | 22 | 1.472e-03 | 0.932 | 0.240 | 5 | | 23 | 1.374e-03 | 0.933 | 0.271 | 5 | | 24 | 1.289e-03 | 0.938 | 0.295 | 5 | | 25 | 1.210e-03 | 0.939 | 0.454 | 5 | | 26 | 1.137e-03 | 0.940 | 0.273 | 5 | | 27 | 1.073e-03 | 0.944 | 0.189 | 4 | | 28 | 1.013e-03 | 0.944 | 0.234 | 4 | | 29 | 9.575e-04 | 0.945 | 0.271 | 3 | | 30 | 9.075e-04 | 0.948 | 0.208 | 3 | | 31 | 8.600e-04 | 0.948 | 0.136 | 3 | | 32 | 8.166e-04 | 0.950 | 0.149 | 3 | | 33 | 7.764e-04 | 0.951 | 0.141 | 3 | | 34 | 7.384e-04 | 0.951 | 0.285 | 3 | | 35 | 7.035e-04 | 0.953 | 0.284 | 3 | | 36 | 6.705e-04 | 0.953 | 0.093 | 2 | | 37 | 6.396e-04 | 0.954 | 0.091 | 2 | | 38 | 6.108e-04 | 0.955 | 0.275 | 2 | | 39 | 5.835e-04 | 0.955 | 0.099 | 2 | | 40 | 5.579e-04 | 0.956 | 0.096 | 2 | | 41 | 5.338e-04 | 0.957 | 0.111 | 2 | | 42 | 5.110e-04 | 0.957 | 0.103 | 2 | | 43 | 4.895e-04 | 0.958 | 0.093 | 2 | | 44 | 4.691e-04 | 0.958 | 0.094 | 2 | | 45 | 4.499e-04 | 0.959 | 0.167 | 2 | | 46 | 4.316e-04 | 0.959 | 0.124 | 2 | | 47 | 4.143e-04 | 0.960 | 0.119 | 2 | | 48 | 3.978e-04 | 0.960 | 0.104 | 2 | | 49 | 3.821e-04 | 0.961 | 0.117 | 2 | | 50 | 3.598e-04 | 0.941 | 0.096 | 2 | | 51 | 3.132e-04 | 0.871 | 0.128 | 2 | | 52 | 2.476e-04 | 0.790 | 0.141 | 2 | | 53 | 1.782e-04 | 0.720 | 0.126 | 2 | | 54 | 1.190e-04 | 0.668 | 0.112 | 2 | | 55 | 7.541e-05 | 0.634 | 0.273 | 2 | | 56 | 4.634e-05 | 0.615 | 0.096 | 2 | | 57 | 2.802e-05 | 0.605 | 0.096 | 2 | | 58 | 1.684e-05 | 0.601 | 0.093 | 2 | | 59 | 1.010e-05 | 0.600 | 0.051 | 1 | | 60 | 6.072e-06 | 0.601 | 0.051 | 1 | | 61 | 3.659e-06 | 0.603 | 0.084 | 1 | | 62 | 2.211e-06 | 0.604 | 0.093 | 1 | | 63 | 1.340e-06 | 0.606 | 0.108 | 1 | | 64 | 8.141e-07 | 0.607 | 0.071 | 1 | ------------------------------------------------ Elapsed: 15.010632991790771 seconds. ------------------------------------------------
# now we compare the decision rules with low and high risk aversion
plot_decision_rule(model, mdr, 'l', 'b', i0=0, n_steps=n_steps, label='$b_t$ (bad)' )
plot_decision_rule(model, mdr, 'l', 'b', i0=1, n_steps=n_steps, label='$b_t$ (good)' )
plot_decision_rule(model, mdr_high_gamma, 'l', 'b', i0=0, n_steps=n_steps, label='$b_t$ (bad) [high gamma]' )
plot_decision_rule(model, mdr_high_gamma, 'l', 'b', i0=1, n_steps=n_steps, label='$b_t$ (good) [high gamma]' )
plot(df['l'], df['l'], linestyle='--', color='black')
plot(df['l'], -0.2*df['c'], linestyle='--', color='black')
legend(loc= 'upper left')
<matplotlib.legend.Legend at 0x7fa1be17acf8>