vasudeva-ram / Julia-SSJ

Training material to help solve Heterogeneous agent New Keynesian (HANK) models in Julia using the Sequence Space Jacobian method (Auclert et. al., 2021)..
13 stars 3 forks source link

testing euler equation #5

Closed floswald closed 3 months ago

floswald commented 3 months ago

I added a unit test for the euler equation. reverse-engineering your EGM step should yield the exact solution that is returned from EGM. At the moment there is something wrong. I've looked for a long time and could not find the problem, it seems I'm going just back step by step on what you did but there is an error remaining. can't exclude I made a mistake of course. you can run the test in VScode by clicking on the "testing" symbol (lab flask) in the left hand menu bar. or

] test

but the VSCode way is super quick, so preferrable.

Now to the test itself. I'm basically confused about how you apply EGM. I'm not sure which is the endogenous part. I tend to think of it that here we choose the x-axis of the policy funciton, keeping the y-axis fixed (the end-of-period state). I am unsure which of those plots is the correct one:

julia> plot(BaseModel.policymat, policies.consumption)
Screenshot 2024-07-16 at 18 04 56

or

julia> plot(policies.saving, policies.consumption)
Screenshot 2024-07-16 at 18 04 31

They are not identical.

Also: why this upward tick in the end in consumption? I would have said it should be close to linear at the upper end, but maybe there is a certain parameter that's causing this.

vasudeva-ram commented 3 months ago

Hi Florian - thanks for adding the test. I'll look at it in detail tomorrow when I get a chance, but wanted to share some immediate thoughts.

First, IMO the right graphical representations of the policy functions would be plot(BaseModel.policygrid, policies.consumption) and plot(BaseModel.policygrid, policies.savings). This tells you, for a specific individual state (i.e., given an asset position of the household) what would the consumption and savings choices of the household be? The savings grid that the households start with is fixed (i.e., they start with one of the savings positions in BaseModel.policygrid), but the household chooses a savings position that if "off-grid" and in that sense, it is an endogenously chosen gridpoint. To make it useful for us, we linearly extrapolate back to our chosen grid (i.e., BaseModel.policygrid).

When I run the model and plot the above graphs, they look identical to what you see here. One thought about the upward tick is that (in your first graph) you may be plotting the incorrect x-axis. The x-axis, while between [0, 200] is nonlinear ("double-exponential" to be precise), while in your plot, it looks like its being plotted linearly. I see that you have BaseModel.policymat instead of BaseModel.policygrid. This could be the source of the incorrect nonlinearity.

Second, my immediate thought w.r.t. the upward tick in the consumption graph is that we don't have enough policy positions for the household. I looked at the parameterization, and for some reason, the [a_min, a_max] for the asset positions is [0.0, 200.0] for n_a = 200. This is too low. I don't know why n_a is set at 200. Making it, say, n_a = 500 would yield the more clearly visible nonlinearity in the consumption - that is, consumption will be significantly curved for the lower positions of the households, and linear all the way to the end. Increasing $\beta$ will increase the nonlinearity also.

Try running it with the x-axis as BaseModel.policygrid and n_a = 500 and let me know if you get the right result.

floswald commented 3 months ago

Hi @vasudeva-ram I can confirm the plots are correct. I was running those with r = 0.05 without adjusting beta in the corresponding way, so that means that rich households have a massive incentive to save, which they do. decreasing r fixes everything. great.

Yeah if you could have a look at my test that would be great. maybe I'm missing the interpolation step back to the policygrid but I can't really see it. let me know what you find!

floswald commented 3 months ago

fixed the test in the end. the merit of the test is that I compute the expectation in a different way to you, so that gives some reassurance.

vasudeva-ram commented 3 months ago

Thanks for the test, @floswald - really appreciate it! I'll take a look at it and the other pull request sometime this weekend. Totally swamped at work here.