clear,clc
%---------------------------------------------------------
% Add folder 'files' to the search path and load the model
%---------------------------------------------------------
addpath('files');
load('model')
%----------------------------------------------------------------------------
% Provide nodes and weights for the quadrature that approximates expectations
%----------------------------------------------------------------------------
n_e=length(shocks); % number of shocks.
[n_nodes,nodes,weights] = Monomials_2(n_e,eye(n_e)); % this quadrature function was written by Judd, Maliar, Maliar and Valero (2014).
nodes=nodes'; % transpose to n_e-by-n_nodes
%----------------------------------
% Make a vector of parameter values
%----------------------------------
BETA=.96; GAMMA=2; PSI=1.5; ALPHA=.3; RHO=.8; DELTA=.1; SIGMA=.08;
params=eval(symparams);
%----------------------------------------------------------------------
% Prepare an initial guess - in this case I use a perturbation solution
%----------------------------------------------------------------------
% Steady state values
kss=((1/BETA-1+DELTA)/ALPHA)^(1/(ALPHA-1));
zss=0;
css=kss^ALPHA-DELTA*kss;
vss=css;
xiss=vss;
qss=BETA;
nxss=[log(kss);zss];
nyss=[log(css);log(xiss);log(qss)];
% Cross moments of the shocks
M=get_moments(nodes,weights,model.order(2));
% Compute the perturbation solution (keep the 4 outputs):
[derivs,stoch_pert,nonstoch_pert,model]=get_pert(model,params,M,nxss,nyss);
% Explanation of outputs:
% derivs=structure with the perturbation solution as explained in Levintal
% (2017): "Fifth-Order Perturbation Solution to DSGE Models".
% stoch_pert=the perturbation solution in the form of unique polynomial coefficients.
% nonstoch_pert=same as stoch_pert but without correction for the model volatility (i.e. this is a perturbation solution of a deterministic version of the model)
%-------------------------------------
% Solve the model by Taylor projection
%-------------------------------------
x0=nxss; % the approximation point (here we use the steady state, but it could be any arbitrary state)
c0=nxss; % the center of the initial guess
% tolerance parameters for the Newton solver
tolX=1e-6;
tolF=1e-6;
maxiter=10;
% model.jacobian='exact'; % this is the default
% model.jacobian='approximate'; % for large models try the approximate jacobian.
initial_guess=stoch_pert;
[coeffs,model]=tpsolve(initial_guess,x0,model,params,c0,nodes,weights,tolX,tolF,maxiter);
%------------------------------------------------------------------
% Compute the residual function and the model variables at point x0
%------------------------------------------------------------------
[R_fun0,g_fun0,Phi_fun0,auxvars0]=residual(coeffs,x0,params,c0,nodes,weights);
%------------------------
% Check the interest rate
%------------------------
logq=g_fun0(3);
Rf=exp(-logq)-1
%----------------------------------------------------------------------------
% Increase risk aversion (gradually) and see how the interest rate falls
%----------------------------------------------------------------------------
GAMMAvec=2:4:82;
Rfvec=zeros(size(GAMMAvec));
i=0;
for GAMMA=GAMMAvec
i=i+1;
params(2)=GAMMA;
[coeffs,model]=tpsolve(coeffs,x0,model,params,c0,nodes,weights,tolX,tolF,maxiter);
[R_fun0,g_fun0,Phi_fun0,auxvars0]=residual(coeffs,x0,params,c0,nodes,weights);
logq=g_fun0(3);
Rfvec(i)=exp(-logq)-1;
end
plot(GAMMAvec,Rfvec)
xlabel('Risk aversion (GAMMA)')
ylabel('Risk-free interest rate (Rf)')