<jupyter_start><jupyter_text>Sudden Stop ModelIn this notebook we replicate the baseline model exposed in `From Sudden Stops to Fisherian Deflation, Quantitative Theory and Policy` by __<NAME> and <NAME>__The file `sudden_stop.yaml` which is printed below, describes the model, and must be included in the same directory as this notebook.  importing necessary functions<jupyter_code>%pylab inline
from dolo import *
from dolo.algos.dtmscc.time_iteration import time_iteration
from dolo.algos.dtmscc.simulations import plot_decision_rule, simulate<jupyter_output><empty_output><jupyter_text>writing the model<jupyter_code>cd ../models
filename = 'https://raw.githubusercontent.com/EconForge/dolo/master/examples/models/sudden_stop.yaml'
filename = '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)<jupyter_output><empty_output><jupyter_text>importing the modelNote, that residuals, are not zero at the calibration we supply. This is because the representative agent is impatientand 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.<jupyter_code>model = yaml_import('sudden_stop.yaml')
model
# to avoid numerical glitches we choose a relatively high number of grid points
mdr = time_iteration(model, verbose=True, orders=[1000])
# 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")

## 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'])<jupyter_output><empty_output><jupyter_text>Sensitivity analysisHere 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$.<jupyter_code># we solve the model with sigma=16
model.set_calibration(sigma=16.0)
mdr_high_gamma = time_iteration(model, verbose=True, orders=[1000])
# 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')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1412609736632083, 'fraction_numerical': 0.01782388933226922, 'mean_word_length': 6.966101694915254, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '593254', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1311, 'n_words': 380, 'path': 'examples/notebooks/sudden_stop.ipynb', 'repo_name': 'zlqs1985/dolo', 'size': '3778'}