Hansen(1985) Replication errors

Has anyone been able to successfully run the replication of Hansen (1985) on CPU not GPU. I’ve updated the VFIToolkit but getting the following errors:

Solve value fn problem
Unrecognized function or variable ‘ReturnFnParams’.

Error in ValueFnIter_Case1_Refine (line 43)
ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn,n_d, n_a, n_z_temp,d_grid, a_grid, zvals,ReturnFnParams,1); % the 1 at the end is to output for refine

Error in ValueFnIter_Case1 (line 629)
[VKron,Policy]=ValueFnIter_Case1_Refine(V0,n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z,ReturnFn,ReturnFnParamsVec,DiscountFactorParamsVec,vfoptions);

Error in code1 (line 86)
[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

1 Like

The ‘replications’ are only intended for GPU (they would otherwise take a fair while to run anyway).

If you are just interested in how to solve a basic RBC, you can check out

(you need the return function file as well)

PS. If your goal is learning value functions, great. If your goal is solving RBC models you will be much better of with Dynare.

PPS. Only basic functionality of VFI Toolkit can be done without a gpu

1 Like

I have two basic questions about Hansen (1985):

  1. why we don’t need to find the general equilibrium interest rate as we do in Aiyagari? I know that Hansen is representative agent unlike Aiyagari, but it is still a general equolibrium model…
  2. Can I compute impulse responses to a tfp shock in Hansen using the toolkit? The public files show how to do a stochastic simulation, but I would lile to do an impulse respose

Please let me know if these questions are ill posed

1: The standard way to solve a representative agent model is to take the competitive decentralized eqm, rewrite it as a system of stochastic difference equations (take first order conditions, combine for Euler eqn and the like), then solve these with perturbation methods (using Dynare), projection methods, etc. Implictly, this involves finding the general eqm interest rate, except that it is no longer constant so instead we are looking for a stochastic process on interest rates.

In a model as simple as the basic RBC in Hansen (1985) both the first and second welfare theorems hold, which means that the competitive equilibrium has the same solutions as the social planner problem. Hence in this model we have an another alternative which is just to solve the social planner problem and use this knowing that it is also the competitive decentralized eqm. This is what VFI Toolkit code for that model is doing, we just solve the social planner problem, which can be written as a value fn problem. Because it is a social planner we don’t need to find the interest rate directly as part of solving, but we can get interest rates later because the interest rate is just the marginal product of capital (net of depreciation) so you can simulate z_t (technology), L_t (labor) and K_t (physical capital) and thus get r_t (interest rate). Notice that the interest rate changes over time.

Note that while you can solve Hansen (1985) in this way (and can use VFI Toolkit), it is not really a recommended approach. If this is the kind of model you want to solve you would be better off doing it in Dynare (or GDSGE if you want something global and non-linear).

In summary, the reason you don’t find the interest rate is more about doing the social planner problem than it is about the representative agent complete market problem vs competitive eqm in incomplete markets problem. Note that in Aiyagari you could always solve for eqm K, rather than eqm r, which is of course anyway what the original Aiyagari (1994) paper actually does. So you can always avoid r per se, but the reason that the Hansen (1985) codes avoid having to find a fixed point for the general eqm is because they are doing the social planner problem.

PS. It is the inclusion of aggregate shocks that means the interest rate in Hansen (1985) is time varying.

2: In principle, you can calculate the impulse response from the shock process and the policy. But there are no tools in VFI Toolkit to do this, so you would have to look up the algorithm/steps for doing so and write code yourself.

2 Likes

@jake: Richter, Throckmorton and Walker developed another toolkit to solve DSGE models using global methods:

Richter, Alexander W., Nathaniel A. Throckmorton, and Todd B. Walker. (2014) “Accuracy, Speed and Robustness of Policy Function Iteration.” Computational Economics, 44, 445–76.

It might be a good alternative to GSDE mentioned by @robertdkirkby, in the sense that it is less of a black box and you have to do a bit more of coding yourself (personally, I consider this a plus). Their solution method is based on time iteration and uses spline interpolation coded in Matlab MEX Fortran.

A nice example of their method: https://onlinelibrary.wiley.com/doi/10.1111/jmcb.13115

1 Like

@jake88: I found this old code of mine that computes the irf of a Markov chain (AR1 discretized as a Markov, with Tauchen method). I replaced my old routine to do Tauchen with the corresponding toolkit function.

Please consider that you may have to do a few adjustments since I see a few warnings in the M editor. Hopefully will give you the right direction if you want to implement this yourself.

clear;clc;close all;

addpath(genpath('C:\Users\aledi\OneDrive\Documents\GitHub\VFIToolkit-matlab'))

rho   = 0.9;
sigma = 0.1;
m     = 3.0;
n_z   = 7;

%[pi_z,s,p,arho,asigma]=markovapprox(rho,sigma,m,N);
[z_grid,pi_z]=discretizeAR1_Tauchen(0.0,rho,sigma,n_z,m);

% Move back to CPU
z_grid = gather(z_grid);
pi_z   = gather(pi_z);
   
z_grid = exp(z_grid);

T = 10000;
H1 = 50;
H2 = 50;
s0 = 4; 
asim_ind = zeros(T,H1+H2);
rand('seed',666);
u = rand(T,H1+H2);

for t=1:T
    asim_ind(t,1)=s0;
    for h=2:H1+H2
        if h==H1+1
            asim_ind(t,h)=min(n_z,s0+1);
        else
            aux =  u(t,h) < cumsum(pi_z(asim_ind(t,h-1),:));
            asim_ind(t,h) = min(find(aux==1));
        end
    end
end

z_sim = z_grid(asim_ind);

z_irf = mean(z_sim(:,H1:H1+H2));
plot(z_irf)
title(['Impulse Response to shock at t=1, given \rho = ',num2str(rho)])

% N.B Here I use only IRF = E_t(y given shock)
% Sims suggests IRF = E_t(y given shock) - E_t(y given NO shock)
1 Like