%--------------------------------------------
% Solve the RBC model by continuation method
%--------------------------------------------
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=1; % 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
%----------------------------------------------------
% Choose parameter values with a closed-form solution
%----------------------------------------------------
BETA=.96; GAMMA=1; ALPHA=.3; RHO=.8; DELTA=1; SIGMA=.02;
params=eval(symparams);
%-----------------------------------------------------------------------
% The closed-form solution for the case GAMMA=1, DELTA=1 for consumption
%-----------------------------------------------------------------------
g=(1-ALPHA*BETA)*exp(z)*k^ALPHA;
%---------------------------------------------------------
% Use the closed-form solution to produce an initial guess
%---------------------------------------------------------
% differentiate the closed-form solution up to fourth order
gx=jacobian(g,x);
gxx=jacobian(gx(:),x);
gxxx=jacobian(gxx(:),x);
gxxxx=jacobian(gxxx(:),x);
% choose some arbitrary state - I use the steady state of the model of
% interest (with DELTA=.1)
k0=((1/BETA-1+.1)/ALPHA)^(1/(ALPHA-1));
z0=0;
x0=[k0;z0];
% compute g(x) and its derivatives at x0
g0=double(subs(g,x(:),x0));
gx0=double(subs(gx,x(:),x0));
gxx0=double(subs(gxx,x(:),x0));
gxxx0=double(subs(gxxx,x(:),x0));
gxxxx0=double(subs(gxxxx,x(:),x0));
% transform the derivatives into a vector of coefficients
[ initial_guess ] = derivs2coeffs(model,g0,gx0,gxx0,gxxx0,gxxxx0);
% this is for order=4. for lower orders include only the relevant
% derivatives, e.g. derivs2coeffs(model,g0,gx0,gxx0) is for second order.
% define the center of the initial guess (this is the point at which we computed
% the derivatives)
c0=x0;
% now we have the initial guess, and we can proceed to solve the model by
% continuation
%-------------------------------------------------------------------------------
% solve by Taylor projection and change the parameters gradually to the
% required level
%-------------------------------------------------------------------------------
tolX=1e-6;
tolF=1e-6;
maxiter=10;
[coeffs,model]=tpsolve(initial_guess,x0,model,params,c0,nodes,weights,tolX,tolF,maxiter);
% Now change the parameters GAMMA and DELTA gradually to their required levels:
GAMMA_original=GAMMA;
GAMMA_target=2;
DELTA_original=DELTA;
DELTA_target=.1;
for h=0:.1:1 % this is the homotopy parameter
GAMMA=(1-h)*GAMMA_original+h*GAMMA_target;
DELTA=(1-h)*DELTA_original+h*DELTA_target;
disp(['GAMMA=' num2str(GAMMA) ' DELTA=' num2str(DELTA)])
params(2)=GAMMA;
params(5)=DELTA;
[coeffs,model]=tpsolve(coeffs,x0,model,params,c0,nodes,weights,tolX,tolF,maxiter);
end