shade-econ / sequence-jacobian

A unified framework to solve and analyze heterogeneous-agent macro models.
MIT License
253 stars 149 forks source link

Method to generate distributional effect in nonlinear impluse response? #15

Closed YE-ZINAN closed 7 months ago

YE-ZINAN commented 1 year ago

Hi! I was trying to generate the impluse response distribution with the nonlinear_impluse_response method. However, the method can only return vectors of aggregate variables. If I want to obtain the household specific variables across the transition path ( like distribution or policies functions for consumption and asset), what should I do? I am looking forward to your gentle reply, thanks!

raphaelhuleux commented 7 months ago

Hello!

Similar question. It would be very useful to be able to obtain the policy functions and the distributions during the transition when computing a non-linear impulse response!

ludwigstraub commented 7 months ago

Hi Raphael (and Ye),

This can be done using the internals keyword argument when calling impulse_nonlinear or solve_impulse_nonlinear.

Here is a short example:

import numpy as np
import matplotlib.pyplot as plt
import sequence_jacobian as sj

calibration = sj.hetblocks.hh_sim.example_calibration()
hh = sj.hetblocks.hh_sim.hh_extended

# compute steady state
ss = hh.steady_state(calibration)

# feed in interest rate shock and obtain distribution along the transition
dr = 0.01 * 0.9 ** np.arange(100)
td = hh.impulse_nonlinear(ss, {"r": dr}, internals=['hh'])

# plot the distribution of assets at different points in time
ts = [0, 5, 10]
fig, ax = plt.subplots()
for t in ts:
    ax.plot(ss.internals['hh']['a_grid'], np.cumsum(np.sum(ss.internals['hh']['D'] + td.internals['hh']['D'][t], axis=0)), label=f't={t}')
ax.legend()
ax.set_xlim([0, 10])
ax.set_xlabel('Assets')
ax.set_ylabel('CDF')
plt.show()

In the example, internals=['hh'] ensures that impulse_nonlinear spits out all the internal objects of the household block named 'hh'. After that, td.internals['hh'] allows you to access the changes in all the household block's internal objects (distributions, policy functions, etc). For example, td.internals['hh']['D'][t] will report the change in the distribution at time t after the shock hits.

Hope this helps!

Ludwig

raphaelhuleux commented 7 months ago

Thanks a lot! Extremely useful.